def __init__(self, modeler): """ Shape tree. Simply record shape set. Compared with BModeler, this does not support winged edge data structure """ self.__modeler = modeler self.__plane = gt.Pln()
def pick_camera_relative(self, camera, cursor, offset): """ pick relative position against camera from camera :param offset: selection plane offset from camera xy pos this is used to place selection ahead camera position :param camera: Camera, :param cursor: Curosr, :return: (plane_key, intersection point) """ # 1. find cursor ray # 2. find selection plane # 3. find intersection with camera offsetted plane # 1) camera offseted plane # 2) intsersection coord = cursor.pos_local.xy R = camera.frusrum_ray(*coord).as_vec() adeq_plns = sorted([(gt.Vec.cross(R, x).length, k) for k, x in zip(('x', 'y', 'z'), gt.Pln().axes)]) cam_pln = camera.tripod.plane proj_ori = gt.Vec(*cam_pln.origin.xy, 0) off_vec = -cam_pln.axis_z.project_on_xy().normalize() * offset off_pln = gt.Pln((proj_ori + off_vec).xyz, (1, 0, 0), (0, 1, 0), (0, 0, 1)) # finding intersection # 0 origin, t amplifier, R, ray vector, P intersection point # O + t*R = P O = cam_pln.origin for _, key in adeq_plns: Px, Ox, Rx = (getattr(i, key) for i in (off_pln.origin, O, R)) t = (Px - Ox) / Rx P = O + t * R for l, comp in zip(self.__constraints, P.xyz): if not l(comp): break else: # not broken - all limit passed return 'xyz'.replace(key, ''), P
def add_pln(self, model, o, x, y, z): """ coordinate values of: :param o: (x, y, z), origin :param x: (x, y, z), x axis :param y: (x, y, z), y axis :param z: (x, y, z), z axis :return: """ return self.__add_shape(model, args=(gt.Pln(o, x, y, z), ), shape_type=st.Pln)
def draw(self): with self.devices.frames[0] as df: df.clear(0, 0, 0, 1) df.clear_depth() with self.devices.cameras[0] as c: self.modeler.render() selection = self.picker.pick(gt.Pln(), c, self.devices.cursors[0]) if selection: key, point = selection if key == 'xy': clr = 0, 0, 1, 0.5 elif key == 'yz': clr = 1, 0, 0, 0.5 else: clr = 0, 1, 0, 0.5 self.modeler.add_raw(self.model, point).clr = clr
def render_world_space(self, aid, pln: gt.Pln, width, height, tdomain_x=(0, 1), tdomain_y=(0, 1)): """ render frame's given attachment on world coordinate system :param aid: int, color attachment id :param pln: Pln, plane to render at :param width: Number, width of render area :param height: Number, height of render area :param tdomain_x: (float, float), texture space x domain :param tdomain_y: (float, float), texture space y domain :return: """ if not isinstance(aid, int): raise TypeError if not isinstance(pln, gt.Pln): raise TypeError if any(not isinstance(d, Number) for d in (width, height)): raise TypeError # calculate quad position # coverage represented as polyline then as coordinates w, h = self.__size quad_pos = gt.Plin((0, 0, 0), (w, 0, 0), (w, h, 0), (0, h, 0)) sm = mx.ScaleMat(x=width / w, y=height / h, z=1) quad_pos = sm * quad_pos quad_pos = pln.orient(obj=quad_pos, ref_pln=gt.Pln()).T texture = self.__frame_bffr.get_attachment(aid) self.__renderer.render_world_space(texture, quad_pos, tdomain_x, tdomain_y)
def __left_button_press(self): """ actions for left button press :return: """ # clear selection if self.__selection: self.__selection.clr = 1, 1, 1, 1 self.__selection = None # check for selection # as this is a separate thread, need context binding with self.__window.context.gl: goid, bitpatt = self.__id_picker.pick(pos=self.__cursor.pos_local, size=(1, 1)) shape = GIDP().get_registered_byvalue(goid[0][0], bitpatt) # if nothing selected, if shape is None: pick = self.__vp.pick(gt.Pln(), self.__camera, self.__cursor) if pick: self.__executor.execute(self.__draw_point, args=(pick[1], )) else: # picking drawn point self.__executor.execute(self.__color_selected, args=(shape, ))
def __init__(self): super().__init__(1000, 1000, 'my window 1', monitor=None, shared=None) self.framerate = 60 # enable camera move o = 200 # self.devices.cameras[0].tripod.lookat(eye=(0, 0, o), at=(0, 0, 0), up=(0, 1, 0)) self.devices.cameras[0].focus_pane(pane=self.devices.panes[0], focus=(0, 0, 0), clip_off=100) # create model self.modeler = AModeler() model = self.modeler.add_model(None) plns = [] self.model1 = model pw, ph = self.devices.panes[0].size.xy n = 7 nw, nh = pw // n, ph // n for x in range(-pw // 2 + nw, pw // 2 - nw + 1, nw): for y in range(-ph // 2 + nh, ph // 2 - nh + 1, nh): plns.append(gt.Pln(o=(x, y, 0))) self.modeler.add_pnt(model, x, y, 1).clr = ClrRGBA(1, 1, 0, 1) e = 100 q = e / 4 h = e / 2 o = e / 10 t = e / 3 pgons = [] # A pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (t, q, 0), (2 * t, q, 0), (3 * q, 0, 0), (e, 0, 0), (h, e, 0), (0, 0, 0))) # B k = e - 3 * o pgons.append( gt.Pgon((0, 0, 0), (k, 0, 0), (e, o, 0), (e, h - o, 0), (k, h, 0), (e, h + o, 0), (e, e - o, 0), (k, e, 0), (0, e, 0), (0, 0, 0))) # C pgons.append( gt.Pgon((q, 0, 0), (h, 0, 0), (e, 0, 0), (e, q, 0), (h, q, 0), (t, t, 0), (t, 2 * t, 0), (h, e - q, 0), (e, e - q, 0), (e, e, 0), (q, e, 0), (0, e - q, 0), (0, q, 0), (q, 0, 0))) # D pgons.append( gt.Pgon((0, 0, 0), (3 * q, 0, 0), (e, q, 0), (e, e - q, 0), (e - q, e, 0), (0, e, 0), (0, 0, 0))) # E k = e / 5 pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, k, 0), (h, k, 0), (h, 2 * k, 0), (e, 2 * k, 0), (e, 3 * k, 0), (h, 3 * k, 0), (h, 4 * k, 0), (e, 4 * k, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # F k = e / 5 pgons.append( gt.Pgon((0, 0, 0), (t, 0, 0), (t, q, 0), (2 * t, q, 0), (2 * t, h, 0), (t, h, 0), (t, 3 * q, 0), (e, 3 * q, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # G pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, 2 * t - o, 0), (e - q - o, 2 * t - o, 0), (e - q, t, 0), (q, t, 0), (q, 2 * t, 0), (e, 2 * t, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # H pgons.append( gt.Pgon((0, 0, 0), (t, 0, 0), (t, t, 0), (2 * t, t, 0), (2 * t, 0, 0), (e, 0, 0), (e, e, 0), (2 * t, e, 0), (2 * t, 2 * t, 0), (t, 2 * t, 0), (t, e, 0), (0, e, 0), (0, 0, 0))) # I pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, t, 0), (2 * t, t, 0), (2 * t, 2 * t, 0), (e, 2 * t, 0), (e, e, 0), (0, e, 0), (0, 2 * t, 0), (t, 2 * t, 0), (t, t, 0), (0, t, 0), (0, 0, 0))) # J pgons.append( gt.Pgon((q, 0, 0), (2 * t, 0, 0), (e - o, t, 0), (e - o, e - q, 0), (e, e - q, 0), (e, e, 0), (h, e, 0), (h, e - q, 0), (2 * t, e - q, 0), (2 * t, h, 0), (h, q, 0), (t, q, 0), (t, h, 0), (0, h, 0), (0, q, 0), (q, 0, 0))) # K pgons.append( gt.Pgon((0, 0, 0), (t, 0, 0), (t, q, 0), (h, 0, 0), (e, 0, 0), (h, h, 0), (e, e - q, 0), (e, e, 0), (t, e - t, 0), (t, e, 0), (0, e, 0), (0, 0, 0))) # L pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, t, 0), (t, t, 0), (t, e, 0), (0, e, 0), (0, 0, 0))) # M pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (1.5 * q, q, 0), (2.5 * q, q, 0), (3 * q, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (3 * q, e, 0), (2.2 * q, h, 0), (1.8 * q, h, 0), (q, e, 0), (0, e, 0), (0, 0, 0))) # N pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (3 * q, e, 0), (3 * q, h, 0), (q, e, 0), (0, e, 0), (0, 0, 0))) # O pgons.append( gt.Pgon((q, 0, 0), (e - q, 0, 0), (e, q, 0), (e, e - q, 0), (e - q, e, 0), (q, e, 0), (0, e - q, 0), (0, q, 0), (q, 0, 0))) # P pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # Q pgons.append( gt.Pgon((q, 0, 0), (h + o, 0, 0), (t * 2 + o, o, 0), (e - o, 0, 0), (e, o, 0), (e - o, t - o, 0), (e, h - o, 0), (e, e - q, 0), (e - q, e, 0), (q, e, 0), (0, e - q, 0), (0, q, 0), (q, 0, 0))) # R pgons.append( gt.Pgon((0, 0, 0), (h, 0, 0), (h, q, 0), (e - q, 0, 0), (e, q, 0), (e - q, h, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # S pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, e - t, 0), (t, e - t, 0), (t, e - t + o, 0), (e, e - t + o, 0), (e, e, 0), (0, e, 0), (0, t, 0), (e - t, t, 0), (e - t, t - o, 0), (0, t - o, 0), (0, 0, 0))) # T pgons.append( gt.Pgon((q, 0, 0), (e - q, 0, 0), (e - q, e - q, 0), (e, e - q, 0), (e, e, 0), (0, e, 0), (0, e - q, 0), (q, e - q, 0), (q, 0, 0))) # U pgons.append( gt.Pgon((q, 0, 0), (e - q, 0, 0), (e, q, 0), (e, e, 0), (e - t, e, 0), (e - t, h, 0), (t, h, 0), (t, e, 0), (0, e, 0), (0, q, 0), (q, 0, 0))) # V pgons.append( gt.Pgon((q, 0, 0), (e - q, 0, 0), (e, e, 0), (e - t, e, 0), (h, h, 0), (t, e, 0), (0, e, 0), (q, 0, 0))) # W f = e / 5 pgons.append( gt.Pgon((f, 0, 0), (f * 2, 0, 0), (h, h, 0), (f * 3, 0, 0), (f * 4, 0, 0), (e, e, 0), (e - f, e, 0), (e - f * 1.5, h, 0), (f * 3, e, 0), (f * 2, e, 0), (f * 1.5, h, 0), (f, e, 0), (0, e, 0), (f, 0, 0))) # X pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (h, t, 0), (e, 0, 0), (e, q, 0), (e - t, h, 0), (e, e, 0), (e - q, e, 0), (h, e - t, 0), (0, e, 0), (0, e - q, 0), (t, h, 0), (0, 0, 0))) # Y pgons.append( gt.Pgon((t, 0, 0), (t * 2, 0, 0), (t * 2, h, 0), (e, e - t, 0), (e, e, 0), (h, e - q, 0), (0, e, 0), (0, e - t, 0), (t, h, 0), (t, 0, 0))) # Z pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, q, 0), (h, q, 0), (e, e, 0), (0, e, 0), (0, e - q, 0), (h, e - q, 0), (0, 0, 0))) for pgon, pln in zip(pgons, plns): self.modeler.add_raw(model, pln.orient(pgon, gt.Pln())).edge_thk = 5 self.model = model
def __init__(self): super().__init__(1000, 1000, 'my window 1', monitor=None, shared=None) self.framerate = 60 # enable camera move o = 200 # self.devices.cameras[0].tripod.lookat(eye=(0, 0, o), at=(0, 0, 0), up=(0, 1, 0)) self.devices.cameras[0].focus_pane(pane=self.devices.panes[0], focus=(0, 0, 0), clip_off=100) # create model self.modeler = AModeler() self.model = self.modeler.add_model(parent=None) plns = [] pw, ph = self.devices.panes[0].size.xy n = 6 nw, nh = pw // n, ph // n for x in range(-pw // 2 + nw, pw // 2 - nw + 1, nw): for y in range(-ph // 2 + nh, ph // 2 - nh + 1, nh): plns.append(gt.Pln(o=(x, y, 0))) self.modeler.add_pnt(self.model, x, y, 1).clr = ClrRGBA(1, 1, 0, 1) e = 100 q = e / 4 h = e / 2 o = e / 10 t = e / 3 pgons = [] # triangle pgons.append(gt.Pgon((0, 0, 0), (e, 0, 0), (0, e, 0), (0, 0, 0))) # rectangle pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # block pgons.append( gt.Pgon((0, 0, 0), (h, 0, 0), (h, h, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # hills pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, h, 0), (q * 3, e, 0), (q * 2, h, 0), (q, e, 0), (0, h, 0), (0, 0, 0))) # beanie pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, h, 0), (q * 3, e, 0), (q, e, 0), (0, h, 0), (0, 0, 0))) # cat pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e, h, 0), (q * 3, e, 0), (q * 3, h, 0), (q, h, 0), (q, e, 0), (0, h, 0), (0, 0, 0))) # reverse house pgons.append( gt.Pgon((h, 0, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, h, 0), (h, 0, 0))) # left dialog pgons.append( gt.Pgon((q, 0, 0), (h, h, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, h, 0), (q, 0, 0))) # diamond pgons.append( gt.Pgon((h, 0, 0), (e, h, 0), (h, e, 0), (0, h, 0), (h, 0, 0))) # quad triangle pgons.append( gt.Pgon((h, 0, 0), (e, 0, 0), (h, e, 0), (0, 0, 0), (h, 0, 0))) # right dialog pgons.append( gt.Pgon((3 * q, 0, 0), (e, h, 0), (e, e, 0), (0, e, 0), (0, h, 0), (h, h, 0), (3 * q, 0, 0))) # torn paper pgons.append( gt.Pgon((0, 0, 0), (-q, -q, 0), (0, -q, 0), (e, -q, 0), (e - q, h, 0), (e, e, 0), (-q, e, 0), (0, h + q, 0), (-q, q, 0), (0, 0, 0))) # flag right pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (3 * q, h, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # cravas pgons.append( gt.Pgon((0, 0, 0), (q, h, 0), (h, q, 0), (e, e, 0), (-h, e, 0), (0, 0, 0))) # mountain pgons.append( gt.Pgon((0, 0, 0), (e, 0, 0), (e - q, 2 * q, 0), (e - q, q, 0), (h, 3 * q, 0), (h, 2 * q, 0), (q, e, 0), (0, 0, 0))) # bow tie pgons.append( gt.Pgon((0, 0, 0), (h, q, 0), (e, 0, 0), (e, e, 0), (h, 3 * q, 0), (0, e, 0), (0, 0, 0))) # pants pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (h, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # bad m pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (h, q, 0), (3 * q, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # M pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (1.5 * q, q, 0), (2.5 * q, q, 0), (3 * q, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (3 * q, e, 0), (2.2 * q, h, 0), (1.8 * q, h, 0), (q, e, 0), (0, e, 0), (0, 0, 0))) # N pgons.append( gt.Pgon((0, 0, 0), (q, 0, 0), (q, h, 0), (3 * q, 0, 0), (e, 0, 0), (e, e, 0), (3 * q, e, 0), (3 * q, h, 0), (q, e, 0), (0, e, 0), (0, 0, 0))) # drill pgons.append( gt.Pgon((h - o, 0, 0), (h + o, 0, 0), (2 * t, h, 0), (2.5 * t, h, 0), (e, h + q, 0), (e, e, 0), (0, e, 0), (0, h + q, 0), (0.5 * t, h, 0), (t, h, 0), (h - o, 0, 0))) # gate pgons.append( gt.Pgon((0, 0, 0), (t, 0, 0), (t, h, 0), (2 * t, h, 0), (2 * t, 0, 0), (e, 0, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # # thin leg pgons.append( gt.Pgon((0, 0, 0), (t, 0, 0), (t, h, 0), (2 * t, h, 0), (2 * t, 0, 0), (e, e, 0), (0, e, 0), (0, 0, 0))) # nepal pgons.append( gt.Pgon((0, 0, 0), (e, q, 0), (q, h, 0), (3 * q, 3 * q, 0), (0, e, 0), (0, 0, 0))) # tick pgons.append( gt.Pgon((0, 0, 0), (h, h, 0), (0, -h, 0), (-h, 0, 0), (0, 0, 0))) for pgon, pln in zip(pgons, plns): self.modeler.add_raw(self.model, pln.orient(pgon, gt.Pln())).edge_thk = 5