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 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
218 std::list<float> aSelectedPoints;
221 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
222 TopoDS_Vertex aVertex;
223 TopoDS_Shape aShape = theContext->SelectedShape();
224 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
225 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
227 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
228 if ( !anOwner.IsNull() ) {
229 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
230 if ( !anAIS.IsNull() ) {
231 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
232 if ( !aPoint.IsNull() )
233 aVertex = TopoDS::Vertex( aPoint->Vertex() );
235 if ( aVertex.IsNull() ) {
236 // the following happens if there are no points in the current curve, there is only a shape
237 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
238 if ( aBrepOwner.IsNull() )
240 if ( aBrepOwner->HasShape() ) {
241 const TopoDS_Shape& aShape = aBrepOwner->Shape();
242 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
244 aVertex = TopoDS::Vertex( aShape );
250 if ( aVertex.IsNull() )
252 aPnt = BRep_Tool::Pnt( aVertex );
253 aSelectedPoints.push_back( aPnt.X() );
254 aSelectedPoints.push_back( aPnt.Y() );
255 aSelectedPoints.push_back( aPnt.Z() );
258 return aSelectedPoints;
260 //=======================================================================
261 // function : setLocalPointContext
262 // purpose : Open/close the viewer local context
263 //=======================================================================
264 //#define USE_GLOBAL_SELECTION
265 void CurveCreator_Utils::setLocalPointContext(
266 Handle(AIS_InteractiveContext) theContext,
269 #ifdef USE_GLOBAL_SELECTION
276 // Open local context if there is no one
277 if ( !theContext->HasOpenedContext() ) {
278 theContext->ClearCurrents( false );
279 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
281 AIS_ListOfInteractive aList;
282 theContext->DisplayedObjects( aList );
284 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
287 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
289 Handle(AIS_InteractiveObject) anAIS = it.Value();
290 if ( !anAIS.IsNull() )
292 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
294 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
295 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
296 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
298 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
300 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
301 theContext->Activate( anAIS, TopAbs_VERTEX );
308 if ( theContext->HasOpenedContext() )
309 theContext->CloseAllContexts();
313 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
314 Handle(V3d_View) theView,
315 const int theX, const int theY,
316 gp_Pnt& thePoint, gp_Pnt& thePoint1,
319 bool isFoundPoint = false;
320 if ( theContext.IsNull() )
323 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
324 theContext->NextSelected() ) {
325 TopoDS_Shape aTShape = theContext->SelectedShape();
326 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
329 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
330 if ( anOwner.IsNull() )
332 const TopLoc_Location& aLocation = anOwner->Location();
333 Handle(AIS_InteractiveObject) anAIS =
334 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
335 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
336 thePoint1, thePoint2 );
342 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
343 Handle(AIS_InteractiveObject) theObject,
344 const int theX, const int theY,
346 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
348 bool isFound = false;
350 if ( theObject.IsNull() || theView.IsNull() )
354 Standard_Real aParameter;
356 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
357 if ( !aLine.IsNull() ) {
358 const Handle(Geom_Line) aGLine = aLine->Line();
359 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
361 aPoint = aGLine->Value( aParameter );
363 Handle(Geom_Point) aPStart;
364 Handle(Geom_Point) aPEnd;
365 aLine->Points( aPStart, aPEnd );
366 aPnt1 = aPStart->Pnt();
367 aPnt2 = aPEnd->Pnt();
369 // in case of Geom line a projection is performed to the infinite line,
370 // so it is necessary to bound it by the line size
372 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
373 isFound = !aLineBox.IsOut( aPoint );
377 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
378 if ( !aShape.IsNull() ) {
379 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
380 if ( !aWire.IsNull() ) {
381 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
382 for ( ; anExp.More(); anExp.Next())
384 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
385 if ( !anEdge.IsNull() ) {
386 Standard_Real aFirst, aLast;
387 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
389 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
390 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
391 if ( !aBSplineCurve.IsNull() ) {
392 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
394 aPoint = aBSplineCurve->Value( aParameter );
395 Standard_Integer anI1, anI2;
396 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
398 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
399 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
416 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
417 const int theX, const int theY,
418 const Handle(Geom_Curve)& theCurve,
419 Standard_Real& theParameter )
421 bool isFound = false;
422 if ( theView.IsNull() )
425 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
427 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
428 Standard_Integer aNbPoint = aProj.NbPoints();
430 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
431 gp_Pnt aNewPoint = aProj.Point( j );
432 theParameter = aProj.Parameter( j );
435 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
437 int aXDelta = abs( aX - theX );
438 int anYDelta = abs( anY - theY );
439 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;