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