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