Salome HOME
Fix for the feature #6: Update of objects (T 1.2): For objects which need updating...
[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 #include "CurveCreator.hxx"
22 #include "CurveCreator_UtilsICurve.hxx"
23
24 #include <GEOMUtils.hxx>
25
26 #include <gp_Pln.hxx>
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Compound.hxx>
33
34 #include <AIS_ListOfInteractive.hxx>
35 #include <AIS_ListIteratorOfListOfInteractive.hxx>
36 #include <AIS_Shape.hxx>
37 #include <AIS_Point.hxx>
38 #include <AIS_Line.hxx>
39 #include <AIS_Trihedron.hxx>
40
41 #include <Geom_Point.hxx>
42 #include <Geom_BSplineCurve.hxx>
43 #include <Geom_Line.hxx>
44
45 #include <TopExp_Explorer.hxx>
46 #include <GeomAPI_ProjectPointOnCurve.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48
49 #include <BRep_Tool.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRepBuilderAPI_MakeVertex.hxx>
52 #include <BRepBuilderAPI_MakeEdge.hxx>
53 #include <BRepBuilderAPI_MakeWire.hxx>
54
55 #include <TColgp_HArray1OfPnt.hxx>
56 #include <GeomAPI_Interpolate.hxx>
57
58 #include <ProjLib.hxx>
59 #include <ElSLib.hxx>
60
61 #include "CurveCreator_ICurve.hxx"
62
63 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
64 const int    SCENE_PIXEL_TOLERANCE = 10;
65
66 //=======================================================================
67 // function : ConvertClickToPoint()
68 // purpose  : Returns the point clicked in 3D view
69 //=======================================================================
70 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
71                                               Handle(V3d_View) theView,
72                                               int& x, int& y )
73 {
74   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
75 }
76
77
78 //=======================================================================
79 // function : ConvertClickToPoint()
80 // purpose  : Returns the point clicked in 3D view
81 //=======================================================================
82 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
83 {
84   return GEOMUtils::ConvertClickToPoint( x, y, aView );
85 }
86
87 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
88                                          TopoDS_Shape& theShape )
89 {
90   BRep_Builder aBuilder;
91   TopoDS_Compound aComp;
92   aBuilder.MakeCompound( aComp );
93   for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
94   {
95     int theISection = iSection;
96
97     CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
98     int aPointSize = theCurve->getNbPoints( theISection );
99     bool aSectIsClosed = theCurve->isClosed( theISection );
100     bool isPolyline = aSectType == CurveCreator::Polyline;
101     int iPoint = 0;
102     gp_Pnt aPrevPoint, aPoint;
103     if ( aPointSize == 1 ) {
104       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
105       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
106       aBuilder.Add( aComp, aVertex );
107     }
108     else if ( aPointSize > 1 ) {
109       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
110       int aHIndex = 1;
111
112       TopoDS_Edge aPointEdge;
113       TopoDS_Vertex aVertex;
114       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
115       aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
116       aBuilder.Add( aComp, aVertex );
117       aHCurvePoints->SetValue(aHIndex++, aPoint);
118       aPrevPoint = aPoint;
119       iPoint++;
120       for( ; iPoint < aPointSize; iPoint++ ) {
121         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
122         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
123         aBuilder.Add( aComp, aVertex );
124         aHCurvePoints->SetValue(aHIndex++, aPoint);
125         if ( isPolyline ) {
126           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
127           aBuilder.Add( aComp, aPointEdge );
128         }
129         aPrevPoint = aPoint;
130       }
131       if( aSectIsClosed && ( aPointSize > 2 ) ) {
132         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
133         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
134         aBuilder.Add( aComp, aVertex );
135         if ( isPolyline ) {
136           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
137           aBuilder.Add( aComp, aPointEdge );
138         }
139       }
140       if( !isPolyline ) {
141         // compute BSpline
142         Handle(Geom_BSplineCurve) aBSplineCurve;
143         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
144         aGBC.Perform();
145         if ( aGBC.IsDone() )
146           aBSplineCurve = aGBC.Curve();
147         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
148         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
149         aBuilder.Add( aComp, aWire );
150       }
151     }
152   }
153   theShape = aComp;
154 }
155
156
157 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
158 {
159   std::list<float> aSelectedPoints;
160
161   gp_Pnt aPnt;
162   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
163     TopoDS_Vertex aVertex;
164     TopoDS_Shape aShape = theContext->SelectedShape();
165     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
166       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
167     else {
168       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
169       if ( !anOwner.IsNull() ) {
170         Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
171         if ( !anAIS.IsNull() ) {
172           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
173           if ( !aPoint.IsNull() )
174             aVertex = TopoDS::Vertex( aPoint->Vertex() );
175         }
176         if ( aVertex.IsNull() ) {
177           // the following happens if there are no points in the current curve, there is only a shape
178           /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
179           if ( aBrepOwner.IsNull() )
180             continue;
181           if ( aBrepOwner->HasShape() ) {
182             const TopoDS_Shape& aShape = aBrepOwner->Shape();
183             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
184             {
185               aVertex = TopoDS::Vertex( aShape );
186             }
187           }*/
188         }
189       }
190     }
191     if ( aVertex.IsNull() )
192       continue;
193     aPnt = BRep_Tool::Pnt( aVertex );
194     aSelectedPoints.push_back( aPnt.X() );
195     aSelectedPoints.push_back( aPnt.Y() );
196     aSelectedPoints.push_back( aPnt.Z() );
197   }
198
199   return aSelectedPoints;
200 }
201 //=======================================================================
202 // function : setLocalPointContext
203 // purpose  : Open/close the viewer local context
204 //=======================================================================
205 //#define USE_GLOBAL_SELECTION
206 void CurveCreator_Utils::setLocalPointContext(
207                                               Handle(AIS_InteractiveContext) theContext,
208                                               const bool theOpen )
209 {
210 #ifdef USE_GLOBAL_SELECTION
211   return;
212 #endif
213   if ( !theContext )
214     return;
215
216   if ( theOpen ) {
217     // Open local context if there is no one
218     if ( !theContext->HasOpenedContext() ) {
219       theContext->ClearCurrents( false );
220       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
221     }
222     AIS_ListOfInteractive aList;
223     theContext->DisplayedObjects( aList );
224     int aLSize = 0;
225     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
226       aLSize++;
227
228     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
229     {
230       Handle(AIS_InteractiveObject) anAIS = it.Value();
231       if ( !anAIS.IsNull() )
232       {
233         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
234         {
235           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
236           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
237           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
238         }
239         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
240         {
241           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
242           theContext->Activate( anAIS, TopAbs_VERTEX );
243         }
244       }
245       continue;
246     }
247   }
248   else {
249     if ( theContext->HasOpenedContext() )
250       theContext->CloseAllContexts();
251   }
252 }
253
254 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
255                                              Handle(V3d_View) theView,
256                                              const int theX, const int theY,
257                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
258                                              gp_Pnt& thePoint2 )
259 {
260   bool isFoundPoint = false;
261   if ( theContext.IsNull() )
262     return isFoundPoint;
263
264   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
265         theContext->NextSelected() ) {
266     TopoDS_Shape aTShape = theContext->SelectedShape();
267     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
268       continue;
269     else {
270       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
271       if ( anOwner.IsNull() )
272         continue;
273       const TopLoc_Location& aLocation = anOwner->Location();
274       Handle(AIS_InteractiveObject) anAIS =
275                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
276       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
277                                                         thePoint1, thePoint2 );
278     }
279   }
280   return isFoundPoint;
281 }
282
283 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
284                                         Handle(AIS_InteractiveObject) theObject,
285                                         const int theX, const int theY,
286                                         gp_Pnt& thePoint,
287                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
288 {
289   bool isFound = false;
290
291   if ( theObject.IsNull() || theView.IsNull() )
292     return isFound;
293
294   gp_Pnt aPoint;
295   Standard_Real aParameter;
296   gp_Pnt aPnt1, aPnt2;
297   Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
298   if ( !aLine.IsNull() ) {
299     const Handle(Geom_Line) aGLine = aLine->Line();
300     isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
301     if ( isFound ) {
302       aPoint = aGLine->Value( aParameter );
303
304       Handle(Geom_Point) aPStart;
305       Handle(Geom_Point) aPEnd;
306       aLine->Points( aPStart, aPEnd );
307       aPnt1 = aPStart->Pnt();
308       aPnt2 = aPEnd->Pnt();
309
310       // in case of Geom line a projection is performed to the infinite line,
311       // so it is necessary to bound it by the line size
312       Bnd_Box aLineBox;
313       aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
314       isFound = !aLineBox.IsOut( aPoint );
315     }
316   }
317   else {
318     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
319     if ( !aShape.IsNull() ) {
320       const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
321       if ( !aWire.IsNull() ) {
322         TopExp_Explorer anExp( aWire, TopAbs_EDGE );
323         for ( ; anExp.More(); anExp.Next())
324         {
325           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
326           if ( !anEdge.IsNull() ) {
327             Standard_Real aFirst, aLast;
328             Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
329
330             if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
331               Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
332               if ( !aBSplineCurve.IsNull() ) {
333                 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
334                 if ( isFound ) {
335                   aPoint = aBSplineCurve->Value( aParameter );
336                   Standard_Integer anI1, anI2;
337                   aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
338
339                   aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
340                   aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
341                 }
342               }
343             }
344           }
345         }
346       }
347     }
348   }
349   if ( isFound ) {
350     thePoint = aPoint;
351     thePoint1 = aPnt1;
352     thePoint2 = aPnt2;
353   }
354   return isFound;
355 }
356
357 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
358                                                  const int theX, const int theY,
359                                                  const Handle(Geom_Curve)& theCurve,
360                                                  Standard_Real& theParameter )
361 {
362   bool isFound = false;
363   if ( theView.IsNull() )
364     return isFound;
365
366   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
367
368   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
369   Standard_Integer aNbPoint = aProj.NbPoints();
370   if (aNbPoint > 0) {
371     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
372       gp_Pnt aNewPoint = aProj.Point( j );
373       theParameter = aProj.Parameter( j );
374
375       int aX, anY;
376       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
377
378       int aXDelta = abs( aX - theX );
379       int anYDelta = abs( anY - theY );
380       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
381     }
382   }
383   return isFound;
384 }