Salome HOME
3cfa835db921703800889a8d995ff0aaeab7cb67
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
1 // File:        PartSet_Tools.h
2 // Created:     28 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_Tools.h>
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_Document.h>
10
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <GeomAPI_Dir.h>
16 #include <GeomAPI_XYZ.h>
17
18 #include <SketchPlugin_Feature.h>
19 #include <SketchPlugin_Sketch.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
21 #include <SketchPlugin_Constraint.h>
22
23 #include <PartSet_FeatureLinePrs.h>
24 #include <PartSet_FeaturePointPrs.h>
25 #include <PartSet_FeatureCirclePrs.h>
26 #include <PartSet_FeatureArcPrs.h>
27
28 #include <XGUI_ViewerPrs.h>
29
30 #include <V3d_View.hxx>
31 #include <gp_Pln.hxx>
32 #include <ProjLib.hxx>
33 #include <ElSLib.hxx>
34 #include <Geom_Line.hxx>
35 #include <GeomAPI_ProjectPointOnCurve.hxx>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 const double PRECISION_TOLERANCE = 0.000001;
42
43 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
44 {
45   if (theView.IsNull())
46     return gp_Pnt();
47
48   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
49   theView->Eye(XEye, YEye, ZEye);
50
51   theView->At(XAt, YAt, ZAt);
52   gp_Pnt EyePoint(XEye, YEye, ZEye);
53   gp_Pnt AtPoint(XAt, YAt, ZAt);
54
55   gp_Vec EyeVector(EyePoint, AtPoint);
56   gp_Dir EyeDir(EyeVector);
57
58   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
59   Standard_Real X, Y, Z;
60   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
61   gp_Pnt ConvertedPoint(X, Y, Z);
62
63   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
64   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
65   return ResultPoint;
66 }
67
68 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
69                                 Handle(V3d_View) theView, double& theX, double& theY)
70 {
71   if (!theSketch)
72     return;
73
74   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
75   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
76
77   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
78     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
79
80   boost::shared_ptr<GeomDataAPI_Dir> aX = 
81     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
82   boost::shared_ptr<GeomDataAPI_Dir> anY = 
83     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
84
85   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
86   gp_Vec aVec(anOriginPnt, thePoint);
87
88   if (!theView.IsNull())
89   {
90     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
91     theView->Eye(XEye, YEye, ZEye);
92
93     theView->At(XAt, YAt, ZAt);
94     gp_Pnt EyePoint(XEye, YEye, ZEye);
95     gp_Pnt AtPoint(XAt, YAt, ZAt);
96
97     gp_Vec anEyeVec(EyePoint, AtPoint);
98     anEyeVec.Normalize();
99
100     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
101                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
102     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
103
104     double aDen = anEyeVec * aNormalVec;
105     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
106
107     gp_Vec aDeltaVec = anEyeVec*aLVec;
108     aVec = aVec - aDeltaVec;
109   }
110   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
111   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
112 }
113
114 void PartSet_Tools::convertTo3D(const double theX, const double theY,
115                                 FeaturePtr theSketch,
116                                 gp_Pnt& thePoint)
117 {
118   if (!theSketch)
119     return;
120
121   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
122
123   boost::shared_ptr<GeomDataAPI_Point> aC = 
124     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
125   boost::shared_ptr<GeomDataAPI_Dir> aX = 
126     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
127   boost::shared_ptr<GeomDataAPI_Dir> aY = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
129
130   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
131     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
132
133   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
134   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
135 }
136
137 void PartSet_Tools::intersectLines(double theX0, double theY0, double theX1, double theY1,
138                                    double theX2, double theY2, double theX3, double theY3,
139                                    double& theX, double& theY)
140 {
141   double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
142   double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
143
144   double aT2 = 0;
145   if (aV1  != 0 && aV2 != 0)
146     aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
147   else
148     aT2 = DBL_MAX;
149
150   theX  = theX2 + aT2*aW1;
151   theY = theY2 + aT2*aW2;
152
153   // the coordinates of two lines are on the common line
154   //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
155   Standard_Real aPrec = PRECISION_TOLERANCE;
156   if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) {
157     projectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);    
158   }
159 }
160
161 void PartSet_Tools::projectPointOnLine(double theX1, double theY1, double theX2, double theY2,
162                                        double thePointX, double thePointY, double& theX, double& theY)
163 {
164   theX = theY = 0;
165
166   Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(theX1, theY1, 0),
167                                      gp_Dir(gp_Vec(gp_Pnt(theX1, theY1, 0), gp_Pnt(theX2, theY2, 0))));
168   GeomAPI_ProjectPointOnCurve aProj(gp_Pnt(thePointX, thePointY, 0), aLine);
169
170   Standard_Integer aNbPoint = aProj.NbPoints();
171   if (aNbPoint > 0) {
172     gp_Pnt aPoint = aProj.Point(1);
173     theX = aPoint.X();
174     theY = aPoint.Y();
175   }
176 }
177
178 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
179                                                                       FeaturePtr theSketch,
180                                                                       FeaturePtr theFeature)
181 {
182   PartSet_FeaturePrs* aFeaturePrs;
183
184   if (theKind == PartSet_FeaturePointPrs::getKind()) {
185     aFeaturePrs = new PartSet_FeaturePointPrs(theSketch);
186   }
187   else if (theKind == PartSet_FeatureLinePrs::getKind()) {
188     aFeaturePrs = new PartSet_FeatureLinePrs(theSketch);
189   }
190   else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
191     aFeaturePrs = new PartSet_FeatureCirclePrs(theSketch);
192   }
193   else if (theKind == PartSet_FeatureArcPrs::getKind()) {
194     aFeaturePrs = new PartSet_FeatureArcPrs(theSketch);
195   }
196   if (theFeature)
197     aFeaturePrs->init(theFeature, FeaturePtr());
198
199   return boost::shared_ptr<PartSet_FeaturePrs>(aFeaturePrs);
200 }
201
202 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
203                                          FeaturePtr theSketch,
204                                          const std::list<XGUI_ViewerPrs>& theFeatures)
205 {
206   double aX, anY;
207   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
208   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
209
210   FeaturePtr aFeature;
211   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
212
213   FeaturePtr aDeltaFeature;   
214   double aMinDelta = -1;
215   XGUI_ViewerPrs aPrs;
216   for (; anIt != aLast; anIt++) {
217     aPrs = *anIt;
218     if (!aPrs.feature())
219       continue;
220     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
221     if (aMinDelta < 0 || aMinDelta > aDelta) {
222       aMinDelta = aDelta;
223       aDeltaFeature = aPrs.feature();
224     }
225   }
226   return aDeltaFeature;
227 }
228
229 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
230                                       double theX, double theY)
231 {
232   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
233                                            theFeature->getKind(), FeaturePtr(), theFeature);
234   double aDelta = 0;
235   if (aFeaturePrs)
236     aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
237
238   return aDelta;
239 }
240
241 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
242 {
243   if (!theFeature)
244     return;
245
246   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
247                                            theFeature->getKind(), FeaturePtr(), theFeature);
248   if (aFeaturePrs)
249   aFeaturePrs->move(theDeltaX, theDeltaY);
250 }
251
252 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
253 {
254   return ModelAPI_PluginManager::get()->rootDocument();
255 }
256
257 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
258                                     const std::string& theAttribute)
259 {
260   if (!theFeature)
261     return;
262   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
263   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
264         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
265   if (aPoint)
266     aPoint->setValue(theX, theY);
267 }
268
269 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
270                                     const std::string& theAttribute)
271 {
272   if (!theFeature)
273     return;
274   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
275   boost::shared_ptr<ModelAPI_AttributeDouble> anAttribute =
276         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
277   if (anAttribute)
278     anAttribute->setValue(theValue);
279 }
280
281 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
282                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
283                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
284 {
285   boost::shared_ptr<ModelAPI_Document> aDoc = document();
286   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
287
288   if (theSketch) {
289     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
290                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
291     aSketch->addSub(aFeature);
292   }
293
294   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
295
296   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
297         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
298   aRef1->setAttr(thePoint1);
299
300   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
301         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
302   aRef2->setAttr(thePoint2);
303
304   if (aFeature) // TODO: generate an error if feature was not created
305     aFeature->execute();
306 }
307
308 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
309                                                                 double theX, double theY)
310 {
311   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
312                                            theFeature->getKind(), FeaturePtr(), theFeature);
313
314   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
315   if (aFeaturePrs)
316     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
317
318   return aPoint2D;
319 }