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