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