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