Salome HOME
c92f1a0abdf521a25719da908401f20a90774c99
[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
31 #include <GeomDataAPI_Point2D.h>
32
33 #include <SketchPlugin_Line.h>
34 #include <SketchPlugin_Circle.h>
35 #include <SketchPlugin_Arc.h>
36
37 #include <BRepExtrema_ExtPC.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <Geom_Curve.hxx>
40 #include <TColGeom_SequenceOfCurve.hxx>
41 #include <gp_Dir.hxx>
42
43 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
44
45 // The class is implemented as a singlton
46 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
47 {
48   if (MyPosMgr == NULL)
49     MyPosMgr = new SketcherPrs_PositionMgr();
50   return MyPosMgr;
51 }
52
53 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
54 {
55 }
56
57
58 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
59                                               const SketcherPrs_SymbolPrs* thePrs)
60 {
61   if (myShapes.count(theLine) == 1) {
62     // Find the map and add new [Presentation - Index] pair
63     PositionsMap& aPosMap = myShapes[theLine];
64     if (aPosMap.count(thePrs) == 1) {
65       // return existing index
66       return aPosMap[thePrs];
67     } else {
68       // Add a new [Presentation - Index] pair
69       int aInd = int(aPosMap.size());
70       aPosMap[thePrs] = aInd;
71       return aInd;
72     }
73   } else {
74     // Create a new map with initial index
75     PositionsMap aPosMap;
76     aPosMap[thePrs] = 0;
77     myShapes[theLine] = aPosMap;
78     return 0;
79   }
80 }
81
82
83 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
84 {
85   gp_Vec aVec;
86   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
87   if (aShape->isEdge()) {
88     std::shared_ptr<GeomAPI_Curve> aCurve =
89       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
90
91     if (aCurve->isCircle()) {
92       GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
93       GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
94       BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
95                                  aEdgePtr->impl<TopoDS_Edge>());
96       int aNb = aExtrema.NbExt();
97       if (aNb > 0) {
98         for (int i = 1; i <= aNb; i++) {
99           if (aExtrema.IsMin(i)) {
100             double aParam = aExtrema.Parameter(i);
101             Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
102             gp_Pnt aP;
103             aCurv->D1(aParam, aP, aVec);
104             break;
105           }
106         }
107       }
108     } else {
109       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
110       GeomPointPtr aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
111       GeomPointPtr aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
112
113       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
114       if (aPn2.IsEqual(theP, Precision::Confusion()))
115         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
116       else
117         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
118     }
119   } else {
120     aVec = gp_Vec(theDir->impl<gp_Dir>());
121   }
122   return aVec;
123 }
124
125 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
126                                             const SketcherPrs_SymbolPrs* thePrs,
127                                             double theStep, GeomPointPtr thePnt)
128 {
129   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
130   gp_Pnt aP; // Central point
131
132   if (thePnt.get()) {
133     return getPointPosition(theShape, thePrs, theStep, thePnt);
134   } else {
135     if (aShape->isEdge()) {
136       std::shared_ptr<GeomAPI_Curve> aCurve =
137         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
138       // this is a circle or arc
139       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
140       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
141       aP = aPnt->impl<gp_Pnt>();
142     } else {
143       // This is a point
144       std::shared_ptr<GeomAPI_Vertex> aVertex =
145         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
146       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
147       aP = aPnt->impl<gp_Pnt>();
148     }
149   }
150   // main vector
151   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
152
153   // Compute shifting vector for a one symbol
154   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
155   aShift.Normalize();
156   aShift.Multiply(theStep * 0.8);
157
158   // Shift the position coordinate according to position index
159   int aPos = getPositionIndex(theShape, thePrs);
160   int aM = 1;
161   if ((aPos % 2) == 0) {
162     // Even position
163     aP.Translate(aShift);
164     if (aPos > 0) {
165       if (aPos % 4 == 0)
166         aM = aPos / 4;
167       else
168         aM = -(aPos + 2) / 4;
169     }
170   } else {
171     // Odd position
172     aP.Translate(-aShift);
173     if (aPos > 1) {
174       if ((aPos - 1) % 4 == 0)
175         aM = (aPos - 1) / 4;
176       else
177         aM = -(aPos + 1) / 4;
178     }
179   }
180   if (aPos > 1) {
181     // Normalize vector along the line
182     aVec1.Normalize();
183     aVec1.Multiply(theStep);
184     aP.Translate(aVec1.Multiplied(aM));
185   }
186   return aP;
187 }
188
189
190 //*****************************************************************
191 //! Returns curves connected to the given point
192 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
193 {
194   std::list<ObjectPtr> aList;
195   GeomAx3Ptr aAx3 = thePrs->plane();
196   ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
197   GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
198
199   int aNbSubs = aOwner->numberOfSubs();
200   for (int i = 0; i < aNbSubs; i++) {
201     FeaturePtr aFeature = aOwner->subFeature(i);
202     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
203       AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204         aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
205       AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206         aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
207
208       GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
209       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
210
211       if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
212         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
213         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
214         aList.push_back(aFeature->firstResult());
215       }
216     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
217               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
218       GeomCurvePtr aCurve;
219       ObjectPtr aResObj;
220       std::list<ResultPtr> aResults = aFeature->results();
221       std::list<ResultPtr>::const_iterator aIt;
222       for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
223         GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
224         if (aShp->isEdge()) {
225           aResObj = (*aIt);
226           aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
227           break;
228         }
229       }
230       if (aCurve.get()) {
231         double aStart = aCurve->startParam();
232         double aEnd = aCurve->endParam();
233         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
234         double aParam;
235         if (aCircle->parameter(thePnt, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
236           aList.push_back(aResObj);
237       }
238     }
239   }
240   return aList;
241 }
242
243 //*****************************************************************
244 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
245   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
246   double theStep, GeomPointPtr thePnt)
247 {
248   std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
249
250   gp_Pnt aP = thePnt->impl<gp_Pnt>();
251   //gp_Vec aVec1 = getVector(theLine, thePrs->plane()->dirX(), aP);
252   std::list<gp_Vec> aVectors;
253   std::list<ObjectPtr>::const_iterator aItCurv;
254   for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
255     ObjectPtr aObject = (*aItCurv);
256     gp_Vec aVec = getVector(aObject, thePrs->plane()->dirX(), aP);
257     aVectors.push_back(aVec);
258   }
259
260   gp_Vec aBase = getVector(theLine, thePrs->plane()->dirX(), aP);
261   std::list<double> aAngles;
262   std::list<gp_Vec>::const_iterator aItVec;
263   for (aItVec = aVectors.cbegin(); aItVec != aVectors.cend(); aItVec++) {
264     gp_Vec aVec = (*aItVec);
265     double aAngle = aBase.Angle(aVec);
266     aAngles.push_back(aAngle);
267   }
268   // Compute shifting vector for a one symbol
269   //gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
270   //aShift.Normalize();
271   //aShift.Multiply(theStep * 1.5);
272   //aP.Translate(aShift);
273   return aP;
274 }
275
276 //*****************************************************************
277 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
278 {
279   std::map<ObjectPtr, PositionsMap>::iterator aIt;
280   std::list<ObjectPtr> aToDel;
281   // Clear map for deleted presentation
282   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
283     PositionsMap& aPosMap = aIt->second;
284     if (aPosMap.count(thePrs) > 0) {
285       // Erase index
286       aPosMap.erase(aPosMap.find(thePrs));
287       if (aPosMap.size() == 0)
288         // Delete the map
289         aToDel.push_back(aIt->first);
290       else {
291         // Reindex objects positions in order to avoid spaces
292         PositionsMap::iterator aIt;
293         int i = 0;
294         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
295           aIt->second = i;
296       }
297     }
298   }
299   std::list<ObjectPtr>::const_iterator aListIt;
300   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
301     myShapes.erase(*aListIt);
302   }
303 }