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 <BRepTopAdaptor_TopolTool.hxx>
36 #include <Geom_Surface.hxx>
37 #include <Geom_SphericalSurface.hxx>
38 #include <Geom_ConicalSurface.hxx>
39 #include <Geom_CylindricalSurface.hxx>
40 #include <Geom_OffsetSurface.hxx>
41 #include <Geom_Plane.hxx>
42 #include <Geom_RectangularTrimmedSurface.hxx>
43 #include <Geom_SweptSurface.hxx>
44 #include <Geom_ToroidalSurface.hxx>
45 #include <GeomAdaptor_HSurface.hxx>
46 #include <GeomLib_IsPlanarSurface.hxx>
47 #include <IntPatch_ImpImpIntersection.hxx>
48 #include <IntTools_Context.hxx>
49 #include <Standard_Type.hxx>
51 #include <TopoDS_Face.hxx>
53 #include <gp_Sphere.hxx>
54 #include <gp_Cylinder.hxx>
55 #include <gp_Cone.hxx>
56 #include <gp_Torus.hxx>
59 GeomAPI_Face::GeomAPI_Face()
64 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
66 if (!theShape->isNull() && theShape->isFace()) {
67 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
71 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
76 if (!theFace->isFace())
79 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
80 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
82 TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
83 TopoDS_Face aInFace = TopoDS::Face(aInShape);
85 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
86 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
88 // Check that surfaces a the same type
89 if (aMySurf->DynamicType() != aInSurf->DynamicType())
92 // Get parameters of surfaces
93 double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
94 aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
95 double aInUMin, aInUMax, aInVMin, aInVMax;
96 aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
98 // Check that parameters are the same
99 if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
100 fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
101 fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
102 fabs(aMyVMax - aInVMax) > Precision::PConfusion())
105 Handle(IntTools_Context) aContext = new IntTools_Context();
106 // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
107 Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
108 && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
110 return aRes == Standard_True;
113 static Handle(Geom_Surface) baseSurface(const TopoDS_Face& theFace)
115 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
116 while (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
117 Handle(Geom_RectangularTrimmedSurface) rts =
118 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
119 aSurf = rts->BasisSurface();
124 bool GeomAPI_Face::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
126 if (!theShape->isFace())
128 if (isSame(theShape))
131 GeomFacePtr anOther = theShape->face();
132 if (isPlanar() && anOther->isPlanar()) {
133 GeomPlanePtr anOwnPlane = getPlane();
134 GeomPlanePtr anOtherPlane = anOther->getPlane();
135 return anOwnPlane->isCoincident(anOtherPlane);
138 TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
139 TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
141 Handle(Geom_Surface) anOwnSurf = baseSurface(anOwnFace);
142 Handle(Geom_Surface) anOtherSurf = baseSurface(anOtherFace);
144 // case of two elementary surfaces
145 if (anOwnSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
146 anOtherSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
148 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(anOwnSurf);
149 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(anOtherSurf);
151 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
152 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
155 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2,
156 Precision::Confusion(),
157 Precision::Confusion());
158 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
161 return anIIInt.TangentFaces();
163 catch (Standard_Failure const&) {
168 // case of two cylindrical surfaces, at least one of which is a swept surface
169 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
170 if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
171 anOwnSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
172 (anOtherSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
173 anOtherSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
175 GeomCylinderPtr anOwnCyl = getCylinder();
176 GeomCylinderPtr anOtherCyl = anOther->getCylinder();
177 return anOwnCyl && anOtherCyl && anOwnCyl->isCoincident(anOtherCyl);
183 bool GeomAPI_Face::isCylindrical() const
185 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
186 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
187 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
188 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
189 if (!aTrimmed.IsNull())
190 aSurf = aTrimmed->BasisSurface();
191 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
194 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
196 std::shared_ptr<GeomAPI_Pln> aResult;
197 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
199 return aResult; // null shape
200 if (aShape.ShapeType() != TopAbs_FACE)
201 return aResult; // not face
202 TopoDS_Face aFace = TopoDS::Face(aShape);
204 return aResult; // not face
205 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
207 return aResult; // no surface
208 GeomLib_IsPlanarSurface isPlanarSurf(aSurf);
210 bool isPlanar = false;
211 if (isPlanarSurf.IsPlanar()) {
212 aPln = isPlanarSurf.Plan();
215 else if (aSurf->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
216 Handle(Geom_OffsetSurface) anOffsetSurf = Handle(Geom_OffsetSurface)::DownCast(aSurf);
217 Handle(Geom_Surface) aBasisSurf = anOffsetSurf->BasisSurface();
218 if (aBasisSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
219 aPln = Handle(Geom_Plane)::DownCast(aBasisSurf)->Pln();
220 gp_Vec aTranslation(aPln.Axis().Direction().XYZ() * anOffsetSurf->Offset());
221 aPln.Translate(aTranslation);
227 double aA, aB, aC, aD;
228 aPln.Coefficients(aA, aB, aC, aD);
229 if (aFace.Orientation() == TopAbs_REVERSED) {
235 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
240 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
242 GeomSpherePtr aSphere;
244 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
245 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
246 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
247 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
248 const gp_Pnt& aCenter = aSph.Location();
249 double aRadius = aSph.Radius();
251 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
252 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
257 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
259 GeomCylinderPtr aCylinder;
261 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
262 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
263 if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
264 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
265 gp_Pnt aLoc = aCyl.Location();
266 const gp_Dir& aDir = aCyl.Position().Direction();
267 double aRadius = aCyl.Radius();
269 double aUMin, aUMax, aVMin, aVMax;
270 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
271 double aHeight = aVMax - aVMin;
273 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
274 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
275 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
276 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
281 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
285 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
286 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
287 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
288 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
289 gp_Pnt aLoc = aCon.Location();
290 gp_Dir aDir = aCon.Position().Direction();
292 double aUMin, aUMax, aVMin, aVMax;
293 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
295 double aSemiAngle = Abs(aCon.SemiAngle());
296 double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
297 double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
299 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
301 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
302 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
303 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
308 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
312 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
313 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
314 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
315 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
316 const gp_Pnt& aLoc = aTor.Location();
317 const gp_Dir& aDir = aTor.Position().Direction();
318 double aMajorRadius = aTor.MajorRadius();
319 double aMinorRadius = aTor.MinorRadius();
321 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
322 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
323 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
328 GeomPointPtr GeomAPI_Face::middlePoint() const
330 GeomPointPtr anInnerPoint;
332 const TopoDS_Face& aFace = impl<TopoDS_Face>();
336 BRepGProp_Face aProp(aFace);
337 double aUMin, aUMax, aVMin, aVMax;
338 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
340 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
344 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
345 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));