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