seGeometry.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #################################################################
  2. # seGeometry.py
  3. # Originally from DirectGeometry.py
  4. # Altered by Yi-Hong Lin, [email protected], 2004
  5. #
  6. # We didn't change anything essential.
  7. # Just because we customized the seSession from DirectSession,
  8. # So we need related files can follow the change.
  9. # However, we don't want to change anything inside the original directool
  10. # to let them can work with our scene editor.
  11. # (If we do change original directools, it will force user has to install the latest version of OUR Panda)
  12. #
  13. #################################################################
  14. from pandac.PandaModules import *
  15. from direct.directtools.DirectGlobals import *
  16. from direct.directtools.DirectUtil import *
  17. import math
  18. class LineNodePath(NodePath):
  19. def __init__(self, parent = None, name = None,
  20. thickness = 1.0, colorVec = VBase4(1)):
  21. # Initialize the superclass
  22. NodePath.__init__(self)
  23. if parent is None:
  24. parent = hidden
  25. # Attach a geomNode to the parent and set self to be
  26. # the resulting node path
  27. self.lineNode = GeomNode("lineNode")
  28. self.assign(parent.attachNewNode( self.lineNode ))
  29. if name:
  30. self.setName(name)
  31. # Create a lineSegs object to hold the line
  32. ls = self.lineSegs = LineSegs()
  33. # Initialize the lineSegs parameters
  34. ls.setThickness(thickness)
  35. ls.setColor(colorVec)
  36. def moveTo( self, *_args ):
  37. apply( self.lineSegs.moveTo, _args )
  38. def drawTo( self, *_args ):
  39. apply( self.lineSegs.drawTo, _args )
  40. def create( self, frameAccurate = 0 ):
  41. self.lineSegs.create( self.lineNode, frameAccurate )
  42. def reset( self ):
  43. self.lineSegs.reset()
  44. self.lineNode.removeAllGeoms()
  45. def isEmpty( self ):
  46. return self.lineSegs.isEmpty()
  47. def setThickness( self, thickness ):
  48. self.lineSegs.setThickness( thickness )
  49. def setColor( self, *_args ):
  50. apply( self.lineSegs.setColor, _args )
  51. def setVertex( self, *_args):
  52. apply( self.lineSegs.setVertex, _args )
  53. def setVertexColor( self, vertex, *_args ):
  54. apply( self.lineSegs.setVertexColor, (vertex,) + _args )
  55. def getCurrentPosition( self ):
  56. return self.lineSegs.getCurrentPosition()
  57. def getNumVertices( self ):
  58. return self.lineSegs.getNumVertices()
  59. def getVertex( self, index ):
  60. return self.lineSegs.getVertex(index)
  61. def getVertexColor( self ):
  62. return self.lineSegs.getVertexColor()
  63. def drawArrow(self, sv, ev, arrowAngle, arrowLength):
  64. """
  65. Do the work of moving the cursor around to draw an arrow from
  66. sv to ev. Hack: the arrows take the z value of the end point
  67. """
  68. self.moveTo(sv)
  69. self.drawTo(ev)
  70. v = sv - ev
  71. # Find the angle of the line
  72. angle = math.atan2(v[1], v[0])
  73. # Get the arrow angles
  74. a1 = angle + deg2Rad(arrowAngle)
  75. a2 = angle - deg2Rad(arrowAngle)
  76. # Get the arrow points
  77. a1x = arrowLength * math.cos(a1)
  78. a1y = arrowLength * math.sin(a1)
  79. a2x = arrowLength * math.cos(a2)
  80. a2y = arrowLength * math.sin(a2)
  81. z = ev[2]
  82. self.moveTo(ev)
  83. self.drawTo(Point3(ev + Point3(a1x, a1y, z)))
  84. self.moveTo(ev)
  85. self.drawTo(Point3(ev + Point3(a2x, a2y, z)))
  86. def drawArrow2d(self, sv, ev, arrowAngle, arrowLength):
  87. """
  88. Do the work of moving the cursor around to draw an arrow from
  89. sv to ev. Hack: the arrows take the z value of the end point
  90. """
  91. self.moveTo(sv)
  92. self.drawTo(ev)
  93. v = sv - ev
  94. # Find the angle of the line
  95. angle = math.atan2(v[2], v[0])
  96. # Get the arrow angles
  97. a1 = angle + deg2Rad(arrowAngle)
  98. a2 = angle - deg2Rad(arrowAngle)
  99. # Get the arrow points
  100. a1x = arrowLength * math.cos(a1)
  101. a1y = arrowLength * math.sin(a1)
  102. a2x = arrowLength * math.cos(a2)
  103. a2y = arrowLength * math.sin(a2)
  104. self.moveTo(ev)
  105. self.drawTo(Point3(ev + Point3(a1x, 0.0, a1y)))
  106. self.moveTo(ev)
  107. self.drawTo(Point3(ev + Point3(a2x, 0.0, a2y)))
  108. def drawLines(self, lineList):
  109. """
  110. Given a list of lists of points, draw a separate line for each list
  111. """
  112. for pointList in lineList:
  113. apply(self.moveTo, pointList[0])
  114. for point in pointList[1:]:
  115. apply(self.drawTo, point)
  116. ##
  117. ## Given a point in space, and a direction, find the point of intersection
  118. ## of that ray with a plane at the specified origin, with the specified normal
  119. def planeIntersect (lineOrigin, lineDir, planeOrigin, normal):
  120. t = 0
  121. offset = planeOrigin - lineOrigin
  122. t = offset.dot(normal) / lineDir.dot(normal)
  123. hitPt = lineDir * t
  124. return hitPt + lineOrigin
  125. def getNearProjectionPoint(nodePath):
  126. # Find the position of the projection of the specified node path
  127. # on the near plane
  128. origin = nodePath.getPos(SEditor.camera)
  129. # project this onto near plane
  130. if origin[1] != 0.0:
  131. return origin * (SEditor.dr.near / origin[1])
  132. else:
  133. # Object is coplaner with camera, just return something reasonable
  134. return Point3(0, SEditor.dr.near, 0)
  135. def getScreenXY(nodePath):
  136. # Where does the node path's projection fall on the near plane
  137. nearVec = getNearProjectionPoint(nodePath)
  138. # Clamp these coordinates to visible screen
  139. nearX = CLAMP(nearVec[0], SEditor.dr.left, SEditor.dr.right)
  140. nearY = CLAMP(nearVec[2], SEditor.dr.bottom, SEditor.dr.top)
  141. # What percentage of the distance across the screen is this?
  142. percentX = (nearX - SEditor.dr.left)/SEditor.dr.nearWidth
  143. percentY = (nearY - SEditor.dr.bottom)/SEditor.dr.nearHeight
  144. # Map this percentage to the same -1 to 1 space as the mouse
  145. screenXY = Vec3((2 * percentX) - 1.0,nearVec[1],(2 * percentY) - 1.0)
  146. # Return the resulting value
  147. return screenXY
  148. def getCrankAngle(center):
  149. # Used to compute current angle of mouse (relative to the coa's
  150. # origin) in screen space
  151. x = SEditor.dr.mouseX - center[0]
  152. y = SEditor.dr.mouseY - center[2]
  153. return (180 + rad2Deg(math.atan2(y,x)))
  154. def relHpr(nodePath, base, h, p, r):
  155. # Compute nodePath2newNodePath relative to base coordinate system
  156. # nodePath2base
  157. mNodePath2Base = nodePath.getMat(base)
  158. # delta scale, orientation, and position matrix
  159. mBase2NewBase = Mat4()
  160. composeMatrix(mBase2NewBase, UNIT_VEC, VBase3(h,p,r), ZERO_VEC,
  161. CSDefault)
  162. # base2nodePath
  163. mBase2NodePath = base.getMat(nodePath)
  164. # nodePath2 Parent
  165. mNodePath2Parent = nodePath.getMat()
  166. # Compose the result
  167. resultMat = mNodePath2Base * mBase2NewBase
  168. resultMat = resultMat * mBase2NodePath
  169. resultMat = resultMat * mNodePath2Parent
  170. # Extract and apply the hpr
  171. hpr = Vec3(0)
  172. decomposeMatrix(resultMat, VBase3(), hpr, VBase3(),
  173. CSDefault)
  174. nodePath.setHpr(hpr)
  175. # Quaternion interpolation
  176. def qSlerp(startQuat, endQuat, t):
  177. startQ = Quat(startQuat)
  178. destQuat = Quat.identQuat()
  179. # Calc dot product
  180. cosOmega = (startQ.getI() * endQuat.getI() +
  181. startQ.getJ() * endQuat.getJ() +
  182. startQ.getK() * endQuat.getK() +
  183. startQ.getR() * endQuat.getR())
  184. # If the above dot product is negative, it would be better to
  185. # go between the negative of the initial and the final, so that
  186. # we take the shorter path.
  187. if ( cosOmega < 0.0 ):
  188. cosOmega *= -1
  189. startQ.setI(-1 * startQ.getI())
  190. startQ.setJ(-1 * startQ.getJ())
  191. startQ.setK(-1 * startQ.getK())
  192. startQ.setR(-1 * startQ.getR())
  193. if ((1.0 + cosOmega) > Q_EPSILON):
  194. # usual case
  195. if ((1.0 - cosOmega) > Q_EPSILON):
  196. # usual case
  197. omega = math.acos(cosOmega)
  198. sinOmega = math.sin(omega)
  199. startScale = math.sin((1.0 - t) * omega)/sinOmega
  200. endScale = math.sin(t * omega)/sinOmega
  201. else:
  202. # ends very close
  203. startScale = 1.0 - t
  204. endScale = t
  205. destQuat.setI(startScale * startQ.getI() +
  206. endScale * endQuat.getI())
  207. destQuat.setJ(startScale * startQ.getJ() +
  208. endScale * endQuat.getJ())
  209. destQuat.setK(startScale * startQ.getK() +
  210. endScale * endQuat.getK())
  211. destQuat.setR(startScale * startQ.getR() +
  212. endScale * endQuat.getR())
  213. else:
  214. # ends nearly opposite
  215. destQuat.setI(-startQ.getJ())
  216. destQuat.setJ(startQ.getI())
  217. destQuat.setK(-startQ.getR())
  218. destQuat.setR(startQ.getK())
  219. startScale = math.sin((0.5 - t) * math.pi)
  220. endScale = math.sin(t * math.pi)
  221. destQuat.setI(startScale * startQ.getI() +
  222. endScale * endQuat.getI())
  223. destQuat.setJ(startScale * startQ.getJ() +
  224. endScale * endQuat.getJ())
  225. destQuat.setK(startScale * startQ.getK() +
  226. endScale * endQuat.getK())
  227. return destQuat