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 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
90 const int theISection, TopoDS_Shape& theShape )
92 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
95 int aPointSize = theCurve->getNbPoints( theISection );
96 bool aSectIsClosed = theCurve->isClosed( theISection );
97 if( aSectType == CurveCreator::Polyline )
100 BRep_Builder aBuilder;
101 TopoDS_Compound aComp;
102 aBuilder.MakeCompound(aComp);
105 gp_Pnt aPrevPoint, aPoint;
106 if ( aPointSize == 1 ) {
107 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
108 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
109 aBuilder.Add( aComp, aVertex );
111 else if ( aPointSize > 1 ) {
112 TopoDS_Edge aPointEdge;
113 TopoDS_Vertex aVertex;
114 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
115 aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
116 aBuilder.Add( aComp, aVertex );
118 for( ; iPoint < aPointSize; iPoint++ ) {
119 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
120 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
121 aBuilder.Add( aComp, aVertex );
122 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
123 aBuilder.Add( aComp, aPointEdge );
126 if( theCurve->isClosed( theISection ) && ( aPointSize > 2 ) ) {
127 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
128 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
129 aBuilder.Add( aComp, aVertex );
130 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
131 aBuilder.Add( aComp, aPointEdge );
137 else if( aSectType == CurveCreator::Spline )
142 std::vector<double> aPoints;
143 for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
145 //Handle_AIS_Point anAISPnt = theCurve->getAISPoint( theISection, iPoint );
146 //aSectionRepresentation.push_back( anAISPnt );
148 CurveCreator::Coordinates aCoords = theCurve->getPoint( theISection, iPoint );
149 double aX = aCoords[0];
150 double aY = aCoords[1];
151 double aZ = aCoords[2];
152 aPoints.push_back( aX );
153 aPoints.push_back( aY );
158 Handle(Geom_BSplineCurve) aBSplineCurve;
159 // fill array for algorithm by the received coordinates
160 int aLen = aPoints.size() / 2;
161 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
162 std::vector<double>::const_iterator aListIter = aPoints.begin();
163 for (int ind = 1; ind <= aLen; ind++) {
164 gp_Pnt aPnt(gp::Origin());
165 aPnt.SetX(*aListIter);
167 aPnt.SetY(*aListIter);
170 aHCurvePoints->SetValue(ind, aPnt);
173 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
176 aBSplineCurve = aGBC.Curve();
178 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
180 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
187 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
189 std::list<float> aSelectedPoints;
192 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
193 TopoDS_Vertex aVertex;
194 TopoDS_Shape aShape = theContext->SelectedShape();
195 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
196 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
198 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
199 if ( !anOwner.IsNull() ) {
200 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
201 if ( !anAIS.IsNull() ) {
202 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
203 if ( !aPoint.IsNull() )
204 aVertex = TopoDS::Vertex( aPoint->Vertex() );
206 if ( aVertex.IsNull() ) {
207 // the following happens if there are no points in the current curve, there is only a shape
208 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
209 if ( aBrepOwner.IsNull() )
211 if ( aBrepOwner->HasShape() ) {
212 const TopoDS_Shape& aShape = aBrepOwner->Shape();
213 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
215 aVertex = TopoDS::Vertex( aShape );
221 if ( aVertex.IsNull() )
223 aPnt = BRep_Tool::Pnt( aVertex );
224 aSelectedPoints.push_back( aPnt.X() );
225 aSelectedPoints.push_back( aPnt.Y() );
226 aSelectedPoints.push_back( aPnt.Z() );
229 return aSelectedPoints;
231 //=======================================================================
232 // function : setLocalPointContext
233 // purpose : Open/close the viewer local context
234 //=======================================================================
235 //#define USE_COMPOUND
236 void CurveCreator_Utils::setLocalPointContext(
237 Handle(AIS_InteractiveContext) theContext,
247 // Open local context if there is no one
248 if ( !theContext->HasOpenedContext() ) {
249 theContext->ClearCurrents( false );
250 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
252 AIS_ListOfInteractive aList;
253 theContext->DisplayedObjects( aList );
255 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
258 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
260 Handle(AIS_InteractiveObject) anAIS = it.Value();
261 if ( !anAIS.IsNull() )
263 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
265 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
266 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
267 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
269 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
271 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
272 theContext->Activate( anAIS, TopAbs_VERTEX );
279 if ( theContext->HasOpenedContext() )
280 theContext->CloseAllContexts();
284 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
285 Handle(V3d_View) theView,
286 const int theX, const int theY,
287 gp_Pnt& thePoint, gp_Pnt& thePoint1,
290 bool isFoundPoint = false;
291 if ( theContext.IsNull() )
294 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
295 theContext->NextSelected() ) {
296 TopoDS_Shape aTShape = theContext->SelectedShape();
297 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
300 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
301 if ( anOwner.IsNull() )
303 const TopLoc_Location& aLocation = anOwner->Location();
304 Handle(AIS_InteractiveObject) anAIS =
305 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
306 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
307 thePoint1, thePoint2 );
313 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
314 Handle(AIS_InteractiveObject) theObject,
315 const int theX, const int theY,
317 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
319 bool isFound = false;
321 if ( theObject.IsNull() || theView.IsNull() )
325 Standard_Real aParameter;
327 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
328 if ( !aLine.IsNull() ) {
329 const Handle(Geom_Line) aGLine = aLine->Line();
330 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
332 aPoint = aGLine->Value( aParameter );
334 Handle(Geom_Point) aPStart;
335 Handle(Geom_Point) aPEnd;
336 aLine->Points( aPStart, aPEnd );
337 aPnt1 = aPStart->Pnt();
338 aPnt2 = aPEnd->Pnt();
340 // in case of Geom line a projection is performed to the infinite line,
341 // so it is necessary to bound it by the line size
343 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
344 isFound = !aLineBox.IsOut( aPoint );
348 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
349 if ( !aShape.IsNull() ) {
350 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
351 if ( !aWire.IsNull() ) {
352 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
353 for ( ; anExp.More(); anExp.Next())
355 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
356 if ( !anEdge.IsNull() ) {
357 Standard_Real aFirst, aLast;
358 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
360 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
361 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
362 if ( !aBSplineCurve.IsNull() ) {
363 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
365 aPoint = aBSplineCurve->Value( aParameter );
366 Standard_Integer anI1, anI2;
367 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
369 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
370 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
387 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
388 const int theX, const int theY,
389 const Handle(Geom_Curve)& theCurve,
390 Standard_Real& theParameter )
392 bool isFound = false;
393 if ( theView.IsNull() )
396 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
398 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
399 Standard_Integer aNbPoint = aProj.NbPoints();
401 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
402 gp_Pnt aNewPoint = aProj.Point( j );
403 theParameter = aProj.Parameter( j );
406 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
408 int aXDelta = abs( aX - theX );
409 int anYDelta = abs( anY - theY );
410 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;