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