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_Point.hxx>
38 #include <AIS_Line.hxx>
39 #include <AIS_Trihedron.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>
49 #include <BRep_Tool.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRepBuilderAPI_MakeVertex.hxx>
52 #include <BRepBuilderAPI_MakeEdge.hxx>
53 #include <BRepBuilderAPI_MakeWire.hxx>
55 #include <TColgp_HArray1OfPnt.hxx>
56 #include <GeomAPI_Interpolate.hxx>
58 #include <ProjLib.hxx>
61 #include "CurveCreator_ICurve.hxx"
63 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
64 const int SCENE_PIXEL_TOLERANCE = 10;
66 //=======================================================================
67 // function : ConvertClickToPoint()
68 // purpose : Returns the point clicked in 3D view
69 //=======================================================================
70 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
71 Handle(V3d_View) theView,
74 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
78 //=======================================================================
79 // function : ConvertClickToPoint()
80 // purpose : Returns the point clicked in 3D view
81 //=======================================================================
82 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
84 return GEOMUtils::ConvertClickToPoint( x, y, aView );
87 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
88 TopoDS_Shape& theShape )
90 BRep_Builder aBuilder;
91 TopoDS_Compound aComp;
92 aBuilder.MakeCompound( aComp );
93 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
95 int theISection = iSection;
97 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
98 int aPointSize = theCurve->getNbPoints( theISection );
99 bool aSectIsClosed = theCurve->isClosed( theISection );
100 bool isPolyline = aSectType == CurveCreator::Polyline;
102 gp_Pnt aPrevPoint, aPoint;
103 if ( aPointSize == 1 ) {
104 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
105 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
106 aBuilder.Add( aComp, aVertex );
108 else if ( aPointSize > 1 ) {
109 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
112 TopoDS_Edge aPointEdge;
113 TopoDS_Vertex aVertex;
114 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
115 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
116 aBuilder.Add( aComp, aVertex );
117 aHCurvePoints->SetValue(aHIndex++, aPoint);
120 for( ; iPoint < aPointSize; iPoint++ ) {
121 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
122 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
123 aBuilder.Add( aComp, aVertex );
124 aHCurvePoints->SetValue(aHIndex++, aPoint);
126 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
127 aBuilder.Add( aComp, aPointEdge );
131 if( aSectIsClosed && ( aPointSize > 2 ) ) {
132 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
133 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
134 aBuilder.Add( aComp, aVertex );
136 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
137 aBuilder.Add( aComp, aPointEdge );
142 Handle(Geom_BSplineCurve) aBSplineCurve;
143 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
146 aBSplineCurve = aGBC.Curve();
147 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
148 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
149 aBuilder.Add( aComp, aWire );
159 ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
162 bool operator < ( const ComparePnt& theOtherPoint ) const
164 bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
165 if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
166 isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
167 if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
168 isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
176 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
177 const CurveCreator_ICurve* theCurve,
178 CurveCreator_ICurve::SectionToPointList& thePoints )
182 std::list<float> aSelectedPoints;
184 std::map<ComparePnt, int> aPntMap;
186 CurveCreator_ICurve::SectionToPointList aPoints;
187 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
188 TopoDS_Vertex aVertex;
189 TopoDS_Shape aShape = theContext->SelectedShape();
190 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
191 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
193 if ( aVertex.IsNull() )
195 aPnt = BRep_Tool::Pnt( aVertex );
196 if ( aPntMap.find( aPnt ) != aPntMap.end() )
200 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
201 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
202 aLast = aPoints.end();
203 for ( ; anIt != aLast; anIt++ )
204 thePoints.push_back( *anIt );
208 //=======================================================================
209 // function : setLocalPointContext
210 // purpose : Open/close the viewer local context
211 //=======================================================================
212 //#define USE_GLOBAL_SELECTION
213 void CurveCreator_Utils::setLocalPointContext(
214 Handle(AIS_InteractiveContext) theContext,
217 #ifdef USE_GLOBAL_SELECTION
224 // Open local context if there is no one
225 if ( !theContext->HasOpenedContext() ) {
226 theContext->ClearCurrents( false );
227 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
229 AIS_ListOfInteractive aList;
230 theContext->DisplayedObjects( aList );
232 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
235 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
237 Handle(AIS_InteractiveObject) anAIS = it.Value();
238 if ( !anAIS.IsNull() )
240 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
242 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
243 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
244 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
246 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
248 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
249 theContext->Activate( anAIS, TopAbs_VERTEX );
256 if ( theContext->HasOpenedContext() )
257 theContext->CloseAllContexts();
261 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
262 Handle(V3d_View) theView,
263 const int theX, const int theY,
264 gp_Pnt& thePoint, gp_Pnt& thePoint1,
267 bool isFoundPoint = false;
268 if ( theContext.IsNull() )
271 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
272 theContext->NextSelected() ) {
273 TopoDS_Shape aTShape = theContext->SelectedShape();
274 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
277 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
278 if ( anOwner.IsNull() )
280 const TopLoc_Location& aLocation = anOwner->Location();
281 Handle(AIS_InteractiveObject) anAIS =
282 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
283 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
284 thePoint1, thePoint2 );
290 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
291 Handle(AIS_InteractiveObject) theObject,
292 const int theX, const int theY,
294 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
296 bool isFound = false;
298 if ( theObject.IsNull() || theView.IsNull() )
302 Standard_Real aParameter;
304 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
305 if ( !aLine.IsNull() ) {
306 const Handle(Geom_Line) aGLine = aLine->Line();
307 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
309 aPoint = aGLine->Value( aParameter );
311 Handle(Geom_Point) aPStart;
312 Handle(Geom_Point) aPEnd;
313 aLine->Points( aPStart, aPEnd );
314 aPnt1 = aPStart->Pnt();
315 aPnt2 = aPEnd->Pnt();
317 // in case of Geom line a projection is performed to the infinite line,
318 // so it is necessary to bound it by the line size
320 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
321 isFound = !aLineBox.IsOut( aPoint );
325 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
326 if ( !aShape.IsNull() ) {
327 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
328 if ( !aWire.IsNull() ) {
329 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
330 for ( ; anExp.More(); anExp.Next())
332 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
333 if ( !anEdge.IsNull() ) {
334 Standard_Real aFirst, aLast;
335 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
337 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
338 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
339 if ( !aBSplineCurve.IsNull() ) {
340 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
342 aPoint = aBSplineCurve->Value( aParameter );
343 Standard_Integer anI1, anI2;
344 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
346 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
347 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
364 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
365 const int theX, const int theY,
366 const Handle(Geom_Curve)& theCurve,
367 Standard_Real& theParameter )
369 bool isFound = false;
370 if ( theView.IsNull() )
373 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
375 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
376 Standard_Integer aNbPoint = aProj.NbPoints();
378 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
379 gp_Pnt aNewPoint = aProj.Point( j );
380 theParameter = aProj.Parameter( j );
383 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
385 int aXDelta = abs( aX - theX );
386 int anYDelta = abs( anY - theY );
387 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;