Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[modules/shaper.git] / src / GeomAPI / GeomAPI_Cone.cpp
1 // Copyright (C) 2018-20xx  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_Cone.h>
22 #include <GeomAPI_Pnt.h>
23 #include <GeomAPI_Dir.h>
24
25 #include <gp_Cone.hxx>
26 #include <Precision.hxx>
27
28 #define MY_CONE implPtr<gp_Cone>()
29
30 static gp_Cone* newCone(const gp_Pnt& theApex, const gp_Dir& theAxis,
31                         const double theSemiAngle, const double theRadius = 0.0)
32 {
33   return new gp_Cone(gp_Ax3(theApex, theAxis), theSemiAngle, theRadius);
34 }
35
36 //=================================================================================================
37 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theApex,
38                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
39                            const double theSemiAngle)
40   : GeomAPI_Interface(newCone(theApex->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theSemiAngle)),
41     myRadius1(Precision::Infinite()),
42     myRadius2(Precision::Infinite())
43 {
44 }
45
46 //=================================================================================================
47 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
48                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
49                            const double theSemiAngle,
50                            const double theRadius)
51   : GeomAPI_Interface(
52       newCone(theLocation->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theSemiAngle, theRadius)),
53     myRadius1(theRadius),
54     myRadius2(Precision::Infinite())
55 {
56 }
57
58 //=================================================================================================
59 GeomAPI_Cone::GeomAPI_Cone(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
60                            const std::shared_ptr<GeomAPI_Dir>& theAxis,
61                            const double theSemiAngle,
62                            const double theRadius1,
63                            const double theRadius2)
64   : myRadius1(theRadius1),
65     myRadius2(theRadius2)
66 {
67   gp_Pnt aLoc = theLocation->impl<gp_Pnt>();
68   gp_Dir aDir = theAxis->impl<gp_Dir>();
69   if (theRadius1 > theRadius2) {
70     aLoc.ChangeCoord() += aDir.XYZ() * (theRadius1 - theRadius2) / Tan(theSemiAngle);
71     aDir.Reverse();
72     myRadius1 = theRadius2;
73     myRadius2 = theRadius1;
74   }
75
76   setImpl(newCone(aLoc, aDir, theSemiAngle, myRadius1));
77 }
78
79 //=================================================================================================
80 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Cone::apex() const
81 {
82   const gp_Pnt& anApex = MY_CONE->Apex();
83   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(anApex.X(), anApex.Y(), anApex.Z()));
84 }
85
86 //=================================================================================================
87 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Cone::location() const
88 {
89   const gp_Pnt& aLoc = MY_CONE->Location();
90   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
91 }
92
93 //=================================================================================================
94 std::shared_ptr<GeomAPI_Dir> GeomAPI_Cone::axis() const
95 {
96   const gp_Dir& anAxis = MY_CONE->Position().Direction();
97   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
98 }
99
100 //=================================================================================================
101 double GeomAPI_Cone::semiAngle() const
102 {
103   return MY_CONE->SemiAngle();
104 }
105
106 //=================================================================================================
107 double GeomAPI_Cone::radius1() const
108 {
109   return myRadius1;
110 }
111
112 //=================================================================================================
113 double GeomAPI_Cone::radius2() const
114 {
115   return myRadius2;
116 }
117
118 //=================================================================================================
119 double GeomAPI_Cone::height() const
120 {
121   if (Precision::IsInfinite(myRadius1) || Precision::IsInfinite(myRadius2))
122     return Precision::Infinite();
123
124   return Abs((myRadius2 - myRadius1) / Tan(semiAngle()));
125 }
126
127 //=================================================================================================
128 bool GeomAPI_Cone::isSemiInfinite() const
129 {
130   return (!Precision::IsInfinite(myRadius1) && Precision::IsInfinite(myRadius2)) ||
131          (Precision::IsInfinite(myRadius1) && !Precision::IsInfinite(myRadius2));
132 }
133
134 //=================================================================================================
135 bool GeomAPI_Cone::isInfinite() const
136 {
137   return Precision::IsInfinite(myRadius1) && Precision::IsInfinite(myRadius2);
138 }