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