/[escript]/trunk/pycad/py_src/transformations.py
ViewVC logotype

Diff of /trunk/pycad/py_src/transformations.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2179 by ksteube, Thu Sep 25 06:43:44 2008 UTC revision 2180 by caltinay, Thu Dec 18 00:30:25 2008 UTC
# Line 42  DEG=math.pi/180. Line 42  DEG=math.pi/180.
42  RAD=1.  RAD=1.
43  class Transformation(object):  class Transformation(object):
44     """     """
45     general class to define an affine transformation x->Ax+b     General class to define an affine transformation M{x->Ax+b}.
46     """     """
47     def __init__(self):     def __init__(self):
48         """         """
49         create a linear transformation         Creates a linear transformation.
50         """         """
51         pass         pass
52    
53     def __call__(self,x=numarray.zeros((3,))):     def __call__(self,x=numarray.zeros((3,))):
54         """         """
55         applies transformation to x         Applies transformation to C{x}.
56         """         """
57         raise NotImplementeError()         raise NotImplementeError()
58    
59  class Translation(Transformation):  class Translation(Transformation):
60      """      """
61      defines a translation 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=numarray.zeros((3,),type=_TYPE)):
64         """         """
65         create linear transformation 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=numarray.array(b,_TYPE)
69    
70      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numarray.zeros((3,))):
71         """         """
72         applies translation to x         Applies translation to C{x}.
73         """         """
74         return numarray.array(x,_TYPE)+self.__b         return numarray.array(x,_TYPE)+self.__b
75          
76  class Rotatation(Transformation):  class Rotatation(Transformation):
77      """      """
78      defines a rotation      Defines a rotation.
79      """      """
80      def __init__(self,axis=numarray.ones((3,),type=_TYPE),point=numarray.zeros((3,),type=_TYPE),angle=0.*RAD):      def __init__(self,axis=numarray.ones((3,),type=_TYPE),point=numarray.zeros((3,),type=_TYPE),angle=0.*RAD):
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=numarray.array(axis,type=_TYPE)
85         self.__point=numarray.array(point,type=_TYPE)         self.__point=numarray.array(point,type=_TYPE)
# Line 85  class Rotatation(Transformation): Line 88  class Rotatation(Transformation):
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=numarray.zeros((3,))):
93         """         """
94         applies rotatation to x         Applies the rotation to C{x}.
95         """         """
96         x=numarray.array(x,_TYPE)         x=numarray.array(x,_TYPE)
97         z=x-self.__point         z=x-self.__point
# Line 100  class Rotatation(Transformation): Line 104  class Rotatation(Transformation):
104           lax2=numarray.dot(axis2,axis2)           lax2=numarray.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
108           else:           else:
109              return x              return x
110         else:         else:
111           return x           return x
112    
113  def _cross(x, y):  def _cross(x, y):
114      """      """
115      Returns the cross product of x and 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 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)
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=numarray.zeros((3,),type=_TYPE)):
124         """         """
125         creates a dilation with a center an a given expansion/contraction factor         Creates a dilation with a center and a given expansion/contraction
126           factor.
127         """         """
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=numarray.array(center,type=_TYPE)
132    
133      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numarray.zeros((3,))):
134         """         """
135         applies dilation to x         Applies dilation to C{x}.
136         """         """
137         x=numarray.array(x,_TYPE)         x=numarray.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 plain      Defines a reflection on a plane.
143      """      """
144      def __init__(self,normal=numarray.ones((3,),type=_TYPE),offset=0.):      def __init__(self,normal=numarray.ones((3,),type=_TYPE),offset=0.):
145         """         """
146         defines a reflection on a plain 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=numarray.array(normal,type=_TYPE)
149         ln=math.sqrt(numarray.dot(self.__normal,self.__normal))         ln=math.sqrt(numarray.dot(self.__normal,self.__normal))
# Line 147  class Reflection(Transformation): Line 154  class Reflection(Transformation):
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=numarray.dot(numarray.array(offset,type=_TYPE),self.__normal)
157    
158      def __call__(self,x=numarray.zeros((3,))):      def __call__(self,x=numarray.zeros((3,))):
159         """         """
160         applies reflection to x         Applies reflection to C{x}.
161         """         """
162         x=numarray.array(x,_TYPE)         x=numarray.array(x,_TYPE)
163         return x - 2*(numarray.dot(x,self.__normal)-self.__offset)*self.__normal         return x - 2*(numarray.dot(x,self.__normal)-self.__offset)*self.__normal
164    

Legend:
Removed from v.2179  
changed lines
  Added in v.2180

  ViewVC Help
Powered by ViewVC 1.1.26