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