1 // Copyright (C) 2014-2020 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_Curve.h"
27 #include "GeomAPI_Cylinder.h"
28 #include "GeomAPI_Cone.h"
29 #include "GeomAPI_Torus.h"
31 #include <BOPTools_AlgoTools.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAdaptor_Surface.hxx>
34 #include <BRepGProp_Face.hxx>
35 #include <BRepTools.hxx>
36 #include <BRepTopAdaptor_TopolTool.hxx>
37 #include <Geom_Surface.hxx>
38 #include <Geom_SphericalSurface.hxx>
39 #include <Geom_ConicalSurface.hxx>
40 #include <Geom_CylindricalSurface.hxx>
41 #include <Geom_OffsetSurface.hxx>
42 #include <Geom_Plane.hxx>
43 #include <Geom_RectangularTrimmedSurface.hxx>
44 #include <Geom_SurfaceOfLinearExtrusion.hxx>
45 #include <Geom_SurfaceOfRevolution.hxx>
46 #include <Geom_SweptSurface.hxx>
47 #include <Geom_ToroidalSurface.hxx>
48 #include <GeomAdaptor_HSurface.hxx>
49 #include <GeomAPI_ExtremaCurveCurve.hxx>
50 #include <GeomLib_IsPlanarSurface.hxx>
51 #include <IntPatch_ImpImpIntersection.hxx>
52 #include <IntTools_Context.hxx>
53 #include <Standard_Type.hxx>
55 #include <TopoDS_Face.hxx>
57 #include <gp_Sphere.hxx>
58 #include <gp_Cylinder.hxx>
59 #include <gp_Cone.hxx>
60 #include <gp_Torus.hxx>
63 GeomAPI_Face::GeomAPI_Face()
68 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
70 if (!theShape->isNull() && theShape->isFace()) {
71 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
75 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
80 if (!theFace->isFace())
83 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
84 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
86 TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
87 TopoDS_Face aInFace = TopoDS::Face(aInShape);
89 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
90 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
92 // Check that surfaces a the same type
93 if (aMySurf->DynamicType() != aInSurf->DynamicType())
96 // Get parameters of surfaces
97 double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
98 aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
99 double aInUMin, aInUMax, aInVMin, aInVMax;
100 aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
102 // Check that parameters are the same
103 if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
104 fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
105 fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
106 fabs(aMyVMax - aInVMax) > Precision::PConfusion())
109 Handle(IntTools_Context) aContext = new IntTools_Context();
110 // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
111 Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
112 && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
114 return aRes == Standard_True;
117 static Handle(Geom_Surface) baseSurface(const TopoDS_Face& theFace)
119 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
120 while (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
121 Handle(Geom_RectangularTrimmedSurface) rts =
122 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
123 aSurf = rts->BasisSurface();
128 bool GeomAPI_Face::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
130 if (!theShape->isFace())
132 if (isSame(theShape))
135 GeomFacePtr anOther = theShape->face();
136 if (isPlanar() && anOther->isPlanar()) {
137 GeomPlanePtr anOwnPlane = getPlane();
138 GeomPlanePtr anOtherPlane = anOther->getPlane();
139 return anOwnPlane->isCoincident(anOtherPlane);
142 TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
143 TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
145 Handle(Geom_Surface) anOwnSurf = baseSurface(anOwnFace);
146 Handle(Geom_Surface) anOtherSurf = baseSurface(anOtherFace);
148 // case of two elementary surfaces
149 if (anOwnSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
150 anOtherSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
152 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(anOwnSurf);
153 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(anOtherSurf);
155 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
156 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
159 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2,
160 Precision::Confusion(),
161 Precision::Confusion());
162 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
165 return anIIInt.TangentFaces();
167 catch (Standard_Failure const&) {
172 // case of two cylindrical surfaces, at least one of which is a swept surface
173 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
174 if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
175 anOwnSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
176 (anOtherSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
177 anOtherSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
179 GeomCylinderPtr anOwnCyl = getCylinder();
180 GeomCylinderPtr anOtherCyl = anOther->getCylinder();
181 if (anOwnCyl && anOtherCyl)
182 return anOwnCyl->isCoincident(anOtherCyl);
184 // compare two swept surfaces of the same type
185 if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) &&
186 anOtherSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) ||
187 (anOwnSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)) &&
188 anOtherSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)))) {
189 Handle(Geom_SweptSurface) anOwnSwept = Handle(Geom_SweptSurface)::DownCast(anOwnSurf);
190 Handle(Geom_SweptSurface) anOtherSwept = Handle(Geom_SweptSurface)::DownCast(anOtherSurf);
192 const gp_Dir& anOwnDir = anOwnSwept->Direction();
193 const gp_Dir& anOtherDir = anOtherSwept->Direction();
195 if (anOwnDir.IsParallel(anOtherDir, Precision::Angular())) {
196 Handle(Geom_Curve) anOwnCurve = anOwnSwept->BasisCurve();
197 Handle(Geom_Curve) anOtherCurve = anOtherSwept->BasisCurve();
198 GeomAPI_ExtremaCurveCurve anExtrema(anOwnCurve, anOtherCurve);
199 return anExtrema.Extrema().IsParallel() &&
200 anExtrema.TotalLowerDistance() < Precision::Confusion();
208 bool GeomAPI_Face::isCylindrical() const
210 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
211 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
212 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
213 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
214 if (!aTrimmed.IsNull())
215 aSurf = aTrimmed->BasisSurface();
216 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
219 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
221 std::shared_ptr<GeomAPI_Pln> aResult;
222 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
224 return aResult; // null shape
225 if (aShape.ShapeType() != TopAbs_FACE)
226 return aResult; // not face
227 TopoDS_Face aFace = TopoDS::Face(aShape);
229 return aResult; // not face
230 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
232 return aResult; // no surface
233 GeomLib_IsPlanarSurface isPlanarSurf(aSurf);
235 bool isPlanar = false;
236 if (isPlanarSurf.IsPlanar()) {
237 aPln = isPlanarSurf.Plan();
240 else if (aSurf->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
241 Handle(Geom_OffsetSurface) anOffsetSurf = Handle(Geom_OffsetSurface)::DownCast(aSurf);
242 Handle(Geom_Surface) aBasisSurf = anOffsetSurf->BasisSurface();
243 if (aBasisSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
244 aPln = Handle(Geom_Plane)::DownCast(aBasisSurf)->Pln();
245 gp_Vec aTranslation(aPln.Axis().Direction().XYZ() * anOffsetSurf->Offset());
246 aPln.Translate(aTranslation);
252 double aA, aB, aC, aD;
253 aPln.Coefficients(aA, aB, aC, aD);
254 if (aFace.Orientation() == TopAbs_REVERSED) {
260 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
265 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
267 GeomSpherePtr aSphere;
269 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
270 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
271 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
272 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
273 const gp_Pnt& aCenter = aSph.Location();
274 double aRadius = aSph.Radius();
276 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
277 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
282 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
284 GeomCylinderPtr aCylinder;
286 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
287 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
288 if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
289 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
290 gp_Pnt aLoc = aCyl.Location();
291 const gp_Dir& aDir = aCyl.Position().Direction();
292 double aRadius = aCyl.Radius();
294 double aUMin, aUMax, aVMin, aVMax;
295 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
296 double aHeight = aVMax - aVMin;
298 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
299 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
300 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
301 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
306 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
310 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
311 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
312 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
313 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
314 gp_Pnt aLoc = aCon.Location();
315 gp_Dir aDir = aCon.Position().Direction();
317 double aUMin, aUMax, aVMin, aVMax;
318 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
320 double aSemiAngle = Abs(aCon.SemiAngle());
321 double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
322 double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
324 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
326 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
327 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
328 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
333 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
337 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
338 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
339 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
340 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
341 const gp_Pnt& aLoc = aTor.Location();
342 const gp_Dir& aDir = aTor.Position().Direction();
343 double aMajorRadius = aTor.MajorRadius();
344 double aMinorRadius = aTor.MinorRadius();
346 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
347 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
348 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
353 GeomPointPtr GeomAPI_Face::middlePoint() const
355 GeomPointPtr anInnerPoint;
357 const TopoDS_Face& aFace = impl<TopoDS_Face>();
361 BRepGProp_Face aProp(aFace);
362 double aUMin, aUMax, aVMin, aVMax;
363 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
365 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
369 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
370 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));