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::IntersectLines(double theX0, double theY0, double theX1, double theY1,
107 double theX2, double theY2, double theX3, double theY3,
108 double& theX, double& theY)
110 double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
111 double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
114 if (aV1 != 0 && aV2 != 0)
115 aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
119 theX = theX2 + aT2*aW1;
120 theY = theY2 + aT2*aW2;
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);
130 void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2, double theY2,
131 double thePointX, double thePointY, double& theX, double& theY)
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);
139 Standard_Integer aNbPoint = aProj.NbPoints();
141 gp_Pnt aPoint = aProj.Point(1);
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)
153 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, theView);
154 PartSet_Tools::ConvertTo2D(aPoint, theSketch, theView, aX, anY);
156 boost::shared_ptr<ModelAPI_Feature> aFeature;
157 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
159 boost::shared_ptr<ModelAPI_Feature> aDeltaFeature;
160 double aMinDelta = -1;
162 for (; anIt != aLast; anIt++) {
166 double aDelta = DistanceToPoint(aPrs.feature(), aX, anY);
167 if (aMinDelta < 0 || aMinDelta > aDelta) {
169 aDeltaFeature = aPrs.feature();
172 return aDeltaFeature;
175 double PartSet_Tools::DistanceToPoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
176 double theX, double theY)
179 if (theFeature->getKind() != "SketchLine")
182 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
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));
190 PartSet_Tools::ProjectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
192 aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));