Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_EdgeWrapper.cpp
index 7ec36cf236a33507de548cfad7bde4f137c137a5..b6c408bb0bba10df6b30c72050ea16e246f00061 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  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
 #include <PlaneGCSSolver_EdgeWrapper.h>
 #include <cmath>
 
+template <typename TYPE>
+static bool isCurve(const GCSCurvePtr& theEntity)
+{
+  return std::dynamic_pointer_cast<TYPE>(theEntity).get();
+}
+
+
 PlaneGCSSolver_EdgeWrapper::PlaneGCSSolver_EdgeWrapper(const GCSCurvePtr theEntity)
   : myEntity(theEntity)
 {
-  std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(myEntity);
-  if (aLine) myType = ENTITY_LINE;
-  else {
-    std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(myEntity);
-    if (anArc) myType = ENTITY_ARC;
-    else {
-      std::shared_ptr<GCS::Circle> aCircle = std::dynamic_pointer_cast<GCS::Circle>(myEntity);
-      if (aCircle) myType = ENTITY_CIRCLE;
-    }
-  }
+  if (isCurve<GCS::Line>(myEntity))
+    myType = ENTITY_LINE;
+  else if (isCurve<GCS::Arc>(myEntity))
+    myType = ENTITY_ARC;
+  else if (isCurve<GCS::Circle>(myEntity))
+    myType = ENTITY_CIRCLE;
+  else if (isCurve<GCS::ArcOfEllipse>(myEntity))
+    myType = ENTITY_ELLIPTIC_ARC;
+  else if (isCurve<GCS::Ellipse>(myEntity))
+    myType = ENTITY_ELLIPSE;
+  else if (isCurve<GCS::BSpline>(myEntity))
+    myType = ENTITY_BSPLINE;
 }
 
 static double squareDistance(const GCS::Point& theP1, const GCS::Point& theP2)
@@ -44,23 +53,36 @@ static double squareDistance(const GCS::Point& theP1, const GCS::Point& theP2)
 
 bool PlaneGCSSolver_EdgeWrapper::isDegenerated() const
 {
-  static const double aSqTol = tolerance * 1e-2;
-  static const double aMaxRadius = 1e8;
+  static const double THE_SQ_TOL = tolerance * 1e-2;
+  static const double THE_ANGLE_TOL = 1.e-5;
+  static const double THE_MAX_RADIUS = 1e8;
+  static const double THE_SQ_MAX_RADIUS = THE_MAX_RADIUS * THE_MAX_RADIUS;
+
   if (myType == ENTITY_LINE) {
     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(myEntity);
-    return squareDistance(aLine->p1, aLine->p2) < aSqTol;
+    return squareDistance(aLine->p1, aLine->p2) < THE_SQ_TOL;
   }
   else if (myType == ENTITY_CIRCLE) {
     std::shared_ptr<GCS::Circle> aCircle = std::dynamic_pointer_cast<GCS::Circle>(myEntity);
-    return *aCircle->rad < tolerance || *aCircle->rad > aMaxRadius;
+    return *aCircle->rad < tolerance || *aCircle->rad > THE_MAX_RADIUS;
   }
   else if (myType == ENTITY_ARC) {
-    static const double anAngleTol = 1.e-5;
     std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(myEntity);
     double anAngleDiff = fabs(*anArc->startAngle - *anArc->endAngle);
     double aSqRadius = squareDistance(anArc->center, anArc->start);
-    return aSqRadius < aSqTol || aSqRadius > aMaxRadius * aMaxRadius || // <- arc radius
-           anAngleDiff < anAngleTol || fabs(anAngleDiff - 2*PI) < anAngleTol; // <- arc angle
+    return aSqRadius < THE_SQ_TOL || aSqRadius > THE_SQ_MAX_RADIUS || // <- arc radius
+           anAngleDiff < THE_ANGLE_TOL || fabs(anAngleDiff - 2*PI) < THE_ANGLE_TOL; // <- arc angle
+  }
+  else if (myType == ENTITY_ELLIPSE) {
+    std::shared_ptr<GCS::Ellipse> anEllipse = std::dynamic_pointer_cast<GCS::Ellipse>(myEntity);
+    return *anEllipse->radmin < tolerance || anEllipse->getRadMaj() > THE_MAX_RADIUS;
+  }
+  else if (myType == ENTITY_ELLIPTIC_ARC) {
+    std::shared_ptr<GCS::ArcOfEllipse> anArc =
+        std::dynamic_pointer_cast<GCS::ArcOfEllipse>(myEntity);
+    double anAngleDiff = fabs(*anArc->startAngle - *anArc->endAngle);
+    return *anArc->radmin < THE_SQ_TOL || anArc->getRadMaj() > THE_SQ_MAX_RADIUS || // <- arc radius
+           anAngleDiff < THE_ANGLE_TOL || fabs(anAngleDiff - 2*PI) < THE_ANGLE_TOL; // <- arc angle
   }
   return false;
 }