Salome HOME
e5809e49101c733205e13395d6d6b4eb95ef8fa8
[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_Line.hxx>
38 #include <AIS_Trihedron.hxx>
39 #include <AIS_LocalContext.hxx>
40
41 #include <Geom_Point.hxx>
42 #include <Geom_BSplineCurve.hxx>
43 #include <Geom_Line.hxx>
44
45 #include <TopExp.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <SelectMgr_Selection.hxx>
50 #include <Select3D_SensitivePoint.hxx>
51
52 #include <BRep_Tool.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRepBuilderAPI_MakeVertex.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57
58 #include <TColgp_HArray1OfPnt.hxx>
59 #include <GeomAPI_Interpolate.hxx>
60
61 #include <ProjLib.hxx>
62 #include <ElSLib.hxx>
63
64 #include <math.h>
65
66 #include "CurveCreator_ICurve.hxx"
67
68 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
69 const int    SCENE_PIXEL_TOLERANCE = 10;
70
71 //=======================================================================
72 // function : ConvertClickToPoint()
73 // purpose  : Returns the point clicked in 3D view
74 //=======================================================================
75 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
76                                               Handle(V3d_View) theView,
77                                               int& x, int& y )
78 {
79   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
80 }
81
82
83 //=======================================================================
84 // function : ConvertClickToPoint()
85 // purpose  : Returns the point clicked in 3D view
86 //=======================================================================
87 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
88 {
89   return GEOMUtils::ConvertClickToPoint( x, y, aView );
90 }
91
92 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
93                                          TopoDS_Shape& theShape )
94 {
95   BRep_Builder aBuilder;
96   TopoDS_Compound aComp;
97   aBuilder.MakeCompound( aComp );
98   for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
99   {
100     int theISection = iSection;
101
102     CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
103     int aPointSize = theCurve->getNbPoints( theISection );
104     bool aSectIsClosed = theCurve->isClosed( theISection );
105     bool isPolyline = aSectType == CurveCreator::Polyline;
106     int iPoint = 0;
107     gp_Pnt aPrevPoint, aPoint;
108     if ( aPointSize == 1 ) {
109       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
110       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
111       aBuilder.Add( aComp, aVertex );
112     }
113     else if ( aPointSize > 1 ) {
114       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
115       int aHIndex = 1;
116
117       TopoDS_Edge aPointEdge;
118       TopoDS_Vertex aVertex;
119       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
120       aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
121       aBuilder.Add( aComp, aVertex );
122       aHCurvePoints->SetValue(aHIndex++, aPoint);
123       aPrevPoint = aPoint;
124       iPoint++;
125       for( ; iPoint < aPointSize; iPoint++ ) {
126         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
127         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
128         aBuilder.Add( aComp, aVertex );
129         aHCurvePoints->SetValue(aHIndex++, aPoint);
130         if ( isPolyline ) {
131           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
132           aBuilder.Add( aComp, aPointEdge );
133         }
134         aPrevPoint = aPoint;
135       }
136       if( aSectIsClosed && ( aPointSize > 2 ) ) {
137         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
138         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
139         aBuilder.Add( aComp, aVertex );
140         if ( isPolyline ) {
141           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
142           aBuilder.Add( aComp, aPointEdge );
143         }
144       }
145       if( !isPolyline ) {
146         // compute BSpline
147         Handle(Geom_BSplineCurve) aBSplineCurve;
148         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
149         aGBC.Perform();
150         if ( aGBC.IsDone() )
151           aBSplineCurve = aGBC.Curve();
152         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
153         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
154         aBuilder.Add( aComp, aWire );
155       }
156     }
157   }
158   theShape = aComp;
159 }
160
161 class ComparePnt
162 {
163 public:
164   ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
165   ~ComparePnt() {}
166
167   bool operator < ( const ComparePnt& theOtherPoint ) const
168   {
169     bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
170     if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
171       isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
172       if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
173         isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
174     }
175     return isLess;
176   }
177 private:
178   gp_Pnt myPoint;
179 };
180
181 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
182                                             const CurveCreator_ICurve* theCurve,
183                                             CurveCreator_ICurve::SectionToPointList& thePoints )
184 {
185   thePoints.clear();
186
187   std::list<float> aSelectedPoints;
188   gp_Pnt aPnt;
189   std::map<ComparePnt, int> aPntMap;
190
191   CurveCreator_ICurve::SectionToPointList aPoints;
192   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
193     TopoDS_Vertex aVertex;
194     TopoDS_Shape aShape = theContext->SelectedShape();
195     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
196       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
197
198     if ( aVertex.IsNull() )
199       continue;
200     aPnt = BRep_Tool::Pnt( aVertex );
201     if ( aPntMap.find( aPnt ) != aPntMap.end() )
202       continue;
203     aPntMap[aPnt] = 0;
204
205     CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
206     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
207                                                             aLast = aPoints.end();
208     for ( ; anIt != aLast; anIt++ )
209       thePoints.push_back( *anIt );
210   }
211 }
212
213 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
214                                             const CurveCreator_ICurve* theCurve,
215                                             const CurveCreator_ICurve::SectionToPointList& thePoints )
216 {
217   AIS_ListOfInteractive aDisplayedList;
218   theContext->DisplayedObjects( aDisplayedList );
219   theContext->ClearSelected( Standard_False );
220
221   for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
222   {
223     Handle(AIS_InteractiveObject) anAIS = it.Value();
224     if ( anAIS.IsNull() )
225       continue;
226     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
227     if ( anAISShape.IsNull() )
228       continue;
229
230     //ASL: we convert list of point indices to list of points coordinates
231     int aSize = thePoints.size();
232     std::vector<gp_Pnt> aPntsToSelect( aSize );
233
234     CurveCreator_ICurve::SectionToPointList::const_iterator
235       aPIt = thePoints.begin(), aPLast = thePoints.end();
236     CurveCreator_ICurve::SectionToPoint aSToPoint;
237     for( int i=0; aPIt != aPLast; aPIt++, i++ )
238     {
239       gp_Pnt aPntToSelect;
240       CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
241       aPntsToSelect[i] = aPntToSelect;
242     }
243
244
245     //ASL: we switch off automatic highlight to improve performance of selection
246     theContext->SetAutomaticHilight( Standard_False );
247
248     Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
249     for( aSelection->Init(); aSelection->More(); aSelection->Next() )
250     {
251       Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
252       Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
253
254       gp_Pnt anOwnerPnt = aSenPnt->Point();
255       Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
256
257
258       CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
259                                                                      aLast = thePoints.end();
260       bool isFound = false;
261       for( int i=0; i<aSize; i++ )
262       {
263         bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
264                            fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
265         if( isIntersect )
266         {
267           theContext->AddOrRemoveSelected( anOwner, Standard_False );
268           break;
269         }
270       }
271     }
272   }
273
274   //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
275   //     and call HilightPicked to draw selected owners
276   theContext->SetAutomaticHilight( Standard_True );
277   theContext->LocalContext()->HilightPicked( Standard_True );
278 }
279
280 //=======================================================================
281 // function : setLocalPointContext
282 // purpose  : Open/close the viewer local context
283 //=======================================================================
284 //#define USE_GLOBAL_SELECTION
285 void CurveCreator_Utils::setLocalPointContext(
286                                               Handle(AIS_InteractiveContext) theContext,
287                                               const bool theOpen )
288 {
289 #ifdef USE_GLOBAL_SELECTION
290   return;
291 #endif
292   if ( !theContext )
293     return;
294
295   if ( theOpen ) {
296     // Open local context if there is no one
297     if ( !theContext->HasOpenedContext() ) {
298       theContext->ClearCurrents( false );
299       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
300     }
301     AIS_ListOfInteractive aList;
302     theContext->DisplayedObjects( aList );
303     int aLSize = 0;
304     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
305       aLSize++;
306
307     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
308     {
309       Handle(AIS_InteractiveObject) anAIS = it.Value();
310       if ( !anAIS.IsNull() )
311       {
312         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
313         {
314           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
315           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
316           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
317         }
318         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
319         {
320           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
321           theContext->Activate( anAIS, TopAbs_VERTEX );
322         }
323       }
324       continue;
325     }
326   }
327   else {
328     if ( theContext->HasOpenedContext() )
329       theContext->CloseAllContexts();
330   }
331 }
332
333 /*bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
334                                              Handle(V3d_View) theView,
335                                              const int theX, const int theY,
336                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
337                                              gp_Pnt& thePoint2 )
338 {
339   bool isFoundPoint = false;
340   if ( theContext.IsNull() )
341     return isFoundPoint;
342
343   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
344         theContext->NextSelected() ) {
345     TopoDS_Shape aTShape = theContext->SelectedShape();
346     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
347       continue;
348     else {
349       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
350       if ( anOwner.IsNull() )
351         continue;
352       const TopLoc_Location& aLocation = anOwner->Location();
353       Handle(AIS_InteractiveObject) anAIS =
354                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
355       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
356                                                         thePoint1, thePoint2 );
357     }
358   }
359   return isFoundPoint;
360 }*/
361
362 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
363                                         Handle(AIS_InteractiveObject) theObject,
364                                         const int theX, const int theY,
365                                         gp_Pnt& thePoint,
366                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
367 {
368   bool isFullFound = false;
369
370   if ( theObject.IsNull() || theView.IsNull() )
371     return isFullFound;
372
373   gp_Pnt aPoint, aFoundPoint;
374   Standard_Real aParameter;
375   gp_Pnt aPnt1, aPnt2, aFoundPnt1, aFoundPnt2;
376   bool isFound = false;
377   {
378     int aDelta, aMinDelta = 2*SCENE_PIXEL_TOLERANCE*SCENE_PIXEL_TOLERANCE;
379     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
380     if ( !aShape.IsNull() ) {
381       const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
382       if ( !aCompound.IsNull() ) {
383         TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
384         for ( ; anExp.More(); anExp.Next())
385         {
386           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
387           if ( anEdge.IsNull() )
388             continue;
389           Standard_Real aFirst, aLast;
390           Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
391           if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
392             Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
393             if ( !aBSplineCurve.IsNull() ) {
394               isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
395                                                 aParameter, aDelta );
396               if ( isFound ) {
397                 aPoint = aBSplineCurve->Value( aParameter );
398                 Standard_Integer anI1, anI2;
399                 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
400
401                 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
402                 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
403               }
404             }
405           }
406           else { // usual curve
407             Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
408             if ( !aGLine.IsNull() ) {
409               isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
410                                                 aDelta );
411               if ( isFound ) {
412                 aPoint = aGLine->Value( aParameter );
413
414                 TopoDS_Vertex V1, V2;
415                 TopExp::Vertices(anEdge, V1, V2, Standard_True);
416                 if (!V1.IsNull() && !V2.IsNull()) 
417                 {
418                   aPnt1 = BRep_Tool::Pnt(V1);
419                   aPnt2 = BRep_Tool::Pnt(V2);
420                 }
421                 gp_Vec aVec1( aPnt1, aPoint );
422                 gp_Vec aVec2( aPnt2, aPoint );
423                 double anAngle = aVec1.Angle( aVec2 );
424                 isFound = fabs( anAngle - M_PI ) < LOCAL_SELECTION_TOLERANCE;
425               }
426             }
427           }
428           if ( isFound && aMinDelta >= aDelta ) {
429             isFullFound = true;
430             aMinDelta = aDelta;
431             aFoundPnt1 = aPnt1;
432             aFoundPnt2 = aPnt2;
433             aFoundPoint = aPoint;
434           }
435         }
436       }
437     }
438   }
439   if ( isFullFound ) {
440     thePoint = aPoint;
441     thePoint = aFoundPoint;
442     thePoint1 = aFoundPnt1;
443     thePoint2 = aFoundPnt2;
444   }
445   return isFullFound;
446 }
447
448 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
449                                                  const int theX, const int theY,
450                                                  const Handle(Geom_Curve)& theCurve,
451                                                  Standard_Real& theParameter,
452                                                  int& theDelta )
453 {
454   bool isFound = false;
455   if ( theView.IsNull() )
456     return isFound;
457
458   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
459
460   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
461   Standard_Integer aNbPoint = aProj.NbPoints();
462   if (aNbPoint > 0) {
463     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
464       gp_Pnt aNewPoint = aProj.Point( j );
465       theParameter = aProj.Parameter( j );
466
467       int aX, anY;
468       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
469
470       int aXDelta = abs( aX - theX );
471       int anYDelta = abs( anY - theY );
472       theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
473       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
474     }
475   }
476   return isFound;
477 }