Salome HOME
1fa61b3378265aaa96134014f620f2de147b8980
[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
74 //=======================================================================
75 // function : setLocalPointContext
76 // purpose  : Open/close the viewer local context
77 //=======================================================================
78 void CurveCreator_Utils::setLocalPointContext(
79                                               Handle(AIS_InteractiveContext) theContext,
80                                               const bool theOpen )
81 {
82   if ( !theContext )
83     return;
84
85   if ( theOpen ) {
86     // Open local context if there is no one
87     if ( !theContext->HasOpenedContext() ) {
88       theContext->ClearCurrents( false );
89       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
90     }
91     AIS_ListOfInteractive aList;
92     theContext->DisplayedObjects( aList );
93     int aLSize = 0;
94     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
95       aLSize++;
96
97     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
98     {
99       Handle(AIS_InteractiveObject) anAIS = it.Value();
100       if ( !anAIS.IsNull() )
101       {
102         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
103         {
104           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
105           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
106           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
107         }
108         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
109         {
110           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
111           theContext->Activate( anAIS, TopAbs_VERTEX );
112         }
113       }
114       continue;
115     }
116   }
117   else {
118     if ( theContext->HasOpenedContext() )
119       theContext->CloseAllContexts();
120   }
121 }
122
123 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
124                                              Handle(V3d_View) theView,
125                                              const int theX, const int theY,
126                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
127                                              gp_Pnt& thePoint2 )
128 {
129   bool isFoundPoint = false;
130   if ( theContext.IsNull() )
131     return isFoundPoint;
132
133   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
134         theContext->NextSelected() ) {
135     TopoDS_Shape aTShape = theContext->SelectedShape();
136     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
137       continue;
138     else {
139       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
140       if ( anOwner.IsNull() )
141         continue;
142       const TopLoc_Location& aLocation = anOwner->Location();
143       Handle(AIS_InteractiveObject) anAIS =
144                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
145       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
146                                                         thePoint1, thePoint2 );
147     }
148   }
149   return isFoundPoint;
150 }
151
152 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
153                                         Handle(AIS_InteractiveObject) theObject,
154                                         const int theX, const int theY,
155                                         gp_Pnt& thePoint,
156                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
157 {
158   bool isFound = false;
159
160   if ( theObject.IsNull() || theView.IsNull() )
161     return isFound;
162
163   gp_Pnt aPoint;
164   Standard_Real aParameter;
165   gp_Pnt aPnt1, aPnt2;
166   Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
167   if ( !aLine.IsNull() ) {
168     const Handle(Geom_Line) aGLine = aLine->Line();
169     isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
170     if ( isFound ) {
171       aPoint = aGLine->Value( aParameter );
172
173       Handle(Geom_Point) aPStart;
174       Handle(Geom_Point) aPEnd;
175       aLine->Points( aPStart, aPEnd );
176       aPnt1 = aPStart->Pnt();
177       aPnt2 = aPEnd->Pnt();
178
179       // in case of Geom line a projection is performed to the infinite line,
180       // so it is necessary to bound it by the line size
181       Bnd_Box aLineBox;
182       aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
183       isFound = !aLineBox.IsOut( aPoint );
184     }
185   }
186   else {
187     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
188     if ( !aShape.IsNull() ) {
189       const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
190       if ( !aWire.IsNull() ) {
191         TopExp_Explorer anExp( aWire, TopAbs_EDGE );
192         for ( ; anExp.More(); anExp.Next())
193         {
194           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
195           if ( !anEdge.IsNull() ) {
196             Standard_Real aFirst, aLast;
197             Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
198
199             if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
200               Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
201               if ( !aBSplineCurve.IsNull() ) {
202                 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
203                 if ( isFound ) {
204                   aPoint = aBSplineCurve->Value( aParameter );
205                   Standard_Integer anI1, anI2;
206                   aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
207
208                   aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
209                   aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
210                 }
211               }
212             }
213           }
214         }
215       }
216     }
217   }
218   if ( isFound ) {
219     thePoint = aPoint;
220     thePoint1 = aPnt1;
221     thePoint2 = aPnt2;
222   }
223   return isFound;
224 }
225
226 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
227                                                  const int theX, const int theY,
228                                                  const Handle(Geom_Curve)& theCurve,
229                                                  Standard_Real& theParameter )
230 {
231   bool isFound = false;
232   if ( theView.IsNull() )
233     return isFound;
234
235   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
236
237   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
238   Standard_Integer aNbPoint = aProj.NbPoints();
239   if (aNbPoint > 0) {
240     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
241       gp_Pnt aNewPoint = aProj.Point( j );
242       theParameter = aProj.Parameter( j );
243
244       int aX, anY;
245       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
246
247       int aXDelta = abs( aX - theX );
248       int anYDelta = abs( anY - theY );
249       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
250     }
251   }
252   return isFound;
253 }