]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
2c7a93b069bab6d65eaaab70c0fd1612d4444d42
[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, boost::shared_ptr<ModelAPI_Feature> 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::IntersectLines(double theX0, double theY0, double theX1, double theY1,
107                                    double theX2, double theY2, double theX3, double theY3,
108                                    double& theX, double& theY)
109 {
110   double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
111   double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
112
113   double aT2 = 0;
114   if (aV1  != 0 && aV2 != 0)
115     aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
116   else
117     aT2 = DBL_MAX;
118
119   theX  = theX2 + aT2*aW1;
120   theY = theY2 + aT2*aW2;
121
122   // the coordinates of two lines are on the common line
123   //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
124   Standard_Real aPrec = PRECISION_TOLERANCE;
125   if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) {
126     ProjectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);    
127   }
128 }
129
130 void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2, double theY2,
131                                        double thePointX, double thePointY, double& theX, double& theY)
132 {
133   theX = theY = 0;
134
135   Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(theX1, theY1, 0),
136                                      gp_Dir(gp_Vec(gp_Pnt(theX1, theY1, 0), gp_Pnt(theX2, theY2, 0))));
137   GeomAPI_ProjectPointOnCurve aProj(gp_Pnt(thePointX, thePointY, 0), aLine);
138
139   Standard_Integer aNbPoint = aProj.NbPoints();
140   if (aNbPoint > 0) {
141     gp_Pnt aPoint = aProj.Point(1);
142     theX = aPoint.X();
143     theY = aPoint.Y();
144   }
145 }
146
147 boost::shared_ptr<ModelAPI_Feature> PartSet_Tools::NearestFeature(QPoint thePoint,
148                                                    Handle_V3d_View theView,
149                                                    boost::shared_ptr<ModelAPI_Feature> theSketch,
150                                                    const std::list<XGUI_ViewerPrs>& theFeatures)
151 {
152   double aX, anY;
153   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, theView);
154   PartSet_Tools::ConvertTo2D(aPoint, theSketch, theView, aX, anY);
155
156   boost::shared_ptr<ModelAPI_Feature> aFeature;
157   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
158
159   boost::shared_ptr<ModelAPI_Feature> aDeltaFeature;   
160   double aMinDelta = -1;
161   XGUI_ViewerPrs aPrs;
162   for (; anIt != aLast; anIt++) {
163     aPrs = *anIt;
164     if (!aPrs.feature())
165       continue;
166     double aDelta = DistanceToPoint(aPrs.feature(), aX, anY);
167     if (aMinDelta < 0 || aMinDelta > aDelta) {
168       aMinDelta = aDelta;
169       aDeltaFeature = aPrs.feature();
170     }
171   }
172   return aDeltaFeature;
173 }
174
175 double PartSet_Tools::DistanceToPoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
176                                       double theX, double theY)
177 {
178   double aDelta = 0;
179   if (theFeature->getKind() != "SketchLine")
180     return aDelta;
181
182   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
183
184   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
185         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
186   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
187         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
188
189   double aX, anY;
190   PartSet_Tools::ProjectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
191
192   aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));
193
194   return aDelta;
195 }