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