Salome HOME
Task 2.3: Creation of Intersection
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
index c336050f9c08bdd5f35cf56cd3e08e8a090303da..8c219972d3c459f931cd1262f5393debaf2a5505 100644 (file)
@@ -24,6 +24,8 @@
 #include<GeomAPI_Circ.h>
 #include<GeomAPI_Dir.h>
 #include<GeomAPI_Lin.h>
+#include<GeomAPI_Ax2.h>
+#include<GeomAPI_Ellipse.h>
 
 #include <BRepAdaptor_Curve.hxx>
 
 #include <Geom_Curve.hxx>
 #include <Geom_Line.hxx>
 #include <Geom_Circle.hxx>
+#include <Geom_Ellipse.hxx>
+#include <Geom_Plane.hxx>
+#include <GeomAPI_IntCS.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <gp_Ax1.hxx>
 #include <gp_Pln.hxx>
+#include <gp_Elips.hxx>
+#include <TopExp.hxx>
 
 #include <GCPnts_AbscissaPoint.hxx>
 
@@ -63,6 +70,8 @@ bool GeomAPI_Edge::isLine() const
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
   double aFirst, aLast;
   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve.IsNull()) // degenerative edge
+    return false;
   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
     return true;
   return false;
@@ -73,6 +82,8 @@ bool GeomAPI_Edge::isCircle() const
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
   double aFirst, aLast;
   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve.IsNull()) // degenerative edge
+    return false;
   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
   {
     // Check the difference of first and last parameters to be equal to the curve period
@@ -87,6 +98,8 @@ bool GeomAPI_Edge::isArc() const
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
   double aFirst, aLast;
   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve.IsNull()) // degenerative edge
+    return false;
   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
   {
     // Check the difference of first and last parameters is not equal the curve period
@@ -96,6 +109,18 @@ bool GeomAPI_Edge::isArc() const
   return false;
 }
 
+bool GeomAPI_Edge::isEllipse() const
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve.IsNull()) // degenerative edge
+    return false;
+  if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
+    return true;
+  return false;
+}
+
 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
 {
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
@@ -116,14 +141,14 @@ std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
 }
 
-std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
+std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
 {
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
   double aFirst, aLast;
   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
-  if (aCurve) {
+  if (!aCurve.IsNull()) {
     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
-    if (aCirc) {
+    if (!aCirc.IsNull()) {
       gp_Pnt aLoc = aCirc->Location();
       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
       gp_Dir anAxis = aCirc->Axis().Direction();
@@ -134,7 +159,24 @@ std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
   return std::shared_ptr<GeomAPI_Circ>(); // not circle
 }
 
-std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
+std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (!aCurve.IsNull()) {
+    Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
+    if (!aElips.IsNull()) {
+      gp_Elips aGpElips = aElips->Elips();
+      std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
+      aEllipse->setImpl(new gp_Elips(aGpElips));
+      return aEllipse;
+    }
+  }
+  return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
+}
+
+std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
 {
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
   double aFirst, aLast;
@@ -240,6 +282,33 @@ bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
   return inPlane;
 }
 
+std::list<std::shared_ptr<GeomAPI_Pnt>> GeomAPI_Edge::intersectWithPlane(
+  const std::shared_ptr<GeomAPI_Pln> thePlane) const
+{
+  std::list<GeomPointPtr> aResList;
+  double aFirst, aLast;
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (!aCurve.IsNull()) {
+    double A, B, C, D;
+    thePlane->coefficients(A, B, C, D);
+    gp_Pln aPln(A, B, C, D);
+
+    Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
+    GeomAPI_IntCS aIntersect;
+    aIntersect.Perform(aCurve, aPlane);
+    if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
+      gp_Pnt aPnt;
+      for (int i = 1; i <= aIntersect.NbPoints(); i++) {
+        aPnt = aIntersect.Point(i);
+        std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
+        aResList.push_back(aPntPtr);
+      }
+    }
+  }
+  return aResList;
+}
+
 double GeomAPI_Edge::length() const
 {
   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
@@ -247,3 +316,41 @@ double GeomAPI_Edge::length() const
   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
 }
+
+bool GeomAPI_Edge::isClosed() const
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  if (aShape.IsNull())
+    return false;
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve.IsNull() || !aCurve->IsPeriodic())
+    return false;
+  aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
+
+  return fabs(aFirst - aLast) < 1.e-9;
+}
+
+bool GeomAPI_Edge::isDegenerated() const
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
+    return false;
+  return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
+}
+
+void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
+{
+  TopoDS_Edge anEdge = impl<TopoDS_Edge>();
+  TopoDS_Vertex aVFirst, aVLast;
+  TopExp::Vertices(anEdge, aVFirst, aVLast);
+  BRep_Builder().UpdateVertex(aVFirst, theTolerance);
+}
+
+void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
+{
+  TopoDS_Edge anEdge = impl<TopoDS_Edge>();
+  TopoDS_Vertex aVFirst, aVLast;
+  TopExp::Vertices(anEdge, aVFirst, aVLast);
+  BRep_Builder().UpdateVertex(aVLast, theTolerance);
+}