1 // Copyright (c) 1991-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 #ifndef _gp_Dir_HeaderFile
16 #define _gp_Dir_HeaderFile
19 #include <Standard_ConstructionError.hxx>
20 #include <Standard_DomainError.hxx>
21 #include <Standard_OutOfRange.hxx>
28 //! Describes a unit vector in 3D space. This unit vector is also called "Direction".
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.
41 //! Creates a direction corresponding to X axis.
46 //! Normalizes the vector theV and creates a direction. Raises ConstructionError if theV.Magnitude() <= Resolution.
47 gp_Dir (const gp_Vec& theV);
49 //! Creates a direction from a triplet of coordinates. Raises ConstructionError if theCoord.Modulus() <= Resolution from gp.
50 gp_Dir (const gp_XYZ& theCoord);
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);
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.
66 //! Remember that all the coordinates of a unit vector are
67 //! implicitly modified when any single one is changed directly.
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);
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);
83 //! Assigns the given value to the X coordinate of this unit vector.
84 void SetX (const Standard_Real theX);
86 //! Assigns the given value to the Y coordinate of this unit vector.
87 void SetY (const Standard_Real theY);
89 //! Assigns the given value to the Z coordinate of this unit vector.
90 void SetZ (const Standard_Real theZ);
92 //! Assigns the three coordinates of theCoord to this unit vector.
93 void SetXYZ (const gp_XYZ& theCoord);
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
100 //! Standard_OutOfRange if theIndex is not 1, 2, or 3.
101 Standard_Real Coord (const Standard_Integer theIndex) const { return coord.Coord (theIndex); }
103 //! Returns for the unit vector its three coordinates theXv, theYv, and theZv.
104 void Coord (Standard_Real& theXv, Standard_Real& theYv, Standard_Real& theZv) const { coord.Coord (theXv, theYv, theZv); }
106 //! Returns the X coordinate for a unit vector.
107 Standard_Real X() const { return coord.X(); }
109 //! Returns the Y coordinate for a unit vector.
110 Standard_Real Y() const { return coord.Y(); }
112 //! Returns the Z coordinate for a unit vector.
113 Standard_Real Z() const { return coord.Z(); }
115 //! for this unit vector, returns its three coordinates as a number triplea.
116 const gp_XYZ& XYZ() const { return coord; }
118 //! Returns True if the angle between the two directions is
119 //! lower or equal to theAngularTolerance.
120 Standard_Boolean IsEqual (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
122 return Angle (theOther) <= theAngularTolerance;
125 //! Returns True if the angle between this unit vector and the unit vector theOther is equal to Pi/2 (normal).
126 Standard_Boolean IsNormal (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
128 Standard_Real anAng = M_PI / 2.0 - Angle (theOther);
133 return anAng <= theAngularTolerance;
136 //! Returns True if the angle between this unit vector and the unit vector theOther is equal to Pi (opposite).
137 Standard_Boolean IsOpposite (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
139 return M_PI - Angle (theOther) <= theAngularTolerance;
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.
145 Standard_Boolean IsParallel (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
147 Standard_Real anAng = Angle (theOther);
148 return anAng <= theAngularTolerance || M_PI - anAng <= theAngularTolerance;
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;
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;
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);
172 void operator ^= (const gp_Dir& theRight) { Cross (theRight); }
174 //! Computes the triple vector product.
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;
181 Standard_NODISCARD gp_Dir operator ^ (const gp_Dir& theRight) const { return Crossed (theRight); }
183 void CrossCross (const gp_Dir& theV1, const gp_Dir& theV2);
185 //! Computes the double vector product this ^ (theV1 ^ theV2).
186 //! - CrossCrossed creates a new unit vector.
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;
195 //! Computes the scalar product
196 Standard_Real Dot (const gp_Dir& theOther) const { return coord.Dot (theOther.coord); }
198 Standard_Real operator * (const gp_Dir& theOther) const { return Dot (theOther); }
200 //! Computes the triple scalar product <me> * (theV1 ^ theV2).
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.
205 Standard_Real DotCross (const gp_Dir& theV1, const gp_Dir& theV2) const
207 return coord.Dot (theV1.coord.Crossed (theV2.coord));
210 void Reverse() { coord.Reverse(); }
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
217 Standard_NODISCARD gp_Dir Reversed() const
224 Standard_NODISCARD gp_Dir operator -() const { return Reversed(); }
226 Standard_EXPORT void Mirror (const gp_Dir& theV);
228 //! Performs the symmetrical transformation of a direction
229 //! with respect to the direction theV which is the center of
231 Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Dir& theV) const;
233 Standard_EXPORT void Mirror (const gp_Ax1& theA1);
235 //! Performs the symmetrical transformation of a direction
236 //! with respect to an axis placement which is the axis
238 Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Ax1& theA1) const;
240 Standard_EXPORT void Mirror (const gp_Ax2& theA2);
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;
247 void Rotate(const gp_Ax1& theA1, const Standard_Real theAng);
249 //! Rotates a direction. theA1 is the axis of the rotation.
250 //! theAng is the angular value of the rotation in radians.
251 Standard_NODISCARD gp_Dir Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
254 aV.Rotate (theA1, theAng);
258 Standard_EXPORT void Transform (const gp_Trsf& theT);
260 //! Transforms a direction with a "Trsf" from gp.
262 //! If the scale factor of the "Trsf" theT is negative then the
263 //! direction <me> is reversed.
264 Standard_NODISCARD gp_Dir Transformed (const gp_Trsf& theT) const
271 //! Dumps the content of me into the stream
272 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
274 //! Inits the content of me from the stream
275 Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
283 #include <gp_Trsf.hxx>
285 // =======================================================================
288 // =======================================================================
289 inline gp_Dir::gp_Dir (const gp_Vec& theV)
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);
302 // =======================================================================
305 // =======================================================================
306 inline gp_Dir::gp_Dir (const gp_XYZ& theXYZ)
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);
318 // =======================================================================
321 // =======================================================================
322 inline gp_Dir::gp_Dir (const Standard_Real theXv,
323 const Standard_Real theYv,
324 const Standard_Real theZv)
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);
333 // =======================================================================
334 // function : SetCoord
336 // =======================================================================
337 inline void gp_Dir::SetCoord (const Standard_Integer theIndex,
338 const Standard_Real theXi)
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]");
348 else if (theIndex == 2)
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);
363 // =======================================================================
364 // function : SetCoord
366 // =======================================================================
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);
377 // =======================================================================
380 // =======================================================================
381 inline void gp_Dir::SetX (const Standard_Real theX)
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);
392 // =======================================================================
395 // =======================================================================
396 inline void gp_Dir::SetY (const Standard_Real theY)
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);
407 // =======================================================================
410 // =======================================================================
411 inline void gp_Dir::SetZ (const Standard_Real theZ)
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);
422 // =======================================================================
425 // =======================================================================
426 inline void gp_Dir::SetXYZ (const gp_XYZ& theXYZ)
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);
438 // =======================================================================
441 // =======================================================================
442 inline void gp_Dir::Cross(const gp_Dir& theRight)
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");
450 // =======================================================================
451 // function : Crossed
453 // =======================================================================
454 inline gp_Dir gp_Dir::Crossed (const gp_Dir& theRight) const
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);
464 // =======================================================================
465 // function : CrossCross
467 // =======================================================================
468 inline void gp_Dir::CrossCross (const gp_Dir& theV1, const gp_Dir& theV2)
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");
476 // =======================================================================
477 // function : CrossCrossed
479 // =======================================================================
480 inline gp_Dir gp_Dir::CrossCrossed (const gp_Dir& theV1, const gp_Dir& theV2) const
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);
490 // =======================================================================
493 // =======================================================================
494 inline void gp_Dir::Rotate(const gp_Ax1& theA1, const Standard_Real theAng)
497 aT.SetRotation (theA1, theAng);
498 coord.Multiply (aT.HVectorialPart());
501 #endif // _gp_Dir_HeaderFile