1 // File: PartSet_Tools.h
2 // Created: 28 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_Tools.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
10 #include <GeomDataAPI_Point.h>
11 #include <GeomDataAPI_Dir.h>
12 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAPI_Dir.h>
15 #include <GeomAPI_XYZ.h>
17 #include <SketchPlugin_Sketch.h>
18 #include <SketchPlugin_Line.h>
20 #include <XGUI_ViewerPrs.h>
22 #include <V3d_View.hxx>
24 #include <ProjLib.hxx>
26 #include <Geom_Line.hxx>
27 #include <GeomAPI_ProjectPointOnCurve.hxx>
33 const double PRECISION_TOLERANCE = 0.000001;
35 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
40 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
41 theView->Eye(XEye, YEye, ZEye);
43 theView->At(XAt, YAt, ZAt);
44 gp_Pnt EyePoint(XEye, YEye, ZEye);
45 gp_Pnt AtPoint(XAt, YAt, ZAt);
47 gp_Vec EyeVector(EyePoint, AtPoint);
48 gp_Dir EyeDir(EyeVector);
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);
55 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
56 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
60 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, boost::shared_ptr<ModelAPI_Feature> theSketch,
61 Handle(V3d_View) theView, double& theX, double& theY)
66 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
67 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
69 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
70 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
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));
77 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
78 gp_Vec aVec(anOriginPnt, thePoint);
80 if (!theView.IsNull())
82 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
83 theView->Eye(XEye, YEye, ZEye);
85 theView->At(XAt, YAt, ZAt);
86 gp_Pnt EyePoint(XEye, YEye, ZEye);
87 gp_Pnt AtPoint(XAt, YAt, ZAt);
89 gp_Vec anEyeVec(EyePoint, AtPoint);
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());
96 double aDen = anEyeVec * aNormalVec;
97 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
99 gp_Vec aDeltaVec = anEyeVec*aLVec;
100 aVec = aVec - aDeltaVec;
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();
106 void PartSet_Tools::convertTo3D(const double theX, const double theY,
107 boost::shared_ptr<ModelAPI_Feature> theSketch,
113 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
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));
122 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
123 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
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());
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)
133 double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
134 double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
137 if (aV1 != 0 && aV2 != 0)
138 aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
142 theX = theX2 + aT2*aW1;
143 theY = theY2 + aT2*aW2;
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);
153 void PartSet_Tools::projectPointOnLine(double theX1, double theY1, double theX2, double theY2,
154 double thePointX, double thePointY, double& theX, double& theY)
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);
162 Standard_Integer aNbPoint = aProj.NbPoints();
164 gp_Pnt aPoint = aProj.Point(1);
170 boost::shared_ptr<ModelAPI_Feature> PartSet_Tools::nearestFeature(QPoint thePoint,
171 Handle_V3d_View theView,
172 boost::shared_ptr<ModelAPI_Feature> theSketch,
173 const std::list<XGUI_ViewerPrs>& theFeatures)
176 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
177 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
179 boost::shared_ptr<ModelAPI_Feature> aFeature;
180 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
182 boost::shared_ptr<ModelAPI_Feature> aDeltaFeature;
183 double aMinDelta = -1;
185 for (; anIt != aLast; anIt++) {
189 double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
190 if (aMinDelta < 0 || aMinDelta > aDelta) {
192 aDeltaFeature = aPrs.feature();
195 return aDeltaFeature;
198 double PartSet_Tools::distanceToPoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
199 double theX, double theY)
202 if (theFeature->getKind() != SKETCH_LINE_KIND)
205 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
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));
213 PartSet_Tools::projectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
215 aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));