Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
index b9dbd45df2f45bf8127e0b0d8f18a20ff43d9dfa..e6453787e9ba54ee047f995dd6d7e06f86125b87 100644 (file)
@@ -4,19 +4,22 @@
 // Created:     2 Dec 2014
 // Author:      Artem ZHIDKOV
 
-#include <GeomAPI_Face.h>
-#include <GeomAPI_Dir.h>
-#include <GeomAPI_Pln.h>
-#include <GeomAPI_Pnt.h>
+#include "GeomAPI_Face.h"
 
-#include <TopoDS_Shape.hxx>
-#include <TopoDS_Face.hxx>
-#include <TopoDS.hxx>
+#include "GeomAPI_Dir.h"
+#include "GeomAPI_Pln.h"
+#include "GeomAPI_Pnt.h"
+
+#include <BOPTools_AlgoTools.hxx>
 #include <BRep_Tool.hxx>
 #include <BRepAdaptor_Surface.hxx>
 #include <Geom_Surface.hxx>
-#include <Geom_Plane.hxx>
+#include <Geom_CylindricalSurface.hxx>
+#include <Geom_RectangularTrimmedSurface.hxx>
 #include <GeomLib_IsPlanarSurface.hxx>
+#include <IntTools_Context.hxx>
+#include <TopoDS.hxx>
+#include <TopoDS_Face.hxx>
 
 GeomAPI_Face::GeomAPI_Face()
   : GeomAPI_Shape()
@@ -32,14 +35,20 @@ GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
 
 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
 {
+  if (!theFace.get())
+    return false;
+
   if (!theFace->isFace())
     return false;
 
   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
 
-  Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(TopoDS::Face(aMyShape));
-  Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(TopoDS::Face(aInShape));
+  TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
+  TopoDS_Face aInFace = TopoDS::Face(aInShape);
+
+  Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
+  Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
 
   // Check that surfaces a the same type
   if (aMySurf->DynamicType() != aInSurf->DynamicType())
@@ -58,44 +67,50 @@ bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
     return false;
 
-  return true;
+  Handle(IntTools_Context) aContext = new IntTools_Context();
+  Standard_Boolean aRes = BOPTools_AlgoTools::CheckSameGeom(aMyFace, aInFace, aContext);
+
+  return aRes == Standard_True;
 }
 
-bool GeomAPI_Face::isPlanar() const
+bool GeomAPI_Face::isCylindrical() const
 {
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
-  GeomLib_IsPlanarSurface isPlanar(aSurf);
-  return isPlanar.IsPlanar();
+  Handle(Geom_RectangularTrimmedSurface) aTrimmed =
+    Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
+  if (!aTrimmed.IsNull())
+    aSurf = aTrimmed->BasisSurface();
+  return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
 }
 
 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
 {
-  const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
-  BRepAdaptor_Surface aSurfAdapt(TopoDS::Face(aShape));
-
-  if (aSurfAdapt.GetType() != GeomAbs_Plane)
-    return std::shared_ptr<GeomAPI_Pln>();
-
-  // Obtain central point
-  double aUMin, aUMax, aVMin, aVMax;
-  aUMin = aSurfAdapt.FirstUParameter();
-  aUMax = aSurfAdapt.LastUParameter();
-  aVMin = aSurfAdapt.FirstVParameter();
-  aVMax = aSurfAdapt.LastVParameter();
-  gp_Pnt aCentralPnt;
-  gp_Vec aDU, aDV;
-  aSurfAdapt.D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
-  std::shared_ptr<GeomAPI_Pnt> aCenter(
-      new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
-
-  // Obtain plane direction
-  gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
-  if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
-    return std::shared_ptr<GeomAPI_Pln>();
-  std::shared_ptr<GeomAPI_Dir> aNormal(
-      new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
-
-  std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));
+  std::shared_ptr<GeomAPI_Pln> aResult;
+  TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
+  if (aShape.IsNull())
+    return aResult;  // null shape
+  if (aShape.ShapeType() != TopAbs_FACE)
+    return aResult;  // not face
+  TopoDS_Face aFace = TopoDS::Face(aShape);
+  if (aFace.IsNull())
+    return aResult;  // not face
+  Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
+  if (aSurf.IsNull())
+    return aResult;  // no surface
+  GeomLib_IsPlanarSurface isPlanar(aSurf);
+  if(!isPlanar.IsPlanar()) {
+    return aResult;
+  }
+  gp_Pln aPln = isPlanar.Plan();
+  double aA, aB, aC, aD;
+  aPln.Coefficients(aA, aB, aC, aD);
+  if (aFace.Orientation() == TopAbs_REVERSED) {
+    aA = -aA;
+    aB = -aB;
+    aC = -aC;
+    aD = -aD;
+  }
+  aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
   return aResult;
 }