Debian Codegraph @

tools/geometry_twister.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # ####################################################################
5 # Copyright (C) 2005-2019 by the FIFE team
6 # http://www.fifengine.net
7 # This file is part of FIFE.
8 #
9 # FIFE is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the
21 # Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # ####################################################################
24
25 try:
26 import Tkinter as TK
27 except ImportError:
28 # Python 3
29 import tkinter as TK
30 xrange = range
31 import math, time
32
33 LMB = 0x100
34 RMB = 0x400
35 DBLCLICK_TRESHOLD = 0.3 # secs
36
37 class Shape(object):
38 def __init__(self, center_pt, sidecount):
39 assert (sidecount in (4, 6))
40 self.rotation = 0
41 self.tilting = 0
42 self.zoomval = 70
43 self.center_pt = center_pt
44 startangle = 0
45 if sidecount == 6:
46 startangle = math.pi / 6.0
47 self.angles = [startangle + 2 * math.pi * (s / float(sidecount)) for s in xrange(sidecount-1)]
48 shape_pts = [(0,0,0)]
49 maxy, miny = 0, 0
50 for a in self.angles:
51 x, y, z = shape_pts[-1]
52 newx, newy = math.cos(a) + x, math.sin(a) + y
53 maxy, miny = max(maxy, newy), min(miny, newy)
54 shape_pts.append((newx, newy, 0))
55 xmove, ymove = 0.5, (abs(miny) + maxy) / 2
56 self.shape_pts = [(pt[0]-xmove, pt[1]-ymove, 0) for pt in shape_pts]
57 self.reflections = self.create_shape_reflections()
58
59 def create_shape_reflections(self):
60 reflections = []
61 sides = len(self.shape_pts)
62 if sides == 4:
63 for x,y in ((1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1)):
64 reflections.append([(pt[0]+x, pt[1]+y, 0) for pt in self.shape_pts])
65 elif sides == 6:
66 W = math.cos(math.pi/6.0)
67 H = math.sin(math.pi/6.0)
68 for x,y in ((2*W,0), (W,1+H), (-W,1+H), (-2*W,0), (-W,-1-H)):
69 reflections.append([(pt[0]+x, pt[1]+y, 0) for pt in self.shape_pts])
70 return reflections
71
72 def tilt(self, degrees):
73 self.tilting += math.radians(degrees)
74 if self.tilting < 0:
75 self.tilting = 0
76 elif self.tilting > math.pi/2 * 0.95:
77 self.tilting = math.pi/2 * 0.95
78
79 def rotate(self, degrees):
80 self.rotation += math.radians(degrees)
81 if self.rotation < 0:
82 self.rotation = 0
83 elif self.rotation > math.pi/2 * 0.95:
84 self.rotation = math.pi/2 * 0.95
85
86 def zoom(self, amount):
87 self.zoomval += amount
88 if self.zoomval < 10:
89 self.zoomval = 10
90 if self.zoomval > 150:
91 self.zoomval = 150
92
93 def transform_pts(self, pts):
94 rotated_pts = []
95 for x, y, z in pts:
96 rotated_pts.append((x * math.cos(self.rotation) - y * math.sin(self.rotation),
97 -(x * math.sin(self.rotation) + y * math.cos(self.rotation)),
98 z))
99 tilted_pts = []
100 for x, y, z in rotated_pts:
101 tilted_pts.append((x,
102 y * math.cos(self.tilting) - z * math.sin(self.tilting),
103 z * math.sin(self.tilting) + z * math.cos(self.tilting)))
104
105 zoomed_pts = []
106 for x, y, z in tilted_pts:
107 zoomed_pts.append((int(round(self.center_pt[0] + self.zoomval * x)),
108 int(round(self.center_pt[1] + self.zoomval * y))))
109 return zoomed_pts
110
111 def get_screen_pts(self):
112 return self.transform_pts(self.shape_pts)
113
114 def get_screen_bounding_box(self, screenpts):
115 x2, y2 = x1, y1 = screenpts[0]
116 for pt in screenpts:
117 x1, y1 = min(x1, pt[0]), min(y1, pt[1])
118 x2, y2 = max(x2, pt[0]), max(y2, pt[1])
119 return (x1, y1), (x2, y2)
120
121 def get_reflections(self):
122 return [self.transform_pts(reflection) for reflection in self.reflections]
123
124 def get_tilting(self):
125 return math.degrees(self.tilting)
126
127 def get_rotation(self):
128 return math.degrees(self.rotation)
129
130 class Gui(object):
131 W = 400
132 H = 400
133 def __init__(self):
134 self.sides = (4, 6)
135 self.cur_side_index = 0
136 self.shape = Shape((Gui.W/2, Gui.H/2), self.sides[0])
137 self.prev_mouse_pt = (0, 0)
138 self.root = TK.Tk()
139 self.canvas = TK.Canvas(self.root, width=Gui.W, height=Gui.H)
140 self.canvas.create_window(285, 280, window=TK.Frame(self.canvas, relief=TK.GROOVE, borderwidth=2), anchor=TK.CENTER)
141 self.canvas.bind('<Motion>', self.on_move)
142 self.canvas.bind('<ButtonPress-1>', self.on_mb)
143 self.canvas.bind('<ButtonPress-3>', self.on_mb)
144 self.canvas.bind('<Configure>', self.on_resize)
145 self.canvas.pack()
146 self.update_view()
147 self.last_click_time = time.time()
148
149 def on_resize(self, event):
150 pass
151
152 def on_move(self, event):
153 if (event.state & RMB):
154 self.shape.zoom((self.prev_mouse_pt[1] - event.y) / 3)
155 self.update_view()
156 elif (event.state & LMB):
157 self.shape.rotate((self.prev_mouse_pt[0] - event.x) / 2)
158 self.shape.tilt((self.prev_mouse_pt[1] - event.y) / 2)
159 self.update_view()
160 self.prev_mouse_pt = (event.x, event.y)
161
162 def on_mb(self, event):
163 self.prev_mouse_pt = (event.x, event.y)
164 if (time.time() - self.last_click_time) < DBLCLICK_TRESHOLD:
165 self.cur_side_index = (self.cur_side_index+1) % len(self.sides)
166 newshape = Shape((Gui.W/2, Gui.H/2), self.sides[self.cur_side_index])
167 newshape.rotation = self.shape.rotation
168 newshape.tilting = self.shape.tilting
169 newshape.zoomval = self.shape.zoomval
170 self.shape = newshape
171 self.update_view()
172 self.last_click_time = time.time()
173
174 def draw_shape(self, shapepts, boundpts=None, fillshape=True):
175 if boundpts:
176 pt1, pt2 = boundpts
177 self.canvas.create_polygon(pt1[0], pt1[1], pt2[0], pt1[1], pt2[0], pt2[1], pt1[0], pt2[1], outline="blue", fill="", tag="boundingbox")
178 fillcolor = "white"
179 if not fillshape:
180 fillcolor = ""
181 self.canvas.create_polygon(fill=fillcolor, outline="black", tag="shape", *self.flatten_pts(shapepts))
182
183 def update_texts(self, size, transform):
184 self.canvas.create_text(10, 20, text="Bounding box size x:%d, y:%d" % (size[0], size[1]), tag="size", anchor=TK.NW)
185 self.canvas.create_text(10, 40, text="Transform x:%d, y:%d" % (transform[0], transform[1]), tag="transform", anchor=TK.NW)
186 self.canvas.create_text(10, 60, text="Tilting: %d" % self.shape.get_tilting(), tag="tilting", anchor=TK.NW)
187 self.canvas.create_text(10, 80, text="Rotation: %d" % self.shape.get_rotation(), tag="rotation", anchor=TK.NW)
188 self.canvas.create_text(10, 330, text="Doubleclick to change shape", anchor=TK.NW)
189 self.canvas.create_text(10, 350, text="Right button + drag = zoom", anchor=TK.NW)
190 self.canvas.create_text(10, 370, text="Left button + drag = tilt/rotate", anchor=TK.NW)
191
192 def flatten_pts(self, pts):
193 return [c for pt in pts for c in pt]
194
195 def update_view(self):
196 self.canvas.delete("all")
197 shapepts = self.shape.get_screen_pts()
198 boundpts = self.shape.get_screen_bounding_box(shapepts)
199 size = boundpts[1][0] - boundpts[0][0], boundpts[1][1] - boundpts[0][1]
200 reflections = self.shape.get_reflections()
201 transform = (reflections[0][0][0] - shapepts[0][0], -(reflections[0][0][1] - shapepts[0][1]))
202
203 for reflection_pts in reflections[1:]:
204 self.draw_shape(reflection_pts, fillshape=False)
205
206 self.draw_shape(shapepts, boundpts)
207 transform_boundpts = self.shape.get_screen_bounding_box(reflections[0])
208 self.draw_shape(reflections[0], transform_boundpts, fillshape=False)
209
210 self.update_texts(size, transform)
211
212 def run(self):
213 self.root.mainloop()
214
215 if __name__ == '__main__':
216 gui = Gui()
217 gui.run()