0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / gp / gp_Vec2d.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 // JCV 08/01/90 Modifs suite a l'introduction des classes XY et Mat2d dans gp
16
17 #define No_Standard_OutOfRange
18
19 #include <gp_Vec2d.hxx>
20
21 #include <gp.hxx>
22 #include <gp_Ax2d.hxx>
23 #include <gp_Dir2d.hxx>
24 #include <gp_Trsf2d.hxx>
25 #include <gp_VectorWithNullMagnitude.hxx>
26 #include <gp_XY.hxx>
27
28 Standard_Boolean gp_Vec2d::IsEqual
29 (const gp_Vec2d& Other, 
30  const Standard_Real LinearTolerance,
31  const Standard_Real AngularTolerance) const
32 {
33   const Standard_Real theNorm = Magnitude();
34   const Standard_Real theOtherNorm = Other.Magnitude();
35   Standard_Real val = theNorm - theOtherNorm;
36   if (val < 0.0) val = -val;
37   // Check for equal lengths
38   const Standard_Boolean isEqualLength = (val <= LinearTolerance);
39   // Check for small vectors
40   if (theNorm > LinearTolerance && theOtherNorm > LinearTolerance)
41   {
42     Standard_Real Ang = Angle(Other);
43     if (Ang < 0.0) Ang = -Ang;
44     // Check for zero angle
45     return isEqualLength && (Ang <= AngularTolerance);
46   }
47   return isEqualLength;
48 }    
49
50 Standard_Real gp_Vec2d::Angle (const gp_Vec2d& Other) const
51 {
52   //    Commentaires :
53   //    Au dessus de 45 degres l'arccos donne la meilleur precision pour le
54   //    calcul de l'angle. Sinon il vaut mieux utiliser l'arcsin.
55   //    Les erreurs commises sont loin d'etre negligeables lorsque l'on est
56   //    proche de zero ou de 90 degres.
57   //    En 2D les valeurs angulaires sont comprises entre -PI et PI
58   const Standard_Real theNorm = Magnitude();
59   const Standard_Real theOtherNorm = Other.Magnitude();
60   if (theNorm <= gp::Resolution() || theOtherNorm <= gp::Resolution())
61     throw gp_VectorWithNullMagnitude();
62
63   const Standard_Real D = theNorm * theOtherNorm;
64   const Standard_Real Cosinus = coord.Dot   (Other.coord) / D;
65   const Standard_Real Sinus = coord.Crossed (Other.coord) / D;
66   if (Cosinus > -0.70710678118655 && Cosinus < 0.70710678118655)
67   {
68     if (Sinus > 0.0)  return  acos (Cosinus);
69     else              return -acos (Cosinus); 
70   }
71   else
72   {
73     if (Cosinus > 0.0) return        asin (Sinus);
74     else
75     { 
76       if (Sinus > 0.0) return   M_PI - asin (Sinus);
77       else             return - M_PI - asin (Sinus);
78     }
79   }  
80 }
81
82 void gp_Vec2d::Mirror (const gp_Ax2d& A1)
83 {
84   const gp_XY& XY = A1.Direction().XY();
85   Standard_Real X = coord.X();
86   Standard_Real Y = coord.Y();
87   Standard_Real A = XY.X();
88   Standard_Real B = XY.Y();
89   Standard_Real M1 = 2.0 * A * B;
90   coord.SetX(((2.0 * A * A) - 1.) * X + M1 * Y);
91   coord.SetY(M1 * X + ((2. * B * B) - 1.0) * Y);
92 }
93
94 gp_Vec2d gp_Vec2d::Mirrored (const gp_Ax2d& A1) const
95 {
96   gp_Vec2d Vres = *this;
97   Vres.Mirror(A1);
98   return Vres;                     
99 }
100
101 void gp_Vec2d::Transform (const gp_Trsf2d& T)
102 {
103   if (T.Form() == gp_Identity || T.Form() == gp_Translation) { }
104   else if (T.Form() == gp_PntMirror) coord.Reverse ();
105   else if (T.Form() == gp_Scale)     coord.Multiply (T.ScaleFactor ());
106   else                               coord.Multiply (T.VectorialPart ());
107 }
108
109 void gp_Vec2d::Mirror (const gp_Vec2d& V)
110 {
111   const Standard_Real D = V.coord.Modulus();
112   if (D > gp::Resolution())
113   {
114     const gp_XY& XY = V.coord;
115     Standard_Real X = XY.X();
116     Standard_Real Y = XY.Y();
117     Standard_Real A = X / D;
118     Standard_Real B = Y / D;
119     Standard_Real M1 = 2.0 * A * B;
120     coord.SetX(((2.0 * A * A) - 1.0) * X + M1 * Y);
121     coord.SetY(M1 * X + ((2.0 * B * B) - 1.0) * Y);
122   }
123 }
124
125 gp_Vec2d gp_Vec2d::Mirrored (const gp_Vec2d& V) const
126 {
127   gp_Vec2d Vres = *this;
128   Vres.Mirror(V);
129   return Vres;                     
130 }