1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAPI_Face.h"
23 #include "GeomAPI_Dir.h"
24 #include "GeomAPI_Pln.h"
25 #include "GeomAPI_Pnt.h"
26 #include "GeomAPI_Sphere.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 <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_ToroidalSurface.hxx>
42 #include <GeomLib_IsPlanarSurface.hxx>
43 #include <IntTools_Context.hxx>
44 #include <Standard_Type.hxx>
46 #include <TopoDS_Face.hxx>
48 #include <gp_Sphere.hxx>
49 #include <gp_Cylinder.hxx>
50 #include <gp_Cone.hxx>
51 #include <gp_Torus.hxx>
54 GeomAPI_Face::GeomAPI_Face()
59 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
61 if (!theShape->isNull() && theShape->isFace()) {
62 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
66 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
71 if (!theFace->isFace())
74 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
75 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
77 TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
78 TopoDS_Face aInFace = TopoDS::Face(aInShape);
80 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
81 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
83 // Check that surfaces a the same type
84 if (aMySurf->DynamicType() != aInSurf->DynamicType())
87 // Get parameters of surfaces
88 double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
89 aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
90 double aInUMin, aInUMax, aInVMin, aInVMax;
91 aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
93 // Check that parameters are the same
94 if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
95 fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
96 fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
97 fabs(aMyVMax - aInVMax) > Precision::PConfusion())
100 Handle(IntTools_Context) aContext = new IntTools_Context();
101 // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
102 Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
103 && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
105 return aRes == Standard_True;
108 bool GeomAPI_Face::isCylindrical() const
110 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
111 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
112 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
113 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
114 if (!aTrimmed.IsNull())
115 aSurf = aTrimmed->BasisSurface();
116 return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
119 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
121 std::shared_ptr<GeomAPI_Pln> aResult;
122 TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
124 return aResult; // null shape
125 if (aShape.ShapeType() != TopAbs_FACE)
126 return aResult; // not face
127 TopoDS_Face aFace = TopoDS::Face(aShape);
129 return aResult; // not face
130 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
132 return aResult; // no surface
133 GeomLib_IsPlanarSurface isPlanar(aSurf);
134 if(!isPlanar.IsPlanar()) {
137 gp_Pln aPln = isPlanar.Plan();
138 double aA, aB, aC, aD;
139 aPln.Coefficients(aA, aB, aC, aD);
140 if (aFace.Orientation() == TopAbs_REVERSED) {
146 aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
150 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
152 GeomSpherePtr aSphere;
154 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
155 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
156 if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
157 gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
158 const gp_Pnt& aCenter = aSph.Location();
159 double aRadius = aSph.Radius();
161 GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
162 aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
167 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
169 GeomCylinderPtr aCylinder;
171 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
172 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
173 if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
174 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
175 gp_Pnt aLoc = aCyl.Location();
176 const gp_Dir& aDir = aCyl.Position().Direction();
177 double aRadius = aCyl.Radius();
179 double aUMin, aUMax, aVMin, aVMax;
180 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
181 double aHeight = aVMax - aVMin;
183 aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
184 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
185 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
186 aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
191 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
195 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
196 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
197 if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
198 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
199 gp_Pnt aLoc = aCon.Location();
200 gp_Dir aDir = aCon.Position().Direction();
202 double aUMin, aUMax, aVMin, aVMax;
203 BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
205 double aSemiAngle = Abs(aCon.SemiAngle());
206 double aRadius1 = aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle());
207 double aRadius2 = aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle());
209 aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
211 GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
212 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
213 aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
218 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
222 const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
223 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
224 if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
225 gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
226 const gp_Pnt& aLoc = aTor.Location();
227 const gp_Dir& aDir = aTor.Position().Direction();
228 double aMajorRadius = aTor.MajorRadius();
229 double aMinorRadius = aTor.MinorRadius();
231 GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
232 GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
233 aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
238 GeomPointPtr GeomAPI_Face::middlePoint() const
240 GeomPointPtr anInnerPoint;
242 const TopoDS_Face& aFace = impl<TopoDS_Face>();
246 BRepGProp_Face aProp(aFace);
247 double aUMin, aUMax, aVMin, aVMax;
248 aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
250 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
254 gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
255 anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));