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 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
217 {
218   std::list<float> aSelectedPoints;
219
220   gp_Pnt aPnt;
221   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
222     TopoDS_Vertex aVertex;
223     TopoDS_Shape aShape = theContext->SelectedShape();
224     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
225       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
226     else {
227       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
228       if ( !anOwner.IsNull() ) {
229         Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
230         if ( !anAIS.IsNull() ) {
231           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
232           if ( !aPoint.IsNull() )
233             aVertex = TopoDS::Vertex( aPoint->Vertex() );
234         }
235         if ( aVertex.IsNull() ) {
236           // the following happens if there are no points in the current curve, there is only a shape
237           /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
238           if ( aBrepOwner.IsNull() )
239             continue;
240           if ( aBrepOwner->HasShape() ) {
241             const TopoDS_Shape& aShape = aBrepOwner->Shape();
242             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
243             {
244               aVertex = TopoDS::Vertex( aShape );
245             }
246           }*/
247         }
248       }
249     }
250     if ( aVertex.IsNull() )
251       continue;
252     aPnt = BRep_Tool::Pnt( aVertex );
253     aSelectedPoints.push_back( aPnt.X() );
254     aSelectedPoints.push_back( aPnt.Y() );
255     aSelectedPoints.push_back( aPnt.Z() );
256   }
257
258   return aSelectedPoints;
259 }
260 //=======================================================================
261 // function : setLocalPointContext
262 // purpose  : Open/close the viewer local context
263 //=======================================================================
264 //#define USE_GLOBAL_SELECTION
265 void CurveCreator_Utils::setLocalPointContext(
266                                               Handle(AIS_InteractiveContext) theContext,
267                                               const bool theOpen )
268 {
269 #ifdef USE_GLOBAL_SELECTION
270   return;
271 #endif
272   if ( !theContext )
273     return;
274
275   if ( theOpen ) {
276     // Open local context if there is no one
277     if ( !theContext->HasOpenedContext() ) {
278       theContext->ClearCurrents( false );
279       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
280     }
281     AIS_ListOfInteractive aList;
282     theContext->DisplayedObjects( aList );
283     int aLSize = 0;
284     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
285       aLSize++;
286
287     for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
288     {
289       Handle(AIS_InteractiveObject) anAIS = it.Value();
290       if ( !anAIS.IsNull() )
291       {
292         if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
293         {
294           theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
295           //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
296           theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
297         }
298         else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
299         {
300           theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
301           theContext->Activate( anAIS, TopAbs_VERTEX );
302         }
303       }
304       continue;
305     }
306   }
307   else {
308     if ( theContext->HasOpenedContext() )
309       theContext->CloseAllContexts();
310   }
311 }
312
313 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
314                                              Handle(V3d_View) theView,
315                                              const int theX, const int theY,
316                                              gp_Pnt& thePoint, gp_Pnt& thePoint1,
317                                              gp_Pnt& thePoint2 )
318 {
319   bool isFoundPoint = false;
320   if ( theContext.IsNull() )
321     return isFoundPoint;
322
323   for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
324         theContext->NextSelected() ) {
325     TopoDS_Shape aTShape = theContext->SelectedShape();
326     if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
327       continue;
328     else {
329       Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
330       if ( anOwner.IsNull() )
331         continue;
332       const TopLoc_Location& aLocation = anOwner->Location();
333       Handle(AIS_InteractiveObject) anAIS =
334                         Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
335       isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
336                                                         thePoint1, thePoint2 );
337     }
338   }
339   return isFoundPoint;
340 }
341
342 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
343                                         Handle(AIS_InteractiveObject) theObject,
344                                         const int theX, const int theY,
345                                         gp_Pnt& thePoint,
346                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
347 {
348   bool isFound = false;
349
350   if ( theObject.IsNull() || theView.IsNull() )
351     return isFound;
352
353   gp_Pnt aPoint;
354   Standard_Real aParameter;
355   gp_Pnt aPnt1, aPnt2;
356   Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
357   if ( !aLine.IsNull() ) {
358     const Handle(Geom_Line) aGLine = aLine->Line();
359     isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
360     if ( isFound ) {
361       aPoint = aGLine->Value( aParameter );
362
363       Handle(Geom_Point) aPStart;
364       Handle(Geom_Point) aPEnd;
365       aLine->Points( aPStart, aPEnd );
366       aPnt1 = aPStart->Pnt();
367       aPnt2 = aPEnd->Pnt();
368
369       // in case of Geom line a projection is performed to the infinite line,
370       // so it is necessary to bound it by the line size
371       Bnd_Box aLineBox;
372       aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
373       isFound = !aLineBox.IsOut( aPoint );
374     }
375   }
376   else {
377     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
378     if ( !aShape.IsNull() ) {
379       const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
380       if ( !aWire.IsNull() ) {
381         TopExp_Explorer anExp( aWire, TopAbs_EDGE );
382         for ( ; anExp.More(); anExp.Next())
383         {
384           const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
385           if ( !anEdge.IsNull() ) {
386             Standard_Real aFirst, aLast;
387             Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
388
389             if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
390               Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
391               if ( !aBSplineCurve.IsNull() ) {
392                 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
393                 if ( isFound ) {
394                   aPoint = aBSplineCurve->Value( aParameter );
395                   Standard_Integer anI1, anI2;
396                   aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
397
398                   aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
399                   aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
400                 }
401               }
402             }
403           }
404         }
405       }
406     }
407   }
408   if ( isFound ) {
409     thePoint = aPoint;
410     thePoint1 = aPnt1;
411     thePoint2 = aPnt2;
412   }
413   return isFound;
414 }
415
416 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
417                                                  const int theX, const int theY,
418                                                  const Handle(Geom_Curve)& theCurve,
419                                                  Standard_Real& theParameter )
420 {
421   bool isFound = false;
422   if ( theView.IsNull() )
423     return isFound;
424
425   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
426
427   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
428   Standard_Integer aNbPoint = aProj.NbPoints();
429   if (aNbPoint > 0) {
430     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
431       gp_Pnt aNewPoint = aProj.Point( j );
432       theParameter = aProj.Parameter( j );
433
434       int aX, anY;
435       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
436
437       int aXDelta = abs( aX - theX );
438       int anYDelta = abs( anY - theY );
439       isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
440     }
441   }
442   return isFound;
443 }