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::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
109 if (!theShape->isFace())
111 TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
112 TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
114 Handle(Geom_Surface) anOwnSurf = BRep_Tool::Surface(anOwnFace);
115 Handle(Geom_Surface) anOtherSurf = BRep_Tool::Surface(anOtherFace);
116 return anOwnSurf == anOtherSurf;
119 bool GeomAPI_Face::isCylindrical() const
121 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
122 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
123 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
124 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
125 if (!aTrimmed.IsNull())
126 aSurf = aTrimmed->BasisSurface();
127 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
130 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
132 std::shared_ptr<GeomAPI_Pln> aResult;
133 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
135 return aResult; // null shape
136 if (aShape.ShapeType() != TopAbs_FACE)
137 return aResult; // not face
138 TopoDS_Face aFace = TopoDS::Face(aShape);
140 return aResult; // not face
141 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
143 return aResult; // no surface
144 GeomLib_IsPlanarSurface isPlanar(aSurf);
145 if(!isPlanar.IsPlanar()) {
148 gp_Pln aPln = isPlanar.Plan();
149 double aA, aB, aC, aD;
150 aPln.Coefficients(aA, aB, aC, aD);
151 if (aFace.Orientation() == TopAbs_REVERSED) {
157 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
161 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
163 GeomSpherePtr aSphere;
165 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
166 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
167 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
168 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
169 const gp_Pnt& aCenter = aSph.Location();
170 double aRadius = aSph.Radius();
172 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
173 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
178 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
180 GeomCylinderPtr aCylinder;
182 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
183 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
184 if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
185 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
186 gp_Pnt aLoc = aCyl.Location();
187 const gp_Dir& aDir = aCyl.Position().Direction();
188 double aRadius = aCyl.Radius();
190 double aUMin, aUMax, aVMin, aVMax;
191 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
192 double aHeight = aVMax - aVMin;
194 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
195 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
196 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
197 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
202 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
206 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
207 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
208 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
209 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
210 gp_Pnt aLoc = aCon.Location();
211 gp_Dir aDir = aCon.Position().Direction();
213 double aUMin, aUMax, aVMin, aVMax;
214 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
216 double aSemiAngle = Abs(aCon.SemiAngle());
217 double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
218 double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
220 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
222 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
223 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
224 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
229 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
233 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
234 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
235 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
236 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
237 const gp_Pnt& aLoc = aTor.Location();
238 const gp_Dir& aDir = aTor.Position().Direction();
239 double aMajorRadius = aTor.MajorRadius();
240 double aMinorRadius = aTor.MinorRadius();
242 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
243 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
244 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
249 GeomPointPtr GeomAPI_Face::middlePoint() const
251 GeomPointPtr anInnerPoint;
253 const TopoDS_Face& aFace = impl<TopoDS_Face>();
257 BRepGProp_Face aProp(aFace);
258 double aUMin, aUMax, aVMin, aVMax;
259 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
261 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
265 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
266 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));