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