Salome HOME
Merge branch 'Pre_2.8.0_development'
[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
29 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
30
31 // The class is implemented as a singlton
32 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
33 {
34   if (MyPosMgr == NULL)
35     MyPosMgr = new SketcherPrs_PositionMgr();
36   return MyPosMgr;
37 }
38
39 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
40 {
41 }
42
43
44 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
45                                               const SketcherPrs_SymbolPrs* thePrs)
46 {
47   if (myShapes.count(theLine) == 1) {
48     // Find the map and add new [Presentation - Index] pair
49     PositionsMap& aPosMap = myShapes[theLine];
50     if (aPosMap.count(thePrs) == 1) {
51       // return existing index
52       return aPosMap[thePrs];
53     } else {
54       // Add a new [Presentation - Index] pair
55       int aInd = int(aPosMap.size());
56       aPosMap[thePrs] = aInd;
57       return aInd;
58     }
59   } else {
60     // Create a new map with initial index
61     PositionsMap aPosMap;
62     aPosMap[thePrs] = 0;
63     myShapes[theLine] = aPosMap;
64     return 0;
65   }
66 }
67
68 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
69                                             const SketcherPrs_SymbolPrs* thePrs,
70                                             double theStep)
71 {
72   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
73   gp_Pnt aP; // Central point
74   gp_Vec aVec1; // main vector
75   if (aShape->isEdge()) {
76     std::shared_ptr<GeomAPI_Curve> aCurve =
77       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
78     std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
79     std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
80     if (aCurve->isLine()) {
81       std::shared_ptr<GeomAPI_Edge> aEdge =
82         std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
83
84       aPnt1 = aEdge->firstPoint();
85       aPnt2 = aEdge->lastPoint();
86
87       // Find the middle point
88       aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
89                   (aPnt1->y() + aPnt2->y())/2.,
90                   (aPnt1->z() + aPnt2->z())/2.);
91
92     } else {
93       // this is a circle or arc
94       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
95       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
96       aP = aPnt->impl<gp_Pnt>();
97
98       aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
99       aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
100     }
101     aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
102   } else {
103     // This is a point
104     std::shared_ptr<GeomAPI_Vertex> aVertex =
105       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
106     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
107     aP = aPnt->impl<gp_Pnt>();
108
109     std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
110     aVec1 = gp_Vec(aDir->impl<gp_Dir>());
111   }
112   // Compute shifting vector for a one symbol
113   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
114   aShift.Normalize();
115   aShift.Multiply(theStep * 0.8);
116
117   // Shift the position coordinate according to position index
118   int aPos = getPositionIndex(theShape, thePrs);
119   int aM = 1;
120   if ((aPos % 2) == 0) {
121     // Even position
122     aP.Translate(aShift);
123     if (aPos > 0) {
124       if (aPos % 4 == 0)
125         aM = aPos / 4;
126       else
127         aM = -(aPos + 2) / 4;
128     }
129   } else {
130     // Odd position
131     aP.Translate(-aShift);
132     if (aPos > 1) {
133       if ((aPos - 1) % 4 == 0)
134         aM = (aPos - 1) / 4;
135       else
136         aM = -(aPos + 1) / 4;
137     }
138   }
139   if (aPos > 1) {
140     // Normalize vector along the line
141     aVec1.Normalize();
142     aVec1.Multiply(theStep);
143     aP.Translate(aVec1.Multiplied(aM));
144   }
145   return aP;
146 }
147
148 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
149 {
150   std::map<ObjectPtr, PositionsMap>::iterator aIt;
151   std::list<ObjectPtr> aToDel;
152   // Clear map for deleted presentation
153   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
154     PositionsMap& aPosMap = aIt->second;
155     if (aPosMap.count(thePrs) > 0) {
156       // Erase index
157       aPosMap.erase(aPosMap.find(thePrs));
158       if (aPosMap.size() == 0)
159         // Delete the map
160         aToDel.push_back(aIt->first);
161       else {
162         // Reindex objects positions in order to avoid spaces
163         PositionsMap::iterator aIt;
164         int i = 0;
165         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
166           aIt->second = i;
167       }
168     }
169   }
170   std::list<ObjectPtr>::const_iterator aListIt;
171   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
172     myShapes.erase(*aListIt);
173   }
174 }