def get_coords_2d_from_mouse_pos_for_zoom(self, mouse_pos=None, get_r=False): """ get the 2d coordinates of the panner plane from the mouse collision """ if mouse_pos is None: mouse_pos = base.mouseWatcherNode.getMouse() # print("mouse_pos: ", mouse_pos[0], mouse_pos[1]) mouse_p_x, mouse_p_y = conventions.getFilmCoordsFromMouseCoords(mouse_pos[0], mouse_pos[1]) # print("mouse_p_x, mouse_p_y: ", mouse_p_x, mouse_p_y) cam_pos = self.get_cam_pos() r0_shoot = (cam_pos + # camera position self.get_e_x_prime() * mouse_p_x + self.get_e_y_prime() * mouse_p_y # camera plane ) # args: planeNormal, planePoint, rayDirection, rayPoint r = math_utils.LinePlaneCollision(self.get_cam_forward_vector_normalized(), self.get_cam_pos(), self.get_cam_forward_vector_normalized(), r0_shoot) in_plane_vec = r - self.get_cam_pos() x1 = np.dot(self.get_e_x_prime(), in_plane_vec) x2 = np.dot(self.get_e_y_prime(), in_plane_vec) # print("x1, x2: ", x1, x2) if get_r == True: return np.array([x1, x2]), r return np.array([x1, x2])
def handle_shift_mouse1(self): """ """ print("handle_shift_mouse1") self.ddem = DragDropEventManager() # ---- calculate (solely camera and object needed and the recorded mouse position before dragging) the self.p_xy_at_init_drag # between -1 and 1 in both x and y mouse_position_before_dragging = base.mouseWatcherNode.getMouse() p_xy_offset = conventions.getFilmCoordsFromMouseCoords( -mouse_position_before_dragging[0], -mouse_position_before_dragging[1], p_x_0=0., p_y_0=0.) self.ddem.add_on_state_change_function( OrbiterOrtho.set_new_panning_orbit_center_from_dragged_mouse, args=(self, p_xy_offset, self.get_orbit_center())) self.ddem.init_dragging()
def get_coords_2d_for_mouse_zoom(self): """ mouse_pos as returned from base.mouseWatcherNode.getMouse() # planeNormal, planePoint, rayDirection, rayPoint """ mouse_pos = base.mouseWatcherNode.getMouse() print("mouse_pos: ", mouse_pos[0], mouse_pos[1]) mouse_p_x, mouse_p_y = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[ 1] # , self.p_xy_at_init_drag[0], self.p_xy_at_init_drag[1] ) # print("self.p_xy_at_init_drag: ", self.p_xy_at_init_drag) print("mouse_p_x, mouse_p_y: ", mouse_p_x, mouse_p_y) cam_pos = self.get_cam_pos() # print("cam_pos: ", cam_pos) r0_shoot = ( cam_pos + # camera position self.get_e_x_prime() * mouse_p_x + self.get_e_y_prime() * mouse_p_y # camera plane ) # print("self.get_e_y_prime(): ", self.get_e_y_prime()) # print("self.get_e_x_prime(): ", self.get_e_x_prime()) # print("r0_shoot: ", r0_shoot) print("self.get_cam_up_vector_normalized(): ", self.get_cam_up_vector_normalized()) # planeNormal, planePoint, rayDirection, rayPoint r = math_utils.LinePlaneCollision( self.get_cam_forward_vector_normalized(), self.get_cam_pos(), self.get_cam_forward_vector_normalized(), r0_shoot) # print("self.get_cam_forward_vector_normalized():, ", self.get_cam_forward_vector_normalized()) # print("r: ", r) in_plane_vec = r - self.get_cam_pos() x1 = np.dot(self.get_e_x_prime(), in_plane_vec) x2 = np.dot(self.get_e_y_prime(), in_plane_vec) # print("x1, x2: ", x1, x2) return np.array([x1, x2])
def set_new_position_from_dragged_mouse(self, p_xy_offset, original_orbit_center): """ There is a new mouse position, now Args: p_xy_offset: the origin point (lies in the camera plane) to which the change point (calculated from the mouse displacement) is being added to """ v_cam_forward = math_utils.p3d_to_np(engine.tq_graphics_basics.tq_render.getRelativeVector( self.p3d_camera, self.p3d_camera.node().getLens().getViewVector())) v_cam_forward = v_cam_forward / np.linalg.norm(v_cam_forward) # print("--- > View Vector", self.p3d_camera.node().getLens().getViewVector()) v_cam_up = math_utils.p3d_to_np(engine.tq_graphics_basics.tq_render.getRelativeVector( self.p3d_camera, self.p3d_camera.node().getLens().getUpVector())) v_cam_up = v_cam_up / np.linalg.norm(v_cam_up) r_cam = math_utils.p3d_to_np(self.p3d_camera.getPos()) e_up = math_utils.p3d_to_np(v_cam_up/np.linalg.norm(v_cam_up)) e_cross = math_utils.p3d_to_np( np.cross(v_cam_forward/np.linalg.norm(v_cam_forward), e_up)) # -- calculate the bijection between mouse coordinates m_x, m_y and plane coordinates p_x, p_y mouse_pos = base.mouseWatcherNode.getMouse() # between -1 and 1 in both x and y p = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[1], p_xy_offset[0], p_xy_offset[1]) drag_vec = p[0] * e_cross + p[1] * e_up print(drag_vec) # print("p: ", p) new_orbit_center = original_orbit_center + (-drag_vec) # self.set_corner_to_coords(math_utils.indexable_vec3_to_p3d_Vec3(new_orbit_center)) print("self.get_cam_coords: ", self.get_cam_coords()) self.x = -np.array(p) self.p_previous_offset = np.array(p) self.set_corner_to_coords(math_utils.indexable_vec3_to_p3d_Vec3(new_orbit_center))
def getCameraMouseRay(camera, mouse_pos): # aufpunkt of the plane -> just choose the camera position r0_obj = math_utils.p3d_to_np(base.cam.getPos()) v_cam_forward = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( camera, camera.node().getLens().getViewVector())) v_cam_forward = v_cam_forward / np.linalg.norm(v_cam_forward) # self.camera_gear.p3d_camera.node().getLens().getViewVector() v_cam_up = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( camera, camera.node().getLens().getUpVector())) v_cam_up = v_cam_up / np.linalg.norm(v_cam_up) r_cam = math_utils.p3d_to_np(camera.getPos()) e_up = math_utils.p3d_to_np(v_cam_up / np.linalg.norm(v_cam_up)) e_cross = math_utils.p3d_to_np( np.cross(v_cam_forward / np.linalg.norm(v_cam_forward), e_up)) # determine the middle origin of the draggable plane (where the plane intersects the camera's forward vector) r0_middle_origin = math_utils.LinePlaneCollision(v_cam_forward, r0_obj, v_cam_forward, r_cam) # p_xy_offset = conventions.getFilmCoordsFromMouseCoords(-self.mouse_position_before_dragging[0], -self.mouse_position_before_dragging[1], p_x_0=0., p_y_0=0.) p_x, p_y = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[1] # , p_xy_offset[0], p_xy_offset[1] ) # p_x, p_y = conventions.getFilmCoordsFromMouseCoords(mouse_pos[0], mouse_pos[1], 0., 0.) in_plane_vector = p_x * e_cross + p_y * e_up ray_aufpunkt = r0_middle_origin + in_plane_vector ray_direction = v_cam_forward return ray_direction, ray_aufpunkt
def getCoords2dForStroke(self): """ mouse_pos as returned from base.mouseWatcherNode.getMouse() """ mouse_pos = base.mouseWatcherNode.getMouse() mouse_p_x, mouse_p_y = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[ 1] # , self.p_xy_at_init_drag[0], self.p_xy_at_init_drag[1] ) # print("self.p_xy_at_init_drag: ", self.p_xy_at_init_drag) # print("mouse_p_x, mouse_p_y: ", mouse_p_x, mouse_p_y) cam_pos = self.camera_gear.get_cam_pos() # print("cam_pos: ", cam_pos) r0_shoot = ( cam_pos + # camera position self.camera_gear.get_e_x_prime() * mouse_p_x + self.camera_gear.get_e_y_prime() * mouse_p_y # camera plane ) # print("self.camera_gear.get_e_y_prime(): ", self.camera_gear.get_e_y_prime()) # print("self.camera_gear.get_e_x_prime(): ", self.camera_gear.get_e_x_prime()) # print("r0_shoot: ", r0_shoot) # print("self.panel_geometry.get_plane_normal_vector(): ", self.panel_geometry.get_plane_normal_vector()) r_strike = math_utils.LinePlaneCollision( self.panel_geometry.get_plane_normal_vector(), self.panel_geometry.r0, self.camera_gear.get_cam_forward_vector_normalized(), r0_shoot) # print("self.camera_gear.get_cam_forward_vector_normalized():, ", self.camera_gear.get_cam_forward_vector_normalized()) # print("r_strike: ", r_strike) in_plane_vec = r_strike - self.panel_geometry.r0 x1_strike = np.dot(self.panel_geometry.n1, in_plane_vec) x2_strike = np.dot(self.panel_geometry.n2, in_plane_vec) # print("x1_strike, x2_strike: ", x1_strike, x2_strike) return np.array([x1_strike, x2_strike])
def handle_shift_mouse1(self): """ """ print("handle_shift_mouse1") self.ddem = DragDropEventManager() # ---- calculate (solely camera and object needed and the recorded mouse position before dragging) the self.p_xy_at_init_drag # between -1 and 1 in both x and y mouse_position_before_dragging = base.mouseWatcherNode.getMouse() _p_xy_offset = conventions.getFilmCoordsFromMouseCoords( -mouse_position_before_dragging[0], -mouse_position_before_dragging[1], p_x_0=self.p_previous_offset[0], p_y_0=self.p_previous_offset[1]) self.p_previous_offset = np.array(_p_xy_offset) self.ddem.add_on_state_change_function( Panner2d.set_new_position_from_dragged_mouse, args=(self, _p_xy_offset, self.get_coords_3d_of_corner())) self.ddem.init_dragging()
def init_dragging(self): """ save original mouse position at start of dragging """ mouse_pos = base.mouseWatcherNode.getMouse( ) # between -1 and 1 in both x and y # filmsize = base.cam.node().getLens().getFilmSize() # the actual width of the film size self.mouse_position_before_dragging = mouse_pos self.p_xy_at_init_drag = conventions.getFilmCoordsFromMouseCoords( -self.mouse_position_before_dragging[0], -self.mouse_position_before_dragging[1], p_x_0=0., p_y_0=0.) print("event_managers: mouse_position_before_dragging: ", self.mouse_position_before_dragging) print("event_managers: p_xy_offset: ", self.p_xy_at_init_drag) self._register_event("escape", sys.exit) # FIXME: why? self._register_event('mouse1-up', self.end_dragging) # create an individual task for an individual dragger object self.dragging_mouse_move_task = taskMgr.add(self.update_dragging_task, 'update_dragging_task')
def set_new_panning_orbit_center_from_dragged_mouse( camera_gear, p_xy_offset, original_orbit_center): """ There is a new mouse position, now Args: camera_gear: camera gear with camera member variable p_xy_offset: the origin point (lies in the camera plane) to which the change point (calculated from the mouse displacement) is being added to """ v_cam_forward = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( camera_gear.camera, camera_gear.camera.node().getLens().getViewVector())) v_cam_forward = v_cam_forward / np.linalg.norm(v_cam_forward) # print("--- > View Vector", camera_gear.camera.node().getLens().getViewVector()) v_cam_up = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( camera_gear.camera, camera_gear.camera.node().getLens().getUpVector())) v_cam_up = v_cam_up / np.linalg.norm(v_cam_up) r_cam = math_utils.p3d_to_np(camera_gear.camera.getPos()) e_up = math_utils.p3d_to_np(v_cam_up / np.linalg.norm(v_cam_up)) e_cross = math_utils.p3d_to_np( np.cross(v_cam_forward / np.linalg.norm(v_cam_forward), e_up)) # determine the middle origin of the draggable plane (where the plane intersects the camera's forward vector) # r0_middle_origin = math_utils.LinePlaneCollision(v_cam_forward, r0_obj, v_cam_forward, r_cam) # print("r0_obj", r0_obj) # print("v_cam_forward", v_cam_forward) # print("v_cam_up", v_cam_up) # print("r_cam", r_cam) # print("e_up", e_up) # print("e_cross", e_cross) # print("r0_middle_origin", r0_middle_origin) # -- calculate the bijection between mouse coordinates m_x, m_y and plane coordinates p_x, p_y mouse_pos = base.mouseWatcherNode.getMouse( ) # between -1 and 1 in both x and y # filmsize = self.camera.node().getLens().getFilmSize() # the actual width of the film size # print("p_xy_offset: ", p_xy_offset) p_x, p_y = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[1], p_xy_offset[0], p_xy_offset[1]) # p_x, p_y = conventions.getFilmCoordsFromMouseCoords(mouse_pos[0], mouse_pos[1], 0., 0.) drag_vec = p_x * e_cross + p_y * e_up # print("px: ", p_x, ", py: ", p_y) # print("drag_vec: ", drag_vec) # camera_new_pos = camera_original_pos + drag_vec new_orbit_center = original_orbit_center + (-drag_vec) # print("original orbit center: ", original_orbit_center) # print("new orbit center: ", new_orbit_center) # print("current orbit center: ", camera_gear.get_orbit_center()) camera_gear.set_orbit_center( math_utils.indexable_vec3_to_p3d_Vec3(new_orbit_center))
def update(self): """ """ # print("counter: ", self.counter) # self.counter += 1 r0_obj = math_utils.p3d_to_np(self.pickablepoint.getPos()) v_cam_forward = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( self.camera, self.camera.node().getLens().getViewVector())) v_cam_forward = v_cam_forward / np.linalg.norm(v_cam_forward) # self.camera.node().getLens().getViewVector() v_cam_up = math_utils.p3d_to_np( engine.tq_graphics_basics.tq_render.getRelativeVector( self.camera, self.camera.node().getLens().getUpVector())) v_cam_up = v_cam_up / np.linalg.norm(v_cam_up) r_cam = math_utils.p3d_to_np(self.camera.getPos()) e_up = math_utils.p3d_to_np(v_cam_up / np.linalg.norm(v_cam_up)) e_cross = math_utils.p3d_to_np( np.cross(v_cam_forward / np.linalg.norm(v_cam_forward), e_up)) # determine the middle origin of the draggable plane (where the plane intersects the camera's forward vector) r0_middle_origin = math_utils.LinePlaneCollision( v_cam_forward, r0_obj, v_cam_forward, r_cam) # print("r0_obj", r0_obj) # print("v_cam_forward", v_cam_forward) # print("v_cam_up", v_cam_up) # print("r_cam", r_cam) # print("e_up", e_up) # print("e_cross", e_cross) # print("r0_middle_origin", r0_middle_origin) # -- calculate the bijection between mouse coordinates m_x, m_y and plane coordinates p_x, p_y mouse_pos = base.mouseWatcherNode.getMouse( ) # between -1 and 1 in both x and y # filmsize = base.cam.node().getLens().getFilmSize() # the actual width of the film size # print("p_xy_offset: ", self.p_xy_at_init_drag) p_x, p_y = conventions.getFilmCoordsFromMouseCoords( mouse_pos[0], mouse_pos[1], self.p_xy_at_init_drag[0], self.p_xy_at_init_drag[1]) # p_x, p_y = conventions.getFilmCoordsFromMouseCoords(mouse_pos[0], mouse_pos[1], 0., 0.) drag_vec = p_x * e_cross + p_y * e_up # print("drag_vec", drag_vec) # set the position while dragging self.this_frame_drag_pos = self.position_before_dragging + Vec3( *drag_vec) self.pickablepoint.setPos(self.this_frame_drag_pos) if self.last_frame_drag_pos is None: self.state_changed = True elif self.last_frame_drag_pos == self.this_frame_drag_pos: self.state_changed = False # TODO: in the future, a state change on drag will probably not only be the position else: self.state_changed = True # update the state between frames self.last_frame_drag_pos = self.this_frame_drag_pos