scripted/manipulatorMath.py

scripted/manipulatorMath.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 import maya.OpenMaya as OpenMaya
40 
41 #
42 # simple plane math class
43 #
44 class planeMath:
45  a = None
46  b = None
47  c = None
48  d = None
49 
50  def __init__(self):
51  a = 0
52  b = 0
53  c = 0
54  d = 0
55 
56  def setPlane(self,pointOnPlane,normalToPlane):
57  _normalToPlane = normalToPlane
58  _normalToPlane.normalize()
59 
60  # Calculate a,b,c,d based on input
61  self.a = _normalToPlane.x
62  self.b = _normalToPlane.y
63  self.c = _normalToPlane.z
64  self.d = -(self.a*pointOnPlane.x + self.b*pointOnPlane.y + self.c*pointOnPlane.z)
65 
66  def intersect(self,linePoint,lineDirection):
67  intersectionPoint = OpenMaya.MPoint()
68  denominator = self.a*lineDirection.x + self.b*lineDirection.y + self.c*lineDirection.z
69  # Verify that the vector and the plane are not parallel.
70  if denominator < .00001:
71  return (False,intersectionPoint)
72 
73  t = -(self.d + self.a*linePoint.x + self.b*linePoint.y + self.c*linePoint.z) / denominator
74 
75  # Calculate the intersection point.
76  scaledLineDirection = OpenMaya.MVector(lineDirection.x*t,lineDirection.y*t,lineDirection.z*t)
77  intersectionPoint = linePoint + scaledLineDirection
78 
79  return (True,intersectionPoint)
80 
81 
82 #
83 # simple line math class
84 #
85 class lineMath:
86 
87  point = None
88  direction = None
89 
90  def setLine(self,linePoint,lineDirection):
91  self.point = linePoint
92  self.direction = lineDirection
93  self.direction.normalize()
94 
95  def closestPoint(self,toPoint):
96  t = self.direction * ( toPoint - self.point )
97  closest = self.point + ( self.direction * t )
98  return (True,closest)
99 
100 
101 #
102 # simple degree radian converted
103 class degreeRadianConverter:
104  M_PI = 3.14159265358979323846
105  def degreesToRadians(self,degrees):
106  return degrees * ( self.M_PI / 180.0 )
107  def radiansToDegrees(self,radians):
108  return radians * ( 180.0 / self.M_PI)
109 
110 #
111 # utility function to return the
112 # value which has the max abs
113 # -10,3,4 returns -10
114 def maxOfAbsThree(a,b,c):
115  aa = abs(a)
116  ab = abs(b)
117  ac = abs(c)
118  if aa > ab and aa > ac:
119  return a
120  if ab > aa and ab > ac:
121  return b
122  return c
123 
124 #
125 # tests for the math classes
126 #
127 def testModule():
128  # degreeRadianConverter
129  drc = degreeRadianConverter()
130  r = drc.degreesToRadians( 45 )
131  print r, " ", r*4
132 
133  d = drc.radiansToDegrees( drc.M_PI/4.0 )
134  print d
135 
136  # lineMath
137  lm = lineMath()
138  point = OpenMaya.MPoint(0,1,0)
139  direction = OpenMaya.MVector( 1,1,0)
140  lm.setLine( point, direction )
141  toPoint = OpenMaya.MPoint(3,0,0)
142  (worked,closestPoint) = lm.closestPoint(toPoint)
143  if worked:
144  print "closest point to line: %g %g %g" % (closestPoint.x,closestPoint.y,closestPoint.z)
145  else:
146  print "Failed to find closest point to line"
147 
148 #
149 # invoke test if run from the
150 # command line
151 #
152 if __name__ == "__main__":
153  testModule()
154 
155 
156