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