1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Utils.h"
21 #include "CurveCreator.hxx"
22 #include "CurveCreator_UtilsICurve.hxx"
24 #include <GEOMUtils.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Compound.hxx>
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>
41 #include <Geom_Point.hxx>
42 #include <Geom_BSplineCurve.hxx>
43 #include <Geom_Line.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>
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>
58 #include <TColgp_HArray1OfPnt.hxx>
59 #include <GeomAPI_Interpolate.hxx>
61 #include <ProjLib.hxx>
66 #include "CurveCreator_ICurve.hxx"
68 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
69 const int SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
70 const int SCENE_PIXEL_POINT_TOLERANCE = 5;
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,
80 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
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 )
90 return GEOMUtils::ConvertClickToPoint( x, y, aView );
93 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
94 TopoDS_Shape& theShape )
96 BRep_Builder aBuilder;
97 TopoDS_Compound aComp;
98 aBuilder.MakeCompound( aComp );
99 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
101 int theISection = iSection;
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;
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 );
114 else if ( aPointSize > 1 ) {
115 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
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);
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);
132 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
133 aBuilder.Add( aComp, aPointEdge );
137 if( aSectIsClosed && ( aPointSize > 2 ) ) {
138 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
139 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
140 aBuilder.Add( aComp, aVertex );
142 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
143 aBuilder.Add( aComp, aPointEdge );
148 Handle(Geom_BSplineCurve) aBSplineCurve;
149 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
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 );
165 ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
168 bool operator < ( const ComparePnt& theOtherPoint ) const
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();
182 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
183 const CurveCreator_ICurve* theCurve,
184 CurveCreator_ICurve::SectionToPointList& thePoints )
188 std::list<float> aSelectedPoints;
190 std::map<ComparePnt, int> aPntMap;
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() );
199 if ( aVertex.IsNull() )
201 aPnt = BRep_Tool::Pnt( aVertex );
202 if ( aPntMap.find( aPnt ) != aPntMap.end() )
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 );
214 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
215 const CurveCreator_ICurve* theCurve,
216 const CurveCreator_ICurve::SectionToPointList& thePoints )
218 AIS_ListOfInteractive aDisplayedList;
219 theContext->DisplayedObjects( aDisplayedList );
220 theContext->ClearSelected( Standard_False );
222 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
224 Handle(AIS_InteractiveObject) anAIS = it.Value();
225 if ( anAIS.IsNull() )
227 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
228 if ( anAISShape.IsNull() )
231 //ASL: we convert list of point indices to list of points coordinates
232 int aSize = thePoints.size();
233 std::vector<gp_Pnt> aPntsToSelect( aSize );
235 CurveCreator_ICurve::SectionToPointList::const_iterator
236 aPIt = thePoints.begin(), aPLast = thePoints.end();
237 CurveCreator_ICurve::SectionToPoint aSToPoint;
238 for( int i=0; aPIt != aPLast; aPIt++, i++ )
241 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
242 aPntsToSelect[i] = aPntToSelect;
246 //ASL: we switch off automatic highlight to improve performance of selection
247 theContext->SetAutomaticHilight( Standard_False );
249 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
250 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
252 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
253 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
255 gp_Pnt anOwnerPnt = aSenPnt->Point();
256 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
259 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
260 aLast = thePoints.end();
261 bool isFound = false;
262 for( int i=0; i<aSize; i++ )
264 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
265 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
268 theContext->AddOrRemoveSelected( anOwner, Standard_False );
275 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
276 // and call HilightPicked to draw selected owners
277 theContext->SetAutomaticHilight( Standard_True );
278 theContext->LocalContext()->HilightPicked( Standard_True );
281 //=======================================================================
282 // function : setLocalPointContext
283 // purpose : Open/close the viewer local context
284 //=======================================================================
285 //#define USE_GLOBAL_SELECTION
286 void CurveCreator_Utils::setLocalPointContext(
287 Handle(AIS_InteractiveContext) theContext,
290 #ifdef USE_GLOBAL_SELECTION
297 // Open local context if there is no one
298 if ( !theContext->HasOpenedContext() ) {
299 theContext->ClearCurrents( false );
300 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
302 AIS_ListOfInteractive aList;
303 theContext->DisplayedObjects( aList );
305 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
308 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
310 Handle(AIS_InteractiveObject) anAIS = it.Value();
311 if ( !anAIS.IsNull() )
313 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
315 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
316 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
317 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
319 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
321 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
322 theContext->Activate( anAIS, TopAbs_VERTEX );
329 if ( theContext->HasOpenedContext() )
330 theContext->CloseAllContexts();
334 /*bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
335 Handle(V3d_View) theView,
336 const int theX, const int theY,
337 gp_Pnt& thePoint, gp_Pnt& thePoint1,
340 bool isFoundPoint = false;
341 if ( theContext.IsNull() )
344 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
345 theContext->NextSelected() ) {
346 TopoDS_Shape aTShape = theContext->SelectedShape();
347 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
350 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
351 if ( anOwner.IsNull() )
353 const TopLoc_Location& aLocation = anOwner->Location();
354 Handle(AIS_InteractiveObject) anAIS =
355 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
356 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
357 thePoint1, thePoint2 );
363 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
364 Handle(AIS_InteractiveObject) theObject,
365 const int theX, const int theY,
367 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
369 bool isFullFound = false;
371 if ( theObject.IsNull() || theView.IsNull() )
373 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
374 if ( aShape.IsNull() )
376 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
377 if ( aCompound.IsNull() )
380 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
381 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
382 Standard_Real aParameter;
383 bool isFound = false;
384 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
385 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
386 for ( ; anExp.More(); anExp.Next())
388 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
389 if ( anEdge.IsNull() )
391 Standard_Real aFirst, aLast;
392 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
393 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
394 Handle(Geom_BSplineCurve) aBSplineCurve =
395 Handle(Geom_BSplineCurve)::DownCast( aCurve );
396 if ( !aBSplineCurve.IsNull() ) {
397 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
398 aParameter, aDelta );
400 aCurPoint = aBSplineCurve->Value( aParameter );
401 Standard_Integer anI1, anI2;
402 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
403 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
404 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
408 else { // a curve built on a polyline edge
409 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
410 if ( aGLine.IsNull() )
412 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
415 aCurPoint = aGLine->Value( aParameter );
416 TopoDS_Vertex V1, V2;
417 TopExp::Vertices( anEdge, V1, V2, Standard_True );
418 if ( V1.IsNull() || V2.IsNull() )
420 aCurPoint1 = BRep_Tool::Pnt(V1);
421 aCurPoint2 = BRep_Tool::Pnt(V2);
423 // check that the projected point is on the bounded curve
424 gp_Vec aVec1( aCurPoint1, aCurPoint );
425 gp_Vec aVec2( aCurPoint2, aCurPoint );
426 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
429 if ( isFound && aMinDelta >= aDelta ) {
433 aFoundPnt1 = aCurPoint1;
434 aFoundPnt2 = aCurPoint2;
435 aFoundPoint = aCurPoint;
439 int aX, anY, aX1, anY1, aX2, anY2;
441 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
442 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
443 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
445 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
446 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
448 thePoint = aFoundPoint;
449 thePoint1 = aFoundPnt1;
450 thePoint2 = aFoundPnt2;
456 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
457 const int theX, const int theY,
458 const Handle(Geom_Curve)& theCurve,
459 Standard_Real& theParameter,
462 bool isFound = false;
463 if ( theView.IsNull() )
466 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
468 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
469 Standard_Integer aNbPoint = aProj.NbPoints();
471 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
472 gp_Pnt aNewPoint = aProj.Point( j );
473 theParameter = aProj.Parameter( j );
476 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
478 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
484 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
485 const int theOtherY, const double theTolerance, int& theDelta )
487 int aXDelta = abs( theX - theOtherX );
488 int anYDelta = abs( theY - theOtherY );
490 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
492 return aXDelta < theTolerance && anYDelta < theTolerance;