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 // JCV 08/01/90 Modifs suite a l'introduction des classes XY et Mat2d dans gp
17 #define No_Standard_OutOfRange
19 #include <gp_Vec2d.hxx>
22 #include <gp_Ax2d.hxx>
23 #include <gp_Dir2d.hxx>
24 #include <gp_Trsf2d.hxx>
25 #include <gp_VectorWithNullMagnitude.hxx>
28 Standard_Boolean gp_Vec2d::IsEqual
29 (const gp_Vec2d& Other,
30 const Standard_Real LinearTolerance,
31 const Standard_Real AngularTolerance) const
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)
42 Standard_Real Ang = Angle(Other);
43 if (Ang < 0.0) Ang = -Ang;
44 // Check for zero angle
45 return isEqualLength && (Ang <= AngularTolerance);
50 Standard_Real gp_Vec2d::Angle (const gp_Vec2d& Other) const
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();
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)
68 if (Sinus > 0.0) return acos (Cosinus);
69 else return -acos (Cosinus);
73 if (Cosinus > 0.0) return asin (Sinus);
76 if (Sinus > 0.0) return M_PI - asin (Sinus);
77 else return - M_PI - asin (Sinus);
82 void gp_Vec2d::Mirror (const gp_Ax2d& A1)
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);
94 gp_Vec2d gp_Vec2d::Mirrored (const gp_Ax2d& A1) const
96 gp_Vec2d Vres = *this;
101 void gp_Vec2d::Transform (const gp_Trsf2d& T)
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 ());
109 void gp_Vec2d::Mirror (const gp_Vec2d& V)
111 const Standard_Real D = V.coord.Modulus();
112 if (D > gp::Resolution())
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);
125 gp_Vec2d gp_Vec2d::Mirrored (const gp_Vec2d& V) const
127 gp_Vec2d Vres = *this;