Salome HOME
[bos #29947] [CEA 29944] SIGSEGV using filet
[modules/shaper.git] / src / GeomAPI / GeomAPI_Wire.cpp
index 6932444bf75af2be55d57c4d5afe616e47caa31b..15a9e38ff5ac189264562e33384cbf4794574a09 100644 (file)
-// File:        GeomAPI_Wire.cpp
-// Created:     06 Oct 2014
-// Author:      Sergey BELASH
+// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
-#include <GeomAPI_Interface.h>
-#include <GeomAPI_Wire.h>
+#include "GeomAPI_Wire.h"
+#include "GeomAPI_Pnt.h"
 
-#include <Standard_TypeDef.hxx>
-#include <TopAbs_ShapeEnum.hxx>
-
-#include <TopoDS.hxx>
-#include <TopoDS_Edge.hxx>
-#include <TopoDS_Wire.hxx>
 #include <BRep_Builder.hxx>
 #include <BRepTools_WireExplorer.hxx>
+#include <Geom_Line.hxx>
+#include <Geom_TrimmedCurve.hxx>
+#include <Precision.hxx>
+#include <Standard_Type.hxx>
+#include <TopoDS.hxx>
+#include <TopoDS_Wire.hxx>
 
-#include <list>
-
-GeomAPI_Wire::GeomAPI_Wire() : GeomAPI_Shape()
+//==================================================================================================
+GeomAPI_Wire::GeomAPI_Wire()
 {
-  TopoDS_Compound aBigWireImpl;
+  TopoDS_Wire* aWire = new TopoDS_Wire();
+
   BRep_Builder aBuilder;
-  aBuilder.MakeCompound(aBigWireImpl);
-  this->setImpl(new TopoDS_Shape(aBigWireImpl));
+  aBuilder.MakeWire(*aWire);
+
+  this->setImpl(aWire);
 }
 
-void GeomAPI_Wire::addEdge(boost::shared_ptr<GeomAPI_Shape> theEdge)
+//==================================================================================================
+GeomAPI_Wire::GeomAPI_Wire(const std::shared_ptr<GeomAPI_Shape>& theShape)
 {
-  const TopoDS_Edge& anEdge = theEdge->impl<TopoDS_Edge>();
-  if (anEdge.ShapeType() != TopAbs_EDGE)
-    return;
-  TopoDS_Shape& aWire = const_cast<TopoDS_Shape&>(impl<TopoDS_Shape>());
-  BRep_Builder aBuilder;
-  aBuilder.Add(aWire, anEdge);
+  if (!theShape->isNull() && theShape->isWire()) {
+    setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
+  }
+}
+
+//==================================================================================================
+bool GeomAPI_Wire::isClosed() const
+{
+  return BRep_Tool::IsClosed(impl<TopoDS_Shape>());
 }
 
-std::list<boost::shared_ptr<GeomAPI_Shape> > GeomAPI_Wire::getEdges()
+//==================================================================================================
+bool GeomAPI_Wire::isPolygon(std::list<GeomPointPtr>& thePoints) const
 {
-  TopoDS_Shape& aShape = const_cast<TopoDS_Shape&>(impl<TopoDS_Shape>());
-  BRepTools_WireExplorer aWireExp(TopoDS::Wire(aShape));
-  std::list<boost::shared_ptr<GeomAPI_Shape> > aResult;
-  for (; aWireExp.More(); aWireExp.Next()) {
-    boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
-    anEdge->setImpl(new TopoDS_Shape(aWireExp.Current()));
-    aResult.push_back(anEdge);
+  const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
+
+  bool isPolygon = true;
+  const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
+  for (BRepTools_WireExplorer anExp(aWire); anExp.More() && isPolygon; anExp.Next()) {
+    const TopoDS_Edge& anEdge = anExp.Current();
+    double aT1, aT2;
+    Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
+    if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
+      gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
+      thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
+    }
+    else
+      isPolygon = false;
   }
-  return aResult;
+
+  if (!isPolygon)
+    thePoints.clear();
+  return isPolygon;
+}
+
+//==================================================================================================
+bool GeomAPI_Wire::isRectangle(std::list<GeomPointPtr>& thePoints) const
+{
+  thePoints.clear();
+
+  const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
+  const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
+  const Handle(Standard_Type)& aTrimmedCurveType = STANDARD_TYPE(Geom_TrimmedCurve);
+
+  gp_XYZ aPrevDir(0, 0, 0);
+
+  for (BRepTools_WireExplorer anExp(aWire); anExp.More(); anExp.Next()) {
+    const TopoDS_Edge& anEdge = anExp.Current();
+    double aT1, aT2;
+    Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
+    if (aC3D.IsNull())
+      continue;
+    while (aC3D->IsKind(aTrimmedCurveType))
+      aC3D = Handle(Geom_TrimmedCurve)::DownCast(aC3D)->BasisCurve();
+    if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
+      gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
+      thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
+    }
+    else
+      return false;
+
+    if (thePoints.size() > 4)
+      return false;
+
+    // collect length of the edge
+    gp_Pnt aStart = aC3D->Value(aT1);
+    gp_Pnt aEnd = aC3D->Value(aT2);
+
+    if (aStart.Distance(aEnd) <= Precision::Confusion())
+      return false;
+
+    // check the edge is orthogonal to the previous
+    gp_XYZ aCurDir = (aEnd.XYZ() - aStart.XYZ()).Normalized();
+    if (aPrevDir.Dot(aCurDir) < Precision::Confusion())
+      aPrevDir = aCurDir;
+    else
+      return false;
+  }
+  return thePoints.size() == 4;
+}
+
+//==================================================================================================
+GeomPointPtr GeomAPI_Wire::middlePoint() const
+{
+  // find middle edge in the wire
+  std::list<GeomShapePtr> aSubs = subShapes(EDGE);
+  size_t aNbSubs = aSubs.size();
+  if (aNbSubs == 0)
+    return GeomPointPtr();
+
+  aNbSubs /= 2;
+  for (; aNbSubs > 0; --aNbSubs)
+    aSubs.pop_front();
+
+  // compute middle point on the middle edge
+  return aSubs.front()->middlePoint();
 }