Salome HOME
Improve constraints placement
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
index 219cc31cc7ddfa2a5fc6c97fb2726ecd8f8d5522..669d67745f0f78399c1f7ed6f5cf59a5047b8bc8 100644 (file)
@@ -27,6 +27,7 @@
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_Ax3.h>
 #include <GeomAPI_Circ.h>
+#include <GeomAPI_Lin2d.h>
 
 #include <GeomDataAPI_Point2D.h>
 
@@ -36,9 +37,9 @@
 #include <SketchPlugin_ConstraintTangent.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
 
-#include <BRepExtrema_ExtPC.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <Geom_Curve.hxx>
+#include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <TColGeom_SequenceOfCurve.hxx>
 #include <gp_Dir.hxx>
 
@@ -140,7 +141,8 @@ bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPoin
   return false;
 }
 
-int SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos, const SketcherPrs_SymbolPrs* thePrs)
+const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
+                                              const SketcherPrs_SymbolPrs* thePrs)
 {
   if (myPntShapes.count(thePrs->feature()) == 0) {
     // Renumerate positions around the specified constraint point for all constraints
@@ -150,12 +152,14 @@ int SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos, const Sketche
 
     int aNbSubs = aOwner->numberOfSubs();
     int aId = 0;
+    std::list<const ModelAPI_Feature*> aFeaList;
     for (int i = 0; i < aNbSubs; i++) {
       FeaturePtr aFeature = aOwner->subFeature(i);
 
       if (myPntShapes.count(aFeature.get()) == 1) {
-        myPntShapes[aFeature.get()] = aId;
+        myPntShapes[aFeature.get()][0] = aId;
         aId++;
+        aFeaList.push_back(aFeature.get());
       } else {
         if (isPntConstraint(aFeature->getKind())) {
           DataPtr aData = aFeature->data();
@@ -172,17 +176,23 @@ int SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos, const Sketche
             }
           }
           if (aContains) {
-            myPntShapes[aFeature.get()] = aId;
+            myPntShapes[aFeature.get()][0] = aId;
             aId++;
+            aFeaList.push_back(aFeature.get());
           }
         }
       }
     }
+    int aSize = (int) aFeaList.size();
+    std::list<const ModelAPI_Feature*>::const_iterator aIt;
+    for (aIt = aFeaList.cbegin(); aIt != aFeaList.cend(); aIt++) {
+      myPntShapes[*aIt][1] = aSize;
+    }
   }
   return myPntShapes[thePrs->feature()];
 }
 
-
+//*****************************************************************
 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
 {
   gp_Vec aVec;
@@ -192,22 +202,11 @@ gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
 
     if (aCurve->isCircle()) {
-      GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
-      GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
-      BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
-                                 aEdgePtr->impl<TopoDS_Edge>());
-      int aNb = aExtrema.NbExt();
-      if (aNb > 0) {
-        for (int i = 1; i <= aNb; i++) {
-          if (aExtrema.IsMin(i)) {
-            double aParam = aExtrema.Parameter(i);
-            Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
-            gp_Pnt aP;
-            aCurv->D1(aParam, aP, aVec);
-            break;
-          }
-        }
-      }
+      Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
+      GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
+      double aParam = anExtr.LowerDistanceParameter();
+      gp_Pnt aP;
+      aCurv->D1(aParam, aP, aVec);
     } else {
       GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
       GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
@@ -224,6 +223,7 @@ gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
   return aVec;
 }
 
+//*****************************************************************
 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
                                             const SketcherPrs_SymbolPrs* thePrs,
                                             double theStep, GeomPointPtr thePnt)
@@ -314,9 +314,16 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
 
       if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
+        // a point corresponds to one of the line end
         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
         aList.push_back(aFeature->firstResult());
+      } else {
+        // Check that a point belongs to the curve
+        GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
+        double aDist = aLin2d.distance(aPnt2d);
+        if (aDist <= Precision::Confusion())
+          aList.push_back(aFeature->firstResult());
       }
     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
@@ -336,8 +343,8 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
         double aStart = aCurve->startParam();
         double aEnd = aCurve->endParam();
         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
