Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCK / HexMatrix.hxx
1
2 // class : Les matrices
3
4 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef __MATRIX_H
24 #define __MATRIX_H
25
26 #include "HexVertex.hxx"
27 #include "HexVector.hxx"
28
29 #include <cmath>
30
31 BEGIN_NAMESPACE_HEXA
32
33 class Matrix 
34 {
35 public:
36     Matrix ();
37     int defTranslation   (Vector* depl);
38     int defTranslation   (double* depl);
39     int defScale         (Vertex* center, double scale);
40     int defScale         (double* center, double scale);
41     int defRotation      (Vertex* center, Vector* depl, double degres);
42     int defSymmetryPoint (Vertex* center);
43     int defSymmetryLine  (Vertex* center, Vector* dir);
44     int defSymmetryPlane (Vertex* center, Vector* normale);
45
46     int perform (Vertex* noeud);
47
48     void getCoeff (double& a11, double& a12, double& a13, double& a14,
49                    double& a21, double& a22, double& a23, double& a24,
50                    double& a31, double& a32, double& a33, double& a34);
51 private:
52     void erase();
53 private:
54     double mat11, mat12, mat13, mat14;
55     double mat21, mat22, mat23, mat24;
56     double mat31, mat32, mat33, mat34;
57 };
58 // ------------------------------------------- Inlining
59 // ========================================================= Constructeur
60 inline Matrix::Matrix ()
61 {
62    erase ();
63 }
64 // ========================================================= erase
65 inline void Matrix::erase ()
66 {
67    mat12 = mat13 = mat14 = 0.0;
68    mat21 = mat23 = mat24 = 0.0;
69    mat31 = mat32 = mat34 = 0.0;
70    mat11 = mat22 = mat33 = 1.0;
71 }
72 // ========================================================= perform
73 inline int Matrix::perform (Vertex* noeud)
74 {
75    double px, py, pz;
76    px = mat11*noeud->getX()+mat12*noeud->getY()+mat13*noeud->getZ()+mat14;
77    py = mat21*noeud->getX()+mat22*noeud->getY()+mat23*noeud->getZ()+mat24;
78    pz = mat31*noeud->getX()+mat32*noeud->getY()+mat33*noeud->getZ()+mat34;
79
80    noeud->setCoord (px, py, pz);
81    return HOK;
82 }
83 // ========================================================= defTranslation
84 inline int Matrix::defTranslation (Vector* boulevard)
85 {
86    erase();
87    mat11 = mat22 = mat33 = 1.0;
88
89    mat14 = boulevard->getDx ();
90    mat24 = boulevard->getDy ();
91    mat34 = boulevard->getDz ();
92
93    return HOK;
94 }
95 // ========================================================= defTranslation (2)
96 inline int Matrix::defTranslation (double* decal)
97 {
98    erase();
99    mat11 = mat22 = mat33 = 1.0;
100
101    mat14 = decal [dir_x];
102    mat24 = decal [dir_y];
103    mat34 = decal [dir_z];
104    return HOK;
105 }
106 // ========================================================= defScale
107 inline int Matrix::defScale (double* center, double scale)
108 {
109    erase();
110    mat11 = mat22 = mat33 = scale;
111
112    mat14 = (1-scale) * center[dir_x];
113    mat24 = (1-scale) * center[dir_y];
114    mat34 = (1-scale) * center[dir_z];
115
116    return HOK;
117 }
118 // ========================================================= defScale
119 inline int Matrix::defScale (Vertex* center, double scale)
120 {
121    if (center==NULL)
122       {
123       erase();
124       return HERR;
125       }
126
127    Real3 coord;
128    int ier = defScale (center->getPoint (coord ), scale);
129    return ier;
130 }
131 // ========================================================= defRotation
132 inline int Matrix::defRotation (Vertex* center, Vector* dir, double degres)
133 {
134    erase();
135
136    double normed = dir->getNorm ();
137    if (normed< 1e-30)
138       return HERR;
139
140    double ux = dir->getDx () / normed;
141    double uy = dir->getDy () / normed;
142    double uz = dir->getDz () / normed;
143
144    double cx = center->getX ();
145    double cy = center->getY ();
146    double cz = center->getZ ();
147
148    double cost = cos (degres*M_PI/180);
149    double sint = sin (degres*M_PI/180);
150
151    mat11 = ux*ux * (1-cost) + cost;
152    mat12 = ux*uy * (1-cost) - uz*sint;
153    mat13 = ux*uz * (1-cost) + uy*sint;
154
155    mat21 = ux*uy * (1-cost) + uz*sint;
156    mat22 = uy*uy * (1-cost) + cost;
157    mat23 = uy*uz * (1-cost) - ux*sint;
158
159    mat31 = ux*uz * (1-cost) - uy*sint;
160    mat32 = uy*uz * (1-cost) + ux*sint;
161    mat33 = uz*uz * (1-cost) + cost;
162
163    mat14 = cx - mat11*cx - mat12*cy - mat13*cz;
164    mat24 = cy - mat21*cx - mat22*cy - mat23*cz;
165    mat34 = cz - mat31*cx - mat32*cy - mat33*cz;
166
167    return HOK;
168 }
169 // ========================================================= defSymmetryPoint
170 inline int Matrix::defSymmetryPoint (Vertex* center)
171 {
172    erase();
173
174    mat11 = mat22 = mat33 = -1;
175
176    mat14 = 2 * center->getX();
177    mat24 = 2 * center->getY();
178    mat34 = 2 * center->getZ();
179
180    return HOK;
181 }
182 // ========================================================= defSymmetryLine
183 //     MH.d = 0        (1)
184 //     CH  = lambda*d  (2)
185 //     MM' = 2MH       (3)
186 // 
187 // (1) et (2) => lambda = ((x-xc)*xd + (y-yc)*yd + (z-zc)*zd) / norme(d)
188 //
189 //     MM' = 2MH (3)
190 // <=> MO + OM' =  2 (MO + OC + CH) 
191 // <=>      OM' =  MO + 2.OC + 2.CH
192 // <=>      OM' = -OM + 2.OC + 2.lambda.d   (2) et (3)
193 //
194 //           x' = -x  + 2*xc + 2*xd*((x-xc)*xd + (y-yc)*yd + (z-zc)*zd)
195 //           y' = -y  + 2*yc + 2*yd*((x-xc)*xd + (y-yc)*yd + (z-zc)*zd)
196 //           z' = -z  + 2*zc + 2*zd*((x-xc)*xd + (y-yc)*yd + (z-zc)*zd)
197 //
198 inline int Matrix::defSymmetryLine (Vertex* center, Vector* dir)
199 {
200    erase ();
201
202    double normed = dir->getNorm ();
203    if (normed< 1e-30)
204       return HERR;
205
206    double xc =  center->getX();
207    double yc =  center->getY();
208    double zc =  center->getZ();
209
210    double xd = dir->getDx() / normed;
211    double yd = dir->getDy() / normed;
212    double zd = dir->getDz() / normed;
213
214    mat11 = 2*xd*xd -1;
215    mat12 = 2*xd*yd;
216    mat13 = 2*xd*zd;
217    mat14 = 2*(xc - xd*(xc*xd + yc*yd + zc*zd));
218
219    mat21 = 2*yd*xd;
220    mat22 = 2*yd*yd - 1;
221    mat23 = 2*yd*zd;
222    mat24 = 2*(yc - yd*(xc*xd + yc*yd + zc*zd));
223
224    mat31 = 2*zd*xd;
225    mat32 = 2*zd*yd;
226    mat33 = 2*zd*zd - 1;
227    mat34 = 2*(zc - zd*(xc*xd + yc*yd + zc*zd));
228
229    return HOK;
230 }
231 // ========================================================= defSymmetryPlane
232 //     CH.n = 0         (1)
233 //     MH   = lambda*n  (2)
234 //     MM'  = 2MH       (3)
235 // 
236 // (1) et (2) => lambda = ((x-xc)*xn + (y-yc)*yn + (z-zc)*zn) / norme(n)
237 //
238 //     MM' = 2MH (3)
239 // <=> MO + OM' =  2.lambda.n
240 // <=>      OM' =  OM + 2.lambda.n
241 //
242 //           x' = x + 2*lambda*xn
243 //           y' = y + 2*lambda*yn
244 //           z' = z + 2*lambda*zn
245 //
246 //           x' = x + 2*xn*((x-xc)*xn + (y-yc)*yn + (z-zc)*zn)
247 //           y' = y + 2*yn*((x-xc)*xn + (y-yc)*yn + (z-zc)*zn)
248 //           z' = z + 2*zn*((x-xc)*xn + (y-yc)*yn + (z-zc)*zn)
249 //
250 inline int Matrix::defSymmetryPlane (Vertex* center, Vector* normale)
251 {
252    erase ();
253
254    double normed = normale->getNorm ();
255    if (normed< 1e-30)
256       return HERR;
257
258    double xc =  center->getX();
259    double yc =  center->getY();
260    double zc =  center->getZ();
261
262    double xn = normale->getDx() / normed;
263    double yn = normale->getDy() / normed;
264    double zn = normale->getDz() / normed;
265
266    mat11 = -2*xn*xn + 1;
267    mat12 = -2*xn*yn;
268    mat13 = -2*xn*zn;
269    mat14 =  2*xn*(xc*xn + yc*yn + zc*zn);
270
271    mat21 = -2*yn*xn;
272    mat22 = -2*yn*yn + 1;
273    mat23 = -2*yn*zn;
274    mat24 =  2*yn*(xc*xn + yc*yn + zc*zn);
275
276    mat31 = -2*zn*xn;
277    mat32 = -2*zn*yn;
278    mat33 = -2*zn*zn + 1;
279    mat34 =  2*zn*(xc*xn + yc*yn + zc*zn);
280
281    return HOK;
282 }
283 // ========================================================= getCoeff
284 inline void Matrix::getCoeff(double& a11, double& a12, double& a13, double& a14,
285                              double& a21, double& a22, double& a23, double& a24,
286                              double& a31, double& a32, double& a33, double& a34)
287 {
288    a11 = mat11;
289    a12 = mat12;
290    a13 = mat13;
291    a14 = mat14;
292
293    a21 = mat21;
294    a22 = mat22;
295    a23 = mat23;
296    a24 = mat24;
297
298    a31 = mat31;
299    a32 = mat32;
300    a33 = mat33;
301    a34 = mat34;
302 }
303 END_NAMESPACE_HEXA
304 #endif