NavlibClient.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import pynavlib.pynavlib_interface as pynav
  2. from UM.Math.Matrix import Matrix
  3. from UM.Math.Vector import Vector
  4. from UM.Math.AxisAlignedBox import AxisAlignedBox
  5. from cura.PickingPass import PickingPass
  6. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  7. from cura.Scene.OverlayNode import OverlayNode, SceneNode
  8. from UM.Resources import Resources
  9. class NavlibClient(pynav.NavlibNavigationModel):
  10. def __init__(self, scene, renderer) -> None:
  11. super().__init__(False, pynav.NavlibOptions.RowMajorOrder)
  12. self._scene = scene
  13. self._renderer = renderer
  14. self._pointer_pick = None
  15. self._was_pick = False
  16. self._hit_selection_only = False
  17. self._picking_pass = None
  18. self._pivot_node = OverlayNode(node=SceneNode(), image_path=Resources.getPath(Resources.Images, "3dx_pivot.png"), size=3.)
  19. def pick(self, x, y, check_selection = False, radius = 0.):
  20. if self._picking_pass is None or radius < 0.:
  21. return None
  22. step = 0.
  23. if radius == 0.:
  24. grid_resolution = 0
  25. else:
  26. grid_resolution = 5
  27. step = (2. * radius) / float(grid_resolution)
  28. min_depth = 99999.
  29. result_position = None
  30. for i in range(grid_resolution + 1):
  31. for j in range(grid_resolution + 1):
  32. coord_x = (x - radius) + i * step
  33. coord_y = (y - radius) + j * step
  34. picked_depth = self._picking_pass.getPickedDepth(coord_x, coord_y)
  35. max_depth = 16777.215
  36. if 0. < picked_depth < max_depth:
  37. valid_hit = True
  38. if check_selection:
  39. selection_pass = self._renderer.getRenderPass("selection")
  40. picked_object_id = selection_pass.getIdAtPosition(coord_x, coord_y)
  41. picked_object = self._scene.findObject(picked_object_id)
  42. from UM.Scene.Selection import Selection
  43. valid_hit = Selection.isSelected(picked_object)
  44. if not valid_hit and grid_resolution > 0.:
  45. continue
  46. elif not valid_hit and grid_resolution == 0.:
  47. return None
  48. if picked_depth < min_depth:
  49. min_depth = picked_depth
  50. result_position = self._picking_pass.getPickedPosition(coord_x, coord_y)
  51. return result_position
  52. def get_pointer_position(self)->pynav.NavlibVector:
  53. from UM.Qt.QtApplication import QtApplication
  54. main_window = QtApplication.getInstance().getMainWindow()
  55. x_n = 2. * main_window._mouse_x / self._scene.getActiveCamera().getViewportWidth() - 1.
  56. y_n = 2. * main_window._mouse_y / self._scene.getActiveCamera().getViewportHeight() - 1.
  57. if self.get_is_view_perspective():
  58. self._was_pick = True
  59. from cura.Utils.Threading import call_on_qt_thread
  60. wrapped_pick = call_on_qt_thread(self.pick)
  61. self._pointer_pick = wrapped_pick(x_n, y_n)
  62. return pynav.NavlibVector(0., 0., 0.)
  63. else:
  64. ray = self._scene.getActiveCamera().getRay(x_n, y_n)
  65. pointer_position = ray.origin + ray.direction
  66. return pynav.NavlibVector(pointer_position.x, pointer_position.y, pointer_position.z)
  67. def get_view_extents(self)->pynav.NavlibBox:
  68. view_width = self._scene.getActiveCamera().getViewportWidth()
  69. view_height = self._scene.getActiveCamera().getViewportHeight()
  70. horizontal_zoom = view_width * self._scene.getActiveCamera().getZoomFactor()
  71. vertical_zoom = view_height * self._scene.getActiveCamera().getZoomFactor()
  72. pt_min = pynav.NavlibVector(-view_width / 2 - horizontal_zoom, -view_height / 2 - vertical_zoom, -9001)
  73. pt_max = pynav.NavlibVector(view_width / 2 + horizontal_zoom, view_height / 2 + vertical_zoom, 9001)
  74. return pynav.NavlibBox(pt_min, pt_max)
  75. def get_view_frustum(self)->pynav.NavlibFrustum:
  76. projection_matrix = self._scene.getActiveCamera().getProjectionMatrix()
  77. half_height = 2. / projection_matrix.getData()[1,1]
  78. half_width = half_height * (projection_matrix.getData()[1,1] / projection_matrix.getData()[0,0])
  79. return pynav.NavlibFrustum(-half_width, half_width, -half_height, half_height, 1., 5000.)
  80. def get_is_view_perspective(self)->bool:
  81. return self._scene.getActiveCamera().isPerspective()
  82. def get_selection_extents(self)->pynav.NavlibBox:
  83. from UM.Scene.Selection import Selection
  84. bounding_box = Selection.getBoundingBox()
  85. if(bounding_box is not None) :
  86. pt_min = pynav.NavlibVector(bounding_box.minimum.x, bounding_box.minimum.y, bounding_box.minimum.z)
  87. pt_max = pynav.NavlibVector(bounding_box.maximum.x, bounding_box.maximum.y, bounding_box.maximum.z)
  88. return pynav.NavlibBox(pt_min, pt_max)
  89. def get_selection_transform(self)->pynav.NavlibMatrix:
  90. return pynav.NavlibMatrix()
  91. def get_is_selection_empty(self)->bool:
  92. from UM.Scene.Selection import Selection
  93. return not Selection.hasSelection()
  94. def get_pivot_visible(self)->bool:
  95. return False
  96. def get_camera_matrix(self)->pynav.NavlibMatrix:
  97. transformation = self._scene.getActiveCamera().getLocalTransformation()
  98. return pynav.NavlibMatrix([[transformation.at(0, 0), transformation.at(0, 1), transformation.at(0, 2), transformation.at(0, 3)],
  99. [transformation.at(1, 0), transformation.at(1, 1), transformation.at(1, 2), transformation.at(1, 3)],
  100. [transformation.at(2, 0), transformation.at(2, 1), transformation.at(2, 2), transformation.at(2, 3)],
  101. [transformation.at(3, 0), transformation.at(3, 1), transformation.at(3, 2), transformation.at(3, 3)]])
  102. def get_coordinate_system(self)->pynav.NavlibMatrix:
  103. return pynav.NavlibMatrix()
  104. def get_front_view(self)->pynav.NavlibMatrix:
  105. return pynav.NavlibMatrix()
  106. def get_model_extents(self)->pynav.NavlibBox:
  107. result_bbox = AxisAlignedBox()
  108. build_volume_bbox = None
  109. for node in DepthFirstIterator(self._scene.getRoot()):
  110. node.setCalculateBoundingBox(True)
  111. if node.__class__.__qualname__ == "CuraSceneNode" :
  112. result_bbox = result_bbox + node.getBoundingBox()
  113. elif node.__class__.__qualname__ == "BuildVolume":
  114. build_volume_bbox = node.getBoundingBox()
  115. if not result_bbox.isValid():
  116. result_bbox = build_volume_bbox
  117. if result_bbox is not None:
  118. pt_min = pynav.NavlibVector(result_bbox.minimum.x, result_bbox.minimum.y, result_bbox.minimum.z)
  119. pt_max = pynav.NavlibVector(result_bbox.maximum.x, result_bbox.maximum.y, result_bbox.maximum.z)
  120. self._scene_center = result_bbox.center
  121. self._scene_radius = (result_bbox.maximum - self._scene_center).length()
  122. return pynav.NavlibBox(pt_min, pt_max)
  123. def get_pivot_position(self)->pynav.NavlibVector:
  124. return pynav.NavlibVector()
  125. def get_hit_look_at(self)->pynav.NavlibVector:
  126. if self._was_pick and self._pointer_pick is not None:
  127. return pynav.NavlibVector(self._pointer_pick.x, self._pointer_pick.y, self._pointer_pick.z)
  128. elif self._was_pick and self._pointer_pick is None:
  129. return None
  130. from cura.Utils.Threading import call_on_qt_thread
  131. wrapped_pick = call_on_qt_thread(self.pick)
  132. picked_position = wrapped_pick(0, 0, self._hit_selection_only, 0.5)
  133. if picked_position is not None:
  134. return pynav.NavlibVector(picked_position.x, picked_position.y, picked_position.z)
  135. def get_units_to_meters(self)->float:
  136. return 0.05
  137. def is_user_pivot(self)->bool:
  138. return False
  139. def set_camera_matrix(self, matrix : pynav.NavlibMatrix):
  140. # !!!!!!
  141. # Hit testing in Orthographic view is not reliable
  142. # Picking starts in camera position, not on near plane
  143. # which results in wrong depth values (visible geometry
  144. # cannot be picked properly) - Workaround needed (camera position offset)
  145. # !!!!!!
  146. if not self.get_is_view_perspective():
  147. affine = matrix._matrix
  148. direction = Vector(-affine[0][2], -affine[1][2], -affine[2][2])
  149. distance = self._scene_center - Vector(affine[0][3], affine[1][3], affine[2][3])
  150. cos_value = direction.dot(distance.normalized())
  151. offset = 0.
  152. if (distance.length() < self._scene_radius) and (cos_value > 0.):
  153. offset = self._scene_radius
  154. elif (distance.length() < self._scene_radius) and (cos_value < 0.):
  155. offset = 2. * self._scene_radius
  156. elif (distance.length() > self._scene_radius) and (cos_value < 0.):
  157. offset = 2. * distance.length()
  158. matrix._matrix[0][3] = matrix._matrix[0][3] - offset * direction.x
  159. matrix._matrix[1][3] = matrix._matrix[1][3] - offset * direction.y
  160. matrix._matrix[2][3] = matrix._matrix[2][3] - offset * direction.z
  161. transformation = Matrix(data = matrix._matrix)
  162. self._scene.getActiveCamera().setTransformation(transformation)
  163. active_camera = self._scene.getActiveCamera()
  164. if active_camera.isPerspective():
  165. camera_position = active_camera.getWorldPosition()
  166. dist = (camera_position - self._pivot_node.getWorldPosition()).length()
  167. scale = dist / 400.
  168. if scale < 1.:
  169. scale = scale * scale
  170. else:
  171. view_width = active_camera.getViewportWidth()
  172. current_size = view_width + (2. * active_camera.getZoomFactor() * view_width)
  173. scale = current_size / view_width * 5.
  174. self._pivot_node.scale(scale)
  175. def set_view_extents(self, extents: pynav.NavlibBox):
  176. view_width = self._scene.getActiveCamera().getViewportWidth()
  177. new_zoom = (extents._min._x + view_width / 2.) / - view_width
  178. self._scene.getActiveCamera().setZoomFactor(new_zoom)
  179. def set_hit_selection_only(self, onlySelection : bool):
  180. self._hit_selection_only = onlySelection
  181. def set_motion_flag(self, motion : bool):
  182. if motion:
  183. width = self._scene.getActiveCamera().getViewportWidth()
  184. height = self._scene.getActiveCamera().getViewportHeight()
  185. self._picking_pass = PickingPass(width, height)
  186. self._renderer.addRenderPass(self._picking_pass)
  187. else:
  188. self._was_pick = False
  189. self._renderer.removeRenderPass(self._picking_pass)
  190. def set_pivot_position(self, position):
  191. self._pivot_node._target_node.setPosition(position=Vector(position._x, position._y, position._z), transform_space = SceneNode.TransformSpace.World)
  192. def set_pivot_visible(self, visible):
  193. if visible:
  194. self._scene.getRoot().addChild(self._pivot_node)
  195. else:
  196. self._scene.getRoot().removeChild(self._pivot_node)