1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <gp_GTrsf2d.hxx>
17 #include <gp_Ax2d.hxx>
18 #include <gp_Mat2d.hxx>
19 #include <gp_Trsf2d.hxx>
21 #include <Precision.hxx>
22 #include <Standard_ConstructionError.hxx>
23 #include <Standard_OutOfRange.hxx>
25 void gp_GTrsf2d::SetAffinity (const gp_Ax2d& A,
26 const Standard_Real Ratio)
30 Standard_Real a = A.Direction().X();
31 Standard_Real b = A.Direction().Y();
32 matrix.SetValue (1, 1, (1.0 - Ratio)*a*a + Ratio);
33 matrix.SetValue (2, 2, (1.0 - Ratio)*b*b + Ratio);
34 matrix.SetValue (1, 2, (1.0 - Ratio)*a*b);
35 matrix.SetValue (2, 1, matrix.Value (1, 2));
36 loc = A.Location().XY();
38 loc.Multiply (matrix);
39 loc.Add (A.Location().XY());
42 void gp_GTrsf2d::SetTranslationPart (const gp_XY& Coord)
45 if (Form() == gp_CompoundTrsf || Form() == gp_Other ||
46 Form() == gp_Translation) { }
47 else if (Form() == gp_Identity) { shape = gp_Translation; }
48 else { shape = gp_CompoundTrsf; }
51 void gp_GTrsf2d::Invert ()
53 if (shape == gp_Other) {
55 loc.Multiply (matrix);
59 gp_Trsf2d T = Trsf2d ();
65 void gp_GTrsf2d::Multiply (const gp_GTrsf2d& T)
67 if (Form() == gp_Other || T.Form() == gp_Other) {
69 loc.Add (T.loc.Multiplied (matrix));
70 matrix.Multiply(T.matrix);
73 gp_Trsf2d T1 = Trsf2d();
74 gp_Trsf2d T2 = T.Trsf2d();
83 void gp_GTrsf2d::Power (const Standard_Integer N)
92 else if (N == -1) { Invert(); }
94 if (N < 0) { Invert(); }
95 if (shape == gp_Other) {
96 Standard_Integer Npower = N;
97 if (Npower < 0) Npower = - Npower;
100 // Standard_Real Tempscale = scale;
101 gp_Mat2d Tempmatrix (matrix);
104 loc.Add (Temploc.Multiplied (matrix));
105 matrix.Multiply (Tempmatrix);
107 if (Npower == 1) { break; }
108 Temploc.Add (Temploc.Multiplied (Tempmatrix));
109 Tempmatrix.Multiply (Tempmatrix);
114 gp_Trsf2d T = Trsf2d ();
121 void gp_GTrsf2d::PreMultiply (const gp_GTrsf2d& T)
123 if (Form() == gp_Other || T.Form() == gp_Other) {
125 loc.Multiply (T.matrix);
127 matrix.PreMultiply(T.matrix);
130 gp_Trsf2d T1 = Trsf2d();
131 gp_Trsf2d T2 = T.Trsf2d();
140 gp_Trsf2d gp_GTrsf2d::Trsf2d () const
142 // Test of orthogonality
143 const Standard_Real aTolerance = Precision::Angular();
144 const Standard_Real aTolerance2 = 2.0 * aTolerance;
146 if ( Form() == gp_Other )
147 throw Standard_ConstructionError("gp_GTrsf2d::Trsf2d() - non-orthogonal GTrsf2d(0)");
149 Standard_Real value = (matrix.Value(1, 1) * matrix.Value(1, 1)
150 + matrix.Value(2, 1) * matrix.Value(2, 1));
151 if ( Abs(value - 1.) > aTolerance2 )
152 throw Standard_ConstructionError("gp_GTrsf2d::Trsf2d() - non-orthogonal GTrsf2d(1)");
154 value = (matrix.Value(1, 2) * matrix.Value(1, 2)
155 + matrix.Value(2, 2) * matrix.Value(2, 2));
156 if ( Abs(value - 1.) > aTolerance2 )
157 throw Standard_ConstructionError("gp_GTrsf2d::Trsf2d() - non-orthogonal GTrsf2d(2)");
159 value = (matrix.Value(1, 1) * matrix.Value(1, 2)
160 + matrix.Value(2, 1) * matrix.Value(2, 2));
161 if ( Abs(value) > aTolerance )
162 throw Standard_ConstructionError("gp_GTrsf2d::Trsf2d() - non-orthogonal GTrsf2d(3)");
165 gp_Trsf2d aTransformation;
166 aTransformation.matrix = matrix;
167 aTransformation.shape = shape;
168 aTransformation.scale = scale;
169 aTransformation.loc = loc;
171 return aTransformation;