Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomAPI / GeomAPI_Cylinder.cpp
1 // Copyright (C) 2018-2022  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_Cylinder.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_Dir.h>
23
24 #include <gp_Cylinder.hxx>
25 #include <Precision.hxx>
26
27 #define MY_CYL implPtr<gp_Cylinder>()
28
29 static gp_Cylinder* newCyl(const gp_Pnt& theLocation, const gp_Dir& theAxis, const double theRadius)
30 {
31   return new gp_Cylinder(gp_Ax3(theLocation, theAxis), theRadius);
32 }
33
34 //=================================================================================================
35 GeomAPI_Cylinder::GeomAPI_Cylinder(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
36                                    const std::shared_ptr<GeomAPI_Dir>& theAxis,
37                                    const double theRadius)
38   : GeomAPI_Interface(newCyl(theLocation->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theRadius)),
39     myHeight(Precision::Infinite())
40 {
41 }
42
43 //=================================================================================================
44 GeomAPI_Cylinder::GeomAPI_Cylinder(const std::shared_ptr<GeomAPI_Pnt>& theLocation,
45                                    const std::shared_ptr<GeomAPI_Dir>& theAxis,
46                                    const double theRadius,
47                                    const double theHeight)
48   : GeomAPI_Interface(newCyl(theLocation->impl<gp_Pnt>(), theAxis->impl<gp_Dir>(), theRadius)),
49     myHeight(theHeight)
50 {
51 }
52
53 //=================================================================================================
54 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Cylinder::location() const
55 {
56   const gp_Pnt& aCenter = MY_CYL->Location();
57   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
58 }
59
60 //=================================================================================================
61 std::shared_ptr<GeomAPI_Dir> GeomAPI_Cylinder::axis() const
62 {
63   const gp_Dir& anAxis = MY_CYL->Position().Direction();
64   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
65 }
66
67 //=================================================================================================
68 double GeomAPI_Cylinder::radius() const
69 {
70   return MY_CYL->Radius();
71 }
72
73 //=================================================================================================
74 double GeomAPI_Cylinder::height() const
75 {
76   return myHeight;
77 }
78
79 //=================================================================================================
80 bool GeomAPI_Cylinder::isInfinite() const
81 {
82   return Precision::IsInfinite(myHeight);
83 }
84
85 //=================================================================================================
86 bool GeomAPI_Cylinder::isCoincident(const GeomCylinderPtr theCylinder, const double theTolerance)
87 {
88   if (!theCylinder)
89     return false;
90
91   gp_Cylinder* anOther = theCylinder->implPtr<gp_Cylinder>();
92   if (fabs(MY_CYL->Radius() - anOther->Radius()) < theTolerance) {
93     gp_Dir aDir1 = MY_CYL->Position().Direction();
94     gp_Dir aDir2 = anOther->Position().Direction();
95     if (aDir1.IsParallel(aDir2, Precision::Angular())) {
96       gp_Pnt aLoc1 = MY_CYL->Location();
97       gp_Pnt aLoc2 = anOther->Location();
98       gp_Vec aVec12(aLoc1, aLoc2);
99       if (aVec12.SquareMagnitude() < theTolerance * theTolerance ||
100           aVec12.IsParallel(aDir1, Precision::Angular())) {
101         return true;
102       }
103     }
104   }
105   return false;
106 }