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_Torus_HeaderFile
16 #define _gp_Torus_HeaderFile
17
18 #include <gp_Ax1.hxx>
19 #include <gp_Ax3.hxx>
20 #include <Standard_ConstructionError.hxx>
21 #include <Standard_DimensionError.hxx>
22 #include <TColStd_Array1OfReal.hxx>
23
24 //! Describes a torus.
25 //! A torus is defined by its major and minor radii and
26 //! positioned in space with a coordinate system (a gp_Ax3
27 //! object) as follows:
28 //! - The origin of the coordinate system is the center of the torus;
29 //! - The surface is obtained by rotating a circle of radius
30 //! equal to the minor radius of the torus about the "main
31 //! Direction" of the coordinate system. This circle is
32 //! located in the plane defined by the origin, the "X
33 //! Direction" and the "main Direction" of the coordinate
34 //! system. It is centered on the "X Axis" of this coordinate
35 //! system, and located at a distance, from the origin of
36 //! this coordinate system, equal to the major radius of the torus;
37 //! - The "X Direction" and "Y Direction" define the
38 //! reference plane of the torus.
39 //! The coordinate system described above is the "local
40 //! coordinate system" of the torus.
41 //! Note: when a gp_Torus torus is converted into a
42 //! Geom_ToroidalSurface torus, some implicit properties
43 //! of its local coordinate system are used explicitly:
44 //! - its origin, "X Direction", "Y Direction" and "main
45 //! Direction" are used directly to define the parametric
46 //! directions on the torus and the origin of the parameters,
47 //! - its implicit orientation (right-handed or left-handed)
48 //! gives the orientation (direct, indirect) to the
49 //! Geom_ToroidalSurface torus.
50 //! See Also
51 //! gce_MakeTorus which provides functions for more
52 //! complex torus constructions
53 //! Geom_ToroidalSurface which provides additional
54 //! functions for constructing tori and works, in particular,
55 //! with the parametric equations of tori.
56 class gp_Torus
57 {
58 public:
59
60 DEFINE_STANDARD_ALLOC
61
62 //! creates an indefinite Torus.
gp_Torus()63 gp_Torus()
64 : majorRadius (RealLast()),
65 minorRadius (RealSmall())
66 {}
67
68 //! a torus centered on the origin of coordinate system
69 //! theA3, with major radius theMajorRadius and minor radius
70 //! theMinorRadius, and with the reference plane defined
71 //! by the origin, the "X Direction" and the "Y Direction" of theA3.
72 //! Warnings :
73 //! It is not forbidden to create a torus with
74 //! theMajorRadius = theMinorRadius = 0.0
75 //! Raises ConstructionError if theMinorRadius < 0.0 or if theMajorRadius < 0.0
gp_Torus(const gp_Ax3 & theA3,const Standard_Real theMajorRadius,const Standard_Real theMinorRadius)76 gp_Torus (const gp_Ax3& theA3, const Standard_Real theMajorRadius, const Standard_Real theMinorRadius)
77 : pos (theA3),
78 majorRadius (theMajorRadius),
79 minorRadius (theMinorRadius)
80 {
81 Standard_ConstructionError_Raise_if (theMinorRadius < 0.0 || theMajorRadius < 0.0,
82 "gp_Torus() - invalid construction parameters");
83 }
84
85 //! Modifies this torus, by redefining its local coordinate
86 //! system so that:
87 //! - its origin and "main Direction" become those of the
88 //! axis theA1 (the "X Direction" and "Y Direction" are then recomputed).
89 //! Raises ConstructionError if the direction of theA1 is parallel to the "XDirection"
90 //! of the coordinate system of the toroidal surface.
SetAxis(const gp_Ax1 & theA1)91 void SetAxis (const gp_Ax1& theA1) { pos.SetAxis (theA1); }
92
93 //! Changes the location of the torus.
SetLocation(const gp_Pnt & theLoc)94 void SetLocation (const gp_Pnt& theLoc) { pos.SetLocation (theLoc); }
95
96 //! Assigns value to the major radius of this torus.
97 //! Raises ConstructionError if theMajorRadius - MinorRadius <= Resolution()
SetMajorRadius(const Standard_Real theMajorRadius)98 void SetMajorRadius (const Standard_Real theMajorRadius)
99 {
100 Standard_ConstructionError_Raise_if (theMajorRadius - minorRadius <= gp::Resolution(),
101 "gp_Torus::SetMajorRadius() - invalid input parameters");
102 majorRadius = theMajorRadius;
103 }
104
105 //! Assigns value to the minor radius of this torus.
106 //! Raises ConstructionError if theMinorRadius < 0.0 or if
107 //! MajorRadius - theMinorRadius <= Resolution from gp.
SetMinorRadius(const Standard_Real theMinorRadius)108 void SetMinorRadius (const Standard_Real theMinorRadius)
109 {
110 Standard_ConstructionError_Raise_if (theMinorRadius < 0.0 || majorRadius - theMinorRadius <= gp::Resolution(),
111 "gp_Torus::SetMinorRadius() - invalid input parameters");
112 minorRadius = theMinorRadius;
113 }
114
115 //! Changes the local coordinate system of the surface.
SetPosition(const gp_Ax3 & theA3)116 void SetPosition (const gp_Ax3& theA3) { pos = theA3; }
117
118 //! Computes the area of the torus.
Area() const119 Standard_Real Area() const { return 4.0 * M_PI * M_PI * minorRadius * majorRadius; }
120
121 //! Reverses the U parametrization of the torus
122 //! reversing the YAxis.
UReverse()123 void UReverse() { pos.YReverse(); }
124
125 //! Reverses the V parametrization of the torus
126 //! reversing the ZAxis.
VReverse()127 void VReverse() { pos.ZReverse(); }
128
129 //! returns true if the Ax3, the local coordinate system of this torus, is right handed.
Direct() const130 Standard_Boolean Direct() const { return pos.Direct(); }
131
132 //! returns the symmetry axis of the torus.
Axis() const133 const gp_Ax1& Axis() const { return pos.Axis(); }
134
135 //! Computes the coefficients of the implicit equation of the surface
136 //! in the absolute Cartesian coordinate system:
137 //! @code
138 //! Coef(1) * X^4 + Coef(2) * Y^4 + Coef(3) * Z^4 +
139 //! Coef(4) * X^3 * Y + Coef(5) * X^3 * Z + Coef(6) * Y^3 * X +
140 //! Coef(7) * Y^3 * Z + Coef(8) * Z^3 * X + Coef(9) * Z^3 * Y +
141 //! Coef(10) * X^2 * Y^2 + Coef(11) * X^2 * Z^2 +
142 //! Coef(12) * Y^2 * Z^2 + Coef(13) * X^2 * Y * Z +
143 //! Coef(14) * X * Y^2 * Z + Coef(15) * X * Y * Z^2 +
144 //! Coef(16) * X^3 + Coef(17) * Y^3 + Coef(18) * Z^3 +
145 //! Coef(19) * X^2 * Y + Coef(20) * X^2 * Z + Coef(21) * Y^2 * X +
146 //! Coef(22) * Y^2 * Z + Coef(23) * Z^2 * X + Coef(24) * Z^2 * Y +
147 //! Coef(25) * X * Y * Z +
148 //! Coef(26) * X^2 + Coef(27) * Y^2 + Coef(28) * Z^2 +
149 //! Coef(29) * X * Y + Coef(30) * X * Z + Coef(31) * Y * Z +
150 //! Coef(32) * X + Coef(33) * Y + Coef(34) * Z +
151 //! Coef(35) = 0.0
152 //! @endcode
153 //! Raises DimensionError if the length of theCoef is lower than 35.
154 Standard_EXPORT void Coefficients (TColStd_Array1OfReal& theCoef) const;
155
156 //! Returns the Torus's location.
Location() const157 const gp_Pnt& Location() const { return pos.Location(); }
158
159 //! Returns the local coordinates system of the torus.
Position() const160 const gp_Ax3& Position() const { return pos; }
161
162 //! returns the major radius of the torus.
MajorRadius() const163 Standard_Real MajorRadius() const { return majorRadius; }
164
165 //! returns the minor radius of the torus.
MinorRadius() const166 Standard_Real MinorRadius() const { return minorRadius; }
167
168 //! Computes the volume of the torus.
Volume() const169 Standard_Real Volume() const
170 {
171 return (M_PI * minorRadius * minorRadius) * (2.0 * M_PI * majorRadius);
172 }
173
174 //! returns the axis X of the torus.
XAxis() const175 gp_Ax1 XAxis() const
176 {
177 return gp_Ax1 (pos.Location(), pos.XDirection());
178 }
179
180 //! returns the axis Y of the torus.
YAxis() const181 gp_Ax1 YAxis() const
182 {
183 return gp_Ax1 (pos.Location(), pos.YDirection());
184 }
185
186 Standard_EXPORT void Mirror (const gp_Pnt& theP);
187
188 //! Performs the symmetrical transformation of a torus
189 //! with respect to the point theP which is the center of the
190 //! symmetry.
191 Standard_NODISCARD Standard_EXPORT gp_Torus Mirrored (const gp_Pnt& theP) const;
192
193 Standard_EXPORT void Mirror (const gp_Ax1& theA1);
194
195 //! Performs the symmetrical transformation of a torus with
196 //! respect to an axis placement which is the axis of the
197 //! symmetry.
198 Standard_NODISCARD Standard_EXPORT gp_Torus Mirrored (const gp_Ax1& theA1) const;
199
200 Standard_EXPORT void Mirror (const gp_Ax2& theA2);
201
202 //! Performs the symmetrical transformation of a torus with respect
203 //! to a plane. The axis placement theA2 locates the plane of the
204 //! of the symmetry : (Location, XDirection, YDirection).
205 Standard_NODISCARD Standard_EXPORT gp_Torus Mirrored (const gp_Ax2& theA2) const;
206
Rotate(const gp_Ax1 & theA1,const Standard_Real theAng)207 void Rotate (const gp_Ax1& theA1, const Standard_Real theAng) { pos.Rotate (theA1, theAng); }
208
209 //! Rotates a torus. theA1 is the axis of the rotation.
210 //! theAng is the angular value of the rotation in radians.
Rotated(const gp_Ax1 & theA1,const Standard_Real theAng) const211 Standard_NODISCARD gp_Torus Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
212 {
213 gp_Torus aC = *this;
214 aC.pos.Rotate (theA1, theAng);
215 return aC;
216 }
217
218 void Scale (const gp_Pnt& theP, const Standard_Real theS);
219
220 //! Scales a torus. S is the scaling value.
221 //! The absolute value of S is used to scale the torus
222 Standard_NODISCARD gp_Torus Scaled (const gp_Pnt& theP, const Standard_Real theS) const;
223
224 void Transform (const gp_Trsf& theT);
225
226 //! Transforms a torus with the transformation theT from class Trsf.
227 Standard_NODISCARD gp_Torus Transformed (const gp_Trsf& theT) const;
228
Translate(const gp_Vec & theV)229 void Translate (const gp_Vec& theV) { pos.Translate (theV); }
230
231 //! Translates a torus in the direction of the vector theV.
232 //! The magnitude of the translation is the vector's magnitude.
Translated(const gp_Vec & theV) const233 Standard_NODISCARD gp_Torus Translated (const gp_Vec& theV) const
234 {
235 gp_Torus aC = *this;
236 aC.pos.Translate (theV);
237 return aC;
238 }
239
Translate(const gp_Pnt & theP1,const gp_Pnt & theP2)240 void Translate (const gp_Pnt& theP1, const gp_Pnt& theP2) { pos.Translate (theP1, theP2); }
241
242 //! Translates a torus from the point theP1 to the point theP2.
Translated(const gp_Pnt & theP1,const gp_Pnt & theP2) const243 Standard_NODISCARD gp_Torus Translated (const gp_Pnt& theP1, const gp_Pnt& theP2) const
244 {
245 gp_Torus aC = *this;
246 aC.pos.Translate (theP1, theP2);
247 return aC;
248 }
249
250 private:
251
252 gp_Ax3 pos;
253 Standard_Real majorRadius;
254 Standard_Real minorRadius;
255
256 };
257
258 //=======================================================================
259 //function : Scale
260 // purpose :
261 //=======================================================================
Scale(const gp_Pnt & theP,const Standard_Real theS)262 inline void gp_Torus::Scale (const gp_Pnt& theP,
263 const Standard_Real theS)
264 {
265 pos.Scale (theP, theS);
266 Standard_Real s = theS;
267 if (s < 0)
268 {
269 s = -s;
270 }
271 majorRadius *= s;
272 minorRadius *= s;
273 }
274
275 //=======================================================================
276 //function : Scaled
277 // purpose :
278 //=======================================================================
Scaled(const gp_Pnt & theP,const Standard_Real theS) const279 inline gp_Torus gp_Torus::Scaled (const gp_Pnt& theP,
280 const Standard_Real theS) const
281 {
282 gp_Torus aC = *this;
283 aC.pos.Scale (theP, theS);
284 aC.majorRadius *= theS;
285 if (aC.majorRadius < 0)
286 {
287 aC.majorRadius = -aC.majorRadius;
288 }
289 aC.minorRadius *= theS;
290 if (aC.minorRadius < 0)
291 {
292 aC.minorRadius = -aC.minorRadius;
293 }
294 return aC;
295 }
296
297 //=======================================================================
298 //function : Transform
299 // purpose :
300 //=======================================================================
Transform(const gp_Trsf & theT)301 inline void gp_Torus::Transform (const gp_Trsf& theT)
302 {
303 pos.Transform (theT);
304 Standard_Real aT = theT.ScaleFactor();
305 if (aT < 0)
306 {
307 aT = -aT;
308 }
309 minorRadius *= aT;
310 majorRadius *= aT;
311 }
312
313 //=======================================================================
314 //function : Transformed
315 // purpose :
316 //=======================================================================
Transformed(const gp_Trsf & theT) const317 inline gp_Torus gp_Torus::Transformed (const gp_Trsf& theT) const
318 {
319 gp_Torus aC = *this;
320 aC.pos.Transform (theT);
321 aC.majorRadius *= theT.ScaleFactor();
322 if (aC.majorRadius < 0)
323 {
324 aC.majorRadius = -aC.majorRadius;
325 }
326 aC.minorRadius *= theT.ScaleFactor();
327 if (aC.minorRadius < 0)
328 {
329 aC.minorRadius = -aC.minorRadius;
330 }
331 return aC;
332 }
333
334 #endif // _gp_Torus_HeaderFile
335