Salome HOME
31921298ba0424bf4a61a7eedc4fcded4f967873
[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 <BRepExtrema_ExtPC.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <Geom_Curve.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   static std::list<std::string> aConstraints;
93   if (aConstraints.size() == 0) {
94     aConstraints.push_back(SketchPlugin_ConstraintTangent::ID());
95     aConstraints.push_back(SketchPlugin_ConstraintPerpendicular::ID());
96   }
97   std::list<std::string>::const_iterator aIt;
98   for (aIt = aConstraints.cbegin(); aIt != aConstraints.cend(); ++aIt) {
99     if ((*aIt) == theName)
100       return true;
101   }
102   return false;
103 }
104
105 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
106 {
107   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
108     AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109       theFeature->data()->attribute(SketchPlugin_Line::START_ID()));
110     AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111       theFeature->data()->attribute(SketchPlugin_Line::END_ID()));
112
113     GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
114     GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
115
116     if (aPnt1->isEqual(thePnt2d) || aPnt2->isEqual(thePnt2d))
117       return true;
118   } else if ((theFeature->getKind() == SketchPlugin_Circle::ID()) ||
119              (theFeature->getKind() == SketchPlugin_Arc::ID())) {
120     GeomCurvePtr aCurve;
121     ObjectPtr aResObj;
122     std::list<ResultPtr> aResults = theFeature->results();
123     std::list<ResultPtr>::const_iterator aIt;
124     for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
125       GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
126       if (aShp->isEdge()) {
127         aResObj = (*aIt);
128         aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
129         break;
130       }
131     }
132     if (aCurve.get()) {
133       double aStart = aCurve->startParam();
134       double aEnd = aCurve->endParam();
135       GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
136       double aParam;
137       if (aCircle->parameter(thePos, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
138         return true;
139     }
140   }
141   return false;
142 }
143
144 const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
145                                               const SketcherPrs_SymbolPrs* thePrs)
146 {
147   if (myPntShapes.count(thePrs->feature()) == 0) {
148     // Renumerate positions around the specified constraint point for all constraints
149     GeomAx3Ptr aAx3 = thePrs->plane();
150     ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
151     GeomPnt2dPtr aPnt2d = thePos->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
152
153     int aNbSubs = aOwner->numberOfSubs();
154     int aId = 0;
155     std::list<const ModelAPI_Feature*> aFeaList;
156     for (int i = 0; i < aNbSubs; i++) {
157       FeaturePtr aFeature = aOwner->subFeature(i);
158
159       if (myPntShapes.count(aFeature.get()) == 1) {
160         myPntShapes[aFeature.get()][0] = aId;
161         aId++;
162         aFeaList.push_back(aFeature.get());
163       } else {
164         if (isPntConstraint(aFeature->getKind())) {
165           DataPtr aData = aFeature->data();
166           AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
167           FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
168           bool aContains = false;
169           if (containsPoint(aObj, aPnt2d, thePos)) {
170             aContains = true;
171           } else {
172             aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
173             aObj = ModelAPI_Feature::feature(aObjRef->object());
174             if (containsPoint(aObj, aPnt2d, thePos)) {
175               aContains = true;
176             }
177           }
178           if (aContains) {
179             myPntShapes[aFeature.get()][0] = aId;
180             aId++;
181             aFeaList.push_back(aFeature.get());
182           }
183         }
184       }
185     }
186     int aSize = (int) aFeaList.size();
187     std::list<const ModelAPI_Feature*>::const_iterator aIt;
188     for (aIt = aFeaList.cbegin(); aIt != aFeaList.cend(); aIt++) {
189       myPntShapes[*aIt][1] = aSize;
190     }
191   }
192   return myPntShapes[thePrs->feature()];
193 }
194
195 //*****************************************************************
196 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
197 {
198   gp_Vec aVec;
199   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
200   if (aShape->isEdge()) {
201     std::shared_ptr<GeomAPI_Curve> aCurve =
202       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
203
204     if (aCurve->isCircle()) {
205       GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
206       GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
207       BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
208                                  aEdgePtr->impl<TopoDS_Edge>());
209       int aNb = aExtrema.NbExt();
210       if (aNb > 0) {
211         for (int i = 1; i <= aNb; i++) {
212           if (aExtrema.IsMin(i)) {
213             double aParam = aExtrema.Parameter(i);
214             Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
215             gp_Pnt aP;
216             aCurv->D1(aParam, aP, aVec);
217             break;
218           }
219         }
220       }
221     } else {
222       GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
223       GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
224
225       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
226       if (aPn2.IsEqual(theP, Precision::Confusion()))
227         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
228       else
229         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
230     }
231   } else {
232     aVec = gp_Vec(theDir->impl<gp_Dir>());
233   }
234   return aVec;
235 }
236
237 //*****************************************************************
238 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
239                                             const SketcherPrs_SymbolPrs* thePrs,
240                                             double theStep, GeomPointPtr thePnt)
241 {
242   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
243   gp_Pnt aP; // Central point
244
245   if (thePnt.get()) {
246     return getPointPosition(theShape, thePrs, theStep, thePnt);
247   } else {
248     if (aShape->isEdge()) {
249       std::shared_ptr<GeomAPI_Curve> aCurve =
250         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
251       // this is a circle or arc
252       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
253       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
254       aP = aPnt->impl<gp_Pnt>();
255     } else {
256       // This is a point
257       std::shared_ptr<GeomAPI_Vertex> aVertex =
258         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
259       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
260       aP = aPnt->impl<gp_Pnt>();
261     }
262   }
263   // main vector
264   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
265
266   // Compute shifting vector for a one symbol
267   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
268   aShift.Normalize();
269   aShift.Multiply(theStep * 0.8);
270
271   // Shift the position coordinate according to position index
272   int aPos = getPositionIndex(theShape, thePrs);
273   int aM = 1;
274   if ((aPos % 2) == 0) {
275     // Even position
276     aP.Translate(aShift);
277     if (aPos > 0) {
278       if (aPos % 4 == 0)
279         aM = aPos / 4;
280       else
281         aM = -(aPos + 2) / 4;
282     }
283   } else {
284     // Odd position
285     aP.Translate(-aShift);
286     if (aPos > 1) {
287       if ((aPos - 1) % 4 == 0)
288         aM = (aPos - 1) / 4;
289       else
290         aM = -(aPos + 1) / 4;
291     }
292   }
293   if (aPos > 1) {
294     // Normalize vector along the line
295     aVec1.Normalize();
296     aVec1.Multiply(theStep);
297     aP.Translate(aVec1.Multiplied(aM));
298   }
299   return aP;
300 }
301
302
303 //*****************************************************************
304 //! Returns curves connected to the given point
305 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
306 {
307   std::list<ObjectPtr> aList;
308   GeomAx3Ptr aAx3 = thePrs->plane();
309   ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
310   GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
311
312   int aNbSubs = aOwner->numberOfSubs();
313   for (int i = 0; i < aNbSubs; i++) {
314     FeaturePtr aFeature = aOwner->subFeature(i);
315     if (!aFeature->firstResult().get()) // There is no result
316       continue;
317
318     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
319       AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
320         aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
321       AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
322         aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
323
324       GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
325       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
326
327       if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
328         // a point corresponds to one of the line end
329         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
330         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
331         aList.push_back(aFeature->firstResult());
332       } else {
333         // Check that a point belongs to the curve
334         GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
335         double aDist = aLin2d.distance(aPnt2d);
336         if (aDist <= Precision::Confusion())
337           aList.push_back(aFeature->firstResult());
338       }
339     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
340               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
341       GeomCurvePtr aCurve;
342       ObjectPtr aResObj;
343       std::list<ResultPtr> aResults = aFeature->results();
344       std::list<ResultPtr>::const_iterator aIt;
345       for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
346         GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
347         if (aShp->isEdge()) {
348           aResObj = (*aIt);
349           aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
350           break;
351         }
352       }
353       if (aCurve.get()) {
354         double aStart = aCurve->startParam();
355         double aEnd = aCurve->endParam();
356         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
357         GeomPointPtr aProjPnt = aCircle->project(thePnt);
358         if (thePnt->distance(aProjPnt) <= Precision::Confusion())
359           aList.push_back(aResObj);
360       }
361     }
362   }
363   return aList;
364 }
365
366 //*****************************************************************
367 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
368   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
369   double theStep, GeomPointPtr thePnt)
370 {
371   gp_Pnt aP = thePnt->impl<gp_Pnt>();
372   GeomDirPtr aNormal = thePrs->plane()->normal();
373   gp_Dir aNormDir = aNormal->impl<gp_Dir>();
374
375   std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
376   std::list<ObjectPtr>::const_iterator aItCurv;
377   std::list<gp_Vec> aVectorsList;
378   // Calculate all vectors
379   for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
380     aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
381   }
382
383   // Position of the symbol
384   const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
385
386   // Angle size of a symbol
387   double aAngleStep = PI * 50./180.;
388
389   std::list<gp_Vec>::const_iterator aItVec;
390   std::list<double> aAngles;
391   std::list<gp_Vec> aVectors;
392   // Select closest vectors and calculate angles between base vector and closest vector
393   for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
394     std::list<gp_Vec>::const_iterator aIt;
395     double aMinAng = 0;
396     gp_Vec aVec = *aItVec;
397     for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
398       double aAng = aVec.AngleWithRef(*aIt, aNormDir);
399       if (aAng != 0) {
400         if (aAng < 0)
401           aAng = 2 * PI + aAng;
402
403         if (aMinAng == 0)
404           aMinAng = aAng;
405         else if (aAng < aMinAng) {
406           aMinAng = aAng;
407         }
408       }
409     }
410     aVectors.push_back(aVec);
411     aAngles.push_back(aMinAng);
412   }
413
414   int aPosCount = 0;
415   double aAng;
416   std::list<double>::const_iterator aItAng;
417
418   double aAngPos;
419   gp_Vec aVecPos;
420   bool aHasPlace = false;
421   //int aIntId = 0; // a position inside a one sector
422   while (aPosCount < aPos[1]) {
423     for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
424          aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
425       aAng = (*aItAng);
426       int Nb = int(aAng / aAngleStep);
427       aPosCount += Nb;
428
429       if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
430         aHasPlace = true;
431         aAngPos = (*aItAng);
432         aVecPos = (*aItVec);
433         //aIntId = aPos[0] - (aPosCount - Nb);
434       }
435     }
436     if (aPosCount < aPos[1]) {
437       aAngleStep -= 0.1;
438       aHasPlace = false;
439       aPosCount = 0;
440     }
441     if (aAngleStep <= 0)
442       break;
443   }
444
445   gp_Ax1 aRotAx(aP, aNormDir);
446   if (aHasPlace) {
447     // rotate base vector on a necessary angle
448     gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
449     aShift.Normalize();
450     aShift.Multiply(theStep * 1.5);
451     return aP.Translated(aShift);
452   }
453   return aP;
454 }
455
456 //*****************************************************************
457 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
458 {
459   std::map<ObjectPtr, PositionsMap>::iterator aIt;
460   std::list<ObjectPtr> aToDel;
461   // Clear map for deleted presentation
462   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
463     PositionsMap& aPosMap = aIt->second;
464     if (aPosMap.count(thePrs) > 0) {
465       // Erase index
466       aPosMap.erase(aPosMap.find(thePrs));
467       if (aPosMap.size() == 0)
468         // Delete the map
469         aToDel.push_back(aIt->first);
470       else {
471         // Reindex objects positions in order to avoid spaces
472         PositionsMap::iterator aIt;
473         int i = 0;
474         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
475           aIt->second = i;
476       }
477     }
478   }
479   std::list<ObjectPtr>::const_iterator aListIt;
480   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
481     myShapes.erase(*aListIt);
482   }
483 }