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_RectangularTrimmedSurface.hxx>
41 #include <Geom_SweptSurface.hxx>
42 #include <Geom_ToroidalSurface.hxx>
43 #include <GeomAdaptor_HSurface.hxx>
44 #include <GeomLib_IsPlanarSurface.hxx>
45 #include <IntPatch_ImpImpIntersection.hxx>
46 #include <IntTools_Context.hxx>
47 #include <Standard_Type.hxx>
49 #include <TopoDS_Face.hxx>
51 #include <gp_Sphere.hxx>
52 #include <gp_Cylinder.hxx>
53 #include <gp_Cone.hxx>
54 #include <gp_Torus.hxx>
57 GeomAPI_Face::GeomAPI_Face()
62 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
64 if (!theShape->isNull() && theShape->isFace()) {
65 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
69 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
74 if (!theFace->isFace())
77 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
78 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
80 TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
81 TopoDS_Face aInFace = TopoDS::Face(aInShape);
83 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
84 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
86 // Check that surfaces a the same type
87 if (aMySurf->DynamicType() != aInSurf->DynamicType())
90 // Get parameters of surfaces
91 double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
92 aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
93 double aInUMin, aInUMax, aInVMin, aInVMax;
94 aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
96 // Check that parameters are the same
97 if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
98 fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
99 fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
100 fabs(aMyVMax - aInVMax) > Precision::PConfusion())
103 Handle(IntTools_Context) aContext = new IntTools_Context();
104 // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
105 Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
106 && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
108 return aRes == Standard_True;
111 static Handle(Geom_Surface) baseSurface(const TopoDS_Face& theFace)
113 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
114 while (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
115 Handle(Geom_RectangularTrimmedSurface) rts =
116 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
117 aSurf = rts->BasisSurface();
122 bool GeomAPI_Face::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
124 if (!theShape->isFace())
126 if (isSame(theShape))
129 GeomFacePtr anOther = theShape->face();
130 if (isPlanar() && anOther->isPlanar()) {
131 GeomPlanePtr anOwnPlane = getPlane();
132 GeomPlanePtr anOtherPlane = anOther->getPlane();
133 return anOwnPlane->isCoincident(anOtherPlane);
136 TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
137 TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
139 Handle(Geom_Surface) anOwnSurf = baseSurface(anOwnFace);
140 Handle(Geom_Surface) anOtherSurf = baseSurface(anOtherFace);
142 // case of two elementary surfaces
143 if (anOwnSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
144 anOtherSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
146 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(anOwnSurf);
147 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(anOtherSurf);
149 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
150 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
153 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2,
154 Precision::Confusion(),
155 Precision::Confusion());
156 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
159 return anIIInt.TangentFaces();
161 catch (Standard_Failure const&) {
166 // case of two cylindrical surfaces, at least one of which is a swept surface
167 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
168 if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
169 anOwnSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
170 (anOtherSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
171 anOtherSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
173 GeomCylinderPtr anOwnCyl = getCylinder();
174 GeomCylinderPtr anOtherCyl = anOther->getCylinder();
175 return anOwnCyl && anOtherCyl && anOwnCyl->isCoincident(anOtherCyl);
181 bool GeomAPI_Face::isCylindrical() const
183 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
184 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
185 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
186 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
187 if (!aTrimmed.IsNull())
188 aSurf = aTrimmed->BasisSurface();
189 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
192 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
194 std::shared_ptr<GeomAPI_Pln> aResult;
195 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
197 return aResult; // null shape
198 if (aShape.ShapeType() != TopAbs_FACE)
199 return aResult; // not face
200 TopoDS_Face aFace = TopoDS::Face(aShape);
202 return aResult; // not face
203 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
205 return aResult; // no surface
206 GeomLib_IsPlanarSurface isPlanar(aSurf);
207 if(!isPlanar.IsPlanar()) {
210 gp_Pln aPln = isPlanar.Plan();
211 double aA, aB, aC, aD;
212 aPln.Coefficients(aA, aB, aC, aD);
213 if (aFace.Orientation() == TopAbs_REVERSED) {
219 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
223 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
225 GeomSpherePtr aSphere;
227 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
228 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
229 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
230 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
231 const gp_Pnt& aCenter = aSph.Location();
232 double aRadius = aSph.Radius();
234 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
235 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
240 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
242 GeomCylinderPtr aCylinder;
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_CylindricalSurface))) {
247 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
248 gp_Pnt aLoc = aCyl.Location();
249 const gp_Dir& aDir = aCyl.Position().Direction();
250 double aRadius = aCyl.Radius();
252 double aUMin, aUMax, aVMin, aVMax;
253 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
254 double aHeight = aVMax - aVMin;
256 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
257 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
258 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
259 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
264 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
268 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
269 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
270 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
271 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
272 gp_Pnt aLoc = aCon.Location();
273 gp_Dir aDir = aCon.Position().Direction();
275 double aUMin, aUMax, aVMin, aVMax;
276 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
278 double aSemiAngle = Abs(aCon.SemiAngle());
279 double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
280 double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
282 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
284 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
285 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
286 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
291 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
295 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
296 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
297 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
298 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
299 const gp_Pnt& aLoc = aTor.Location();
300 const gp_Dir& aDir = aTor.Position().Direction();
301 double aMajorRadius = aTor.MajorRadius();
302 double aMinorRadius = aTor.MinorRadius();
304 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
305 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
306 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
311 GeomPointPtr GeomAPI_Face::middlePoint() const
313 GeomPointPtr anInnerPoint;
315 const TopoDS_Face& aFace = impl<TopoDS_Face>();
319 BRepGProp_Face aProp(aFace);
320 double aUMin, aUMax, aVMin, aVMax;
321 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
323 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
327 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
328 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));