Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <V3d_View.hxx>
8 #include <gp_Pln.hxx>
9 #include <ProjLib.hxx>
10 #include <ElSLib.hxx>
11
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <SketchPlugin_Sketch.h>
17
18 #ifdef _DEBUG
19 #include <QDebug>
20 #endif
21
22 gp_Pnt PartSet_Tools::ConvertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
23 {
24   if (theView.IsNull())
25     return gp_Pnt();
26
27   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
28   theView->Eye(XEye, YEye, ZEye);
29
30   theView->At(XAt, YAt, ZAt);
31   gp_Pnt EyePoint(XEye, YEye, ZEye);
32   gp_Pnt AtPoint(XAt, YAt, ZAt);
33
34   gp_Vec EyeVector(EyePoint, AtPoint);
35   gp_Dir EyeDir(EyeVector);
36
37   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
38   Standard_Real X, Y, Z;
39   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
40   gp_Pnt ConvertedPoint(X, Y, Z);
41
42   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
43   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
44   return ResultPoint;
45 }
46
47 void PartSet_Tools::ConvertTo2D(const gp_Pnt& thePoint, boost::shared_ptr<ModelAPI_Feature> theSketch,
48                                 Handle(V3d_View) theView, double& theX, double& theY)
49 {
50   if (!theSketch)
51     return;
52
53   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
54   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
55
56   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
57     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
58
59   boost::shared_ptr<GeomDataAPI_Dir> aX = 
60     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
61   boost::shared_ptr<GeomDataAPI_Dir> anY = 
62     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
63
64   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
65   gp_Vec aVec(anOriginPnt, thePoint);
66
67   if (!theView.IsNull())
68   {
69     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
70     theView->Eye(XEye, YEye, ZEye);
71
72     theView->At(XAt, YAt, ZAt);
73     gp_Pnt EyePoint(XEye, YEye, ZEye);
74     gp_Pnt AtPoint(XAt, YAt, ZAt);
75
76     gp_Vec anEyeVec(EyePoint, AtPoint);
77     anEyeVec.Normalize();
78
79     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
80                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
81     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
82
83     double aDen = anEyeVec*aNormalVec;
84     double aLVec = aDen != 0 ? aVec*aNormalVec/aDen : aVec*aNormalVec;
85
86     gp_Vec aDeltaVec = anEyeVec*aLVec;
87     aVec = aVec - aDeltaVec;
88   }
89   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
90   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
91 }