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 if ( aPointSize == 0 )
108 bool aSectIsClosed = theCurve->isClosed( theISection );
109 bool isPolyline = aSectType == CurveCreator::Polyline;
112 gp_Pnt aPrevPoint, aPoint;
113 // filters the curve points to skip equal points
114 std::vector<gp_Pnt> aPoints;
115 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
116 aPoints.push_back( aPoint );
119 for( ; iPoint < aPointSize; iPoint++ ) {
120 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
121 if ( !isEqualPoints( aPrevPoint, aPoint ) )
122 aPoints.push_back( aPoint );
125 int aNbPoints = aPoints.size();
127 if ( aNbPoints == 1 ) {
128 aPoint = aPoints.front();
129 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
130 aBuilder.Add( aComp, aVertex );
132 else if ( aNbPoints > 1 ) {
133 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aNbPoints);
135 TopoDS_Edge aPointEdge;
136 TopoDS_Vertex aVertex;
138 std::vector<gp_Pnt>::const_iterator aPointIt = aPoints.begin(), aPointLast = aPoints.end();
142 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
143 aBuilder.Add( aComp, aVertex );
144 aHCurvePoints->SetValue(aHIndex++, aPoint);
147 for( ; aPointIt != aPointLast; aPointIt++ ) {
149 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
150 aBuilder.Add( aComp, aVertex );
151 aHCurvePoints->SetValue(aHIndex++, aPoint);
153 TopoDS_Edge aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
154 aBuilder.Add( aComp, aPointEdge );
158 if( aSectIsClosed && ( aNbPoints > 2 ) ) {
159 aPoint = aPoints.front();
160 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
161 aBuilder.Add( aComp, aVertex );
163 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
164 aBuilder.Add( aComp, aPointEdge );
169 Handle(Geom_BSplineCurve) aBSplineCurve;
170 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
173 aBSplineCurve = aGBC.Curve();
174 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
175 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
176 aBuilder.Add( aComp, aWire );
183 class CompareSectionToPoint
186 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
187 : mySectionId( theISection ), myPointId( theIPoint ) {};
188 ~CompareSectionToPoint() {}
190 bool operator < ( const CompareSectionToPoint& theOther ) const
192 bool isLess = mySectionId < theOther.mySectionId;
193 if ( !isLess && mySectionId == theOther.mySectionId )
194 isLess = myPointId < theOther.myPointId;
204 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
205 const CurveCreator_ICurve* theCurve,
206 CurveCreator_ICurve::SectionToPointList& thePoints )
210 std::list<float> aSelectedPoints;
212 std::map<CompareSectionToPoint, int> aPointsMap;
214 CurveCreator_ICurve::SectionToPointList aPoints;
215 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
216 TopoDS_Vertex aVertex;
217 TopoDS_Shape aShape = theContext->SelectedShape();
218 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
219 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
221 if ( aVertex.IsNull() )
223 aPnt = BRep_Tool::Pnt( aVertex );
225 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
226 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
227 aLast = aPoints.end();
228 CompareSectionToPoint aPoint;
229 for ( ; anIt != aLast; anIt++ ) {
230 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
231 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
233 aPointsMap[aPoint] = 0;
235 thePoints.push_back( *anIt );
240 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
241 const CurveCreator_ICurve* theCurve,
242 const CurveCreator_ICurve::SectionToPointList& thePoints )
247 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
248 if ( anAIS.IsNull() )
250 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
251 if ( anAISShape.IsNull() )
254 //ASL: we convert list of point indices to list of points coordinates
255 int aSize = thePoints.size();
256 std::vector<gp_Pnt> aPntsToSelect( aSize );
258 CurveCreator_ICurve::SectionToPointList::const_iterator
259 aPIt = thePoints.begin(), aPLast = thePoints.end();
260 CurveCreator_ICurve::SectionToPoint aSToPoint;
261 for( int i=0; aPIt != aPLast; aPIt++, i++ )
264 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
265 aPntsToSelect[i] = aPntToSelect;
268 theContext->ClearSelected( Standard_False );
269 //ASL: we switch off automatic highlight to improve performance of selection
270 theContext->SetAutomaticHilight( Standard_False );
272 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
273 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
275 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
276 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
278 gp_Pnt anOwnerPnt = aSenPnt->Point();
279 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
282 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
283 aLast = thePoints.end();
284 bool isFound = false;
285 for( int i=0; i<aSize; i++ )
287 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
288 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
291 theContext->AddOrRemoveSelected( anOwner, Standard_False );
297 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
298 // and call HilightPicked to draw selected owners
299 theContext->SetAutomaticHilight( Standard_True );
300 theContext->LocalContext()->HilightPicked( Standard_True );
303 //=======================================================================
304 // function : setLocalPointContext
305 // purpose : Open/close the viewer local context
306 //=======================================================================
307 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
308 Handle(AIS_InteractiveContext) theContext,
315 // Open local context if there is no one
316 if ( !theContext->HasOpenedContext() ) {
317 theContext->ClearCurrents( false );
318 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
320 // load the curve AIS object to the local context with the point selection
321 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
322 if ( !anAIS.IsNull() )
324 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
326 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
327 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
332 if ( theContext->HasOpenedContext() )
333 theContext->CloseAllContexts();
337 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
338 Handle(AIS_InteractiveObject) theObject,
339 const int theX, const int theY,
341 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
343 bool isFullFound = false;
345 if ( theObject.IsNull() || theView.IsNull() )
347 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
348 if ( aShape.IsNull() )
350 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
351 if ( aCompound.IsNull() )
354 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
355 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
356 Standard_Real aParameter;
357 bool isFound = false;
358 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
359 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
360 for ( ; anExp.More(); anExp.Next())
362 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
363 if ( anEdge.IsNull() )
365 Standard_Real aFirst, aLast;
366 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
367 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
368 Handle(Geom_BSplineCurve) aBSplineCurve =
369 Handle(Geom_BSplineCurve)::DownCast( aCurve );
370 if ( !aBSplineCurve.IsNull() ) {
371 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
372 aParameter, aDelta );
374 aCurPoint = aBSplineCurve->Value( aParameter );
375 Standard_Integer anI1, anI2;
376 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
377 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
378 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
382 else { // a curve built on a polyline edge
383 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
384 if ( aGLine.IsNull() )
386 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
389 aCurPoint = aGLine->Value( aParameter );
390 TopoDS_Vertex V1, V2;
391 TopExp::Vertices( anEdge, V1, V2, Standard_True );
392 if ( V1.IsNull() || V2.IsNull() )
394 aCurPoint1 = BRep_Tool::Pnt(V1);
395 aCurPoint2 = BRep_Tool::Pnt(V2);
397 // check that the projected point is on the bounded curve
398 gp_Vec aVec1( aCurPoint1, aCurPoint );
399 gp_Vec aVec2( aCurPoint2, aCurPoint );
400 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
403 if ( isFound && aMinDelta >= aDelta ) {
407 aFoundPnt1 = aCurPoint1;
408 aFoundPnt2 = aCurPoint2;
409 aFoundPoint = aCurPoint;
413 int aX, anY, aX1, anY1, aX2, anY2;
415 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
416 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
417 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
419 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
420 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
422 thePoint = aFoundPoint;
423 thePoint1 = aFoundPnt1;
424 thePoint2 = aFoundPnt2;
430 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
431 const int theX, const int theY,
432 const Handle(Geom_Curve)& theCurve,
433 Standard_Real& theParameter,
436 bool isFound = false;
437 if ( theView.IsNull() )
440 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
442 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
443 Standard_Integer aNbPoint = aProj.NbPoints();
445 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
446 gp_Pnt aNewPoint = aProj.Point( j );
447 theParameter = aProj.Parameter( j );
450 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
452 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
458 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
459 const int theOtherY, const double theTolerance, int& theDelta )
461 int aXDelta = abs( theX - theOtherX );
462 int anYDelta = abs( theY - theOtherY );
464 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
466 return aXDelta < theTolerance && anYDelta < theTolerance;
469 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
471 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );