Salome HOME
1775167850ea90c1f35e20cca96947adf9fc4922
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Utils.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CurveCreator_Utils.h"
21
22 #include <GEOMUtils.hxx>
23
24 #include <gp_Pln.hxx>
25
26 #include <TopoDS.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <TopoDS_Edge.hxx>
30
31 #include <AIS_ListOfInteractive.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_Shape.hxx>
34 #include <AIS_Point.hxx>
35 #include <AIS_Line.hxx>
36 #include <AIS_Trihedron.hxx>
37
38 #include <Geom_Point.hxx>
39 #include <Geom_BSplineCurve.hxx>
40 #include <Geom_Line.hxx>
41
42 #include <TopExp_Explorer.hxx>
43 #include <BRep_Tool.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 #include <SelectMgr_EntityOwner.hxx>
46
47 #include <ProjLib.hxx>
48 #include <ElSLib.hxx>
49
50 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
51 const int SCENE_PIXEL_TOLERANCE = 10;
52
53 //=======================================================================
54 // function : ConvertClickToPoint()
55 // purpose  : Returns the point clicked in 3D view
56 //=======================================================================
57 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
58                                               Handle(V3d_View) theView,
59                                               int& x, int& y )
60 {
61   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
62 }
63
64
65 //=======================================================================
66 // function : ConvertClickToPoint()
67 // purpose  : Returns the point clicked in 3D view
68 //=======================================================================
69 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
70 {
71   return GEOMUtils::ConvertClickToPoint( x, y, aView );
72   /*
73   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
74   aView->Eye( XEye, YEye, ZEye );
75
76   aView->At( XAt, YAt, ZAt );
77   gp_Pnt EyePoint( XEye, YEye, ZEye );
78   gp_Pnt AtPoint( XAt, YAt, ZAt );
79
80   gp_Vec EyeVector( EyePoint, AtPoint );
81   gp_Dir EyeDir( EyeVector );
82
83   gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
84   Standard_Real X, Y, Z;
85   aView->Convert( x, y, X, Y, Z );
86   gp_Pnt ConvertedPoint( X, Y, Z );
87
88   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
89   gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
90   return ResultPoint;
91   */
92 }
93
94 //=======================================================================
95 // function : setLocalPointContext
96 // purpose  : Open/close the viewer local context
97 //=======================================================================
98 void CurveCreator_Utils::setLocalPointContext(
99                                               Handle(AIS_InteractiveContext) theContext,
100                                               const bool theOpen )
101 {
102   if ( !theContext )
103     return;
104
105   if ( theOpen ) {
106     // Open local context if there is no one
107     if ( !theContext->HasOpenedContext() ) {
108       theContext->ClearCurrents( false );
109       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
110     }
111     AIS_ListOfInteractive aList;
112     theContext->DisplayedObjects( aList );
113     int aLSize = 0;
114     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
115       aLSize++;
116
117     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
118     {
119       Handle(AIS_InteractiveObject) anAIS = it.Value();
120       if ( !anAIS.IsNull() )
121       {
122         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
123         {
124           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
125           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
126           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
127         }
128         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
129         {
130           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
131           theContext->Activate( anAIS, TopAbs_VERTEX );
132         }
133       }
134       continue;
135     }
136   }
137   else {
138     if ( theContext->HasOpenedContext() )
139       theContext->CloseAllContexts();
140   }
141 }
142
143 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
144                                              Handle(V3d_View) theView,
145                                              const int theX, const int theY,
146                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
147                                              gp_Pnt& thePoint2 )
148 {
149   bool isFoundPoint = false;
150   if ( theContext.IsNull() )
151     return isFoundPoint;
152
153   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
154         theContext->NextSelected() ) {
155     TopoDS_Shape aTShape = theContext->SelectedShape();
156     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
157       continue;
158     else {
159       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
160       if ( anOwner.IsNull() )
161         continue;
162       const TopLoc_Location& aLocation = anOwner->Location();
163       Handle(AIS_InteractiveObject) anAIS =
164                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
165       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
166                                                         thePoint1, thePoint2 );
167     }
168   }
169   return isFoundPoint;
170 }
171
172 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
173                                         Handle(AIS_InteractiveObject) theObject,
174                                         const int theX, const int theY,
175                                         gp_Pnt& thePoint,
176                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
177 {
178   bool isFound = false;
179
180   if ( theObject.IsNull() || theView.IsNull() )
181     return isFound;
182
183   gp_Pnt aPoint;
184   Standard_Real aParameter;
185   gp_Pnt aPnt1, aPnt2;
186   Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
187   if ( !aLine.IsNull() ) {
188     const Handle(Geom_Line) aGLine = aLine->Line();
189     isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
190     if ( isFound ) {
191       aPoint = aGLine->Value( aParameter );
192
193       Handle(Geom_Point) aPStart;
194       Handle(Geom_Point) aPEnd;
195       aLine->Points( aPStart, aPEnd );
196       aPnt1 = aPStart->Pnt();
197       aPnt2 = aPEnd->Pnt();
198
199       // in case of Geom line a projection is performed to the infinite line,
200       // so it is necessary to bound it by the line size
201       Bnd_Box aLineBox;
202       aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
203       isFound = !aLineBox.IsOut( aPoint );
204     }
205   }
206   else {
207     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
208     if ( !aShape.IsNull() ) {
209       const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
210       if ( !aWire.IsNull() ) {
211         TopExp_Explorer anExp( aWire, TopAbs_EDGE );
212         for ( ; anExp.More(); anExp.Next())
213         {
214           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
215           if ( !anEdge.IsNull() ) {
216             Standard_Real aFirst, aLast;
217             Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
218
219             if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
220               Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
221               if ( !aBSplineCurve.IsNull() ) {
222                 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
223                 if ( isFound ) {
224                   aPoint = aBSplineCurve->Value( aParameter );
225                   Standard_Integer anI1, anI2;
226                   aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
227
228                   aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
229                   aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
230                 }
231               }
232             }
233           }
234         }
235       }
236     }
237   }
238   if ( isFound ) {
239     thePoint = aPoint;
240     thePoint1 = aPnt1;
241     thePoint2 = aPnt2;
242   }
243   return isFound;
244 }
245
246 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
247                                                  const int theX, const int theY,
248                                                  const Handle(Geom_Curve)& theCurve,
249                                                  Standard_Real& theParameter )
250 {
251   bool isFound = false;
252   if ( theView.IsNull() )
253     return isFound;
254
255   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
256
257   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
258   Standard_Integer aNbPoint = aProj.NbPoints();
259   if (aNbPoint > 0) {
260     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
261       gp_Pnt aNewPoint = aProj.Point( j );
262       theParameter = aProj.Parameter( j );
263
264       int aX, anY;
265       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
266
267       int aXDelta = abs( aX - theX );
268       int anYDelta = abs( anY - theY );
269       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
270     }
271   }
272   return isFound;
273 }