Salome HOME
1fcc4084030de42aba451f7fe357f77c117470e8
[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 #include <AIS_LocalContext.hxx>
40
41 #include <Geom_Point.hxx>
42 #include <Geom_BSplineCurve.hxx>
43 #include <Geom_Line.hxx>
44
45 #include <TopExp.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <SelectMgr_Selection.hxx>
50 #include <Select3D_SensitivePoint.hxx>
51
52 #include <BRep_Tool.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRepBuilderAPI_MakeVertex.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57
58 #include <TColgp_HArray1OfPnt.hxx>
59 #include <GeomAPI_Interpolate.hxx>
60
61 #include <ProjLib.hxx>
62 #include <ElSLib.hxx>
63
64 #include <math.h>
65
66 #include "CurveCreator_ICurve.hxx"
67
68 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
69 const int    SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
70 const int    SCENE_PIXEL_POINT_TOLERANCE = 5;
71
72 //=======================================================================
73 // function : ConvertClickToPoint()
74 // purpose  : Returns the point clicked in 3D view
75 //=======================================================================
76 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
77                                               Handle(V3d_View) theView,
78                                               int& x, int& y )
79 {
80   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
81 }
82
83
84 //=======================================================================
85 // function : ConvertClickToPoint()
86 // purpose  : Returns the point clicked in 3D view
87 //=======================================================================
88 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
89 {
90   return GEOMUtils::ConvertClickToPoint( x, y, aView );
91 }
92
93 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
94                                          TopoDS_Shape& theShape )
95 {
96   BRep_Builder aBuilder;
97   TopoDS_Compound aComp;
98   aBuilder.MakeCompound( aComp );
99   for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
100   {
101     int theISection = iSection;
102
103     CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
104     int aPointSize = theCurve->getNbPoints( theISection );
105     bool aSectIsClosed = theCurve->isClosed( theISection );
106     bool isPolyline = aSectType == CurveCreator::Polyline;
107     int iPoint = 0;
108     gp_Pnt aPrevPoint, aPoint;
109     if ( aPointSize == 1 ) {
110       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
111       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
112       aBuilder.Add( aComp, aVertex );
113     }
114     else if ( aPointSize > 1 ) {
115       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
116       int aHIndex = 1;
117
118       TopoDS_Edge aPointEdge;
119       TopoDS_Vertex aVertex;
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       aPrevPoint = aPoint;
125       iPoint++;
126       for( ; iPoint < aPointSize; iPoint++ ) {
127         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
128         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
129         aBuilder.Add( aComp, aVertex );
130         aHCurvePoints->SetValue(aHIndex++, aPoint);
131         if ( isPolyline ) {
132           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
133           aBuilder.Add( aComp, aPointEdge );
134         }
135         aPrevPoint = aPoint;
136       }
137       if( aSectIsClosed && ( aPointSize > 2 ) ) {
138         CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
139         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
140         aBuilder.Add( aComp, aVertex );
141         if ( isPolyline ) {
142           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
143           aBuilder.Add( aComp, aPointEdge );
144         }
145       }
146       if( !isPolyline ) {
147         // compute BSpline
148         Handle(Geom_BSplineCurve) aBSplineCurve;
149         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
150         aGBC.Perform();
151         if ( aGBC.IsDone() )
152           aBSplineCurve = aGBC.Curve();
153         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
154         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
155         aBuilder.Add( aComp, aWire );
156       }
157     }
158   }
159   theShape = aComp;
160 }
161
162 class ComparePnt
163 {
164 public:
165   ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
166   ~ComparePnt() {}
167
168   bool operator < ( const ComparePnt& theOtherPoint ) const
169   {
170     bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
171     if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
172       isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
173       if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
174         isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
175     }
176     return isLess;
177   }
178 private:
179   gp_Pnt myPoint;
180 };
181
182 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
183                                             const CurveCreator_ICurve* theCurve,
184                                             CurveCreator_ICurve::SectionToPointList& thePoints )
185 {
186   thePoints.clear();
187
188   std::list<float> aSelectedPoints;
189   gp_Pnt aPnt;
190   std::map<ComparePnt, int> aPntMap;
191
192   CurveCreator_ICurve::SectionToPointList aPoints;
193   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
194     TopoDS_Vertex aVertex;
195     TopoDS_Shape aShape = theContext->SelectedShape();
196     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
197       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
198
199     if ( aVertex.IsNull() )
200       continue;
201     aPnt = BRep_Tool::Pnt( aVertex );
202     if ( aPntMap.find( aPnt ) != aPntMap.end() )
203       continue;
204     aPntMap[aPnt] = 0;
205
206     CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
207     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
208                                                             aLast = aPoints.end();
209     for ( ; anIt != aLast; anIt++ )
210       thePoints.push_back( *anIt );
211   }
212 }
213
214 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
215                                             const CurveCreator_ICurve* theCurve,
216                                             const CurveCreator_ICurve::SectionToPointList& thePoints )
217 {
218   if ( !theCurve )
219     return;
220
221   Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
222   if ( anAIS.IsNull() )
223     return;
224   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
225   if ( anAISShape.IsNull() )
226     return;
227
228   //ASL: we convert list of point indices to list of points coordinates
229   int aSize = thePoints.size();
230   std::vector<gp_Pnt> aPntsToSelect( aSize );
231
232   CurveCreator_ICurve::SectionToPointList::const_iterator
233                      aPIt = thePoints.begin(), aPLast = thePoints.end();
234   CurveCreator_ICurve::SectionToPoint aSToPoint;
235   for( int i=0; aPIt != aPLast; aPIt++, i++ )
236   {
237     gp_Pnt aPntToSelect;
238     CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
239     aPntsToSelect[i] = aPntToSelect;
240   }
241
242   theContext->ClearSelected( Standard_False );
243   //ASL: we switch off automatic highlight to improve performance of selection
244   theContext->SetAutomaticHilight( Standard_False );
245
246   Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
247   for( aSelection->Init(); aSelection->More(); aSelection->Next() )
248   {
249     Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
250     Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
251
252     gp_Pnt anOwnerPnt = aSenPnt->Point();
253     Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
254
255
256     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
257                                                                    aLast = thePoints.end();
258     bool isFound = false;
259     for( int i=0; i<aSize; i++ )
260     {
261       bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
262                          fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
263       if( isIntersect )
264       {
265         theContext->AddOrRemoveSelected( anOwner, Standard_False );
266         break;
267       }
268     }
269   }
270
271   //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
272   //     and call HilightPicked to draw selected owners
273   theContext->SetAutomaticHilight( Standard_True );
274   theContext->LocalContext()->HilightPicked( Standard_True );
275 }
276
277 //=======================================================================
278 // function : setLocalPointContext
279 // purpose  : Open/close the viewer local context
280 //=======================================================================
281 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
282                                                Handle(AIS_InteractiveContext) theContext,
283                                                const bool theOpen )
284 {
285   if ( !theContext )
286     return;
287
288   if ( theOpen ) {
289     // Open local context if there is no one
290     if ( !theContext->HasOpenedContext() ) {
291       theContext->ClearCurrents( false );
292       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
293     }
294     // load the curve AIS object to the local context with the point selection
295     Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
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_VERTEX ) );
302       }
303     }
304   }
305   else {
306     if ( theContext->HasOpenedContext() )
307       theContext->CloseAllContexts();
308   }
309 }
310
311 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
312                                         Handle(AIS_InteractiveObject) theObject,
313                                         const int theX, const int theY,
314                                         gp_Pnt& thePoint,
315                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
316 {
317   bool isFullFound = false;
318
319   if ( theObject.IsNull() || theView.IsNull() )
320     return isFullFound;
321   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
322   if ( aShape.IsNull() )
323     return isFullFound;
324   const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
325   if ( aCompound.IsNull() )
326     return isFullFound;
327
328   gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
329   gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
330   Standard_Real aParameter;
331   bool isFound = false;
332   int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
333   TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
334   for ( ; anExp.More(); anExp.Next())
335   {
336     const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
337     if ( anEdge.IsNull() )
338       continue;
339     Standard_Real aFirst, aLast;
340     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
341     if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
342       Handle(Geom_BSplineCurve) aBSplineCurve =
343                           Handle(Geom_BSplineCurve)::DownCast( aCurve );
344       if ( !aBSplineCurve.IsNull() ) {
345         isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
346                                           aParameter, aDelta );
347         if ( isFound ) {
348           aCurPoint = aBSplineCurve->Value( aParameter );
349           Standard_Integer anI1, anI2;
350           aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
351           aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
352           aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
353         }
354       }
355     }
356     else { // a curve built on a polyline edge
357       Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
358       if ( aGLine.IsNull() )
359         continue;
360       isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
361                                         aDelta );
362       if ( isFound ) {
363         aCurPoint = aGLine->Value( aParameter );
364         TopoDS_Vertex V1, V2;
365         TopExp::Vertices( anEdge, V1, V2, Standard_True );
366         if ( V1.IsNull() || V2.IsNull() )
367           continue;
368         aCurPoint1 = BRep_Tool::Pnt(V1);
369         aCurPoint2 = BRep_Tool::Pnt(V2);
370
371         // check that the projected point is on the bounded curve
372         gp_Vec aVec1( aCurPoint1, aCurPoint );
373         gp_Vec aVec2( aCurPoint2, aCurPoint );
374         isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
375       }
376     }
377     if ( isFound && aMinDelta >= aDelta ) {
378       aMinDelta = aDelta;
379
380       isFullFound = true;
381       aFoundPnt1 = aCurPoint1;
382       aFoundPnt2 = aCurPoint2;
383       aFoundPoint = aCurPoint;
384     }
385   }
386   if ( isFullFound ) {
387     int aX, anY, aX1, anY1, aX2, anY2;
388     int aDelta;
389     CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
390     CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
391     CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
392
393     isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
394                   !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
395     if ( isFullFound ) {
396       thePoint = aFoundPoint;
397       thePoint1 = aFoundPnt1;
398       thePoint2 = aFoundPnt2;
399     }
400   }
401   return isFullFound;
402 }
403
404 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
405                                                  const int theX, const int theY,
406                                                  const Handle(Geom_Curve)& theCurve,
407                                                  Standard_Real& theParameter,
408                                                  int& theDelta )
409 {
410   bool isFound = false;
411   if ( theView.IsNull() )
412     return isFound;
413
414   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
415
416   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
417   Standard_Integer aNbPoint = aProj.NbPoints();
418   if (aNbPoint > 0) {
419     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
420       gp_Pnt aNewPoint = aProj.Point( j );
421       theParameter = aProj.Parameter( j );
422
423       int aX, anY;
424       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
425
426       isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
427     }
428   }
429   return isFound;
430 }
431
432 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
433                                         const int theOtherY, const double theTolerance, int& theDelta )
434 {
435   int aXDelta = abs( theX - theOtherX );
436   int anYDelta = abs( theY - theOtherY );
437
438   theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
439
440   return aXDelta < theTolerance && anYDelta < theTolerance;
441 }