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