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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1312 - (show annotations)
Mon Sep 24 06:18:44 2007 UTC (11 years, 11 months ago) by ksteube
File MIME type: text/x-python
File size: 4894 byte(s)
The MPI branch is hereby closed. All future work should be in trunk.

Previously in revision 1295 I merged the latest changes to trunk into trunk-mpi-branch.
In this revision I copied all files from trunk-mpi-branch over the corresponding
trunk files. I did not use 'svn merge', it was a copy.

1 #
2 # $Id$
3 #
4 #######################################################
5 #
6 # Copyright 2003-2007 by ACceSS MNRF
7 # Copyright 2007 by University of Queensland
8 #
9 # http://esscc.uq.edu.au
10 # Primary Business: Queensland, Australia
11 # Licensed under the Open Software License version 3.0
12 # http://www.opensource.org/licenses/osl-3.0.php
13 #
14 #######################################################
15 #
16
17 """
18 transformations
19
20 @var __author__: name of author
21 @var __copyright__: copyrights
22 @var __license__: licence agreement
23 @var __url__: url entry point on documentation
24 @var __version__: version
25 @var __date__: date of the version
26 @var DEG: unit of degree
27 @var RAD: unit of radiant
28 """
29
30 __author__="Lutz Gross, l.gross@uq.edu.au"
31 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
32 http://www.access.edu.au
33 Primary Business: Queensland, Australia"""
34 __license__="""Licensed under the Open Software License version 3.0
35 http://www.opensource.org/licenses/osl-3.0.php"""
36 __url__="http://www.iservo.edu.au/esys/escript"
37 __version__="$Revision:$"
38 __date__="$Date:$"
39
40 import numarray
41 import math
42
43 _TYPE=numarray.Float64
44 DEG=math.pi/180.
45 RAD=1.
46 class Transformation(object):
47 """
48 general class to define an affine transformation x->Ax+b
49 """
50 def __init__(self):
51 """
52 create a linear transformation
53 """
54 pass
55 def __call__(self,x=numarray.zeros((3,))):
56 """
57 applies transformation to x
58 """
59 raise NotImplementeError()
60 class Translation(Transformation):
61 """
62 defines a translation x->x+b
63 """
64 def __init__(self,b=numarray.zeros((3,),type=_TYPE)):
65 """
66 create linear transformation x->x+b
67 """
68 super(Translation, self).__init__()
69 self.__b=numarray.array(b,_TYPE)
70 def __call__(self,x=numarray.zeros((3,))):
71 """
72 applies translation to x
73 """
74 return numarray.array(x,_TYPE)+self.__b
75
76 class Rotatation(Transformation):
77 """
78 defines a rotation
79 """
80 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
83 """
84 self.__axis=numarray.array(axis,type=_TYPE)
85 self.__point=numarray.array(point,type=_TYPE)
86 lax=numarray.dot(self.__axis,self.__axis)
87 if not lax>0:
88 raise ValueError("points must be distinct.")
89 self.__axis/=math.sqrt(lax)
90 self.__angle=float(angle)
91 def __call__(self,x=numarray.zeros((3,))):
92 """
93 applies rotatation to x
94 """
95 x=numarray.array(x,_TYPE)
96 z=x-self.__point
97 z0=numarray.dot(z,self.__axis)
98 z_per=z-z0*self.__axis
99 lz_per=numarray.dot(z_per,z_per)
100 if lz_per>0:
101 axis1=z_per/math.sqrt(lz_per)
102 axis2=_cross(axis1,self.__axis)
103 lax2=numarray.dot(axis2,axis2)
104 if lax2>0:
105 axis2/=math.sqrt(lax2)
106 return z0*self.__axis+math.sqrt(lz_per)*(math.cos(self.__angle)*axis1-math.sin(self.__angle)*axis2)+self.__point
107 else:
108 return x
109 else:
110 return x
111 def _cross(x, y):
112 """
113 Returns the cross product of x and y
114 """
115 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)
116
117 class Dilation(Transformation):
118 """
119 defines a dilation
120 """
121 def __init__(self,factor=1.,center=numarray.zeros((3,),type=_TYPE)):
122 """
123 creates a dilation with a center an a given expansion/contraction factor
124 """
125 if not abs(factor)>0:
126 raise ValueError("factor must be non-zero.")
127 self.__factor=factor
128 self.__center=numarray.array(center,type=_TYPE)
129 def __call__(self,x=numarray.zeros((3,))):
130 """
131 applies dilation to x
132 """
133 x=numarray.array(x,_TYPE)
134 return self.__factor*(x-self.__center)+self.__center
135
136 class Reflection(Transformation):
137 """
138 defines a reflection on a plain
139 """
140 def __init__(self,normal=numarray.ones((3,),type=_TYPE),offset=0.):
141 """
142 defines a reflection on a plain defined in normal form
143 """
144 self.__normal=numarray.array(normal,type=_TYPE)
145 ln=math.sqrt(numarray.dot(self.__normal,self.__normal))
146 if not ln>0.:
147 raise ValueError("normal must have positive length.")
148 self.__normal/=ln
149 if isinstance(offset,float) or isinstance(offset,int):
150 self.__offset=offset/ln
151 else:
152 self.__offset=numarray.dot(numarray.array(offset,type=_TYPE),self.__normal)
153 def __call__(self,x=numarray.zeros((3,))):
154 """
155 applies reflection to x
156 """
157 x=numarray.array(x,_TYPE)
158 return x - 2*(numarray.dot(x,self.__normal)-self.__offset)*self.__normal

  ViewVC Help
Powered by ViewVC 1.1.26