Salome HOME
Replace boost::shared_ptr<ModelAPI_Feature> on FeaturePtr
[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
10 #include <GeomDataAPI_Point.h>
11 #include <GeomDataAPI_Dir.h>
12 #include <GeomDataAPI_Point2D.h>
13
14 #include <GeomAPI_Dir.h>
15 #include <GeomAPI_XYZ.h>
16
17 #include <SketchPlugin_Sketch.h>
18 #include <SketchPlugin_Line.h>
19
20 #include <XGUI_ViewerPrs.h>
21
22 #include <V3d_View.hxx>
23 #include <gp_Pln.hxx>
24 #include <ProjLib.hxx>
25 #include <ElSLib.hxx>
26 #include <Geom_Line.hxx>
27 #include <GeomAPI_ProjectPointOnCurve.hxx>
28
29 #ifdef _DEBUG
30 #include <QDebug>
31 #endif
32
33 const double PRECISION_TOLERANCE = 0.000001;
34
35 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
36 {
37   if (theView.IsNull())
38     return gp_Pnt();
39
40   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
41   theView->Eye(XEye, YEye, ZEye);
42
43   theView->At(XAt, YAt, ZAt);
44   gp_Pnt EyePoint(XEye, YEye, ZEye);
45   gp_Pnt AtPoint(XAt, YAt, ZAt);
46
47   gp_Vec EyeVector(EyePoint, AtPoint);
48   gp_Dir EyeDir(EyeVector);
49
50   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
51   Standard_Real X, Y, Z;
52   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
53   gp_Pnt ConvertedPoint(X, Y, Z);
54
55   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
56   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
57   return ResultPoint;
58 }
59
60 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
61                                 Handle(V3d_View) theView, double& theX, double& theY)
62 {
63   if (!theSketch)
64     return;
65
66   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
67   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
68
69   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
70     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
71
72   boost::shared_ptr<GeomDataAPI_Dir> aX = 
73     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
74   boost::shared_ptr<GeomDataAPI_Dir> anY = 
75     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
76
77   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
78   gp_Vec aVec(anOriginPnt, thePoint);
79
80   if (!theView.IsNull())
81   {
82     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
83     theView->Eye(XEye, YEye, ZEye);
84
85     theView->At(XAt, YAt, ZAt);
86     gp_Pnt EyePoint(XEye, YEye, ZEye);
87     gp_Pnt AtPoint(XAt, YAt, ZAt);
88
89     gp_Vec anEyeVec(EyePoint, AtPoint);
90     anEyeVec.Normalize();
91
92     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
93                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
94     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
95
96     double aDen = anEyeVec * aNormalVec;
97     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
98
99     gp_Vec aDeltaVec = anEyeVec*aLVec;
100     aVec = aVec - aDeltaVec;
101   }
102   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
103   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
104 }
105
106 void PartSet_Tools::convertTo3D(const double theX, const double theY,
107                                 FeaturePtr theSketch,
108                                 gp_Pnt& thePoint)
109 {
110   if (!theSketch)
111     return;
112
113   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
114
115   boost::shared_ptr<GeomDataAPI_Point> aC = 
116     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
117   boost::shared_ptr<GeomDataAPI_Dir> aX = 
118     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
119   boost::shared_ptr<GeomDataAPI_Dir> aY = 
120     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
121
122   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
123     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
124
125   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
126   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
127 }
128
129 void PartSet_Tools::intersectLines(double theX0, double theY0, double theX1, double theY1,
130                                    double theX2, double theY2, double theX3, double theY3,
131                                    double& theX, double& theY)
132 {
133   double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
134   double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
135
136   double aT2 = 0;
137   if (aV1  != 0 && aV2 != 0)
138     aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
139   else
140     aT2 = DBL_MAX;
141
142   theX  = theX2 + aT2*aW1;
143   theY = theY2 + aT2*aW2;
144
145   // the coordinates of two lines are on the common line
146   //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
147   Standard_Real aPrec = PRECISION_TOLERANCE;
148   if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) {
149     projectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);    
150   }
151 }
152
153 void PartSet_Tools::projectPointOnLine(double theX1, double theY1, double theX2, double theY2,
154                                        double thePointX, double thePointY, double& theX, double& theY)
155 {
156   theX = theY = 0;
157
158   Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(theX1, theY1, 0),
159                                      gp_Dir(gp_Vec(gp_Pnt(theX1, theY1, 0), gp_Pnt(theX2, theY2, 0))));
160   GeomAPI_ProjectPointOnCurve aProj(gp_Pnt(thePointX, thePointY, 0), aLine);
161
162   Standard_Integer aNbPoint = aProj.NbPoints();
163   if (aNbPoint > 0) {
164     gp_Pnt aPoint = aProj.Point(1);
165     theX = aPoint.X();
166     theY = aPoint.Y();
167   }
168 }
169
170 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint,
171                                                    Handle_V3d_View theView,
172                                                    FeaturePtr theSketch,
173                                                    const std::list<XGUI_ViewerPrs>& theFeatures)
174 {
175   double aX, anY;
176   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
177   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
178
179   FeaturePtr aFeature;
180   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
181
182   FeaturePtr aDeltaFeature;   
183   double aMinDelta = -1;
184   XGUI_ViewerPrs aPrs;
185   for (; anIt != aLast; anIt++) {
186     aPrs = *anIt;
187     if (!aPrs.feature())
188       continue;
189     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
190     if (aMinDelta < 0 || aMinDelta > aDelta) {
191       aMinDelta = aDelta;
192       aDeltaFeature = aPrs.feature();
193     }
194   }
195   return aDeltaFeature;
196 }
197
198 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
199                                       double theX, double theY)
200 {
201   double aDelta = 0;
202   if (theFeature->getKind() != SKETCH_LINE_KIND)
203     return aDelta;
204
205   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
206
207   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
208         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
209   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
210         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
211
212   double aX, anY;
213   PartSet_Tools::projectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
214
215   aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));
216
217   return aDelta;
218 }