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