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