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