Salome HOME
9014671a5c2bd6a2fcd5f90ebb22b2af0f6a4afa
[modules/shaper.git] / src / GeomAPI / GeomAPI_Cone.cpp
1 // Copyright (C) 2018-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAPI_Cone.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_Dir.h>
23
24 #include <gp_Cone.hxx>
25 #include <Precision.hxx>
26
27 #define MY_CONE implPtr<gp_Cone>()
28
29 static gp_Cone* newCone(const gp_Pnt& theApex, const gp_Dir& theAxis,
30                         const double theSemiAngle, const double theRadius = 0.0)
31 {
32   return new gp_Cone(gp_Ax3(theApex, theAxis), theSemiAngle, theRadius);
33 }
34
35 //=================================================================================================
36 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theApex,
37                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
38                            const double theSemiAngle)
39   : GeomAPI_Interface(newCone(theApex->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theSemiAngle)),
40     myRadius1(Precision::Infinite()),
41     myRadius2(Precision::Infinite())
42 {
43 }
44
45 //=================================================================================================
46 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
47                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
48                            const double theSemiAngle,
49                            const double theRadius)
50   : GeomAPI_Interface(
51       newCone(theLocation->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theSemiAngle, theRadius)),
52     myRadius1(theRadius),
53     myRadius2(Precision::Infinite())
54 {
55 }
56
57 //=================================================================================================
58 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
59                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
60                            const double theSemiAngle,
61                            const double theRadius1,
62                            const double theRadius2)
63   : myRadius1(theRadius1),
64     myRadius2(theRadius2)
65 {
66   gp_Pnt aLoc = theLocation->impl<gp_Pnt>();
67   gp_Dir aDir = theAxis->impl<gp_Dir>();
68   if (theRadius1 > theRadius2) {
69     aLoc.ChangeCoord() += aDir.XYZ() * (theRadius1 - theRadius2) / Tan(theSemiAngle);
70     aDir.Reverse();
71     myRadius1 = theRadius2;
72     myRadius2 = theRadius1;
73   }
74
75   setImpl(newCone(aLoc, aDir, theSemiAngle, myRadius1));
76 }
77
78 //=================================================================================================
79 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Cone::apex() const
80 {
81   const gp_Pnt& anApex = MY_CONE->Apex();
82   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(anApex.X(), anApex.Y(), anApex.Z()));
83 }
84
85 //=================================================================================================
86 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Cone::location() const
87 {
88   const gp_Pnt& aLoc = MY_CONE->Location();
89   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
90 }
91
92 //=================================================================================================
93 std::shared_ptr<GeomAPI_Dir> GeomAPI_Cone::axis() const
94 {
95   const gp_Dir& anAxis = MY_CONE->Position().Direction();
96   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
97 }
98
99 //=================================================================================================
100 double GeomAPI_Cone::semiAngle() const
101 {
102   return MY_CONE->SemiAngle();
103 }
104
105 //=================================================================================================
106 double GeomAPI_Cone::radius1() const
107 {
108   return myRadius1;
109 }
110
111 //=================================================================================================
112 double GeomAPI_Cone::radius2() const
113 {
114   return myRadius2;
115 }
116
117 //=================================================================================================
118 double GeomAPI_Cone::height() const
119 {
120   if (Precision::IsInfinite(myRadius1) || Precision::IsInfinite(myRadius2))
121     return Precision::Infinite();
122
123   return Abs((myRadius2 - myRadius1) / Tan(semiAngle()));
124 }
125
126 //=================================================================================================
127 bool GeomAPI_Cone::isSemiInfinite() const
128 {
129   return (!Precision::IsInfinite(myRadius1) && Precision::IsInfinite(myRadius2)) ||
130          (Precision::IsInfinite(myRadius1) && !Precision::IsInfinite(myRadius2));
131 }
132
133 //=================================================================================================
134 bool GeomAPI_Cone::isInfinite() const
135 {
136   return Precision::IsInfinite(myRadius1) && Precision::IsInfinite(myRadius2);
137 }