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_TOLERANCE = 10;
71 //=======================================================================
72 // function : ConvertClickToPoint()
73 // purpose : Returns the point clicked in 3D view
74 //=======================================================================
75 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
76 Handle(V3d_View) theView,
79 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
83 //=======================================================================
84 // function : ConvertClickToPoint()
85 // purpose : Returns the point clicked in 3D view
86 //=======================================================================
87 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
89 return GEOMUtils::ConvertClickToPoint( x, y, aView );
92 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
93 TopoDS_Shape& theShape )
95 BRep_Builder aBuilder;
96 TopoDS_Compound aComp;
97 aBuilder.MakeCompound( aComp );
98 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
100 int theISection = iSection;
102 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
103 int aPointSize = theCurve->getNbPoints( theISection );
104 bool aSectIsClosed = theCurve->isClosed( theISection );
105 bool isPolyline = aSectType == CurveCreator::Polyline;
107 gp_Pnt aPrevPoint, aPoint;
108 if ( aPointSize == 1 ) {
109 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
110 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
111 aBuilder.Add( aComp, aVertex );
113 else if ( aPointSize > 1 ) {
114 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
117 TopoDS_Edge aPointEdge;
118 TopoDS_Vertex aVertex;
119 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
120 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
121 aBuilder.Add( aComp, aVertex );
122 aHCurvePoints->SetValue(aHIndex++, aPoint);
125 for( ; iPoint < aPointSize; iPoint++ ) {
126 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
127 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
128 aBuilder.Add( aComp, aVertex );
129 aHCurvePoints->SetValue(aHIndex++, aPoint);
131 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
132 aBuilder.Add( aComp, aPointEdge );
136 if( aSectIsClosed && ( aPointSize > 2 ) ) {
137 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
138 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
139 aBuilder.Add( aComp, aVertex );
141 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
142 aBuilder.Add( aComp, aPointEdge );
147 Handle(Geom_BSplineCurve) aBSplineCurve;
148 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
151 aBSplineCurve = aGBC.Curve();
152 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
153 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
154 aBuilder.Add( aComp, aWire );
164 ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
167 bool operator < ( const ComparePnt& theOtherPoint ) const
169 bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
170 if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
171 isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
172 if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
173 isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
181 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
182 const CurveCreator_ICurve* theCurve,
183 CurveCreator_ICurve::SectionToPointList& thePoints )
187 std::list<float> aSelectedPoints;
189 std::map<ComparePnt, int> aPntMap;
191 CurveCreator_ICurve::SectionToPointList aPoints;
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() );
198 if ( aVertex.IsNull() )
200 aPnt = BRep_Tool::Pnt( aVertex );
201 if ( aPntMap.find( aPnt ) != aPntMap.end() )
205 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
206 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
207 aLast = aPoints.end();
208 for ( ; anIt != aLast; anIt++ )
209 thePoints.push_back( *anIt );
213 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
214 const CurveCreator_ICurve* theCurve,
215 const CurveCreator_ICurve::SectionToPointList& thePoints )
217 AIS_ListOfInteractive aDisplayedList;
218 theContext->DisplayedObjects( aDisplayedList );
219 theContext->ClearSelected( Standard_False );
221 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
223 Handle(AIS_InteractiveObject) anAIS = it.Value();
224 if ( anAIS.IsNull() )
226 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
227 if ( anAISShape.IsNull() )
230 //ASL: we convert list of point indices to list of points coordinates
231 int aSize = thePoints.size();
232 std::vector<gp_Pnt> aPntsToSelect( aSize );
234 CurveCreator_ICurve::SectionToPointList::const_iterator
235 aPIt = thePoints.begin(), aPLast = thePoints.end();
236 CurveCreator_ICurve::SectionToPoint aSToPoint;
237 for( int i=0; aPIt != aPLast; aPIt++, i++ )
240 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
241 aPntsToSelect[i] = aPntToSelect;
245 //ASL: we switch off automatic highlight to improve performance of selection
246 theContext->SetAutomaticHilight( Standard_False );
248 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
249 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
251 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
252 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
254 gp_Pnt anOwnerPnt = aSenPnt->Point();
255 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
258 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
259 aLast = thePoints.end();
260 bool isFound = false;
261 for( int i=0; i<aSize; i++ )
263 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
264 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
267 theContext->AddOrRemoveSelected( anOwner, Standard_False );
274 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
275 // and call HilightPicked to draw selected owners
276 theContext->SetAutomaticHilight( Standard_True );
277 theContext->LocalContext()->HilightPicked( Standard_True );
280 //=======================================================================
281 // function : setLocalPointContext
282 // purpose : Open/close the viewer local context
283 //=======================================================================
284 //#define USE_GLOBAL_SELECTION
285 void CurveCreator_Utils::setLocalPointContext(
286 Handle(AIS_InteractiveContext) theContext,
289 #ifdef USE_GLOBAL_SELECTION
296 // Open local context if there is no one
297 if ( !theContext->HasOpenedContext() ) {
298 theContext->ClearCurrents( false );
299 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
301 AIS_ListOfInteractive aList;
302 theContext->DisplayedObjects( aList );
304 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
307 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
309 Handle(AIS_InteractiveObject) anAIS = it.Value();
310 if ( !anAIS.IsNull() )
312 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
314 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
315 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
316 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
318 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
320 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
321 theContext->Activate( anAIS, TopAbs_VERTEX );
328 if ( theContext->HasOpenedContext() )
329 theContext->CloseAllContexts();
333 /*bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
334 Handle(V3d_View) theView,
335 const int theX, const int theY,
336 gp_Pnt& thePoint, gp_Pnt& thePoint1,
339 bool isFoundPoint = false;
340 if ( theContext.IsNull() )
343 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
344 theContext->NextSelected() ) {
345 TopoDS_Shape aTShape = theContext->SelectedShape();
346 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
349 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
350 if ( anOwner.IsNull() )
352 const TopLoc_Location& aLocation = anOwner->Location();
353 Handle(AIS_InteractiveObject) anAIS =
354 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
355 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
356 thePoint1, thePoint2 );
362 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
363 Handle(AIS_InteractiveObject) theObject,
364 const int theX, const int theY,
366 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
368 bool isFullFound = false;
370 if ( theObject.IsNull() || theView.IsNull() )
373 gp_Pnt aPoint, aFoundPoint;
374 Standard_Real aParameter;
375 gp_Pnt aPnt1, aPnt2, aFoundPnt1, aFoundPnt2;
376 bool isFound = false;
378 int aDelta, aMinDelta = 2*SCENE_PIXEL_TOLERANCE*SCENE_PIXEL_TOLERANCE;
379 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
380 if ( !aShape.IsNull() ) {
381 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
382 if ( !aCompound.IsNull() ) {
383 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
384 for ( ; anExp.More(); anExp.Next())
386 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
387 if ( anEdge.IsNull() )
389 Standard_Real aFirst, aLast;
390 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
391 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
392 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
393 if ( !aBSplineCurve.IsNull() ) {
394 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
395 aParameter, aDelta );
397 aPoint = aBSplineCurve->Value( aParameter );
398 Standard_Integer anI1, anI2;
399 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
401 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
402 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
406 else { // usual curve
407 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
408 if ( !aGLine.IsNull() ) {
409 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
412 aPoint = aGLine->Value( aParameter );
414 TopoDS_Vertex V1, V2;
415 TopExp::Vertices(anEdge, V1, V2, Standard_True);
416 if (!V1.IsNull() && !V2.IsNull())
418 aPnt1 = BRep_Tool::Pnt(V1);
419 aPnt2 = BRep_Tool::Pnt(V2);
421 gp_Vec aVec1( aPnt1, aPoint );
422 gp_Vec aVec2( aPnt2, aPoint );
423 double anAngle = aVec1.Angle( aVec2 );
424 isFound = fabs( anAngle - M_PI ) < LOCAL_SELECTION_TOLERANCE;
428 if ( isFound && aMinDelta >= aDelta ) {
433 aFoundPoint = aPoint;
441 thePoint = aFoundPoint;
442 thePoint1 = aFoundPnt1;
443 thePoint2 = aFoundPnt2;
448 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
449 const int theX, const int theY,
450 const Handle(Geom_Curve)& theCurve,
451 Standard_Real& theParameter,
454 bool isFound = false;
455 if ( theView.IsNull() )
458 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
460 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
461 Standard_Integer aNbPoint = aProj.NbPoints();
463 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
464 gp_Pnt aNewPoint = aProj.Point( j );
465 theParameter = aProj.Parameter( j );
468 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
470 int aXDelta = abs( aX - theX );
471 int anYDelta = abs( anY - theY );
472 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
473 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;