]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Builder.cpp
Salome HOME
SketchSolver library refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_Builder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Builder.h"
8 #include <SketchSolver_ConstraintCoincidence.h>
9 #include <SketchSolver_ConstraintDistance.h>
10 #include <SketchSolver_ConstraintLength.h>
11 #include <SketchSolver_ConstraintRigid.h>
12
13 #include <GeomAPI_Edge.h>
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_ResultConstruction.h>
22
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintDistance.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_ConstraintFillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMirror.h>
34 #include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintPerpendicular.h>
36 #include <SketchPlugin_ConstraintRadius.h>
37 #include <SketchPlugin_ConstraintRigid.h>
38 #include <SketchPlugin_ConstraintTangent.h>
39 #include <SketchPlugin_ConstraintVertical.h>
40
41
42 // Initialization of constraint builder self pointer
43 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
44
45 SketchSolver_Builder* SketchSolver_Builder::getInstance()
46 {
47   if (!mySelf)
48     mySelf = new SketchSolver_Builder();
49   return mySelf;
50 }
51
52 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
53 {
54   SolverConstraintPtr aResult;
55   DataPtr aData = theConstraint->data();
56   if (!aData || !aData->isValid())
57     return aResult;
58
59   if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
60     return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
61   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
62     return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
63   } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
64 ////    return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
65   } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
66 ////    return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
67   } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
68     return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
69   } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
70     return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
71   } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
72 ////    return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
73   } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
74     return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
75   } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
76     return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
77   } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
78 ////    return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
79   } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
80 ////    return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
81   } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
82     return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
83   } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
84     return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
85   }
86   return aResult;
87 }
88
89 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
90 {
91   DataPtr aData = theFixedFeature->data();
92   if (!aData || !aData->isValid())
93     return SolverConstraintPtr();
94   return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
95 }
96
97
98
99 bool SketchSolver_Builder::createWorkplane(
100     CompositeFeaturePtr theSketch,
101     std::vector<Slvs_Entity>& theEntities,
102     std::vector<Slvs_Param>& theParameters)
103 {
104   DataPtr aSketchData = theSketch->data();
105   if (!aSketchData || !aSketchData->isValid())
106     return false; // the sketch is incorrect
107
108   // Get parameters of workplane
109   std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
110       SketchPlugin_Sketch::DIRX_ID());
111   std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
112       SketchPlugin_Sketch::NORM_ID());
113   std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
114       SketchPlugin_Sketch::ORIGIN_ID());
115   // Create SolveSpace entity corresponding to the sketch origin
116   if (!createEntity(anOrigin, theEntities, theParameters))
117     return false;
118   Slvs_hEntity anOriginID = theEntities.back().h;
119   // Create SolveSpace entity corresponding the the sketch normal
120   if (!createNormal(aNorm, aDirX, theEntities, theParameters))
121     return false;
122   Slvs_hEntity aNormalID = theEntities.back().h;
123
124   // Create workplane
125   Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
126   Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
127   theEntities.push_back(aWorkplane);
128   return true;
129 }
130
131 bool SketchSolver_Builder::createEntity(
132     AttributePtr theAttribute,
133     std::vector<Slvs_Entity>& theEntities,
134     std::vector<Slvs_Param>& theParameters)
135 {
136   Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
137   Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
138
139   // Point in 3D
140   std::shared_ptr<GeomDataAPI_Point> aPoint =
141       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
142   if (aPoint) {
143     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
144     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
145     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
146     theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
147         aParamID-2, aParamID-1, aParamID));
148     return true;
149   }
150   // Point in 2D
151   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
152     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
153   if (aPoint2D) {
154     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
155     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
156     theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
157       aParamID-1, aParamID));
158     return true;
159   }
160   // Scalar value (used for the distance entities)
161   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
162   if (aScalar) {
163     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
164     theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
165       SLVS_E_UNKNOWN, aParamID));
166     return true;
167   }
168   // unknown attribute type
169   return false;
170 }
171
172 bool SketchSolver_Builder::createEntity(
173     FeaturePtr theFeature,
174     std::vector<Slvs_Entity>& theEntities,
175     std::vector<Slvs_Param>& theParameters)
176 {
177   if (!theFeature->data()->isValid())
178     return false;
179
180   // SketchPlugin features
181   std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
182       SketchPlugin_Feature>(theFeature);
183   if (!aFeature)
184     return false;
185
186   // Verify the feature by its kind
187   const std::string& aFeatureKind = aFeature->getKind();
188   DataPtr aData = aFeature->data();
189   // Line
190   if (aFeatureKind == SketchPlugin_Line::ID()) {
191     AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
192     AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
193     if (!aStart->isInitialized() || !aEnd->isInitialized())
194       return false;
195     if (!createEntity(aStart, theEntities, theParameters) ||
196         !createEntity(aEnd, theEntities, theParameters))
197       return false;
198     Slvs_hEntity aLineID = theEntities.back().h + 1;
199     theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
200         aLineID-2, aLineID-1));
201   }
202   // Circle
203   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
204     AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
205     AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
206     if (!aCenter->isInitialized() || !aRadius->isInitialized())
207       return false;
208     if (!createEntity(aCenter, theEntities, theParameters) ||
209         !createEntity(aRadius, theEntities, theParameters))
210       return false;
211     Slvs_hEntity aCircID = theEntities.back().h;
212     theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
213       SLVS_E_UNKNOWN, aCircID-1));
214   }
215   // Arc
216   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
217     AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
218     AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
219     AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
220     if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
221       return false;
222     if (!createEntity(aCenter, theEntities, theParameters) ||
223         !createEntity(aStart, theEntities, theParameters) ||
224         !createEntity(aEnd, theEntities, theParameters))
225       return false;
226     Slvs_hEntity anArcID = theEntities.back().h;
227     theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
228         SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
229   }
230   // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
231   else if (aFeatureKind == SketchPlugin_Point::ID()) {
232     AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
233     if (!aPoint->isInitialized() ||
234         !createEntity(aPoint, theEntities, theParameters))
235       return false;
236     // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
237     // No need to add another entity.
238   }
239   return true;
240 }
241
242 bool SketchSolver_Builder::createNormal(
243     AttributePtr theNormal,
244     AttributePtr theDirX,
245     std::vector<Slvs_Entity>& theEntities,
246     std::vector<Slvs_Param>& theParameters)
247 {
248   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
249   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
250   if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance))
251     return false;
252   // calculate Y direction
253   std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
254
255   // quaternion parameters of normal vector
256   double qw, qx, qy, qz;
257   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
258                       &qx, &qy, &qz);
259   double aNormCoord[4] = { qw, qx, qy, qz };
260
261   // Create parameters of the normal
262   Slvs_hParam aCurParam = theParameters.back().h;
263   for (int i = 0; i < 4; i++)
264     theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
265
266   // Create a normal
267   Slvs_hEntity aCurEntity = theEntities.back().h + 1;
268   Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
269       aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
270   theEntities.push_back(aNormal);
271   return true;
272 }