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_Lin_HeaderFile
16 #define _gp_Lin_HeaderFile
17 
18 #include <gp_Ax1.hxx>
19 #include <gp_Ax2.hxx>
20 #include <gp_Dir.hxx>
21 #include <gp_Pnt.hxx>
22 #include <gp_Trsf.hxx>
23 #include <gp_Vec.hxx>
24 #include <Standard_ConstructionError.hxx>
25 
26 //! Describes a line in 3D space.
27 //! A line is positioned in space with an axis (a gp_Ax1
28 //! object) which gives it an origin and a unit vector.
29 //! A line and an axis are similar objects, thus, we can
30 //! convert one into the other. A line provides direct access
31 //! to the majority of the edit and query functions available
32 //! on its positioning axis. In addition, however, a line has
33 //! specific functions for computing distances and positions.
34 //! See Also
35 //! gce_MakeLin which provides functions for more complex
36 //! line constructions
37 //! Geom_Line which provides additional functions for
38 //! constructing lines and works, in particular, with the
39 //! parametric equations of lines
40 class gp_Lin
41 {
42 public:
43 
44   DEFINE_STANDARD_ALLOC
45 
46   //! Creates a Line corresponding to Z axis of the
47   //! reference coordinate system.
gp_Lin()48   gp_Lin() {}
49 
50   //! Creates a line defined by axis theA1.
gp_Lin(const gp_Ax1 & theA1)51   gp_Lin (const gp_Ax1& theA1)
52   : pos (theA1)
53   {}
54 
55   //! Creates a line passing through point theP and parallel to
56   //! vector theV (theP and theV are, respectively, the origin and
57   //! the unit vector of the positioning axis of the line).
gp_Lin(const gp_Pnt & theP,const gp_Dir & theV)58   gp_Lin (const gp_Pnt& theP, const gp_Dir& theV)
59   : pos (theP, theV)
60   {}
61 
Reverse()62   void Reverse()
63   {
64     pos.Reverse();
65   }
66 
67   //! Reverses the direction of the line.
68   //! Note:
69   //! -   Reverse assigns the result to this line, while
70   //! -   Reversed creates a new one.
Reversed() const71   Standard_NODISCARD gp_Lin Reversed() const
72   {
73     gp_Lin aL = *this;
74     aL.pos.Reverse();
75     return aL;
76   }
77 
78   //! Changes the direction of the line.
SetDirection(const gp_Dir & theV)79   void SetDirection (const gp_Dir& theV) { pos.SetDirection (theV); }
80 
81   //! Changes the location point (origin) of the line.
SetLocation(const gp_Pnt & theP)82   void SetLocation (const gp_Pnt& theP) { pos.SetLocation (theP); }
83 
84   //! Complete redefinition of the line.
85   //! The "Location" point of <theA1> is the origin of the line.
86   //! The "Direction" of <theA1> is  the direction of the line.
SetPosition(const gp_Ax1 & theA1)87   void SetPosition (const gp_Ax1& theA1) { pos = theA1; }
88 
89   //! Returns the direction of the line.
Direction() const90   const gp_Dir& Direction() const { return pos.Direction(); }
91 
92   //! Returns the location point (origin) of the line.
Location() const93   const gp_Pnt& Location() const { return pos.Location(); }
94 
95   //! Returns the axis placement one axis with the same
96   //! location and direction as <me>.
Position() const97   const gp_Ax1& Position() const { return pos; }
98 
99   //! Computes the angle between two lines in radians.
Angle(const gp_Lin & theOther) const100   Standard_Real Angle (const gp_Lin& theOther) const
101   {
102     return pos.Direction().Angle (theOther.pos.Direction());
103   }
104 
105   //! Returns true if this line contains the point theP, that is, if the
106   //! distance between point theP and this line is less than or
107   //! equal to theLinearTolerance..
Contains(const gp_Pnt & theP,const Standard_Real theLinearTolerance) const108   Standard_Boolean Contains (const gp_Pnt& theP, const Standard_Real theLinearTolerance) const
109   {
110     return Distance (theP) <= theLinearTolerance;
111   }
112 
113   //! Computes the distance between <me> and the point theP.
114   Standard_Real Distance (const gp_Pnt& theP) const;
115 
116   //! Computes the distance between two lines.
117   Standard_EXPORT Standard_Real Distance (const gp_Lin& theOther) const;
118 
119   //! Computes the square distance between <me> and the point theP.
120   Standard_Real SquareDistance (const gp_Pnt& theP) const;
121 
122   //! Computes the square distance between two lines.
SquareDistance(const gp_Lin & theOther) const123   Standard_Real SquareDistance (const gp_Lin& theOther) const
124   {
125     Standard_Real aD = Distance (theOther);
126     return aD * aD;
127   }
128 
129   //! Computes the line normal to the direction of <me>, passing
130   //! through the point theP.  Raises ConstructionError
131   //! if the distance between <me> and the point theP is lower
132   //! or equal to Resolution from gp because there is an infinity of
133   //! solutions in 3D space.
134   gp_Lin Normal (const gp_Pnt& theP) const;
135 
136   Standard_EXPORT void Mirror (const gp_Pnt& theP);
137 
138   //! Performs the symmetrical transformation of a line
139   //! with respect to the point theP which is the center of
140   //! the symmetry.
141   Standard_NODISCARD Standard_EXPORT gp_Lin Mirrored (const gp_Pnt& theP) const;
142 
143   Standard_EXPORT void Mirror (const gp_Ax1& theA1);
144 
145   //! Performs the symmetrical transformation of a line
146   //! with respect to an axis placement which is the axis
147   //! of the symmetry.
148   Standard_NODISCARD Standard_EXPORT gp_Lin Mirrored (const gp_Ax1& theA1) const;
149 
150   Standard_EXPORT void Mirror (const gp_Ax2& theA2);
151 
152   //! Performs the symmetrical transformation of a line
153   //! with respect to a plane. The axis placement  <theA2>
154   //! locates the plane of the symmetry :
155   //! (Location, XDirection, YDirection).
156   Standard_NODISCARD Standard_EXPORT gp_Lin Mirrored (const gp_Ax2& theA2) const;
157 
Rotate(const gp_Ax1 & theA1,const Standard_Real theAng)158   void Rotate (const gp_Ax1& theA1, const Standard_Real theAng) { pos.Rotate (theA1, theAng); }
159 
160   //! Rotates a line. A1 is the axis of the rotation.
161   //! Ang is the angular value of the rotation in radians.
Rotated(const gp_Ax1 & theA1,const Standard_Real theAng) const162   Standard_NODISCARD gp_Lin Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
163   {
164     gp_Lin aL = *this;
165     aL.pos.Rotate (theA1, theAng);
166     return aL;
167   }
168 
Scale(const gp_Pnt & theP,const Standard_Real theS)169   void Scale (const gp_Pnt& theP, const Standard_Real theS) { pos.Scale (theP, theS); }
170 
171   //! Scales a line. theS is the scaling value.
172   //! The "Location" point (origin) of the line is modified.
173   //! The "Direction" is reversed if the scale is negative.
Scaled(const gp_Pnt & theP,const Standard_Real theS) const174   Standard_NODISCARD gp_Lin Scaled (const gp_Pnt& theP, const Standard_Real theS) const
175   {
176     gp_Lin aL = *this;
177     aL.pos.Scale (theP, theS);
178     return aL;
179   }
180 
Transform(const gp_Trsf & theT)181   void Transform (const gp_Trsf& theT) { pos.Transform (theT); }
182 
183   //! Transforms a line with the transformation theT from class Trsf.
Transformed(const gp_Trsf & theT) const184   Standard_NODISCARD gp_Lin Transformed (const gp_Trsf& theT) const
185   {
186     gp_Lin aL = *this;
187     aL.pos.Transform (theT);
188     return aL;
189   }
190 
Translate(const gp_Vec & theV)191   void Translate (const gp_Vec& theV) { pos.Translate (theV); }
192 
193   //! Translates a line in the direction of the vector theV.
194   //! The magnitude of the translation is the vector's magnitude.
Translated(const gp_Vec & theV) const195   Standard_NODISCARD gp_Lin Translated (const gp_Vec& theV) const
196   {
197     gp_Lin aL = *this;
198     aL.pos.Translate (theV);
199     return aL;
200   }
201 
Translate(const gp_Pnt & theP1,const gp_Pnt & theP2)202   void Translate (const gp_Pnt& theP1, const gp_Pnt& theP2) { pos.Translate (theP1, theP2); }
203 
204   //! Translates a line from the point theP1 to the point theP2.
Translated(const gp_Pnt & theP1,const gp_Pnt & theP2) const205   Standard_NODISCARD gp_Lin Translated (const gp_Pnt& theP1, const gp_Pnt& theP2) const
206   {
207     gp_Lin aL = *this;
208     aL.pos.Translate (gp_Vec(theP1, theP2));
209     return aL;
210   }
211 
212 private:
213 
214   gp_Ax1 pos;
215 
216 };
217 
218 //=======================================================================
219 //function : Distance
220 // purpose :
221 //=======================================================================
Distance(const gp_Pnt & theP) const222 inline Standard_Real gp_Lin::Distance (const gp_Pnt& theP) const
223 {
224   gp_XYZ aCoord = theP.XYZ();
225   aCoord.Subtract ((pos.Location()).XYZ());
226   aCoord.Cross ((pos.Direction()).XYZ());
227   return aCoord.Modulus();
228 }
229 
230 //=======================================================================
231 //function : SquareDistance
232 // purpose :
233 //=======================================================================
SquareDistance(const gp_Pnt & theP) const234 inline Standard_Real gp_Lin::SquareDistance(const gp_Pnt& theP) const
235 {
236   const gp_Pnt& aLoc = pos.Location();
237   gp_Vec aV (theP.X() - aLoc.X(),
238              theP.Y() - aLoc.Y(),
239              theP.Z() - aLoc.Z());
240   aV.Cross (pos.Direction());
241   return aV.SquareMagnitude();
242 }
243 
244 //=======================================================================
245 //function : Normal
246 // purpose :
247 //=======================================================================
Normal(const gp_Pnt & theP) const248 inline gp_Lin gp_Lin::Normal(const gp_Pnt& theP) const
249 {
250   const gp_Pnt& aLoc = pos.Location();
251   gp_Dir aV (theP.X() - aLoc.X(),
252              theP.Y() - aLoc.Y(),
253              theP.Z() - aLoc.Z());
254   aV = pos.Direction().CrossCrossed (aV, pos.Direction());
255   return gp_Lin(theP, aV);
256 }
257 
258 #endif // _gp_Lin_HeaderFile
259