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>
45 #include <TopExp_Explorer.hxx>
46 #include <GeomAPI_ProjectPointOnCurve.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <SelectMgr_Selection.hxx>
49 #include <Select3D_SensitivePoint.hxx>
51 #include <BRep_Tool.hxx>
52 #include <BRep_Builder.hxx>
53 #include <BRepBuilderAPI_MakeVertex.hxx>
54 #include <BRepBuilderAPI_MakeEdge.hxx>
55 #include <BRepBuilderAPI_MakeWire.hxx>
57 #include <TColgp_HArray1OfPnt.hxx>
58 #include <GeomAPI_Interpolate.hxx>
60 #include <ProjLib.hxx>
63 #include "CurveCreator_ICurve.hxx"
65 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
66 const int SCENE_PIXEL_TOLERANCE = 10;
68 //=======================================================================
69 // function : ConvertClickToPoint()
70 // purpose : Returns the point clicked in 3D view
71 //=======================================================================
72 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
73 Handle(V3d_View) theView,
76 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
80 //=======================================================================
81 // function : ConvertClickToPoint()
82 // purpose : Returns the point clicked in 3D view
83 //=======================================================================
84 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
86 return GEOMUtils::ConvertClickToPoint( x, y, aView );
89 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
90 TopoDS_Shape& theShape )
92 BRep_Builder aBuilder;
93 TopoDS_Compound aComp;
94 aBuilder.MakeCompound( aComp );
95 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
97 int theISection = iSection;
99 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
100 int aPointSize = theCurve->getNbPoints( theISection );
101 bool aSectIsClosed = theCurve->isClosed( theISection );
102 bool isPolyline = aSectType == CurveCreator::Polyline;
104 gp_Pnt aPrevPoint, aPoint;
105 if ( aPointSize == 1 ) {
106 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
107 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
108 aBuilder.Add( aComp, aVertex );
110 else if ( aPointSize > 1 ) {
111 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
114 TopoDS_Edge aPointEdge;
115 TopoDS_Vertex aVertex;
116 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
117 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
118 aBuilder.Add( aComp, aVertex );
119 aHCurvePoints->SetValue(aHIndex++, aPoint);
122 for( ; iPoint < aPointSize; iPoint++ ) {
123 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
124 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
125 aBuilder.Add( aComp, aVertex );
126 aHCurvePoints->SetValue(aHIndex++, aPoint);
128 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
129 aBuilder.Add( aComp, aPointEdge );
133 if( aSectIsClosed && ( aPointSize > 2 ) ) {
134 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
135 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
136 aBuilder.Add( aComp, aVertex );
138 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
139 aBuilder.Add( aComp, aPointEdge );
144 Handle(Geom_BSplineCurve) aBSplineCurve;
145 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
148 aBSplineCurve = aGBC.Curve();
149 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
150 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
151 aBuilder.Add( aComp, aWire );
161 ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
164 bool operator < ( const ComparePnt& theOtherPoint ) const
166 bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
167 if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
168 isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
169 if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
170 isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
178 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
179 const CurveCreator_ICurve* theCurve,
180 CurveCreator_ICurve::SectionToPointList& thePoints )
184 std::list<float> aSelectedPoints;
186 std::map<ComparePnt, int> aPntMap;
188 CurveCreator_ICurve::SectionToPointList aPoints;
189 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
190 TopoDS_Vertex aVertex;
191 TopoDS_Shape aShape = theContext->SelectedShape();
192 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
193 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
195 if ( aVertex.IsNull() )
197 aPnt = BRep_Tool::Pnt( aVertex );
198 if ( aPntMap.find( aPnt ) != aPntMap.end() )
202 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
203 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
204 aLast = aPoints.end();
205 for ( ; anIt != aLast; anIt++ )
206 thePoints.push_back( *anIt );
210 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
211 const CurveCreator_ICurve* theCurve,
212 const CurveCreator_ICurve::SectionToPointList& thePoints )
214 AIS_ListOfInteractive aDisplayedList;
215 theContext->DisplayedObjects( aDisplayedList );
216 theContext->ClearSelected( Standard_False );
218 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
220 Handle(AIS_InteractiveObject) anAIS = it.Value();
221 if ( anAIS.IsNull() )
223 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
224 if ( anAISShape.IsNull() )
227 //ASL: we convert list of point indices to list of points coordinates
228 int aSize = thePoints.size();
229 std::vector<gp_Pnt> aPntsToSelect( aSize );
231 CurveCreator_ICurve::SectionToPointList::const_iterator
232 aPIt = thePoints.begin(), aPLast = thePoints.end();
233 CurveCreator_ICurve::SectionToPoint aSToPoint;
234 for( int i=0; aPIt != aPLast; aPIt++, i++ )
237 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
238 aPntsToSelect[i] = aPntToSelect;
242 //ASL: we switch off automatic highlight to improve performance of selection
243 theContext->SetAutomaticHilight( Standard_False );
245 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
246 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
248 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
249 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
251 gp_Pnt anOwnerPnt = aSenPnt->Point();
252 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
255 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
256 aLast = thePoints.end();
257 bool isFound = false;
258 for( int i=0; i<aSize; i++ )
260 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
261 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
264 theContext->AddOrRemoveSelected( anOwner, Standard_False );
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 );
277 //=======================================================================
278 // function : setLocalPointContext
279 // purpose : Open/close the viewer local context
280 //=======================================================================
281 //#define USE_GLOBAL_SELECTION
282 void CurveCreator_Utils::setLocalPointContext(
283 Handle(AIS_InteractiveContext) theContext,
286 #ifdef USE_GLOBAL_SELECTION
293 // Open local context if there is no one
294 if ( !theContext->HasOpenedContext() ) {
295 theContext->ClearCurrents( false );
296 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
298 AIS_ListOfInteractive aList;
299 theContext->DisplayedObjects( aList );
301 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
304 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
306 Handle(AIS_InteractiveObject) anAIS = it.Value();
307 if ( !anAIS.IsNull() )
309 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
311 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
312 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
313 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
315 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
317 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
318 theContext->Activate( anAIS, TopAbs_VERTEX );
325 if ( theContext->HasOpenedContext() )
326 theContext->CloseAllContexts();
330 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
331 Handle(V3d_View) theView,
332 const int theX, const int theY,
333 gp_Pnt& thePoint, gp_Pnt& thePoint1,
336 bool isFoundPoint = false;
337 if ( theContext.IsNull() )
340 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
341 theContext->NextSelected() ) {
342 TopoDS_Shape aTShape = theContext->SelectedShape();
343 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
346 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
347 if ( anOwner.IsNull() )
349 const TopLoc_Location& aLocation = anOwner->Location();
350 Handle(AIS_InteractiveObject) anAIS =
351 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
352 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
353 thePoint1, thePoint2 );
359 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
360 Handle(AIS_InteractiveObject) theObject,
361 const int theX, const int theY,
363 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
365 bool isFound = false;
367 if ( theObject.IsNull() || theView.IsNull() )
371 Standard_Real aParameter;
373 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
374 if ( !aLine.IsNull() ) {
375 const Handle(Geom_Line) aGLine = aLine->Line();
376 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
378 aPoint = aGLine->Value( aParameter );
380 Handle(Geom_Point) aPStart;
381 Handle(Geom_Point) aPEnd;
382 aLine->Points( aPStart, aPEnd );
383 aPnt1 = aPStart->Pnt();
384 aPnt2 = aPEnd->Pnt();
386 // in case of Geom line a projection is performed to the infinite line,
387 // so it is necessary to bound it by the line size
389 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
390 isFound = !aLineBox.IsOut( aPoint );
394 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
395 if ( !aShape.IsNull() ) {
396 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
397 if ( !aWire.IsNull() ) {
398 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
399 for ( ; anExp.More(); anExp.Next())
401 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
402 if ( !anEdge.IsNull() ) {
403 Standard_Real aFirst, aLast;
404 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
406 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
407 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
408 if ( !aBSplineCurve.IsNull() ) {
409 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
411 aPoint = aBSplineCurve->Value( aParameter );
412 Standard_Integer anI1, anI2;
413 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
415 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
416 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
433 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
434 const int theX, const int theY,
435 const Handle(Geom_Curve)& theCurve,
436 Standard_Real& theParameter )
438 bool isFound = false;
439 if ( theView.IsNull() )
442 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
444 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
445 Standard_Integer aNbPoint = aProj.NbPoints();
447 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
448 gp_Pnt aNewPoint = aProj.Point( j );
449 theParameter = aProj.Parameter( j );
452 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
454 int aXDelta = abs( aX - theX );
455 int anYDelta = abs( anY - theY );
456 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;