-        double aParam;
-        if (aCircle->parameter(thePnt, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
+        GeomPointPtr aProjPnt = aCircle->project(thePnt);
+        if (thePnt->distance(aProjPnt) <= Precision::Confusion())
           aList.push_back(aResObj);
       }
     }
@@ -362,8 +369,15 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
     aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
   }
 
+  // Position of the symbol
+  const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
+
+  // Angle size of a symbol
+  double aAngleStep = PI * 50./180.;
+
   std::list<gp_Vec>::const_iterator aItVec;
-  std::map<double, gp_Vec> aAngVectors;
+  std::list<double> aAngles;
+  std::list<gp_Vec> aVectors;
   // Select closest vectors and calculate angles between base vector and closest vector
   for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
     std::list<gp_Vec>::const_iterator aIt;
@@ -382,41 +396,67 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
         }
       }
     }
-    aAngVectors[aMinAng] = aVec;
+    if ((aMinAng * 2) >= aAngleStep) {
+      aVectors.push_back(aVec);
+      aAngles.push_back(aMinAng);
+    }
   }
 
-  // Angle size of a symbol for a first level
-  static const double aAngleStep = PI * 50./180.;
-
-  // Position of the symbol
-  int aPos = getPositionIndex(thePnt, thePrs);
-
-  //std::list<double>::const_iterator aItAng;
   gp_Ax1 aRotAx(aP, aNormDir);
-  int aPosId = 0; // Last used position
+  gp_Vec aVecPos;
+  // If number of angle less then number of symbols then each symbol can be placed
+  // directly inside of the angle
+  if (aAngles.size() >= aPos[1]) {
+    int aId = aPos[0];
+    std::list<gp_Vec>::iterator aVIt = aVectors.begin();
+    std::advance(aVIt, aId);
+    aVecPos = *aVIt;
+
+    gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
+    aShift.Normalize();
+    aShift.Multiply(theStep * 1.5);
+    return aP.Translated(aShift);
+  }
+
+  // A case when there are a lot of symbols
+  int aPosCount = 0;
   double aAng;
-  gp_Vec aPrevVec;
-  std::map<double, gp_Vec>::const_iterator aItAng;
-  for (aItAng = aAngVectors.cbegin(); aItAng != aAngVectors.cend(); ++aItAng) {
-    aAng = aItAng->first;
-    aPrevVec = aItAng->second;
-    if (aAng >= aAngleStep) {
-      gp_Vec aShift;
+  std::list<double>::const_iterator aItAng;
+
+  double aAngPos;
+  bool aHasPlace = false;
+  //int aIntId = 0; // a position inside a one sector
+  while (aPosCount < aPos[1]) {
+    for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
+         aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
+      aAng = (*aItAng);
       int Nb = int(aAng / aAngleStep);
-      if ((aPos >= aPosId) && (aPos < (aPosId + Nb))) {
-        // rotate base vector on a necessary angle
-        aShift = aPrevVec.Rotated(aRotAx, aAngleStep + aAngleStep * (aPos - aPosId));
-        aShift.Normalize();
-        aShift.Multiply(theStep * 1.5);
-        return aP.Translated(aShift);
+      aPosCount += Nb;
+
+      if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
+        aHasPlace = true;
+        aAngPos = (*aItAng);
+        aVecPos = (*aItVec);
+        //aIntId = aPos[0] - (aPosCount - Nb);
       }
-      aPosId += Nb;
     }
+    if (aPosCount < aPos[1]) {
+      aAngleStep -= 0.1;
+      aHasPlace = false;
+      aPosCount = 0;
+    }
+    if (aAngleStep <= 0)
+      break;
   }
-  gp_Vec aShift = aPrevVec.Rotated(aRotAx, aAngleStep);
-  aShift.Normalize();
-  aShift.Multiply(theStep * 1.5);
-  return aP.Translated(aShift);
+
+  if (aHasPlace) {
+    // rotate base vector on a necessary angle
+    gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
+    aShift.Normalize();
+    aShift.Multiply(theStep * 1.5);
+    return aP.Translated(aShift);
+  }
+  return aP;
 }
 
 //*****************************************************************