1 // File gp_Ax2d.cxx, JCV 06/90
2 // File gp_Trsf.cxx, REG 03/06/90 nouvelle version
3 // JCV 08/01/91 Modif introduction des classes Mat2d et XY dans gp
5 #define No_Standard_OutOfRange
7 #include <gp_Trsf2d.ixx>
9 #include <Standard_ConstructionError.hxx>
11 void gp_Trsf2d::SetMirror (const gp_Ax2d& A)
15 const gp_Dir2d& V = A.Direction ();
16 const gp_Pnt2d& P = A.Location ();
17 Standard_Real VX = V.X();
18 Standard_Real VY = V.Y();
19 Standard_Real X0 = P.X();
20 Standard_Real Y0 = P.Y();
21 matrix.SetCol (1, gp_XY (1.0 - 2.0 * VX * VX, -2.0 * VX * VY));
22 matrix.SetCol (2, gp_XY (-2.0 * VX * VY, 1.0 - 2.0 * VY * VY));
24 loc.SetCoord (-2.0 * ((VX * VX - 1.0) * X0 + (VX * VY * Y0)),
25 -2.0 * ((VX * VY * X0) + (VY * VY - 1.0) * Y0));
28 void gp_Trsf2d::SetTransformation (const gp_Ax2d& FromA1,
31 shape = gp_CompoundTrsf;
33 //matrix from XOY to A2 :
34 const gp_XY& V1 = ToA2.Direction().XY();
35 gp_XY V2 (-V1.Y(), V1.X());
36 matrix.SetCol (1, V1);
37 matrix.SetCol (2, V2);
38 loc = ToA2.Location().XY();
40 loc.Multiply (matrix);
42 //matrix FromA1 to XOY
43 const gp_XY& V3 = FromA1.Direction().XY();
44 gp_XY V4 (-V3.Y(), V3.X());
45 gp_Mat2d MA1 (V3, V4);
46 gp_XY MA1loc = FromA1.Location().XY();
47 //matrix * MA1 => FromA1 ToA2
48 MA1loc.Multiply (matrix);
50 matrix.Multiply (MA1);
53 void gp_Trsf2d::SetTransformation (const gp_Ax2d& A)
55 shape = gp_CompoundTrsf;
57 const gp_XY& V1 = A.Direction().XY();
58 gp_XY V2 (-V1.Y(), V1.X());
59 matrix.SetCol (1, V1);
60 matrix.SetCol (2, V2);
61 loc = A.Location().XY();
63 loc.Multiply (matrix);
67 void gp_Trsf2d::SetTranslationPart (const gp_Vec2d& V)
70 Standard_Real X = loc.X();
72 Standard_Real Y = loc.Y();
74 if (X <= gp::Resolution() && Y <= gp::Resolution()) {
75 if (shape == gp_Identity || shape == gp_PntMirror ||
76 shape == gp_Scale || shape == gp_Rotation ||
77 shape == gp_Ax1Mirror ) { }
78 else if (shape == gp_Translation) { shape = gp_Identity; }
79 else { shape = gp_CompoundTrsf; }
82 if (shape == gp_Translation || shape == gp_Scale ||
83 shape == gp_PntMirror) { }
84 else if (shape == gp_Identity) { shape = gp_Translation; }
85 else { shape = gp_CompoundTrsf; }
89 void gp_Trsf2d::SetScaleFactor (const Standard_Real S)
92 Standard_Real X = loc.X();
94 Standard_Real Y = loc.Y();
96 if (X <= gp::Resolution() && Y <= gp::Resolution()) {
97 if (shape == gp_Identity || shape == gp_Rotation) { }
98 else if (shape == gp_Scale) { shape = gp_Identity; }
99 else if (shape == gp_PntMirror) { shape = gp_Translation; }
100 else { shape = gp_CompoundTrsf; }
103 if (shape == gp_Identity || shape == gp_Rotation ||
104 shape == gp_Scale) { }
105 else if (shape == gp_PntMirror) { shape = gp_Translation; }
106 else { shape = gp_CompoundTrsf; }
110 if (shape == gp_PntMirror || shape == gp_Ax1Mirror) { }
111 else if (shape == gp_Identity || shape == gp_Scale) {
112 shape = gp_PntMirror;
114 else { shape = gp_CompoundTrsf; }
117 if (shape == gp_Scale) { }
118 else if (shape == gp_Identity || shape == gp_Translation ||
119 shape == gp_PntMirror) { shape = gp_Scale; }
120 else { shape = gp_CompoundTrsf; }
125 gp_Mat2d gp_Trsf2d::VectorialPart () const
127 if (scale == 1.0) return matrix;
129 if (shape == gp_Scale || shape == gp_PntMirror)
130 M.SetDiagonal (matrix.Value(1,1) * scale, matrix.Value(2,2) * scale);
136 Standard_Real gp_Trsf2d::RotationPart () const
138 return ATan2 ( matrix.Value(2,1), matrix.Value(1,1) );
141 void gp_Trsf2d::Invert()
144 // X' = scale * R * X + T => X = (R / scale) * ( X' - T)
146 // Pour les gp_Trsf2d puisque le scale est extrait de la matrice R
147 // on a toujours determinant (R) = 1 et R-1 = R transposee.
148 if (shape == gp_Identity) { }
149 else if ( shape == gp_Translation || shape == gp_PntMirror) {
152 else if ( shape == gp_Scale) {
153 Standard_Real As = scale;
154 if (As < 0) As = - As;
155 Standard_ConstructionError_Raise_if(As <= gp::Resolution(),"");
157 loc.Multiply (-scale);
160 Standard_Real As = scale;
161 if (As < 0) As = - As;
162 Standard_ConstructionError_Raise_if(As <= gp::Resolution(),"");
165 loc.Multiply (matrix);
166 loc.Multiply (-scale);
170 void gp_Trsf2d::Multiply(const gp_Trsf2d& T)
172 if (T.shape == gp_Identity) { }
173 else if (shape == gp_Identity) {
179 else if (shape == gp_Rotation && T.shape == gp_Rotation) {
180 if (loc.X() != 0.0 || loc.Y() != 0.0) {
181 loc.Add (T.loc.Multiplied (matrix));
183 matrix.Multiply(T.matrix);
185 else if (shape == gp_Translation && T.shape == gp_Translation) {
188 else if (shape == gp_Scale && T.shape == gp_Scale) {
189 loc.Add (T.loc.Multiplied(scale));
190 scale = scale * T.scale;
192 else if (shape == gp_PntMirror && T.shape == gp_PntMirror) {
194 shape = gp_Translation;
195 loc.Add (T.loc.Reversed());
197 else if (shape == gp_Ax1Mirror && T.shape == gp_Ax1Mirror) {
199 loc.Add (T.loc.Multiplied (matrix));
200 matrix.Multiply(T.matrix);
202 else if ((shape == gp_CompoundTrsf || shape == gp_Rotation ||
203 shape == gp_Ax1Mirror) && T.shape == gp_Translation) {
205 Tloc.Multiply (matrix);
206 if (scale != 1.0) Tloc.Multiply (scale);
209 else if ((shape == gp_Scale || shape == gp_PntMirror)
210 && T.shape == gp_Translation) {
212 Tloc.Multiply (scale);
215 else if (shape == gp_Translation &&
216 (T.shape == gp_CompoundTrsf ||
217 T.shape == gp_Rotation || T.shape == gp_Ax1Mirror)) {
218 shape = gp_CompoundTrsf;
223 else if (shape == gp_Translation &&
224 (T.shape == gp_Scale || T.shape == gp_PntMirror)) {
229 else if ((shape == gp_PntMirror || shape == gp_Scale) &&
230 (T.shape == gp_PntMirror || T.shape == gp_Scale)) {
231 shape = gp_CompoundTrsf;
233 Tloc.Multiply (scale);
235 scale = scale * T.scale;
237 else if ((shape == gp_CompoundTrsf || shape == gp_Rotation ||
238 shape == gp_Ax1Mirror)
239 && (T.shape == gp_Scale || T.shape == gp_PntMirror)) {
240 shape = gp_CompoundTrsf;
242 Tloc.Multiply(matrix);
243 if (scale == 1.0) scale = T.scale;
245 Tloc.Multiply (scale);
246 scale = scale * T.scale;
250 else if ((T.shape == gp_CompoundTrsf || T.shape == gp_Rotation ||
251 T.shape == gp_Ax1Mirror)
252 && (shape == gp_Scale || shape == gp_PntMirror)) {
253 shape = gp_CompoundTrsf;
255 Tloc.Multiply (scale);
256 scale = scale * T.scale;
261 shape = gp_CompoundTrsf;
263 Tloc.Multiply (matrix);
265 Tloc.Multiply (scale);
266 scale = scale * T.scale;
268 else { scale = T.scale; }
270 matrix.Multiply (T.matrix);
274 void gp_Trsf2d::Power (const Standard_Integer N)
276 if (shape == gp_Identity) { }
281 matrix.SetIdentity();
282 loc = gp_XY (0.0, 0.0);
285 else if (N == -1) Invert();
288 if (shape == gp_Translation) {
289 Standard_Integer Npower = N;
290 if (Npower < 0) Npower = - Npower;
294 if (IsOdd(Npower)) loc.Add (Temploc);
295 if (Npower == 1) break;
296 Temploc.Add (Temploc);
300 else if (shape == gp_Scale) {
301 Standard_Integer Npower = N;
302 if (Npower < 0) Npower = - Npower;
305 Standard_Real Tempscale = scale;
308 loc.Add (Temploc.Multiplied(scale));
309 scale = scale * Tempscale;
311 if (Npower == 1) break;
312 Temploc.Add (Temploc.Multiplied(Tempscale));
313 Tempscale = Tempscale * Tempscale;
317 else if (shape == gp_Rotation) {
318 Standard_Integer Npower = N;
319 if (Npower < 0) Npower = - Npower;
321 gp_Mat2d Tempmatrix (matrix);
322 if (loc.X() == 0.0 && loc.Y() == 0.0) {
324 if (IsOdd(Npower)) matrix.Multiply (Tempmatrix);
325 if (Npower == 1) break;
326 Tempmatrix.Multiply (Tempmatrix);
334 loc.Add (Temploc.Multiplied (matrix));
335 matrix.Multiply (Tempmatrix);
337 if (Npower == 1) break;
338 Temploc.Add (Temploc.Multiplied (Tempmatrix));
339 Tempmatrix.Multiply (Tempmatrix);
344 else if (shape == gp_PntMirror || shape == gp_Ax1Mirror) {
348 matrix.SetIdentity ();
349 loc.SetCoord (0.0, 0.0);
353 shape = gp_CompoundTrsf;
354 Standard_Integer Npower = N;
355 if (Npower < 0) Npower = - Npower;
357 matrix.SetDiagonal (scale*matrix.Value(1,1),
358 scale*matrix.Value(2,2));
360 Standard_Real Tempscale = scale;
361 gp_Mat2d Tempmatrix (matrix);
364 loc.Add ((Temploc.Multiplied (matrix)).Multiplied (scale));
365 scale = scale * Tempscale;
366 matrix.Multiply (Tempmatrix);
368 if (Npower == 1) break;
369 Tempscale = Tempscale * Tempscale;
370 Temploc.Add ( (Temploc.Multiplied (Tempmatrix)).Multiplied
373 Tempmatrix.Multiply (Tempmatrix);
381 void gp_Trsf2d::PreMultiply (const gp_Trsf2d& T)
383 if (T.shape == gp_Identity) { }
384 else if (shape == gp_Identity) {
390 else if (shape == gp_Rotation && T.shape == gp_Rotation) {
391 loc.Multiply (T.matrix);
393 matrix.PreMultiply(T.matrix);
395 else if (shape == gp_Translation && T.shape == gp_Translation) {
398 else if (shape == gp_Scale && T.shape == gp_Scale) {
399 loc.Multiply (T.scale);
401 scale = scale * T.scale;
403 else if (shape == gp_PntMirror && T.shape == gp_PntMirror) {
405 shape = gp_Translation;
409 else if (shape == gp_Ax1Mirror && T.shape == gp_Ax1Mirror) {
411 loc.Multiply (T.matrix);
413 matrix.PreMultiply(T.matrix);
415 else if ((shape == gp_CompoundTrsf || shape == gp_Rotation ||
416 shape == gp_Ax1Mirror) && T.shape == gp_Translation) {
419 else if ((shape == gp_Scale || shape == gp_PntMirror)
420 && T.shape == gp_Translation) {
423 else if (shape == gp_Translation &&
424 (T.shape == gp_CompoundTrsf ||
425 T.shape == gp_Rotation || T.shape == gp_Ax1Mirror)) {
426 shape = gp_CompoundTrsf;
428 if (T.scale == 1.0) loc.Multiply (T.matrix);
431 loc.Multiply (matrix);
432 loc.Multiply (scale);
436 else if ((T.shape == gp_Scale || T.shape == gp_PntMirror)
437 && shape == gp_Translation) {
438 loc.Multiply (T.scale);
443 else if ((shape == gp_PntMirror || shape == gp_Scale) &&
444 (T.shape == gp_PntMirror || T.shape == gp_Scale)) {
445 shape = gp_CompoundTrsf;
446 loc.Multiply (T.scale);
448 scale = scale * T.scale;
450 else if ((shape == gp_CompoundTrsf || shape == gp_Rotation ||
451 shape == gp_Ax1Mirror)
452 && (T.shape == gp_Scale || T.shape == gp_PntMirror)) {
453 shape = gp_CompoundTrsf;
454 loc.Multiply (T.scale);
456 scale = scale * T.scale;
458 else if ((T.shape == gp_CompoundTrsf || T.shape == gp_Rotation ||
459 T.shape == gp_Ax1Mirror)
460 && (shape == gp_Scale || shape == gp_PntMirror)) {
461 shape = gp_CompoundTrsf;
463 if (T.scale == 1.0) loc.Multiply (T.matrix);
465 loc.Multiply (matrix);
466 loc.Multiply (T.scale);
467 scale = T.scale * scale;
472 shape = gp_CompoundTrsf;
473 loc.Multiply (T.matrix);
474 if (T.scale != 1.0) {
475 loc.Multiply(T.scale); scale = scale * T.scale;
478 matrix.PreMultiply(T.matrix);