Salome HOME
49892641961967dae0d903f327d14dd20e6d9e15
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_PositionMgr.h"
22 #include "SketcherPrs_Tools.h"
23
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Curve.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Ax3.h>
29 #include <GeomAPI_Circ.h>
30 #include <GeomAPI_Lin2d.h>
31
32 #include <GeomDataAPI_Point2D.h>
33
34 #include <SketchPlugin_Line.h>
35 #include <SketchPlugin_Circle.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_ConstraintTangent.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
39
40 #include <TopoDS_Vertex.hxx>
41 #include <Geom_Curve.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
43 #include <TColGeom_SequenceOfCurve.hxx>
44 #include <gp_Dir.hxx>
45
46 #include <array>
47
48 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
49
50 #define PI 3.1415926535897932
51
52 // The class is implemented as a singlton
53 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
54 {
55   if (MyPosMgr == NULL)
56     MyPosMgr = new SketcherPrs_PositionMgr();
57   return MyPosMgr;
58 }
59
60 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
61 {
62 }
63
64
65 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
66                                               const SketcherPrs_SymbolPrs* thePrs)
67 {
68   if (myShapes.count(theLine) == 1) {
69     // Find the map and add new [Presentation - Index] pair
70     PositionsMap& aPosMap = myShapes[theLine];
71     if (aPosMap.count(thePrs) == 1) {
72       // return existing index
73       return aPosMap[thePrs];
74     } else {
75       // Add a new [Presentation - Index] pair
76       int aInd = int(aPosMap.size());
77       aPosMap[thePrs] = aInd;
78       return aInd;
79     }
80   } else {
81     // Create a new map with initial index
82     PositionsMap aPosMap;
83     aPosMap[thePrs] = 0;
84     myShapes[theLine] = aPosMap;
85     return 0;
86   }
87 }
88
89
90 bool SketcherPrs_PositionMgr::isPntConstraint(const std::string& theName)
91 {
92   return ((theName == SketchPlugin_ConstraintTangent::ID()) ||
93     (theName == SketchPlugin_ConstraintPerpendicular::ID()));
94 }
95
96 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
97 {
98   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
99     AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100       theFeature->data()->attribute(SketchPlugin_Line::START_ID()));
101     AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102       theFeature->data()->attribute(SketchPlugin_Line::END_ID()));
103
104     GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
105     GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
106
107     if (aPnt1->isEqual(thePnt2d) || aPnt2->isEqual(thePnt2d))
108       return true;
109   } else if ((theFeature->getKind() == SketchPlugin_Circle::ID()) ||
110              (theFeature->getKind() == SketchPlugin_Arc::ID())) {
111     GeomCurvePtr aCurve;
112     ObjectPtr aResObj;
113     std::list<ResultPtr> aResults = theFeature->results();
114     std::list<ResultPtr>::const_iterator aIt;
115     for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
116       GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
117       if (aShp->isEdge()) {
118         aResObj = (*aIt);
119         aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
120         break;
121       }
122     }
123     if (aCurve.get()) {
124       double aStart = aCurve->startParam();
125       double aEnd = aCurve->endParam();
126       GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
127       double aParam;
128       if (aCircle->parameter(thePos, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
129         return true;
130     }
131   }
132   return false;
133 }
134
135 const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
136                                               const SketcherPrs_SymbolPrs* thePrs)
137 {
138   if (myPntShapes.count(thePrs->feature()) == 0) {
139     // Renumerate positions around the specified constraint point for all constraints
140     GeomAx3Ptr aAx3 = thePrs->plane();
141     ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
142     GeomPnt2dPtr aPnt2d = thePos->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
143
144     int aNbSubs = aOwner->numberOfSubs();
145     int aId = 0;
146     std::list<const ModelAPI_Feature*> aFeaList;
147     for (int i = 0; i < aNbSubs; i++) {
148       FeaturePtr aFeature = aOwner->subFeature(i);
149
150       bool aUseFeature = ((myPntShapes.count(aFeature.get()) == 1) ||
151                          (isPntConstraint(aFeature->getKind())));
152       if (aUseFeature) {
153         DataPtr aData = aFeature->data();
154         AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
155         FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
156         bool aContains = false;
157         if (containsPoint(aObj, aPnt2d, thePos)) {
158           aContains = true;
159         } else {
160           aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
161           aObj = ModelAPI_Feature::feature(aObjRef->object());
162           if (containsPoint(aObj, aPnt2d, thePos)) {
163             aContains = true;
164           }
165         }
166         if (aContains) {
167           myPntShapes[aFeature.get()][0] = aId;
168           aId++;
169           aFeaList.push_back(aFeature.get());
170         }
171       }
172     }
173     int aSize = (int) aFeaList.size();
174     std::list<const ModelAPI_Feature*>::const_iterator aIt;
175     for (aIt = aFeaList.cbegin(); aIt != aFeaList.cend(); aIt++) {
176       myPntShapes[*aIt][1] = aSize;
177     }
178   }
179   return myPntShapes[thePrs->feature()];
180 }
181
182 //*****************************************************************
183 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
184 {
185   gp_Vec aVec;
186   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
187   if (aShape->isEdge()) {
188     std::shared_ptr<GeomAPI_Curve> aCurve =
189       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
190
191     if (aCurve->isCircle()) {
192       Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
193       GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
194       double aParam = anExtr.LowerDistanceParameter();
195       gp_Pnt aP;
196       aCurv->D1(aParam, aP, aVec);
197       // 2458: check correct orientation of the vector
198       if (aVec.SquareMagnitude() > Precision::Confusion()) {
199         std::shared_ptr<GeomAPI_Edge> aCircEdge(new GeomAPI_Edge(aShape));
200         double aFirstParam, aLastParam;
201         aCircEdge->getRange(aFirstParam, aLastParam);
202         // if parameter is near the LastParam, make the vector go inside (reverse)
203         double aDelta = aLastParam - aParam;
204         while (aDelta < -Precision::Confusion())
205           aDelta += 2. * M_PI;
206         while (aDelta > 2. * M_PI - Precision::Confusion())
207           aDelta -= 2. * M_PI;
208         if (fabs(aDelta) < Precision::Confusion())
209           aVec.Reverse();
210       }
211     } else {
212       GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
213       GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
214
215       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
216       if (aPn2.IsEqual(theP, Precision::Confusion()))
217         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
218       else
219         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
220     }
221   } else {
222     aVec = gp_Vec(theDir->impl<gp_Dir>());
223   }
224   return aVec;
225 }
226
227 //*****************************************************************
228 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
229                                             const SketcherPrs_SymbolPrs* thePrs,
230                                             double theStep, GeomPointPtr thePnt)
231 {
232   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
233   gp_Pnt aP; // Central point
234
235   if (thePnt.get()) {
236     return getPointPosition(theShape, thePrs, theStep, thePnt);
237   } else {
238     if (aShape->isEdge()) {
239       std::shared_ptr<GeomAPI_Curve> aCurve =
240         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
241       // this is a circle or arc
242       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
243       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
244       aP = aPnt->impl<gp_Pnt>();
245     } else {
246       // This is a point
247       std::shared_ptr<GeomAPI_Vertex> aVertex =
248         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
249       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
250       aP = aPnt->impl<gp_Pnt>();
251     }
252   }
253   // main vector
254   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
255
256   // Compute shifting vector for a one symbol
257   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
258   aShift.Normalize();
259   aShift.Multiply(theStep * 0.8);
260
261   // Shift the position coordinate according to position index
262   int aPos = getPositionIndex(theShape, thePrs);
263   int aM = 1;
264   if ((aPos % 2) == 0) {
265     // Even position
266     aP.Translate(aShift);
267     if (aPos > 0) {
268       if (aPos % 4 == 0)
269         aM = aPos / 4;
270       else
271         aM = -(aPos + 2) / 4;
272     }
273   } else {
274     // Odd position
275     aP.Translate(-aShift);
276     if (aPos > 1) {
277       if ((aPos - 1) % 4 == 0)
278         aM = (aPos - 1) / 4;
279       else
280         aM = -(aPos + 1) / 4;
281     }
282   }
283   if (aPos > 1) {
284     // Normalize vector along the line
285     aVec1.Normalize();
286     aVec1.Multiply(theStep);
287     aP.Translate(aVec1.Multiplied(aM));
288   }
289   return aP;
290 }
291
292
293 //*****************************************************************
294 //! Returns curves connected to the given point
295 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
296 {
297   std::list<ObjectPtr> aList;
298   GeomAx3Ptr aAx3 = thePrs->plane();
299   ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
300   GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
301
302   int aNbSubs = aOwner->numberOfSubs();
303   for (int i = 0; i < aNbSubs; i++) {
304     FeaturePtr aFeature = aOwner->subFeature(i);
305     if (!aFeature->firstResult().get() || aFeature->firstResult()->isDisabled())
306       continue;  // There is no result
307
308     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
309       AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
310         aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
311       AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
312         aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
313
314       GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
315       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
316
317       if (aPnt1->distance(aPnt2d) <= Precision::Confusion() ||
318           aPnt2->distance(aPnt2d) <= Precision::Confusion()) {
319         // a point corresponds to one of the line end
320         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
321         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
322         aList.push_back(aFeature->firstResult());
323       } else {
324         // Check that a point belongs to the curve
325         GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
326         double aDist = aLin2d.distance(aPnt2d);
327         if (aDist <= Precision::Confusion())
328           aList.push_back(aFeature->firstResult());
329       }
330     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
331               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
332       GeomCurvePtr aCurve;
333       ObjectPtr aResObj;
334       std::list<ResultPtr> aResults = aFeature->results();
335       std::list<ResultPtr>::const_iterator aIt;
336       for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
337         GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
338         if (aShp->isEdge()) {
339           aResObj = (*aIt);
340           aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
341           break;
342         }
343       }
344       if (aCurve.get()) {
345         double aStart = aCurve->startParam();
346         double aEnd = aCurve->endParam();
347         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
348         GeomPointPtr aProjPnt = aCircle->project(thePnt);
349         if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
350           aList.push_back(aResObj);
351       }
352     }
353   }
354   return aList;
355 }
356
357 //*****************************************************************
358 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
359   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
360   double theStep, GeomPointPtr thePnt)
361 {
362   gp_Pnt aP = thePnt->impl<gp_Pnt>();
363   GeomDirPtr aNormal = thePrs->plane()->normal();
364   gp_Dir aNormDir = aNormal->impl<gp_Dir>();
365
366   std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
367   std::list<ObjectPtr>::const_iterator aItCurv;
368   std::list<gp_Vec> aVectorsList;
369   // Calculate all vectors
370   for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
371     aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
372   }
373
374   // Position of the symbol
375   const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
376
377   // Angle size of a symbol
378   //double aAngleStep = PI * 50./180.;
379   double aAngleStep = PI/4.;
380
381   std::list<gp_Vec>::const_iterator aItVec;
382   std::list<double> aAngles;
383   std::list<gp_Vec> aVectors;
384   // Select closest vectors and calculate angles between base vector and closest vector
385   for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
386     std::list<gp_Vec>::const_iterator aIt;
387     double aMinAng = 0;
388     gp_Vec aVec = *aItVec;
389     for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
390       double aAng = aVec.AngleWithRef(*aIt, aNormDir);
391       if (aAng != 0) {
392         if (aAng < 0)
393           aAng = 2 * PI + aAng;
394
395         if (aMinAng == 0)
396           aMinAng = aAng;
397         else if (aAng < aMinAng) {
398           aMinAng = aAng;
399         }
400       }
401     }
402     if (aMinAng >= aAngleStep) {
403       aVectors.push_back(aVec);
404       aAngles.push_back(aMinAng);
405     }
406   }
407
408   gp_Ax1 aRotAx(aP, aNormDir);
409   gp_Vec aVecPos;
410   // If number of angle less then number of symbols then each symbol can be placed
411   // directly inside of the angle
412   if (aAngles.size() >= aPos[1] && !aVectors.empty()) {
413     int aId = aPos[0];
414     aVecPos = *(std::next(aVectors.begin(), aId));
415
416     gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
417     aShift.Normalize();
418     aShift.Multiply(theStep * 1.5);
419     return aP.Translated(aShift);
420   }
421
422   // A case when there are a lot of symbols
423   int aPosCount = 0;
424   double aAng;
425   std::list<double>::const_iterator aItAng;
426
427   double aAngPos;
428   bool aHasPlace = false;
429   //int aIntId = 0; // a position inside a one sector
430   while (aPosCount < aPos[1]) {
431     for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
432          aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
433       aAng = (*aItAng);
434       int Nb = int(aAng / aAngleStep);
435       aPosCount += Nb;
436
437       if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
438         aHasPlace = true;
439         aAngPos = (*aItAng);
440         aVecPos = (*aItVec);
441         //aIntId = aPos[0] - (aPosCount - Nb);
442       }
443     }
444     if (aPosCount < aPos[1]) {
445       aAngleStep -= 0.1;
446       aHasPlace = false;
447       aPosCount = 0;
448     }
449     if (aAngleStep <= 0)
450       break;
451   }
452
453   if (aHasPlace) {
454     // rotate base vector on a necessary angle
455     gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
456     aShift.Normalize();
457     aShift.Multiply(theStep * 1.5);
458     return aP.Translated(aShift);
459   }
460   return aP;
461 }
462
463 //*****************************************************************
464 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
465 {
466   std::map<ObjectPtr, PositionsMap>::iterator aIt;
467   std::list<ObjectPtr> aToDel;
468   // Clear map for deleted presentation
469   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
470     PositionsMap& aPosMap = aIt->second;
471     if (aPosMap.count(thePrs) > 0) {
472       // Erase index
473       aPosMap.erase(aPosMap.find(thePrs));
474       if (aPosMap.size() == 0)
475         // Delete the map
476         aToDel.push_back(aIt->first);
477       else {
478         // Reindex objects positions in order to avoid spaces
479         PositionsMap::iterator aIt;
480         int i = 0;
481         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
482           aIt->second = i;
483       }
484     }
485   }
486   std::list<ObjectPtr>::const_iterator aListIt;
487   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
488     myShapes.erase(*aListIt);
489   }
490 }