Test for 0022778: Bug in BRepMesh
[occt.git] / src / Graphic3d / Graphic3d_Vector.cxx
1 // Created by: NW,JPB,CAL
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 // Modified     27/12/98 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
22
23
24 //-Version      
25
26 //-Design       Declaration des variables specifiques aux vecteurs
27
28 //-Warning      Un vecteur est defini par ses composantes ou par
29 //              deux points
30 //              Il peut etre normalise
31
32 //-References   
33
34 //-Language     C++ 2.0
35
36 //-Declarations
37
38 // for the class
39 #include <Graphic3d_Vector.ixx>
40
41 #include <Standard_Boolean.hxx>
42
43 //-Aliases
44
45 //-Global data definitions
46
47 #define Graphic3d_Vector_MyEpsilon 0.000001
48
49 //      -- les coordonnees du vecteur
50 //      MyX             :       Standard_ShortReal;
51 //      MyY             :       Standard_ShortReal;
52 //      MyZ             :       Standard_ShortReal;
53
54 //      -- la norme du vecteur
55 //      MyNorme         :       Standard_ShortReal;
56
57 //-Constructors
58
59 Graphic3d_Vector::Graphic3d_Vector ():
60 MyX (Standard_ShortReal (1.0)),
61 MyY (Standard_ShortReal (1.0)),
62 MyZ (Standard_ShortReal (1.0)),
63 MyNorme (Standard_ShortReal (1.0)) {
64 }
65
66 Graphic3d_Vector::Graphic3d_Vector (const Standard_Real AX, const Standard_Real AY, const Standard_Real AZ):
67 MyX (Standard_ShortReal (AX)),
68 MyY (Standard_ShortReal (AY)),
69 MyZ (Standard_ShortReal (AZ)),
70 MyNorme (Standard_ShortReal (Graphic3d_Vector::NormeOf (AX, AY, AZ))) {
71 }
72
73 Graphic3d_Vector::Graphic3d_Vector (const Graphic3d_Vertex& APoint1, const Graphic3d_Vertex& APoint2) {
74
75 Standard_Real X1, Y1, Z1;
76 Standard_Real X2, Y2, Z2;
77
78         APoint1.Coord (X1, Y1, Z1);
79         APoint2.Coord (X2, Y2, Z2);
80
81         MyX     = Standard_ShortReal (X2 - X1);
82         MyY     = Standard_ShortReal (Y2 - Y1);
83         MyZ     = Standard_ShortReal (Z2 - Z1);
84
85         MyNorme = Standard_ShortReal (Graphic3d_Vector::NormeOf (X2 - X1, Y2 - Y1, Z2 - Z1));
86
87 }
88
89 //-Destructors
90
91 //-Methods, in order
92
93 void Graphic3d_Vector::Coord (Standard_Real& AX, Standard_Real& AY, Standard_Real& AZ) const {
94
95         AX      = Standard_Real (MyX);
96         AY      = Standard_Real (MyY);
97         AZ      = Standard_Real (MyZ);
98
99 }
100
101 Standard_Real Graphic3d_Vector::X () const {
102
103         return Standard_Real (MyX);
104
105 }
106
107 Standard_Real Graphic3d_Vector::Y () const {
108
109         return Standard_Real (MyY);
110
111 }
112
113 Standard_Real Graphic3d_Vector::Z () const {
114
115         return Standard_Real (MyZ);
116
117 }
118
119 void Graphic3d_Vector::Normalize () {
120
121         if (Abs (MyNorme) <= RealEpsilon ())
122                 Graphic3d_VectorError::Raise ("The norm is null");
123
124         if (!IsNormalized()) // CQO CTS40181
125             {   
126                MyX      = MyX / MyNorme;
127                MyY      = MyY / MyNorme;
128                MyZ      = MyZ / MyNorme;
129             }
130
131         MyNorme = Standard_ShortReal (1.0);
132
133 }
134
135 void Graphic3d_Vector::SetCoord (const Standard_Real Xnew, const Standard_Real Ynew, const Standard_Real Znew) {
136
137         MyX     = Standard_ShortReal (Xnew);
138         MyY     = Standard_ShortReal (Ynew);
139         MyZ     = Standard_ShortReal (Znew);
140
141         MyNorme = Standard_ShortReal (Graphic3d_Vector::NormeOf (Standard_Real (MyX), Standard_Real (MyY), Standard_Real (MyZ)));
142
143 }
144
145 void Graphic3d_Vector::SetXCoord (const Standard_Real Xnew) {
146
147         MyX     = Standard_ShortReal (Xnew);
148
149         MyNorme = Standard_ShortReal (Graphic3d_Vector::NormeOf (Standard_Real (MyX), Standard_Real (MyY), Standard_Real (MyZ)));
150
151 }
152
153 void Graphic3d_Vector::SetYCoord (const Standard_Real Ynew) {
154
155         MyY     = Standard_ShortReal (Ynew);
156
157         MyNorme = Standard_ShortReal (Graphic3d_Vector::NormeOf (Standard_Real (MyX), Standard_Real (MyY), Standard_Real (MyZ)));
158
159 }
160
161 void Graphic3d_Vector::SetZCoord (const Standard_Real Znew) {
162
163         MyZ     = Standard_ShortReal (Znew);
164
165         MyNorme = Standard_ShortReal (Graphic3d_Vector::NormeOf (Standard_Real (MyX), Standard_Real (MyY), Standard_Real (MyZ)));
166
167 }
168
169 Standard_Boolean Graphic3d_Vector::LengthZero () const {
170
171         return (Abs (Standard_Real (MyNorme)) <= RealEpsilon ());
172
173 }
174
175 Standard_Boolean Graphic3d_Vector::IsNormalized () const {
176
177         return (Abs (Standard_Real (MyNorme) - 1.0) <=
178                                 Graphic3d_Vector_MyEpsilon);
179
180 }
181
182 Standard_Boolean Graphic3d_Vector::IsParallel (const Graphic3d_Vector& AV1, const Graphic3d_Vector& AV2) {
183
184   Standard_Real aDif1 = 0, aDif2 = 0, aDif3 = 0;
185
186   aDif1 = AV1.X () * AV2.Y () - AV1.Y () * AV2.X ();
187   aDif2 = AV1.X () * AV2.Z () - AV1.Z () * AV2.X ();
188   aDif3 = AV1.Y () * AV2.Z () - AV1.Z () * AV2.Y ();
189
190   return (  (Abs (aDif1) <= Graphic3d_Vector_MyEpsilon) &&
191             (Abs (aDif2) <= Graphic3d_Vector_MyEpsilon) &&
192             (Abs (aDif3) <= Graphic3d_Vector_MyEpsilon)  );
193 }
194
195 Standard_Real Graphic3d_Vector::NormeOf (const Standard_Real AX, const Standard_Real AY, const Standard_Real AZ) {
196
197         return (Sqrt (AX*AX+AY*AY+AZ*AZ));
198
199 }
200
201 Standard_Real Graphic3d_Vector::NormeOf (const Graphic3d_Vector& AVector) {
202
203 Standard_Real X, Y, Z;
204
205         AVector.Coord(X, Y, Z);
206         return (Graphic3d_Vector::NormeOf (X, Y, Z));
207
208 }