0032137: Coding Rules - merge redundant .lxx files into header files within Package gp
[occt.git] / src / gp / gp_Pnt.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_Pnt_HeaderFile
16 #define _gp_Pnt_HeaderFile
17
18 #include <Standard.hxx>
19 #include <Standard_DefineAlloc.hxx>
20 #include <Standard_Handle.hxx>
21 #include <gp_XYZ.hxx>
22 #include <Standard_Real.hxx>
23 #include <Standard_Integer.hxx>
24 #include <Standard_Boolean.hxx>
25
26 class gp_XYZ;
27 class Standard_OutOfRange;
28 class gp_Ax1;
29 class gp_Ax2;
30 class gp_Trsf;
31 class gp_Vec;
32
33 //! Defines a 3D cartesian point.
34 class gp_Pnt 
35 {
36 public:
37
38   DEFINE_STANDARD_ALLOC
39
40   //! Creates a point with zero coordinates.
41   gp_Pnt() {}
42
43   //! Creates a point from a XYZ object.
44   gp_Pnt (const gp_XYZ& theCoord)
45   : coord (theCoord)
46   {}
47
48   //! Creates a  point with its 3 cartesian's coordinates : theXp, theYp, theZp.
49   gp_Pnt (const Standard_Real theXp, const Standard_Real theYp, const Standard_Real theZp)
50   : coord (theXp, theYp, theZp)
51   {}
52
53   //! Changes the coordinate of range theIndex :
54   //! theIndex = 1 => X is modified
55   //! theIndex = 2 => Y is modified
56   //! theIndex = 3 => Z is modified
57   //! Raised if theIndex != {1, 2, 3}.
58   void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi)
59   {
60     coord.SetCoord (theIndex, theXi);
61   }
62
63   //! For this point, assigns  the values theXp, theYp and theZp to its three coordinates.
64   void SetCoord (const Standard_Real theXp, const Standard_Real theYp, const Standard_Real theZp)
65   {
66     coord.SetCoord (theXp, theYp, theZp);
67   }
68
69   //! Assigns the given value to the X coordinate of this point.
70   void SetX (const Standard_Real theX) { coord.SetX (theX); }
71
72   //! Assigns the given value to the Y coordinate of this point.
73   void SetY (const Standard_Real theY) { coord.SetY (theY); }
74
75   //! Assigns the given value to the Z coordinate of this point.
76   void SetZ (const Standard_Real theZ) { coord.SetZ (theZ); }
77
78   //! Assigns the three coordinates of theCoord to this point.
79   void SetXYZ (const gp_XYZ& theCoord) { coord = theCoord; }
80
81   //! Returns the coordinate of corresponding to the value of theIndex :
82   //! theIndex = 1 => X is returned
83   //! theIndex = 2 => Y is returned
84   //! theIndex = 3 => Z is returned
85   //! Raises OutOfRange if theIndex != {1, 2, 3}.
86   //! Raised if theIndex != {1, 2, 3}.
87   Standard_Real Coord (const Standard_Integer theIndex) const { return coord.Coord (theIndex); }
88
89   //! For this point gives its three coordinates theXp, theYp and theZp.
90   void Coord (Standard_Real& theXp, Standard_Real& theYp, Standard_Real& theZp) const
91   {
92     coord.Coord (theXp, theYp, theZp);
93   }
94
95   //! For this point, returns its X coordinate.
96   Standard_Real X() const { return coord.X(); }
97
98   //! For this point, returns its Y coordinate.
99   Standard_Real Y() const { return coord.Y(); }
100
101   //! For this point, returns its Z coordinate.
102   Standard_Real Z() const { return coord.Z(); }
103
104   //! For this point, returns its three coordinates as a XYZ object.
105   const gp_XYZ& XYZ() const { return coord; }
106
107   //! For this point, returns its three coordinates as a XYZ object.
108   const gp_XYZ& Coord() const { return coord; }
109
110   //! Returns the coordinates of this point.
111   //! Note: This syntax allows direct modification of the returned value.
112   gp_XYZ& ChangeCoord() { return coord; }
113
114   //! Assigns the result of the following expression to this point
115   //! (theAlpha*this + theBeta*theP) / (theAlpha + theBeta)
116   void BaryCenter (const Standard_Real theAlpha, const gp_Pnt& theP, const Standard_Real theBeta)
117   {
118     coord.SetLinearForm (theAlpha, coord, theBeta, theP.coord);
119     coord.Divide (theAlpha + theBeta);
120   }
121
122   //! Comparison
123   //! Returns True if the distance between the two points is
124   //! lower or equal to theLinearTolerance.
125   Standard_Boolean IsEqual (const gp_Pnt& theOther, const Standard_Real theLinearTolerance) const
126   {
127     return Distance (theOther) <= theLinearTolerance;
128   }
129
130   //! Computes the distance between two points.
131   Standard_Real Distance (const gp_Pnt& theOther) const;
132
133   //! Computes the square distance between two points.
134   Standard_Real SquareDistance (const gp_Pnt& theOther) const;
135
136   //! Performs the symmetrical transformation of a point
137   //! with respect to the point theP which is the center of
138   //! the  symmetry.
139   Standard_EXPORT void Mirror (const gp_Pnt& theP);
140
141   //! Performs the symmetrical transformation of a point
142   //! with respect to an axis placement which is the axis
143   //! of the symmetry.
144   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Pnt& theP) const;
145
146   Standard_EXPORT void Mirror (const gp_Ax1& theA1);
147
148   //! Performs the symmetrical transformation of a point
149   //! with respect to a plane. The axis placement theA2 locates
150   //! the plane of the symmetry : (Location, XDirection, YDirection).
151   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Ax1& theA1) const;
152
153   Standard_EXPORT void Mirror (const gp_Ax2& theA2);
154
155   //! Rotates a point. theA1 is the axis of the rotation.
156   //! theAng is the angular value of the rotation in radians.
157   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Ax2& theA2) const;
158
159   void Rotate (const gp_Ax1& theA1, const Standard_Real theAng);
160
161   Standard_NODISCARD gp_Pnt Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
162   {
163     gp_Pnt aP = *this;
164     aP.Rotate (theA1, theAng);
165     return aP;
166   }
167
168   //! Scales a point. theS is the scaling value.
169   void Scale (const gp_Pnt& theP, const Standard_Real theS);
170
171   Standard_NODISCARD gp_Pnt Scaled (const gp_Pnt& theP, const Standard_Real theS) const
172   {
173     gp_Pnt aPres = *this;
174     aPres.Scale (theP, theS);
175     return aPres;
176   }
177
178   //! Transforms a point with the transformation T.
179   Standard_EXPORT void Transform (const gp_Trsf& theT);
180
181   Standard_NODISCARD gp_Pnt Transformed (const gp_Trsf& theT) const
182   {
183     gp_Pnt aP = *this;
184     aP.Transform (theT);
185     return aP;
186   }
187
188   //! Translates a point in the direction of the vector theV.
189   //! The magnitude of the translation is the vector's magnitude.
190   void Translate (const gp_Vec& theV);
191
192   Standard_NODISCARD gp_Pnt Translated (const gp_Vec& theV) const;
193
194   //! Translates a point from the point theP1 to the point theP2.
195   void Translate (const gp_Pnt& theP1, const gp_Pnt& theP2)
196   {
197     coord.Add (theP2.coord);
198     coord.Subtract (theP1.coord);
199   }
200
201   Standard_NODISCARD gp_Pnt Translated (const gp_Pnt& theP1, const gp_Pnt& theP2) const
202   {
203     gp_Pnt aP = *this;
204     aP.Translate (theP1, theP2);
205     return aP;
206   }
207
208   //! Dumps the content of me into the stream
209   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
210
211   //! Inits the content of me from the stream
212   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
213
214 private:
215
216   gp_XYZ coord;
217
218 };
219
220 #include <gp_Trsf.hxx>
221 #include <gp_Vec.hxx>
222 #include <gp_XYZ.hxx>
223
224 //=======================================================================
225 //function : Distance
226 // purpose :
227 //=======================================================================
228 inline Standard_Real gp_Pnt::Distance (const gp_Pnt& theOther) const
229 {
230   Standard_Real aD=0,aDD;
231   const gp_XYZ& aXYZ = theOther.coord;
232   aDD = coord.X(); aDD -= aXYZ.X(); aDD *= aDD; aD += aDD;
233   aDD = coord.Y(); aDD -= aXYZ.Y(); aDD *= aDD; aD += aDD;
234   aDD = coord.Z(); aDD -= aXYZ.Z(); aDD *= aDD; aD += aDD;
235   return sqrt (aD);
236 }
237
238 //=======================================================================
239 //function : SquareDistance
240 // purpose :
241 //=======================================================================
242 inline Standard_Real gp_Pnt::SquareDistance (const gp_Pnt& theOther) const
243 {
244   Standard_Real aD=0, aDD;
245   const gp_XYZ& XYZ = theOther.coord;
246   aDD = coord.X(); aDD -= XYZ.X(); aDD *= aDD; aD += aDD;
247   aDD = coord.Y(); aDD -= XYZ.Y(); aDD *= aDD; aD += aDD;
248   aDD = coord.Z(); aDD -= XYZ.Z(); aDD *= aDD; aD += aDD;
249   return aD;
250 }
251
252 //=======================================================================
253 //function : Rotate
254 // purpose :
255 //=======================================================================
256 inline void gp_Pnt::Rotate (const gp_Ax1& theA1, const Standard_Real theAng)
257 {
258   gp_Trsf aT;
259   aT.SetRotation (theA1, theAng);
260   aT.Transforms (coord);
261 }
262
263 //=======================================================================
264 //function : Scale
265 // purpose :
266 //=======================================================================
267 inline void gp_Pnt::Scale (const gp_Pnt& theP, const Standard_Real theS)
268 {
269   gp_XYZ aXYZ = theP.coord;
270   aXYZ.Multiply (1.0 - theS);
271   coord.Multiply (theS);
272   coord.Add (aXYZ);
273 }
274
275 //=======================================================================
276 //function : Translate
277 // purpose :
278 //=======================================================================
279 inline void gp_Pnt::Translate(const gp_Vec& theV)
280 {
281   coord.Add (theV.XYZ());
282 }
283
284 //=======================================================================
285 //function : Translated
286 // purpose :
287 //=======================================================================
288 inline gp_Pnt gp_Pnt::Translated (const gp_Vec& theV) const
289 {
290   gp_Pnt aP = *this;
291   aP.coord.Add (theV.XYZ());
292   return aP;
293 }
294
295 #endif // _gp_Pnt_HeaderFile