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.hxx"
21 #include "CurveCreator.hxx"
22 #include "CurveCreator_Curve.hxx"
23 #include "CurveCreator_UtilsICurve.hxx"
25 #include <GEOMUtils.hxx>
30 #include <TopoDS_Vertex.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Compound.hxx>
35 #include <AIS_ListOfInteractive.hxx>
36 #include <AIS_ListIteratorOfListOfInteractive.hxx>
37 #include <AIS_Shape.hxx>
38 #include <AIS_Line.hxx>
39 #include <AIS_Trihedron.hxx>
40 #include <AIS_LocalContext.hxx>
42 #include <Geom_Point.hxx>
43 #include <Geom_BSplineCurve.hxx>
44 #include <Geom_Line.hxx>
45 #include <Geom_Curve.hxx>
46 #include <Geom_TrimmedCurve.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <GeomAPI_ProjectPointOnCurve.hxx>
52 #include <SelectMgr_EntityOwner.hxx>
53 #include <SelectMgr_Selection.hxx>
54 #include <Select3D_SensitivePoint.hxx>
56 #include <BRep_Tool.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BRepBuilderAPI_MakeVertex.hxx>
59 #include <BRepBuilderAPI_MakeEdge.hxx>
60 #include <BRepBuilderAPI_MakeWire.hxx>
61 #include <BRepTools_WireExplorer.hxx>
63 #include <TColgp_HArray1OfPnt.hxx>
64 #include <TColStd_HArray1OfBoolean.hxx>
65 #include <TColStd_Array1OfReal.hxx>
66 #include <TColgp_Array1OfVec.hxx>
67 #include <GeomAPI_Interpolate.hxx>
69 #include <ProjLib.hxx>
74 #include "CurveCreator_ICurve.hxx"
76 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
77 const int SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
78 const int SCENE_PIXEL_POINT_TOLERANCE = 5;
86 * This static function returns the curve of original type from the edge.
88 * \param theEdge the edge
89 * \return the curve of original type. Can be null handle.
91 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
93 Handle(Geom_Curve) aResult;
95 if (theEdge.IsNull()) {
102 aResult = BRep_Tool::Curve(theEdge, aF, aL);
104 if (aResult.IsNull()) {
108 // Get the curve of original type
109 Handle(Standard_Type) aType = aResult->DynamicType();
111 while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
112 Handle(Geom_TrimmedCurve) aTrCurve =
113 Handle(Geom_TrimmedCurve)::DownCast(aResult);
115 aResult = aTrCurve->BasisCurve();
116 aType = aResult->DynamicType();
122 //=======================================================================
123 // function : ConvertClickToPoint()
124 // purpose : Returns the point clicked in 3D view
125 //=======================================================================
126 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
127 Handle(V3d_View) theView,
130 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
134 //=======================================================================
135 // function : ConvertClickToPoint()
136 // purpose : Returns the point clicked in 3D view
137 //=======================================================================
138 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
140 // the 3D point, that is a projection of the pixels to the XYZ view plane
141 //return GEOMUtils::ConvertClickToPoint( x, y, aView );
143 // we need the projection to the XOY plane
144 // 1. find a point in the plane of the eye and the normal to the plane
145 Standard_Real X, Y, Z;
146 Quantity_Parameter Vx, Vy, Vz;
147 aView->ConvertWithProj( x, y, X, Y, Z, Vx, Vy, Vz );
149 // 2. build a ray from the point by the normal to the XOY plane and intersect it
150 // The ray equation is the following : p(x,y,z) = p0(x,y,z) + t*V(x,y,z)
151 // X,Y,Z - defines p0(x,y,z), Vx,Vy,Vz - defines V(x,y,z)
152 // p(x,y,z) - is a searched point, t - should to be calculated by the condition of XOY plane
153 // The system of equations is the following:
154 // p(x) = p0(x)+t*V(x)
155 // p(y) = p0(y)+t*V(y)
156 // p(z) = p0(z)+t*V(z)
159 Standard_Real aXp, aYp, aZp;
160 //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
161 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
162 if ( fabs( Vz ) > aPrec ) {
163 Standard_Real aT = -Z/Vz;
168 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
170 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
172 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
175 /*std::cout << "ConvertClickToPoint: " << std::endl
176 << "XYZ1 = (" << X << ", " << Y << ", " << Z << "); " << std::endl
177 << "Vxyz = (" << Vx << ", " << Vy << ", " << Vz << "); " << std::endl
178 << "Resp = (" << aXp << ", " << aYp << ", " << aZp << "); " << std::endl;*/
180 gp_Pnt ResultPoint( aXp, aYp, aZp );
184 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
185 TopoDS_Shape& theShape )
187 BRep_Builder aBuilder;
188 TopoDS_Compound aComp;
189 aBuilder.MakeCompound( aComp );
190 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
192 int theISection = iSection;
194 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
195 int aPointSize = theCurve->getNbPoints( theISection );
196 if ( aPointSize == 0 )
199 bool aSectIsClosed = theCurve->isClosed( theISection );
200 bool isPolyline = aSectType == CurveCreator::Polyline;
203 gp_Pnt aPrevPoint, aPoint;
204 // filters the curve points to skip equal points
205 std::vector<gp_Pnt> aPoints;
206 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
207 aPoints.push_back( aPoint );
210 for( ; iPoint < aPointSize; iPoint++ ) {
211 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
212 if ( !isEqualPoints( aPrevPoint, aPoint ) )
213 aPoints.push_back( aPoint );
216 int aNbPoints = aPoints.size();
218 if ( aNbPoints == 1 ) {
219 aPoint = aPoints.front();
220 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
221 aBuilder.Add( aComp, aVertex );
223 else if ( aNbPoints > 1 ) {
224 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aNbPoints);
225 TColgp_Array1OfVec aTangents(1, aNbPoints);
226 Handle(TColStd_HArray1OfBoolean) aTangentFlags = new TColStd_HArray1OfBoolean(1, aNbPoints);
227 gp_Vec aNullVec(0, 0, 0);
229 TopoDS_Edge aPointEdge;
230 TopoDS_Vertex aVertex;
232 std::vector<gp_Pnt>::const_iterator aPointIt = aPoints.begin(), aPointLast = aPoints.end();
236 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
237 aBuilder.Add( aComp, aVertex );
239 aHCurvePoints->SetValue( aHIndex, aPoint );
240 aTangents.SetValue( aHIndex, aNullVec );
241 aTangentFlags->SetValue( aHIndex, Standard_False );
247 for( ; aPointIt != aPointLast; aPointIt++ ) {
249 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
250 aBuilder.Add( aComp, aVertex );
252 TopoDS_Edge aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
253 aBuilder.Add( aComp, aPointEdge );
256 aHCurvePoints->SetValue( aHIndex, aPoint );
257 aTangents.SetValue( aHIndex, aNullVec );
258 aTangentFlags->SetValue( aHIndex, Standard_False );
263 if( aSectIsClosed && ( aNbPoints > 2 ) ) {
264 aPoint = aPoints.front();
265 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
266 aBuilder.Add( aComp, aVertex );
268 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
269 aBuilder.Add( aComp, aPointEdge );
274 Handle(Geom_BSplineCurve) aBSplineCurve;
275 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
276 // correct the spline degree to be as 3 for non-periodic spline if number of points
277 // less than 3. It is need to have a knot in each spline point. This knots are used
278 // to found a neighbour points when a new point is inserted between two existing.
279 if (!aSectIsClosed ) {
280 if (aHCurvePoints->Length() == 3)
281 aGBC.Load(aTangents, aTangentFlags);
286 aBSplineCurve = aGBC.Curve();
287 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
288 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
289 aBuilder.Add( aComp, aWire );
297 * This is an intermediate structure for curve construction.
301 Section3D() : myIsClosed(false), myIsBSpline(false)
306 Handle(TColgp_HArray1OfPnt) myPoints;
309 //=======================================================================
310 // function : constructCurve
312 //=======================================================================
313 bool CurveCreator_Utils::constructCurve
314 (const TopoDS_Shape theShape,
315 CurveCreator_Curve *theCurve,
318 if (theShape.IsNull()) {
322 // Collect wires or vertices from shape.
323 TopTools_ListOfShape aWOrV;
324 TopAbs_ShapeEnum aType = theShape.ShapeType();
326 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
327 aWOrV.Append(theShape);
328 } else if (aType == TopAbs_COMPOUND) {
329 TopoDS_Iterator aShIter(theShape);
331 for (; aShIter.More(); aShIter.Next()) {
332 const TopoDS_Shape &aSubShape = aShIter.Value();
334 aType = aSubShape.ShapeType();
336 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
337 aWOrV.Append(aSubShape);
339 // Only subshapes of types wire or vertex are supported.
344 // Only wire (vertex) or compound of wires (vertices) are supported.
348 // Treat each wire or vertex. Get points, compute the working plane.
350 Standard_Integer aPlaneStatus = PLN_FREE;
351 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
352 std::list<Section3D> aListSec;
354 for (; anIter.More(); anIter.Next()) {
357 aSec3D.myPoints = CurveCreator_Utils::getPoints
358 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
360 if (aSec3D.myPoints.IsNull()) {
364 aListSec.push_back(aSec3D);
366 if (aPlaneStatus != PLN_FIXED) {
368 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
372 // Check if it is possible to change a computed coordinate system by
373 // XOY, XOZ or YOZ or parallel to them.
374 gp_Pnt aO(0., 0., 0.);
375 gp_Dir aNDir(0., 0., 1.);
376 gp_Dir aXDir(1., 0., 0.);
378 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
380 switch (aPlaneStatus) {
383 // Change the location.
384 aO.SetZ(aPlane.Location().Z());
385 anAxis.SetLocation(aO);
386 aPlane.SetPosition(anAxis);
391 // Fixed origin + OX axis
392 const gp_Dir &aPlnX = aPlane.Position().XDirection();
394 if (Abs(aPlnX.Z()) <= aTolAng) {
395 // Make a coordinate system parallel to XOY.
396 aO.SetZ(aPlane.Location().Z());
397 anAxis.SetLocation(aO);
398 aPlane.SetPosition(anAxis);
399 } else if (Abs(aPlnX.Y()) <= aTolAng) {
400 // Make a coordinate system parallel to XOZ.
401 aO.SetY(aPlane.Location().Y());
402 aNDir.SetCoord(0., 1., 0.);
403 aXDir.SetCoord(0., 0., 1.);
404 anAxis = gp_Ax3(aO, aNDir, aXDir);
405 aPlane.SetPosition(anAxis);
406 } else if (Abs(aPlnX.X()) <= aTolAng) {
407 // Make a coordinate system parallel to YOZ.
408 aO.SetX(aPlane.Location().X());
409 aNDir.SetCoord(1., 0., 0.);
410 aXDir.SetCoord(0., 1., 0.);
411 anAxis = gp_Ax3(aO, aNDir, aXDir);
412 aPlane.SetPosition(anAxis);
418 const gp_Dir &aPlnN = aPlane.Position().Direction();
419 gp_Dir aYDir(0., 1., 0.);
421 if (aPlnN.IsParallel(aNDir, aTolAng)) {
422 // Make a coordinate system parallel to XOY.
423 aO.SetZ(aPlane.Location().Z());
424 anAxis.SetLocation(aO);
425 aPlane.SetPosition(anAxis);
426 } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
427 // Make a coordinate system parallel to XOZ.
428 aO.SetY(aPlane.Location().Y());
429 aNDir.SetCoord(0., 1., 0.);
430 aXDir.SetCoord(0., 0., 1.);
431 anAxis = gp_Ax3(aO, aNDir, aXDir);
432 aPlane.SetPosition(anAxis);
433 } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
434 // Make a coordinate system parallel to YOZ.
435 aO.SetX(aPlane.Location().X());
436 aNDir.SetCoord(1., 0., 0.);
437 aXDir.SetCoord(0., 1., 0.);
438 anAxis = gp_Ax3(aO, aNDir, aXDir);
439 aPlane.SetPosition(anAxis);
446 aPlane.SetPosition(anAxis);
450 // Compute 2d points.
451 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
452 Standard_Real aTolConf2 =
453 Precision::Confusion()*Precision::Confusion();
457 for (; aSecIt != aListSec.end(); ++aSecIt) {
459 CurveCreator::Coordinates aCoords;
461 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
462 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
464 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
465 // The point doesn't lie on the plane.
469 ElSLib::Parameters(aPlane, aPnt, aX, aY);
470 aCoords.push_back(aX);
471 aCoords.push_back(aY);
474 // Add a new section to the curve.
475 const std::string aSecName =
476 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
477 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
478 CurveCreator::Spline : CurveCreator::Polyline;
480 theCurve->addSectionInternal(aSecName, aSecType,
481 aSecIt->myIsClosed, aCoords);
484 // Set the local coordinate system.
485 theLocalCS = aPlane.Position();
490 class CompareSectionToPoint
493 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
494 : mySectionId( theISection ), myPointId( theIPoint ) {};
495 ~CompareSectionToPoint() {}
497 bool operator < ( const CompareSectionToPoint& theOther ) const
499 bool isLess = mySectionId < theOther.mySectionId;
500 if ( !isLess && mySectionId == theOther.mySectionId )
501 isLess = myPointId < theOther.myPointId;
511 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
512 const CurveCreator_ICurve* theCurve,
513 CurveCreator_ICurve::SectionToPointList& thePoints )
517 std::list<float> aSelectedPoints;
519 std::map<CompareSectionToPoint, int> aPointsMap;
521 CurveCreator_ICurve::SectionToPointList aPoints;
522 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
523 TopoDS_Vertex aVertex;
524 TopoDS_Shape aShape = theContext->SelectedShape();
525 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
526 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
528 if ( aVertex.IsNull() )
530 aPnt = BRep_Tool::Pnt( aVertex );
532 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
533 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
534 aLast = aPoints.end();
535 CompareSectionToPoint aPoint;
536 for ( ; anIt != aLast; anIt++ ) {
537 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
538 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
540 aPointsMap[aPoint] = 0;
542 thePoints.push_back( *anIt );
547 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
548 const CurveCreator_ICurve* theCurve,
549 const CurveCreator_ICurve::SectionToPointList& thePoints )
554 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
555 if ( anAIS.IsNull() )
557 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
558 if ( anAISShape.IsNull() )
561 //ASL: we convert list of point indices to list of points coordinates
562 int aSize = thePoints.size();
563 std::vector<gp_Pnt> aPntsToSelect( aSize );
565 CurveCreator_ICurve::SectionToPointList::const_iterator
566 aPIt = thePoints.begin(), aPLast = thePoints.end();
567 CurveCreator_ICurve::SectionToPoint aSToPoint;
568 for( int i=0; aPIt != aPLast; aPIt++, i++ )
571 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
572 aPntsToSelect[i] = aPntToSelect;
575 theContext->ClearSelected( Standard_False );
576 //ASL: we switch off automatic highlight to improve performance of selection
577 theContext->SetAutomaticHilight( Standard_False );
579 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
580 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
582 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
583 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
585 gp_Pnt anOwnerPnt = aSenPnt->Point();
586 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
589 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
590 aLast = thePoints.end();
591 bool isFound = false;
592 for( int i=0; i<aSize; i++ )
594 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
595 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
598 theContext->AddOrRemoveSelected( anOwner, Standard_False );
604 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
605 // and call HilightPicked to draw selected owners
606 theContext->SetAutomaticHilight( Standard_True );
607 theContext->LocalContext()->HilightPicked( Standard_True );
610 //=======================================================================
611 // function : setLocalPointContext
612 // purpose : Open/close the viewer local context
613 //=======================================================================
614 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
615 Handle(AIS_InteractiveContext) theContext,
622 // Open local context if there is no one
623 if ( !theContext->HasOpenedContext() ) {
624 theContext->ClearCurrents( false );
625 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
627 // load the curve AIS object to the local context with the point selection
628 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
629 if ( !anAIS.IsNull() )
631 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
633 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
634 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
639 if ( theContext->HasOpenedContext() )
640 theContext->CloseAllContexts();
644 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
645 Handle(AIS_InteractiveObject) theObject,
646 const int theX, const int theY,
648 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
650 bool isFullFound = false;
652 if ( theObject.IsNull() || theView.IsNull() )
654 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
655 if ( aShape.IsNull() )
657 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
658 if ( aCompound.IsNull() )
661 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
662 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
663 Standard_Real aParameter;
664 bool isFound = false;
665 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
666 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
667 for ( ; anExp.More(); anExp.Next())
669 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
670 if ( anEdge.IsNull() )
672 Standard_Real aFirst, aLast;
673 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
674 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
675 Handle(Geom_BSplineCurve) aBSplineCurve =
676 Handle(Geom_BSplineCurve)::DownCast( aCurve );
677 if ( !aBSplineCurve.IsNull() ) {
678 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
679 aParameter, aDelta );
681 aCurPoint = aBSplineCurve->Value( aParameter );
682 Standard_Integer anI1, anI2;
683 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
684 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
685 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
689 else { // a curve built on a polyline edge
690 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
691 if ( aGLine.IsNull() )
693 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
696 aCurPoint = aGLine->Value( aParameter );
697 TopoDS_Vertex V1, V2;
698 TopExp::Vertices( anEdge, V1, V2, Standard_True );
699 if ( V1.IsNull() || V2.IsNull() )
701 aCurPoint1 = BRep_Tool::Pnt(V1);
702 aCurPoint2 = BRep_Tool::Pnt(V2);
704 // check that the projected point is on the bounded curve
705 gp_Vec aVec1( aCurPoint1, aCurPoint );
706 gp_Vec aVec2( aCurPoint2, aCurPoint );
707 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
710 if ( isFound && aMinDelta >= aDelta ) {
714 aFoundPnt1 = aCurPoint1;
715 aFoundPnt2 = aCurPoint2;
716 aFoundPoint = aCurPoint;
720 int aX, anY, aX1, anY1, aX2, anY2;
722 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
723 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
724 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
726 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
727 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
729 thePoint = aFoundPoint;
730 thePoint1 = aFoundPnt1;
731 thePoint2 = aFoundPnt2;
737 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
738 const int theX, const int theY,
739 const Handle(Geom_Curve)& theCurve,
740 Standard_Real& theParameter,
743 bool isFound = false;
744 if ( theView.IsNull() )
747 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
749 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
750 Standard_Integer aNbPoint = aProj.NbPoints();
752 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
753 gp_Pnt aNewPoint = aProj.Point( j );
754 theParameter = aProj.Parameter( j );
757 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
759 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
765 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
766 const int theOtherY, const double theTolerance, int& theDelta )
768 int aXDelta = abs( theX - theOtherX );
769 int anYDelta = abs( theY - theOtherY );
771 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
773 return aXDelta < theTolerance && anYDelta < theTolerance;
776 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
778 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
781 //=======================================================================
782 // function : getPoints
784 //=======================================================================
785 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
786 (const TopoDS_Shape &theShape,
790 Handle(TColgp_HArray1OfPnt) aResult;
795 if (theShape.IsNull()) {
799 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
801 if (aShType == TopAbs_VERTEX) {
802 // There is a single point.
803 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
805 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
808 } else if (aShType != TopAbs_WIRE) {
809 // The shape is neither a vertex nor a wire.
814 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
817 // Empty wires are not allowed.
821 // Treat the first edge.
822 TopoDS_Edge anEdge = anExp.Current();
823 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
825 if (aCurve.IsNull()) {
829 // Check the curve type.
830 Handle(Standard_Type) aType = aCurve->DynamicType();
832 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
834 } else if (aType != STANDARD_TYPE(Geom_Line)) {
835 // The curve is neither a line or a BSpline. It is not valid.
839 // Go to the next edge.
840 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
845 // There should be a single BSpline curve in the wire.
850 // Construct a section from poles of BSpline.
851 Handle(Geom_BSplineCurve) aBSplCurve =
852 Handle(Geom_BSplineCurve)::DownCast(aCurve);
854 // Check if the edge is valid. It should not be based on trimmed curve.
855 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
859 TopExp::Vertices(anEdge, aV[0], aV[1]);
861 for (i = 0; i < 2; i++) {
862 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
863 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
865 if (!aPnt.IsEqual(aCP[i], aTol)) {
870 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
872 const Standard_Integer aNbPoints = aBSplCurve->NbKnots();
873 TColStd_Array1OfReal aKnots(1, aNbPoints);
875 aBSplCurve->Knots(aKnots);
876 aResult = new TColgp_HArray1OfPnt(1, aBSplCurve->NbKnots());
878 for (i = aKnots.Lower(); i <= aKnots.Upper(); ++i) {
879 aResult->SetValue(i, aBSplCurve->Value(aKnots.Value(i)));
882 // This is a polyline.
883 TopTools_ListOfShape aVertices;
884 Standard_Integer aNbVtx = 1;
887 aVertices.Append(aFirstVtx);
889 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
890 anEdge = anExp.Current();
891 aCurve = GetCurve(anEdge);
893 if (aCurve.IsNull()) {
897 aType = aCurve->DynamicType();
899 if (aType != STANDARD_TYPE(Geom_Line)) {
900 // The curve is not a line. It is not valid.
904 // Add the current vertex to the list.
905 aVertices.Append(anExp.CurrentVertex());
908 // Check if the section is closed.
909 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
911 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
913 // Fill the array of points.
914 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
917 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
919 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
920 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
922 aResult->SetValue(i, aPnt);
928 //=======================================================================
929 // function : FindPlane
931 //=======================================================================
932 void CurveCreator_Utils::FindPlane
933 (const Handle_TColgp_HArray1OfPnt &thePoints,
935 Standard_Integer &thePlnStatus)
937 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
938 // The plane can't be defined or is fixed. Nothing to change.
943 const Standard_Real aTolConf = Precision::Confusion();
945 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
946 const gp_Pnt &aPnt = thePoints->Value(i);
948 switch (thePlnStatus) {
951 thePlane.SetLocation(aPnt);
952 thePlnStatus = PLN_ORIGIN;
956 // Fix origin + OX axis
957 const gp_Pnt &aPlnLoc = thePlane.Location();
959 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
961 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
962 gp_Ax3 aXNorm(aPlnLoc, aXDir);
963 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
965 thePlane.SetPosition(aNewPlnPos);
966 thePlnStatus = PLN_OX;
973 gp_Lin aXLin(thePlane.XAxis());
974 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
976 if (aSqrDist > aTolConf*aTolConf) {
977 // Compute main axis.
978 const gp_Pnt &aPlnLoc = thePlane.Location();
979 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
980 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
981 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
984 thePlane.SetPosition(aNewPlnPos);
985 thePlnStatus = PLN_FIXED;