1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAPI_Face.h"
22 #include "GeomAPI_Dir.h"
23 #include "GeomAPI_Pln.h"
24 #include "GeomAPI_Pnt.h"
25 #include "GeomAPI_Sphere.h"
26 #include "GeomAPI_Cylinder.h"
27 #include "GeomAPI_Cone.h"
28 #include "GeomAPI_Torus.h"
30 #include <BOPTools_AlgoTools.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRepAdaptor_Surface.hxx>
33 #include <BRepGProp_Face.hxx>
34 #include <BRepTools.hxx>
35 #include <Geom_Surface.hxx>
36 #include <Geom_SphericalSurface.hxx>
37 #include <Geom_ConicalSurface.hxx>
38 #include <Geom_CylindricalSurface.hxx>
39 #include <Geom_RectangularTrimmedSurface.hxx>
40 #include <Geom_ToroidalSurface.hxx>
41 #include <GeomLib_IsPlanarSurface.hxx>
42 #include <IntTools_Context.hxx>
43 #include <Standard_Type.hxx>
45 #include <TopoDS_Face.hxx>
47 #include <gp_Sphere.hxx>
48 #include <gp_Cylinder.hxx>
49 #include <gp_Cone.hxx>
50 #include <gp_Torus.hxx>
53 GeomAPI_Face::GeomAPI_Face()
58 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
60 if (!theShape->isNull() && theShape->isFace()) {
61 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
65 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
70 if (!theFace->isFace())
73 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
74 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
76 TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
77 TopoDS_Face aInFace = TopoDS::Face(aInShape);
79 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
80 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
82 // Check that surfaces a the same type
83 if (aMySurf->DynamicType() != aInSurf->DynamicType())
86 // Get parameters of surfaces
87 double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
88 aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
89 double aInUMin, aInUMax, aInVMin, aInVMax;
90 aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
92 // Check that parameters are the same
93 if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
94 fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
95 fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
96 fabs(aMyVMax - aInVMax) > Precision::PConfusion())
99 Handle(IntTools_Context) aContext = new IntTools_Context();
100 // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
101 Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
102 && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
104 return aRes == Standard_True;
107 bool GeomAPI_Face::isCylindrical() const
109 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
110 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
111 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
112 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
113 if (!aTrimmed.IsNull())
114 aSurf = aTrimmed->BasisSurface();
115 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
118 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
120 std::shared_ptr<GeomAPI_Pln> aResult;
121 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
123 return aResult; // null shape
124 if (aShape.ShapeType() != TopAbs_FACE)
125 return aResult; // not face
126 TopoDS_Face aFace = TopoDS::Face(aShape);
128 return aResult; // not face
129 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
131 return aResult; // no surface
132 GeomLib_IsPlanarSurface isPlanar(aSurf);
133 if(!isPlanar.IsPlanar()) {
136 gp_Pln aPln = isPlanar.Plan();
137 double aA, aB, aC, aD;
138 aPln.Coefficients(aA, aB, aC, aD);
139 if (aFace.Orientation() == TopAbs_REVERSED) {
145 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
149 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
151 GeomSpherePtr aSphere;
153 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
154 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
155 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
156 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
157 const gp_Pnt& aCenter = aSph.Location();
158 double aRadius = aSph.Radius();
160 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
161 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
166 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
168 GeomCylinderPtr aCylinder;
170 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
171 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
172 if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
173 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
174 gp_Pnt aLoc = aCyl.Location();
175 const gp_Dir& aDir = aCyl.Position().Direction();
176 double aRadius = aCyl.Radius();
178 double aUMin, aUMax, aVMin, aVMax;
179 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
180 double aHeight = aVMax - aVMin;
182 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
183 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
184 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
185 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
190 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
194 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
195 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
196 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
197 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
198 gp_Pnt aLoc = aCon.Location();
199 gp_Dir aDir = aCon.Position().Direction();
201 double aUMin, aUMax, aVMin, aVMax;
202 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
204 double aSemiAngle = Abs(aCon.SemiAngle());
205 double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
206 double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
208 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
210 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
211 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
212 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
217 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
221 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
222 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
223 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
224 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
225 const gp_Pnt& aLoc = aTor.Location();
226 const gp_Dir& aDir = aTor.Position().Direction();
227 double aMajorRadius = aTor.MajorRadius();
228 double aMinorRadius = aTor.MinorRadius();
230 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
231 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
232 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
237 GeomPointPtr GeomAPI_Face::middlePoint() const
239 GeomPointPtr anInnerPoint;
241 const TopoDS_Face& aFace = impl<TopoDS_Face>();
245 BRepGProp_Face aProp(aFace);
246 double aUMin, aUMax, aVMin, aVMax;
247 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
249 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
253 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
254 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));