Salome HOME
OCC functionality moving out from the widget
[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
88 //#define USE_COMPOUND
89 #include "CurveCreator_Curve.hxx" // TODO - remove
90 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
91                                          const int theISection, TopoDS_Shape& theShape,
92                                          std::vector<Handle_AIS_InteractiveObject>& aSectionRepresentation )
93 {
94   CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
95
96
97   int aPointSize = theCurve->getNbPoints( theISection );
98   bool aSectIsClosed = theCurve->isClosed( theISection );
99   if( aSectType == CurveCreator::Polyline )
100   {
101 #ifdef USE_COMPOUND
102     BRep_Builder aBuilder;
103     TopoDS_Compound aComp;
104     aBuilder.MakeCompound(aComp);
105
106     int iPoint = 0;
107     gp_Pnt aPrevPoint, aPoint;
108     if ( aPointSize == 1 ) {
109       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
110       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
111       aBuilder.Add( aComp, aVertex );
112     }
113     else if ( aPointSize > 1 ) {
114       TopoDS_Edge aPointEdge;
115       TopoDS_Vertex aVertex;
116       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
117       aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
118       aBuilder.Add( aComp, aVertex );
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         aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
125         aBuilder.Add( aComp, aPointEdge );
126         aPrevPoint = aPoint;
127       }
128       if( aSectIsClosed && ( aPointSize > 2 ) ) {
129         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
130         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
131         aBuilder.Add( aComp, aVertex );
132         aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
133         aBuilder.Add( aComp, aPointEdge );
134       }
135       theShape = aComp;
136     }
137 #endif
138   }
139   else if( aSectType == CurveCreator::Spline )
140   {
141     std::vector<double> aPoints;
142     for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
143     {
144       CurveCreator::Coordinates aCoords = theCurve->getPoint( theISection, iPoint );
145       double aX = aCoords[0];
146       double aY = aCoords[1];
147       double aZ = aCoords[2];
148       aPoints.push_back( aX );
149       aPoints.push_back( aY );
150     }
151
152     if( aPointSize > 1 )
153     {
154       Handle(Geom_BSplineCurve) aBSplineCurve;
155       // fill array for algorithm by the received coordinates
156       int aLen = aPoints.size() / 2;
157       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
158       std::vector<double>::const_iterator aListIter = aPoints.begin();
159       for (int ind = 1; ind <= aLen; ind++) {
160         gp_Pnt aPnt(gp::Origin());
161         aPnt.SetX(*aListIter);
162         aListIter++;
163         aPnt.SetY(*aListIter);
164         aListIter++;
165         aPnt.SetZ(0);
166         aHCurvePoints->SetValue(ind, aPnt);
167       }
168       // compute BSpline
169       GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
170       aGBC.Perform();
171       if (aGBC.IsDone()) {
172         aBSplineCurve = aGBC.Curve();
173       }
174       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
175
176       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
177       theShape = aWire;
178     }
179   }
180
181   const CurveCreator_Curve* aCurve = dynamic_cast<const CurveCreator_Curve*>( theCurve );
182   if ( !aCurve )
183     return;
184
185   if( aSectType == CurveCreator::Polyline )
186   {
187 #ifndef USE_COMPOUND
188     int iPoint = 0; 
189     for( ; iPoint < ( aPointSize - 1 ) ; iPoint++ ){
190       Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint);
191       aSectionRepresentation.push_back( anAISPnt );
192       Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, iPoint+1 );
193       aSectionRepresentation.push_back( aLine );
194     }
195     if( aPointSize != 0 ){
196       Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint); 
197       aSectionRepresentation.push_back( anAISPnt );
198       if( aSectIsClosed && ( aPointSize > 1 ) ){
199         Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, 0 );
200         aSectionRepresentation.push_back( aLine );
201       }
202     }
203 #endif
204   }
205   else if( aSectType == CurveCreator::Spline )
206   {
207     std::vector<double> aPoints;
208     for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
209     {
210       Handle_AIS_Point anAISPnt = aCurve->getAISPoint( theISection, iPoint );
211       aSectionRepresentation.push_back( anAISPnt );
212     }
213   }
214 }
215
216 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
217                                          TopoDS_Shape& theShape )
218 {
219   BRep_Builder aBuilder;
220   TopoDS_Compound aComp;
221   aBuilder.MakeCompound( aComp );
222   for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
223   {
224     int theISection = iSection;
225
226     CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
227     int aPointSize = theCurve->getNbPoints( theISection );
228     bool aSectIsClosed = theCurve->isClosed( theISection );
229     bool isPolyline = aSectType == CurveCreator::Polyline;
230     int iPoint = 0;
231     gp_Pnt aPrevPoint, aPoint;
232     if ( aPointSize == 1 ) {
233       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
234       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
235       aBuilder.Add( aComp, aVertex );
236     }
237     else if ( aPointSize > 1 ) {
238       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
239       int aHIndex = 1;
240
241       TopoDS_Edge aPointEdge;
242       TopoDS_Vertex aVertex;
243       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
244       aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
245       aBuilder.Add( aComp, aVertex );
246       aHCurvePoints->SetValue(aHIndex++, aPoint);
247       aPrevPoint = aPoint;
248       iPoint++;
249       for( ; iPoint < aPointSize; iPoint++ ) {
250         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
251         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
252         aBuilder.Add( aComp, aVertex );
253         aHCurvePoints->SetValue(aHIndex++, aPoint);
254         if ( isPolyline ) {
255           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
256           aBuilder.Add( aComp, aPointEdge );
257         }
258         aPrevPoint = aPoint;
259       }
260       if( aSectIsClosed && ( aPointSize > 2 ) ) {
261         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
262         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
263         aBuilder.Add( aComp, aVertex );
264         if ( isPolyline ) {
265           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
266           aBuilder.Add( aComp, aPointEdge );
267         }
268       }
269       if( !isPolyline ) {
270         // compute BSpline
271         Handle(Geom_BSplineCurve) aBSplineCurve;
272         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
273         aGBC.Perform();
274         if ( aGBC.IsDone() )
275           aBSplineCurve = aGBC.Curve();
276         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
277         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
278         aBuilder.Add( aComp, aWire );
279       }
280     }
281   }
282   theShape = aComp;
283 }
284
285
286 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
287 {
288   std::list<float> aSelectedPoints;
289
290   gp_Pnt aPnt;
291   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
292     TopoDS_Vertex aVertex;
293     TopoDS_Shape aShape = theContext->SelectedShape();
294     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
295       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
296     else {
297       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
298       if ( !anOwner.IsNull() ) {
299         Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
300         if ( !anAIS.IsNull() ) {
301           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
302           if ( !aPoint.IsNull() )
303             aVertex = TopoDS::Vertex( aPoint->Vertex() );
304         }
305         if ( aVertex.IsNull() ) {
306           // the following happens if there are no points in the current curve, there is only a shape
307           /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
308           if ( aBrepOwner.IsNull() )
309             continue;
310           if ( aBrepOwner->HasShape() ) {
311             const TopoDS_Shape& aShape = aBrepOwner->Shape();
312             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
313             {
314               aVertex = TopoDS::Vertex( aShape );
315             }
316           }*/
317         }
318       }
319     }
320     if ( aVertex.IsNull() )
321       continue;
322     aPnt = BRep_Tool::Pnt( aVertex );
323     aSelectedPoints.push_back( aPnt.X() );
324     aSelectedPoints.push_back( aPnt.Y() );
325     aSelectedPoints.push_back( aPnt.Z() );
326   }
327
328   return aSelectedPoints;
329 }
330 //=======================================================================
331 // function : setLocalPointContext
332 // purpose  : Open/close the viewer local context
333 //=======================================================================
334 //#define USE_GLOBAL_SELECTION
335 void CurveCreator_Utils::setLocalPointContext(
336                                               Handle(AIS_InteractiveContext) theContext,
337                                               const bool theOpen )
338 {
339 #ifdef USE_GLOBAL_SELECTION
340   return;
341 #endif
342   if ( !theContext )
343     return;
344
345   if ( theOpen ) {
346     // Open local context if there is no one
347     if ( !theContext->HasOpenedContext() ) {
348       theContext->ClearCurrents( false );
349       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
350     }
351     AIS_ListOfInteractive aList;
352     theContext->DisplayedObjects( aList );
353     int aLSize = 0;
354     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
355       aLSize++;
356
357     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
358     {
359       Handle(AIS_InteractiveObject) anAIS = it.Value();
360       if ( !anAIS.IsNull() )
361       {
362         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
363         {
364           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
365           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
366           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
367         }
368         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
369         {
370           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
371           theContext->Activate( anAIS, TopAbs_VERTEX );
372         }
373       }
374       continue;
375     }
376   }
377   else {
378     if ( theContext->HasOpenedContext() )
379       theContext->CloseAllContexts();
380   }
381 }
382
383 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
384                                              Handle(V3d_View) theView,
385                                              const int theX, const int theY,
386                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
387                                              gp_Pnt& thePoint2 )
388 {
389   bool isFoundPoint = false;
390   if ( theContext.IsNull() )
391     return isFoundPoint;
392
393   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
394         theContext->NextSelected() ) {
395     TopoDS_Shape aTShape = theContext->SelectedShape();
396     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
397       continue;
398     else {
399       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
400       if ( anOwner.IsNull() )
401         continue;
402       const TopLoc_Location& aLocation = anOwner->Location();
403       Handle(AIS_InteractiveObject) anAIS =
404                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
405       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
406                                                         thePoint1, thePoint2 );
407     }
408   }
409   return isFoundPoint;
410 }
411
412 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
413                                         Handle(AIS_InteractiveObject) theObject,
414                                         const int theX, const int theY,
415                                         gp_Pnt& thePoint,
416                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
417 {
418   bool isFound = false;
419
420   if ( theObject.IsNull() || theView.IsNull() )
421     return isFound;
422
423   gp_Pnt aPoint;
424   Standard_Real aParameter;
425   gp_Pnt aPnt1, aPnt2;
426   Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
427   if ( !aLine.IsNull() ) {
428     const Handle(Geom_Line) aGLine = aLine->Line();
429     isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
430     if ( isFound ) {
431       aPoint = aGLine->Value( aParameter );
432
433       Handle(Geom_Point) aPStart;
434       Handle(Geom_Point) aPEnd;
435       aLine->Points( aPStart, aPEnd );
436       aPnt1 = aPStart->Pnt();
437       aPnt2 = aPEnd->Pnt();
438
439       // in case of Geom line a projection is performed to the infinite line,
440       // so it is necessary to bound it by the line size
441       Bnd_Box aLineBox;
442       aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
443       isFound = !aLineBox.IsOut( aPoint );
444     }
445   }
446   else {
447     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
448     if ( !aShape.IsNull() ) {
449       const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
450       if ( !aWire.IsNull() ) {
451         TopExp_Explorer anExp( aWire, TopAbs_EDGE );
452         for ( ; anExp.More(); anExp.Next())
453         {
454           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
455           if ( !anEdge.IsNull() ) {
456             Standard_Real aFirst, aLast;
457             Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
458
459             if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
460               Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
461               if ( !aBSplineCurve.IsNull() ) {
462                 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
463                 if ( isFound ) {
464                   aPoint = aBSplineCurve->Value( aParameter );
465                   Standard_Integer anI1, anI2;
466                   aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
467
468                   aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
469                   aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
470                 }
471               }
472             }
473           }
474         }
475       }
476     }
477   }
478   if ( isFound ) {
479     thePoint = aPoint;
480     thePoint1 = aPnt1;
481     thePoint2 = aPnt2;
482   }
483   return isFound;
484 }
485
486 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
487                                                  const int theX, const int theY,
488                                                  const Handle(Geom_Curve)& theCurve,
489                                                  Standard_Real& theParameter )
490 {
491   bool isFound = false;
492   if ( theView.IsNull() )
493     return isFound;
494
495   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
496
497   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
498   Standard_Integer aNbPoint = aProj.NbPoints();
499   if (aNbPoint > 0) {
500     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
501       gp_Pnt aNewPoint = aProj.Point( j );
502       theParameter = aProj.Parameter( j );
503
504       int aX, anY;
505       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
506
507       int aXDelta = abs( aX - theX );
508       int anYDelta = abs( anY - theY );
509       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
510     }
511   }
512   return isFound;
513 }