1 // Copyright (c) 1991-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 #ifndef _gp_Dir_HeaderFile
16 #define _gp_Dir_HeaderFile
17 
18 #include <gp_XYZ.hxx>
19 #include <Standard_ConstructionError.hxx>
20 #include <Standard_DomainError.hxx>
21 #include <Standard_OutOfRange.hxx>
22 
23 class gp_Vec;
24 class gp_Ax1;
25 class gp_Ax2;
26 class gp_Trsf;
27 
28 //! Describes a unit vector in 3D space. This unit vector is also called "Direction".
29 //! See Also
30 //! gce_MakeDir which provides functions for more complex
31 //! unit vector constructions
32 //! Geom_Direction which provides additional functions for
33 //! constructing unit vectors and works, in particular, with the
34 //! parametric equations of unit vectors.
35 class gp_Dir
36 {
37 public:
38 
39   DEFINE_STANDARD_ALLOC
40 
41   //! Creates a direction corresponding to X axis.
gp_Dir()42   gp_Dir()
43   : coord (1., 0., 0.)
44   {}
45 
46   //! Normalizes the vector theV and creates a direction. Raises ConstructionError if theV.Magnitude() <= Resolution.
47   gp_Dir (const gp_Vec& theV);
48 
49   //! Creates a direction from a triplet of coordinates. Raises ConstructionError if theCoord.Modulus() <= Resolution from gp.
50   gp_Dir (const gp_XYZ& theCoord);
51 
52   //! Creates a direction with its 3 cartesian coordinates. Raises ConstructionError if Sqrt(theXv*theXv + theYv*theYv + theZv*theZv) <= Resolution
53   //! Modification of the direction's coordinates
54   //! If Sqrt (theXv*theXv + theYv*theYv + theZv*theZv) <= Resolution from gp where
55   //! theXv, theYv ,theZv are the new coordinates it is not possible to
56   //! construct the direction and the method raises the
57   //! exception ConstructionError.
58   gp_Dir (const Standard_Real theXv, const Standard_Real theYv, const Standard_Real theZv);
59 
60   //! For this unit vector,  assigns the value Xi to:
61   //! -   the X coordinate if theIndex is 1, or
62   //! -   the Y coordinate if theIndex is 2, or
63   //! -   the Z coordinate if theIndex is 3,
64   //! and then normalizes it.
65   //! Warning
66   //! Remember that all the coordinates of a unit vector are
67   //! implicitly modified when any single one is changed directly.
68   //! Exceptions
69   //! Standard_OutOfRange if theIndex is not 1, 2, or 3.
70   //! Standard_ConstructionError if either of the following
71   //! is less than or equal to gp::Resolution():
72   //! -   Sqrt(Xv*Xv + Yv*Yv + Zv*Zv), or
73   //! -   the modulus of the number triple formed by the new
74   //! value theXi and the two other coordinates of this vector
75   //! that were not directly modified.
76   void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi);
77 
78   //! For this unit vector,  assigns the values theXv, theYv and theZv to its three coordinates.
79   //! Remember that all the coordinates of a unit vector are
80   //! implicitly modified when any single one is changed directly.
81   void SetCoord (const Standard_Real theXv, const Standard_Real theYv, const Standard_Real theZv);
82 
83   //! Assigns the given value to the X coordinate of this   unit vector.
84   void SetX (const Standard_Real theX);
85 
86   //! Assigns the given value to the Y coordinate of this   unit vector.
87   void SetY (const Standard_Real theY);
88 
89   //! Assigns the given value to the Z  coordinate of this   unit vector.
90   void SetZ (const Standard_Real theZ);
91 
92   //! Assigns the three coordinates of theCoord to this unit vector.
93   void SetXYZ (const gp_XYZ& theCoord);
94 
95   //! Returns the coordinate of range theIndex :
96   //! theIndex = 1 => X is returned
97   //! Ithendex = 2 => Y is returned
98   //! theIndex = 3 => Z is returned
99   //! Exceptions
100   //! Standard_OutOfRange if theIndex is not 1, 2, or 3.
Coord(const Standard_Integer theIndex) const101   Standard_Real Coord (const Standard_Integer theIndex) const  { return coord.Coord (theIndex); }
102 
103   //! Returns for the  unit vector  its three coordinates theXv, theYv, and theZv.
Coord(Standard_Real & theXv,Standard_Real & theYv,Standard_Real & theZv) const104   void Coord (Standard_Real& theXv, Standard_Real& theYv, Standard_Real& theZv) const  { coord.Coord (theXv, theYv, theZv); }
105 
106   //! Returns the X coordinate for a  unit vector.
X() const107   Standard_Real X() const { return coord.X(); }
108 
109   //! Returns the Y coordinate for a  unit vector.
Y() const110   Standard_Real Y() const { return coord.Y(); }
111 
112   //! Returns the Z coordinate for a  unit vector.
Z() const113   Standard_Real Z() const { return coord.Z(); }
114 
115   //! for this unit vector, returns  its three coordinates as a number triplea.
XYZ() const116   const gp_XYZ& XYZ() const { return coord; }
117 
118   //! Returns True if the angle between the two directions is
119   //! lower or equal to theAngularTolerance.
IsEqual(const gp_Dir & theOther,const Standard_Real theAngularTolerance) const120   Standard_Boolean IsEqual (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
121   {
122     return Angle (theOther) <= theAngularTolerance;
123   }
124 
125   //! Returns True if  the angle between this unit vector and the unit vector theOther is equal to Pi/2 (normal).
IsNormal(const gp_Dir & theOther,const Standard_Real theAngularTolerance) const126   Standard_Boolean IsNormal (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
127   {
128     Standard_Real anAng = M_PI / 2.0 - Angle (theOther);
129     if (anAng < 0)
130     {
131       anAng = -anAng;
132     }
133     return anAng <= theAngularTolerance;
134   }
135 
136   //! Returns True if  the angle between this unit vector and the unit vector theOther is equal to  Pi (opposite).
IsOpposite(const gp_Dir & theOther,const Standard_Real theAngularTolerance) const137   Standard_Boolean IsOpposite (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
138   {
139     return M_PI - Angle (theOther) <= theAngularTolerance;
140   }
141 
142   //! Returns true if the angle between this unit vector and the
143   //! unit vector theOther is equal to 0 or to Pi.
144   //! Note: the tolerance criterion is given by theAngularTolerance.
IsParallel(const gp_Dir & theOther,const Standard_Real theAngularTolerance) const145   Standard_Boolean IsParallel (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
146   {
147     Standard_Real anAng = Angle (theOther);
148     return anAng <= theAngularTolerance || M_PI - anAng <= theAngularTolerance;
149   }
150 
151   //! Computes the angular value in radians between <me> and
152   //! <theOther>. This value is always positive in 3D space.
153   //! Returns the angle in the range [0, PI]
154   Standard_EXPORT Standard_Real Angle (const gp_Dir& theOther) const;
155 
156   //! Computes the angular value between <me> and <theOther>.
157   //! <theVRef> is the direction of reference normal to <me> and <theOther>
158   //! and its orientation gives the positive sense of rotation.
159   //! If the cross product <me> ^ <theOther> has the same orientation
160   //! as <theVRef> the angular value is positive else negative.
161   //! Returns the angular value in the range -PI and PI (in radians). Raises  DomainError if <me> and <theOther> are not parallel this exception is raised
162   //! when <theVRef> is in the same plane as <me> and <theOther>
163   //! The tolerance criterion is Resolution from package gp.
164   Standard_EXPORT Standard_Real AngleWithRef (const gp_Dir& theOther, const gp_Dir& theVRef) const;
165 
166   //! Computes the cross product between two directions
167   //! Raises the exception ConstructionError if the two directions
168   //! are parallel because the computed vector cannot be normalized
169   //! to create a direction.
170   void Cross (const gp_Dir& theRight);
171 
operator ^=(const gp_Dir & theRight)172   void operator ^= (const gp_Dir& theRight) { Cross (theRight); }
173 
174   //! Computes the triple vector product.
175   //! <me> ^ (V1 ^ V2)
176   //! Raises the exception ConstructionError if V1 and V2 are parallel
177   //! or <me> and (V1^V2) are parallel because the computed vector
178   //! can't be normalized to create a direction.
179   Standard_NODISCARD gp_Dir Crossed (const gp_Dir& theRight) const;
180 
operator ^(const gp_Dir & theRight) const181   Standard_NODISCARD gp_Dir operator ^ (const gp_Dir& theRight) const { return Crossed (theRight); }
182 
183   void CrossCross (const gp_Dir& theV1, const gp_Dir& theV2);
184 
185   //! Computes the double vector product this ^ (theV1 ^ theV2).
186   //! -   CrossCrossed creates a new unit vector.
187   //! Exceptions
188   //! Standard_ConstructionError if:
189   //! -   theV1 and theV2 are parallel, or
190   //! -   this unit vector and (theV1 ^ theV2) are parallel.
191   //! This is because, in these conditions, the computed vector
192   //! is null and cannot be normalized.
193   Standard_NODISCARD gp_Dir CrossCrossed (const gp_Dir& theV1, const gp_Dir& theV2) const;
194 
195   //! Computes the scalar product
Dot(const gp_Dir & theOther) const196   Standard_Real Dot (const gp_Dir& theOther) const { return coord.Dot (theOther.coord); }
197 
operator *(const gp_Dir & theOther) const198   Standard_Real operator * (const gp_Dir& theOther) const { return Dot (theOther); }
199 
200   //! Computes the triple scalar product <me> * (theV1 ^ theV2).
201   //! Warnings :
202   //! The computed vector theV1' = theV1 ^ theV2 is not normalized
203   //! to create a unitary vector. So this method never
204   //! raises an exception even if theV1 and theV2 are parallel.
DotCross(const gp_Dir & theV1,const gp_Dir & theV2) const205   Standard_Real DotCross (const gp_Dir& theV1, const gp_Dir& theV2) const
206   {
207     return coord.Dot (theV1.coord.Crossed (theV2.coord));
208   }
209 
Reverse()210   void Reverse() { coord.Reverse(); }
211 
212   //! Reverses the orientation of a direction
213   //! geometric transformations
214   //! Performs the symmetrical transformation of a direction
215   //! with respect to the direction V which is the center of
216   //! the  symmetry.]
Reversed() const217   Standard_NODISCARD gp_Dir Reversed() const
218   {
219     gp_Dir aV = *this;
220     aV.coord.Reverse();
221     return aV;
222   }
223 
operator -() const224   Standard_NODISCARD gp_Dir operator -() const { return Reversed(); }
225 
226   Standard_EXPORT void Mirror (const gp_Dir& theV);
227 
228   //! Performs the symmetrical transformation of a direction
229   //! with respect to the direction theV which is the center of
230   //! the  symmetry.
231   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Dir& theV) const;
232 
233   Standard_EXPORT void Mirror (const gp_Ax1& theA1);
234 
235   //! Performs the symmetrical transformation of a direction
236   //! with respect to an axis placement which is the axis
237   //! of the symmetry.
238   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Ax1& theA1) const;
239 
240   Standard_EXPORT void Mirror (const gp_Ax2& theA2);
241 
242   //! Performs the symmetrical transformation of a direction
243   //! with respect to a plane. The axis placement theA2 locates
244   //! the plane of the symmetry : (Location, XDirection, YDirection).
245   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Ax2& theA2) const;
246 
247   void Rotate(const gp_Ax1& theA1, const Standard_Real theAng);
248 
249   //! Rotates a direction. theA1 is the axis of the rotation.
250   //! theAng is the angular value of the rotation in radians.
Rotated(const gp_Ax1 & theA1,const Standard_Real theAng) const251   Standard_NODISCARD gp_Dir Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
252   {
253     gp_Dir aV = *this;
254     aV.Rotate (theA1, theAng);
255     return aV;
256   }
257 
258   Standard_EXPORT void Transform (const gp_Trsf& theT);
259 
260   //! Transforms a direction with a "Trsf" from gp.
261   //! Warnings :
262   //! If the scale factor of the "Trsf" theT is negative then the
263   //! direction <me> is reversed.
Transformed(const gp_Trsf & theT) const264   Standard_NODISCARD gp_Dir Transformed (const gp_Trsf& theT) const
265   {
266     gp_Dir aV = *this;
267     aV.Transform (theT);
268     return aV;
269   }
270 
271   //! Dumps the content of me into the stream
272   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
273 
274   //! Inits the content of me from the stream
275   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
276 
277 private:
278 
279   gp_XYZ coord;
280 
281 };
282 
283 #include <gp_Trsf.hxx>
284 
285 // =======================================================================
286 // function : gp_Dir
287 // purpose  :
288 // =======================================================================
gp_Dir(const gp_Vec & theV)289 inline gp_Dir::gp_Dir (const gp_Vec& theV)
290 {
291   const gp_XYZ& aXYZ = theV.XYZ();
292   Standard_Real aX = aXYZ.X();
293   Standard_Real aY = aXYZ.Y();
294   Standard_Real aZ = aXYZ.Z();
295   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
296   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
297   coord.SetX (aX / aD);
298   coord.SetY (aY / aD);
299   coord.SetZ (aZ / aD);
300 }
301 
302 // =======================================================================
303 // function : gp_Dir
304 // purpose  :
305 // =======================================================================
gp_Dir(const gp_XYZ & theXYZ)306 inline gp_Dir::gp_Dir (const gp_XYZ& theXYZ)
307 {
308   Standard_Real aX = theXYZ.X();
309   Standard_Real aY = theXYZ.Y();
310   Standard_Real aZ = theXYZ.Z();
311   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
312   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
313   coord.SetX (aX / aD);
314   coord.SetY (aY / aD);
315   coord.SetZ (aZ / aD);
316 }
317 
318 // =======================================================================
319 // function : gp_Dir
320 // purpose  :
321 // =======================================================================
gp_Dir(const Standard_Real theXv,const Standard_Real theYv,const Standard_Real theZv)322 inline gp_Dir::gp_Dir (const Standard_Real theXv,
323                        const Standard_Real theYv,
324                        const Standard_Real theZv)
325 {
326   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv + theZv * theZv);
327   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
328   coord.SetX (theXv / aD);
329   coord.SetY (theYv / aD);
330   coord.SetZ (theZv / aD);
331 }
332 
333 // =======================================================================
334 // function : SetCoord
335 // purpose  :
336 // =======================================================================
SetCoord(const Standard_Integer theIndex,const Standard_Real theXi)337 inline void gp_Dir::SetCoord (const Standard_Integer theIndex,
338                               const Standard_Real theXi)
339 {
340   Standard_Real aX = coord.X();
341   Standard_Real aY = coord.Y();
342   Standard_Real aZ = coord.Z();
343   Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 3, "gp_Dir::SetCoord() - index is out of range [1, 3]");
344   if (theIndex == 1)
345   {
346     aX = theXi;
347   }
348   else if (theIndex == 2)
349   {
350     aY = theXi;
351   }
352   else
353   {
354     aZ = theXi;
355   }
356   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
357   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetCoord() - result vector has zero norm");
358   coord.SetX (aX / aD);
359   coord.SetY (aY / aD);
360   coord.SetZ (aZ / aD);
361 }
362 
363 // =======================================================================
364 // function : SetCoord
365 // purpose  :
366 // =======================================================================
SetCoord(const Standard_Real theXv,const Standard_Real theYv,const Standard_Real theZv)367 inline void gp_Dir::SetCoord (const Standard_Real theXv,
368                               const Standard_Real theYv,
369                               const Standard_Real theZv) {
370   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv + theZv * theZv);
371   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetCoord() - input vector has zero norm");
372   coord.SetX (theXv / aD);
373   coord.SetY (theYv / aD);
374   coord.SetZ (theZv / aD);
375 }
376 
377 // =======================================================================
378 // function : SetX
379 // purpose  :
380 // =======================================================================
SetX(const Standard_Real theX)381 inline void gp_Dir::SetX (const Standard_Real theX)
382 {
383   Standard_Real anY = coord.Y();
384   Standard_Real aZ = coord.Z();
385   Standard_Real aD = sqrt (theX * theX + anY * anY + aZ * aZ);
386   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetX() - result vector has zero norm");
387   coord.SetX (theX / aD);
388   coord.SetY (anY / aD);
389   coord.SetZ (aZ / aD);
390 }
391 
392 // =======================================================================
393 // function : SetY
394 // purpose  :
395 // =======================================================================
SetY(const Standard_Real theY)396 inline void gp_Dir::SetY (const Standard_Real theY)
397 {
398   Standard_Real aZ = coord.Z();
399   Standard_Real aX = coord.X();
400   Standard_Real aD = sqrt (aX * aX + theY * theY + aZ * aZ);
401   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetY() - result vector has zero norm");
402   coord.SetX (aX / aD);
403   coord.SetY (theY / aD);
404   coord.SetZ (aZ / aD);
405 }
406 
407 // =======================================================================
408 // function : SetZ
409 // purpose  :
410 // =======================================================================
SetZ(const Standard_Real theZ)411 inline void gp_Dir::SetZ (const Standard_Real theZ)
412 {
413   Standard_Real aX = coord.X();
414   Standard_Real anY = coord.Y();
415   Standard_Real aD = sqrt (aX * aX + anY * anY + theZ * theZ);
416   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetZ() - result vector has zero norm");
417   coord.SetX (aX / aD);
418   coord.SetY (anY / aD);
419   coord.SetZ (theZ / aD);
420 }
421 
422 // =======================================================================
423 // function : SetXYZ
424 // purpose  :
425 // =======================================================================
SetXYZ(const gp_XYZ & theXYZ)426 inline void gp_Dir::SetXYZ (const gp_XYZ& theXYZ)
427 {
428   Standard_Real aX = theXYZ.X();
429   Standard_Real anY = theXYZ.Y();
430   Standard_Real aZ = theXYZ.Z();
431   Standard_Real aD = sqrt(aX * aX + anY * anY + aZ * aZ);
432   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetX() - input vector has zero norm");
433   coord.SetX (aX / aD);
434   coord.SetY (anY / aD);
435   coord.SetZ (aZ / aD);
436 }
437 
438 // =======================================================================
439 // function : Cross
440 // purpose  :
441 // =======================================================================
Cross(const gp_Dir & theRight)442 inline void gp_Dir::Cross(const gp_Dir& theRight)
443 {
444   coord.Cross (theRight.coord);
445   Standard_Real aD = coord.Modulus();
446   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::Cross() - result vector has zero norm");
447   coord.Divide (aD);
448 }
449 
450 // =======================================================================
451 // function : Crossed
452 // purpose  :
453 // =======================================================================
Crossed(const gp_Dir & theRight) const454 inline gp_Dir gp_Dir::Crossed (const gp_Dir& theRight) const
455 {
456   gp_Dir aV = *this;
457   aV.coord.Cross (theRight.coord);
458   Standard_Real aD = aV.coord.Modulus();
459   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::Crossed() - result vector has zero norm");
460   aV.coord.Divide (aD);
461   return aV;
462 }
463 
464 // =======================================================================
465 // function : CrossCross
466 // purpose  :
467 // =======================================================================
CrossCross(const gp_Dir & theV1,const gp_Dir & theV2)468 inline void gp_Dir::CrossCross (const gp_Dir& theV1, const gp_Dir& theV2)
469 {
470   coord.CrossCross (theV1.coord, theV2.coord);
471   Standard_Real aD = coord.Modulus();
472   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::CrossCross() - result vector has zero norm");
473   coord.Divide (aD);
474 }
475 
476 // =======================================================================
477 // function : CrossCrossed
478 // purpose  :
479 // =======================================================================
CrossCrossed(const gp_Dir & theV1,const gp_Dir & theV2) const480 inline gp_Dir gp_Dir::CrossCrossed (const gp_Dir& theV1, const gp_Dir& theV2) const
481 {
482   gp_Dir aV = *this;
483   (aV.coord).CrossCross (theV1.coord, theV2.coord);
484   Standard_Real aD = aV.coord.Modulus();
485   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::CrossCrossed() - result vector has zero norm");
486   aV.coord.Divide (aD);
487   return aV;
488 }
489 
490 // =======================================================================
491 // function : Rotate
492 // purpose  :
493 // =======================================================================
Rotate(const gp_Ax1 & theA1,const Standard_Real theAng)494 inline void gp_Dir::Rotate(const gp_Ax1& theA1, const Standard_Real theAng)
495 {
496   gp_Trsf aT;
497   aT.SetRotation (theA1, theAng);
498   coord.Multiply (aT.HVectorialPart());
499 }
500 
501 #endif // _gp_Dir_HeaderFile
502