revision 2406 by jfenwick, Thu Apr 23 23:58:29 2009 UTC revision 2407 by jfenwick, Fri May 1 01:28:06 2009 UTC
# Line 34  transformations Line 34  transformations
34
35  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
36
37  import numarray  import numpy
38  import math  import math
39
40  _TYPE=numarray.Float64  _TYPE=numpy.float64
41  DEG=math.pi/180.  DEG=math.pi/180.
43  class Transformation(object):  class Transformation(object):
# Line 50  class Transformation(object): Line 50  class Transformation(object):
50         """         """
51         pass         pass
52
53     def __call__(self,x=numarray.zeros((3,))):     def __call__(self,x=numpy.zeros((3,))):
54         """         """
55         Applies transformation to C{x}.         Applies transformation to C{x}.
56         """         """
# Line 60  class Translation(Transformation): Line 60  class Translation(Transformation):
60      """      """
61      Defines a translation M{x->x+b}.      Defines a translation M{x->x+b}.
62      """      """
63      def __init__(self,b=numarray.zeros((3,),type=_TYPE)):      def __init__(self,b=numpy.zeros((3,),dtype=_TYPE)):
64         """         """
65         Creates the linear transformation M{x->x+b}.         Creates the linear transformation M{x->x+b}.
66         """         """
67         super(Translation, self).__init__()         super(Translation, self).__init__()
68         self.__b=numarray.array(b,_TYPE)         self.__b=numpy.array(b,_TYPE)
69
70      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numpy.zeros((3,))):
71         """         """
72         Applies translation to C{x}.         Applies translation to C{x}.
73         """         """
74         return numarray.array(x,_TYPE)+self.__b         return numpy.array(x,_TYPE)+self.__b
75
76  class Rotatation(Transformation):  class Rotatation(Transformation):
77      """      """
78      Defines a rotation.      Defines a rotation.
79      """      """
81         """         """
82         Creates a rotation using an axis and a point on the axis.         Creates a rotation using an axis and a point on the axis.
83         """         """
84         self.__axis=numarray.array(axis,type=_TYPE)         self.__axis=numpy.array(axis,dtype=_TYPE)
85         self.__point=numarray.array(point,type=_TYPE)         self.__point=numpy.array(point,dtype=_TYPE)
86         lax=numarray.dot(self.__axis,self.__axis)         lax=numpy.dot(self.__axis,self.__axis)
87         if not lax>0:         if not lax>0:
88            raise ValueError("points must be distinct.")            raise ValueError("points must be distinct.")
89         self.__axis/=math.sqrt(lax)         self.__axis/=math.sqrt(lax)
90         self.__angle=float(angle)         self.__angle=float(angle)
91
92      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numpy.zeros((3,))):
93         """         """
94         Applies the rotation to C{x}.         Applies the rotation to C{x}.
95         """         """
96         x=numarray.array(x,_TYPE)         x=numpy.array(x,_TYPE)
97         z=x-self.__point         z=x-self.__point
98         z0=numarray.dot(z,self.__axis)         z0=numpy.dot(z,self.__axis)
99         z_per=z-z0*self.__axis         z_per=z-z0*self.__axis
100         lz_per=numarray.dot(z_per,z_per)         lz_per=numpy.dot(z_per,z_per)
101         if lz_per>0:         if lz_per>0:
102           axis1=z_per/math.sqrt(lz_per)           axis1=z_per/math.sqrt(lz_per)
103           axis2=_cross(axis1,self.__axis)           axis2=_cross(axis1,self.__axis)
104           lax2=numarray.dot(axis2,axis2)           lax2=numpy.dot(axis2,axis2)
105           if lax2>0:           if lax2>0:
106              axis2/=math.sqrt(lax2)              axis2/=math.sqrt(lax2)
107              return z0*self.__axis+math.sqrt(lz_per)*(math.cos(self.__angle)*axis1-math.sin(self.__angle)*axis2)+self.__point              return z0*self.__axis+math.sqrt(lz_per)*(math.cos(self.__angle)*axis1-math.sin(self.__angle)*axis2)+self.__point
# Line 114  def _cross(x, y): Line 114  def _cross(x, y):
114      """      """
115      Returns the cross product of C{x} and C{y}.      Returns the cross product of C{x} and C{y}.
116      """      """
117      return numarray.array([x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2], x[0] * y[1] - x[1] * y[0]], _TYPE)      return numpy.array([x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2], x[0] * y[1] - x[1] * y[0]], _TYPE)
118
119  class Dilation(Transformation):  class Dilation(Transformation):
120      """      """
121      Defines a dilation.      Defines a dilation.
122      """      """
123      def __init__(self,factor=1.,center=numarray.zeros((3,),type=_TYPE)):      def __init__(self,factor=1.,center=numpy.zeros((3,),dtype=_TYPE)):
124         """         """
125         Creates a dilation with a center and a given expansion/contraction         Creates a dilation with a center and a given expansion/contraction
126         factor.         factor.
# Line 128  class Dilation(Transformation): Line 128  class Dilation(Transformation):
128         if not abs(factor)>0:         if not abs(factor)>0:
129            raise ValueError("factor must be non-zero.")            raise ValueError("factor must be non-zero.")
130         self.__factor=factor         self.__factor=factor
131         self.__center=numarray.array(center,type=_TYPE)         self.__center=numpy.array(center,dtype=_TYPE)
132
133      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numpy.zeros((3,))):
134         """         """
135         Applies dilation to C{x}.         Applies dilation to C{x}.
136         """         """
137         x=numarray.array(x,_TYPE)         x=numpy.array(x,_TYPE)
138         return self.__factor*(x-self.__center)+self.__center         return self.__factor*(x-self.__center)+self.__center
139
140  class Reflection(Transformation):  class Reflection(Transformation):
141      """      """
142      Defines a reflection on a plane.      Defines a reflection on a plane.
143      """      """
144      def __init__(self,normal=numarray.ones((3,),type=_TYPE),offset=0.):      def __init__(self,normal=numpy.ones((3,),dtype=_TYPE),offset=0.):
145         """         """
146         Defines a reflection on a plane defined in normal form.         Defines a reflection on a plane defined in normal form.
147         """         """
148         self.__normal=numarray.array(normal,type=_TYPE)         self.__normal=numpy.array(normal,dtype=_TYPE)
149         ln=math.sqrt(numarray.dot(self.__normal,self.__normal))         ln=math.sqrt(numpy.dot(self.__normal,self.__normal))
150         if not ln>0.:         if not ln>0.:
151            raise ValueError("normal must have positive length.")            raise ValueError("normal must have positive length.")
152         self.__normal/=ln         self.__normal/=ln
153         if isinstance(offset,float) or isinstance(offset,int):         if isinstance(offset,float) or isinstance(offset,int):
154            self.__offset=offset/ln            self.__offset=offset/ln
155         else:         else:
156            self.__offset=numarray.dot(numarray.array(offset,type=_TYPE),self.__normal)            self.__offset=numpy.dot(numpy.array(offset,dtype=_TYPE),self.__normal)
157
158      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numpy.zeros((3,))):
159         """         """
160         Applies reflection to C{x}.         Applies reflection to C{x}.
161         """         """
162         x=numarray.array(x,_TYPE)         x=numpy.array(x,_TYPE)
163         return x - 2*(numarray.dot(x,self.__normal)-self.__offset)*self.__normal         return x - 2*(numpy.dot(x,self.__normal)-self.__offset)*self.__normal
164

Legend:
 Removed from v.2406 changed lines Added in v.2407