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 );
88 //#define USE_COMPOUND
89 #include "CurveCreator_Curve.hxx" // TODO - remove
90 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
91 const int theISection, TopoDS_Shape& theShape,
92 std::vector<Handle_AIS_InteractiveObject>& aSectionRepresentation )
94 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
97 int aPointSize = theCurve->getNbPoints( theISection );
98 bool aSectIsClosed = theCurve->isClosed( theISection );
99 if( aSectType == CurveCreator::Polyline )
102 BRep_Builder aBuilder;
103 TopoDS_Compound aComp;
104 aBuilder.MakeCompound(aComp);
107 gp_Pnt aPrevPoint, aPoint;
108 if ( aPointSize == 1 ) {
109 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
110 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
111 aBuilder.Add( aComp, aVertex );
113 else if ( aPointSize > 1 ) {
114 TopoDS_Edge aPointEdge;
115 TopoDS_Vertex aVertex;
116 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
117 aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
118 aBuilder.Add( aComp, aVertex );
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 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
125 aBuilder.Add( aComp, aPointEdge );
128 if( aSectIsClosed && ( aPointSize > 2 ) ) {
129 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
130 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
131 aBuilder.Add( aComp, aVertex );
132 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
133 aBuilder.Add( aComp, aPointEdge );
139 else if( aSectType == CurveCreator::Spline )
141 std::vector<double> aPoints;
142 for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
144 CurveCreator::Coordinates aCoords = theCurve->getPoint( theISection, iPoint );
145 double aX = aCoords[0];
146 double aY = aCoords[1];
147 double aZ = aCoords[2];
148 aPoints.push_back( aX );
149 aPoints.push_back( aY );
154 Handle(Geom_BSplineCurve) aBSplineCurve;
155 // fill array for algorithm by the received coordinates
156 int aLen = aPoints.size() / 2;
157 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
158 std::vector<double>::const_iterator aListIter = aPoints.begin();
159 for (int ind = 1; ind <= aLen; ind++) {
160 gp_Pnt aPnt(gp::Origin());
161 aPnt.SetX(*aListIter);
163 aPnt.SetY(*aListIter);
166 aHCurvePoints->SetValue(ind, aPnt);
169 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
172 aBSplineCurve = aGBC.Curve();
174 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
176 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
181 const CurveCreator_Curve* aCurve = dynamic_cast<const CurveCreator_Curve*>( theCurve );
185 if( aSectType == CurveCreator::Polyline )
189 for( ; iPoint < ( aPointSize - 1 ) ; iPoint++ ){
190 Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint);
191 aSectionRepresentation.push_back( anAISPnt );
192 Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, iPoint+1 );
193 aSectionRepresentation.push_back( aLine );
195 if( aPointSize != 0 ){
196 Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint);
197 aSectionRepresentation.push_back( anAISPnt );
198 if( aSectIsClosed && ( aPointSize > 1 ) ){
199 Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, 0 );
200 aSectionRepresentation.push_back( aLine );
205 else if( aSectType == CurveCreator::Spline )
207 std::vector<double> aPoints;
208 for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
210 Handle_AIS_Point anAISPnt = aCurve->getAISPoint( theISection, iPoint );
211 aSectionRepresentation.push_back( anAISPnt );
216 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
217 TopoDS_Shape& theShape )
219 BRep_Builder aBuilder;
220 TopoDS_Compound aComp;
221 aBuilder.MakeCompound( aComp );
222 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
224 int theISection = iSection;
226 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
227 int aPointSize = theCurve->getNbPoints( theISection );
228 bool aSectIsClosed = theCurve->isClosed( theISection );
229 bool isPolyline = aSectType == CurveCreator::Polyline;
231 gp_Pnt aPrevPoint, aPoint;
232 if ( aPointSize == 1 ) {
233 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
234 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
235 aBuilder.Add( aComp, aVertex );
237 else if ( aPointSize > 1 ) {
238 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
241 TopoDS_Edge aPointEdge;
242 TopoDS_Vertex aVertex;
243 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
244 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
245 aBuilder.Add( aComp, aVertex );
246 aHCurvePoints->SetValue(aHIndex++, aPoint);
249 for( ; iPoint < aPointSize; iPoint++ ) {
250 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
251 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
252 aBuilder.Add( aComp, aVertex );
253 aHCurvePoints->SetValue(aHIndex++, aPoint);
255 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
256 aBuilder.Add( aComp, aPointEdge );
260 if( aSectIsClosed && ( aPointSize > 2 ) ) {
261 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
262 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
263 aBuilder.Add( aComp, aVertex );
265 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
266 aBuilder.Add( aComp, aPointEdge );
271 Handle(Geom_BSplineCurve) aBSplineCurve;
272 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
275 aBSplineCurve = aGBC.Curve();
276 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
277 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
278 aBuilder.Add( aComp, aWire );
286 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
288 std::list<float> aSelectedPoints;
291 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
292 TopoDS_Vertex aVertex;
293 TopoDS_Shape aShape = theContext->SelectedShape();
294 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
295 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
297 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
298 if ( !anOwner.IsNull() ) {
299 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
300 if ( !anAIS.IsNull() ) {
301 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
302 if ( !aPoint.IsNull() )
303 aVertex = TopoDS::Vertex( aPoint->Vertex() );
305 if ( aVertex.IsNull() ) {
306 // the following happens if there are no points in the current curve, there is only a shape
307 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
308 if ( aBrepOwner.IsNull() )
310 if ( aBrepOwner->HasShape() ) {
311 const TopoDS_Shape& aShape = aBrepOwner->Shape();
312 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
314 aVertex = TopoDS::Vertex( aShape );
320 if ( aVertex.IsNull() )
322 aPnt = BRep_Tool::Pnt( aVertex );
323 aSelectedPoints.push_back( aPnt.X() );
324 aSelectedPoints.push_back( aPnt.Y() );
325 aSelectedPoints.push_back( aPnt.Z() );
328 return aSelectedPoints;
330 //=======================================================================
331 // function : setLocalPointContext
332 // purpose : Open/close the viewer local context
333 //=======================================================================
334 //#define USE_GLOBAL_SELECTION
335 void CurveCreator_Utils::setLocalPointContext(
336 Handle(AIS_InteractiveContext) theContext,
339 #ifdef USE_GLOBAL_SELECTION
346 // Open local context if there is no one
347 if ( !theContext->HasOpenedContext() ) {
348 theContext->ClearCurrents( false );
349 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
351 AIS_ListOfInteractive aList;
352 theContext->DisplayedObjects( aList );
354 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
357 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
359 Handle(AIS_InteractiveObject) anAIS = it.Value();
360 if ( !anAIS.IsNull() )
362 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
364 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
365 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
366 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
368 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
370 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
371 theContext->Activate( anAIS, TopAbs_VERTEX );
378 if ( theContext->HasOpenedContext() )
379 theContext->CloseAllContexts();
383 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
384 Handle(V3d_View) theView,
385 const int theX, const int theY,
386 gp_Pnt& thePoint, gp_Pnt& thePoint1,
389 bool isFoundPoint = false;
390 if ( theContext.IsNull() )
393 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
394 theContext->NextSelected() ) {
395 TopoDS_Shape aTShape = theContext->SelectedShape();
396 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
399 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
400 if ( anOwner.IsNull() )
402 const TopLoc_Location& aLocation = anOwner->Location();
403 Handle(AIS_InteractiveObject) anAIS =
404 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
405 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
406 thePoint1, thePoint2 );
412 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
413 Handle(AIS_InteractiveObject) theObject,
414 const int theX, const int theY,
416 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
418 bool isFound = false;
420 if ( theObject.IsNull() || theView.IsNull() )
424 Standard_Real aParameter;
426 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
427 if ( !aLine.IsNull() ) {
428 const Handle(Geom_Line) aGLine = aLine->Line();
429 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
431 aPoint = aGLine->Value( aParameter );
433 Handle(Geom_Point) aPStart;
434 Handle(Geom_Point) aPEnd;
435 aLine->Points( aPStart, aPEnd );
436 aPnt1 = aPStart->Pnt();
437 aPnt2 = aPEnd->Pnt();
439 // in case of Geom line a projection is performed to the infinite line,
440 // so it is necessary to bound it by the line size
442 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
443 isFound = !aLineBox.IsOut( aPoint );
447 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
448 if ( !aShape.IsNull() ) {
449 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
450 if ( !aWire.IsNull() ) {
451 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
452 for ( ; anExp.More(); anExp.Next())
454 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
455 if ( !anEdge.IsNull() ) {
456 Standard_Real aFirst, aLast;
457 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
459 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
460 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
461 if ( !aBSplineCurve.IsNull() ) {
462 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
464 aPoint = aBSplineCurve->Value( aParameter );
465 Standard_Integer anI1, anI2;
466 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
468 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
469 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
486 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
487 const int theX, const int theY,
488 const Handle(Geom_Curve)& theCurve,
489 Standard_Real& theParameter )
491 bool isFound = false;
492 if ( theView.IsNull() )
495 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
497 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
498 Standard_Integer aNbPoint = aProj.NbPoints();
500 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
501 gp_Pnt aNewPoint = aProj.Point( j );
502 theParameter = aProj.Parameter( j );
505 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
507 int aXDelta = abs( aX - theX );
508 int anYDelta = abs( anY - theY );
509 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;