1 // Copyright (C) 2013-2016 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, or (at your option) any later version.
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_Section.hxx"
24 #include "CurveCreator_UtilsICurve.hxx"
26 #include <GEOMUtils.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Wire.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Compound.hxx>
36 #include <AIS_ListOfInteractive.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38 #include <AIS_Shape.hxx>
39 #include <AIS_Line.hxx>
40 #include <AIS_Trihedron.hxx>
41 #include <AIS_LocalContext.hxx>
43 #include <Geom_Point.hxx>
44 #include <Geom_BSplineCurve.hxx>
45 #include <Geom_Line.hxx>
46 #include <Geom_Curve.hxx>
47 #include <Geom_TrimmedCurve.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <GeomAPI_ProjectPointOnCurve.hxx>
53 #include <SelectMgr_EntityOwner.hxx>
54 #include <SelectMgr_Selection.hxx>
55 #include <Select3D_SensitivePoint.hxx>
57 #include <BRep_Tool.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRepBuilderAPI_MakeVertex.hxx>
60 #include <BRepBuilderAPI_MakeEdge.hxx>
61 #include <BRepBuilderAPI_MakeWire.hxx>
62 #include <BRepTools_WireExplorer.hxx>
64 #include <TColgp_HArray1OfPnt.hxx>
65 #include <TColStd_HArray1OfBoolean.hxx>
66 #include <TColStd_Array1OfReal.hxx>
67 #include <TColgp_Array1OfVec.hxx>
68 #include <GeomAPI_Interpolate.hxx>
70 #include <ProjLib.hxx>
75 #include "CurveCreator_ICurve.hxx"
77 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
78 const int SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
79 const int SCENE_PIXEL_POINT_TOLERANCE = 5;
87 * This static function returns the curve of original type from the edge.
89 * \param theEdge the edge
90 * \return the curve of original type. Can be null handle.
92 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
94 Handle(Geom_Curve) aResult;
96 if (theEdge.IsNull()) {
103 aResult = BRep_Tool::Curve(theEdge, aF, aL);
105 if (aResult.IsNull()) {
109 // Get the curve of original type
110 Handle(Standard_Type) aType = aResult->DynamicType();
112 while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
113 Handle(Geom_TrimmedCurve) aTrCurve =
114 Handle(Geom_TrimmedCurve)::DownCast(aResult);
116 aResult = aTrCurve->BasisCurve();
117 aType = aResult->DynamicType();
123 //=======================================================================
124 // function : ConvertClickToPoint()
125 // purpose : Returns the point clicked in 3D view
126 //=======================================================================
127 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
128 Handle(V3d_View) theView,
131 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
135 //=======================================================================
136 // function : ConvertClickToPoint()
137 // purpose : Returns the point clicked in 3D view
138 //=======================================================================
139 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
141 // the 3D point, that is a projection of the pixels to the XYZ view plane
142 //return GEOMUtils::ConvertClickToPoint( x, y, aView );
144 // we need the projection to the XOY plane
145 // 1. find a point in the plane of the eye and the normal to the plane
146 Standard_Real X, Y, Z;
147 Standard_Real Vx, Vy, Vz;
148 aView->ConvertWithProj( x, y, X, Y, Z, Vx, Vy, Vz );
150 // 2. build a ray from the point by the normal to the XOY plane and intersect it
151 // The ray equation is the following : p(x,y,z) = p0(x,y,z) + t*V(x,y,z)
152 // X,Y,Z - defines p0(x,y,z), Vx,Vy,Vz - defines V(x,y,z)
153 // p(x,y,z) - is a searched point, t - should to be calculated by the condition of XOY plane
154 // The system of equations is the following:
155 // p(x) = p0(x)+t*V(x)
156 // p(y) = p0(y)+t*V(y)
157 // p(z) = p0(z)+t*V(z)
160 Standard_Real aXp, aYp, aZp;
161 //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
162 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
163 if ( fabs( Vz ) > aPrec ) {
164 Standard_Real aT = -Z/Vz;
169 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
171 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
173 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
176 /*std::cout << "ConvertClickToPoint: " << std::endl
177 << "XYZ1 = (" << X << ", " << Y << ", " << Z << "); " << std::endl
178 << "Vxyz = (" << Vx << ", " << Vy << ", " << Vz << "); " << std::endl
179 << "Resp = (" << aXp << ", " << aYp << ", " << aZp << "); " << std::endl;*/
181 gp_Pnt ResultPoint( aXp, aYp, aZp );
185 //=======================================================================
186 // function : constructBSpline
188 //=======================================================================
189 bool CurveCreator_Utils::constructBSpline(
190 const Handle(TColgp_HArray1OfPnt)& thePoints,
191 const Standard_Boolean theIsClosed,
192 Handle(Geom_BSplineCurve)& theBSpline)
194 const int aPointCount = thePoints->Length();
195 if (aPointCount <= 1)
200 // Calculate the tangents.
201 TColgp_Array1OfVec aTangents(1, aPointCount);
202 Handle(TColStd_HArray1OfBoolean) aTangentFlags =
203 new TColStd_HArray1OfBoolean(1, aPointCount);
204 GeomAPI_Interpolate aInterpolator(thePoints, theIsClosed, 0);
205 if (aPointCount == 2)
207 aTangentFlags->SetValue(1, Standard_False);
208 aTangentFlags->SetValue(2, Standard_False);
212 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
215 if (aPN != 1 || theIsClosed)
217 const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
218 aTangent = gp_Vec(thePoints->Value(aPN1),
219 thePoints->Value(aPN)).Normalized();
221 if (aPN < aPointCount || theIsClosed)
223 const Standard_Integer aPN2 = (aPN != aPointCount) ? (aPN + 1) : 1;
224 const gp_Vec aTangent2 = aTangent +
225 gp_Vec(thePoints->Value(aPN), thePoints->Value(aPN2)).Normalized();
226 if (aTangent2.SquareMagnitude() >= Precision::SquareConfusion())
228 aTangent = aTangent2.Normalized();
232 aTangent = -aTangent;
235 aTangents.SetValue(aPN, aTangent);
236 aTangentFlags->SetValue(aPN, Standard_True);
241 aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
242 aInterpolator.Perform();
243 const bool aResult = (aInterpolator.IsDone() == Standard_True);
246 theBSpline = aInterpolator.Curve();
251 //=======================================================================
252 // function : constructWire
254 //=======================================================================
255 TopoDS_Wire CurveCreator_Utils::ConstructWire(
256 Handle(TColgp_HArray1OfPnt) thePoints,
257 const bool theIsPolyline,
258 const bool theIsClosed)
261 BRep_Builder aBuilder;
262 aBuilder.MakeWire(aWire);
263 const int aPointCount = thePoints->Length();
266 const TopoDS_Vertex aFirstVertex =
267 BRepBuilderAPI_MakeVertex(thePoints->Value(1));
268 TopoDS_Vertex aVertex = aFirstVertex;
269 for (Standard_Integer aPN = 1; aPN < aPointCount; ++aPN)
271 const TopoDS_Vertex aVertex2 =
272 BRepBuilderAPI_MakeVertex(thePoints->Value(aPN + 1));
273 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aVertex2));
276 if (theIsClosed && aPointCount > 1)
278 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aFirstVertex));
283 Handle(Geom_BSplineCurve) aBSpline;
284 if (constructBSpline(thePoints, theIsClosed, aBSpline))
286 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aBSpline));
292 //=======================================================================
293 // function : constructShape
295 //=======================================================================
296 void CurveCreator_Utils::constructShape(
297 const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape)
299 BRep_Builder aBuilder;
300 TopoDS_Compound aShape;
301 aBuilder.MakeCompound(aShape);
302 const int aSectionCount = theCurve->getNbSections();
303 for (int aSectionI = 0; aSectionI < aSectionCount; ++aSectionI)
305 const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
306 if (aTmpPointCount == 0)
311 // Get the different points.
312 Handle(TColgp_HArray1OfPnt) aPoints = theCurve->GetDifferentPoints( aSectionI );
313 const int aPointCount = aPoints->Length();
314 const bool isClosed = theCurve->isClosed(aSectionI);
316 // Add the vertices to the shape.
317 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
319 aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex(aPoints->Value(aPN)));
322 // Add the wire to the shape.
323 const bool isPolyline =
324 (theCurve->getSectionType(aSectionI) == CurveCreator::Polyline);
325 const TopoDS_Wire aWire = ConstructWire(aPoints, isPolyline, isClosed);
328 aBuilder.Add(aShape, aWire);
335 * This is an intermediate structure for curve construction.
339 Section3D() : myIsClosed(false), myIsBSpline(false)
344 Handle(TColgp_HArray1OfPnt) myPoints;
347 //=======================================================================
348 // function : constructCurve
350 //=======================================================================
351 bool CurveCreator_Utils::constructCurve
352 (const TopoDS_Shape theShape,
353 CurveCreator_Curve *theCurve,
356 if (theShape.IsNull()) {
360 // Collect wires or vertices from shape.
361 TopTools_ListOfShape aWOrV;
362 TopAbs_ShapeEnum aType = theShape.ShapeType();
364 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
365 aWOrV.Append(theShape);
366 } else if (aType == TopAbs_COMPOUND) {
367 TopoDS_Iterator aShIter(theShape);
369 for (; aShIter.More(); aShIter.Next()) {
370 const TopoDS_Shape &aSubShape = aShIter.Value();
372 aType = aSubShape.ShapeType();
374 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
375 aWOrV.Append(aSubShape);
377 // Only subshapes of types wire or vertex are supported.
382 // Only wire (vertex) or compound of wires (vertices) are supported.
386 // Treat each wire or vertex. Get points, compute the working plane.
388 Standard_Integer aPlaneStatus = PLN_FREE;
389 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
390 std::list<Section3D> aListSec;
392 for (; anIter.More(); anIter.Next()) {
395 aSec3D.myPoints = CurveCreator_Utils::getPoints
396 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
398 if (aSec3D.myPoints.IsNull()) {
402 aListSec.push_back(aSec3D);
404 if (aPlaneStatus != PLN_FIXED) {
406 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
410 // Check if it is possible to change a computed coordinate system by
411 // XOY, XOZ or YOZ or parallel to them.
412 gp_Pnt aO(0., 0., 0.);
413 gp_Dir aNDir(0., 0., 1.);
414 gp_Dir aXDir(1., 0., 0.);
416 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
418 switch (aPlaneStatus) {
421 // Change the location.
422 aO.SetZ(aPlane.Location().Z());
423 anAxis.SetLocation(aO);
424 aPlane.SetPosition(anAxis);
429 // Fixed origin + OX axis
430 const gp_Dir &aPlnX = aPlane.Position().XDirection();
432 if (Abs(aPlnX.Z()) <= aTolAng) {
433 // Make a coordinate system parallel to XOY.
434 aO.SetZ(aPlane.Location().Z());
435 anAxis.SetLocation(aO);
436 aPlane.SetPosition(anAxis);
437 } else if (Abs(aPlnX.Y()) <= aTolAng) {
438 // Make a coordinate system parallel to XOZ.
439 aO.SetY(aPlane.Location().Y());
440 aNDir.SetCoord(0., 1., 0.);
441 aXDir.SetCoord(0., 0., 1.);
442 anAxis = gp_Ax3(aO, aNDir, aXDir);
443 aPlane.SetPosition(anAxis);
444 } else if (Abs(aPlnX.X()) <= aTolAng) {
445 // Make a coordinate system parallel to YOZ.
446 aO.SetX(aPlane.Location().X());
447 aNDir.SetCoord(1., 0., 0.);
448 aXDir.SetCoord(0., 1., 0.);
449 anAxis = gp_Ax3(aO, aNDir, aXDir);
450 aPlane.SetPosition(anAxis);
456 const gp_Dir &aPlnN = aPlane.Position().Direction();
457 gp_Dir aYDir(0., 1., 0.);
459 if (aPlnN.IsParallel(aNDir, aTolAng)) {
460 // Make a coordinate system parallel to XOY.
461 aO.SetZ(aPlane.Location().Z());
462 anAxis.SetLocation(aO);
463 aPlane.SetPosition(anAxis);
464 } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
465 // Make a coordinate system parallel to XOZ.
466 aO.SetY(aPlane.Location().Y());
467 aNDir.SetCoord(0., 1., 0.);
468 aXDir.SetCoord(0., 0., 1.);
469 anAxis = gp_Ax3(aO, aNDir, aXDir);
470 aPlane.SetPosition(anAxis);
471 } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
472 // Make a coordinate system parallel to YOZ.
473 aO.SetX(aPlane.Location().X());
474 aNDir.SetCoord(1., 0., 0.);
475 aXDir.SetCoord(0., 1., 0.);
476 anAxis = gp_Ax3(aO, aNDir, aXDir);
477 aPlane.SetPosition(anAxis);
484 aPlane.SetPosition(anAxis);
488 // Compute 2d points.
489 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
490 Standard_Real aTolConf2 =
491 Precision::Confusion()*Precision::Confusion();
495 for (; aSecIt != aListSec.end(); ++aSecIt) {
497 CurveCreator::Coordinates aCoords;
499 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
500 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
502 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
503 // The point doesn't lie on the plane.
507 ElSLib::Parameters(aPlane, aPnt, aX, aY);
508 aCoords.push_back(aX);
509 aCoords.push_back(aY);
512 // Add a new section to the curve.
513 const std::string aSecName =
514 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
515 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
516 CurveCreator::Spline : CurveCreator::Polyline;
518 theCurve->addSectionInternal(aSecName, aSecType,
519 aSecIt->myIsClosed, aCoords);
522 // Set the local coordinate system.
523 theLocalCS = aPlane.Position();
528 class CompareSectionToPoint
531 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
532 : mySectionId( theISection ), myPointId( theIPoint ) {};
533 ~CompareSectionToPoint() {}
535 bool operator < ( const CompareSectionToPoint& theOther ) const
537 bool isLess = mySectionId < theOther.mySectionId;
538 if ( !isLess && mySectionId == theOther.mySectionId )
539 isLess = myPointId < theOther.myPointId;
549 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
550 const CurveCreator_ICurve* theCurve,
551 CurveCreator_ICurve::SectionToPointList& thePoints )
555 std::list<double> aSelectedPoints;
557 std::map<CompareSectionToPoint, int> aPointsMap;
559 CurveCreator_ICurve::SectionToPointList aPoints;
560 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
561 TopoDS_Vertex aVertex;
562 TopoDS_Shape aShape = theContext->SelectedShape();
563 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
564 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
566 if ( aVertex.IsNull() )
568 aPnt = BRep_Tool::Pnt( aVertex );
570 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
571 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
572 aLast = aPoints.end();
573 CompareSectionToPoint aPoint;
574 for ( ; anIt != aLast; anIt++ ) {
575 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
576 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
578 aPointsMap[aPoint] = 0;
580 thePoints.push_back( *anIt );
585 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
586 const CurveCreator_ICurve* theCurve,
587 const CurveCreator_ICurve::SectionToPointList& thePoints )
592 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
593 if ( anAIS.IsNull() )
595 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
596 if ( anAISShape.IsNull() )
599 //ASL: we convert list of point indices to list of points coordinates
600 int aSize = thePoints.size();
601 std::vector<gp_Pnt> aPntsToSelect( aSize );
603 CurveCreator_ICurve::SectionToPointList::const_iterator
604 aPIt = thePoints.begin(), aPLast = thePoints.end();
605 CurveCreator_ICurve::SectionToPoint aSToPoint;
606 for( int i=0; aPIt != aPLast; aPIt++, i++ )
609 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
610 aPntsToSelect[i] = aPntToSelect;
613 theContext->ClearSelected( Standard_False );
614 //ASL: we switch off automatic highlight to improve performance of selection
615 theContext->SetAutomaticHilight( Standard_False );
617 Handle(SelectMgr_Selection) aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
619 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
620 aLast = thePoints.end();
621 bool isFound = false;
622 for( int i=0; i<aSize; i++ )
624 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
626 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
627 if( aHSenEntity.IsNull() )
629 Handle(SelectBasics_SensitiveEntity) aSenEntity = aHSenEntity->BaseSensitive();
631 Handle(Select3D_SensitivePoint) aSenPnt = Handle(Select3D_SensitivePoint)::DownCast( aSenEntity );
633 gp_Pnt anOwnerPnt = aSenPnt->Point();
634 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast( aSenPnt->OwnerId() );
636 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
637 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
640 theContext->AddOrRemoveSelected( anOwner, Standard_False );
646 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
647 // and call HilightPicked to draw selected owners
648 theContext->SetAutomaticHilight( Standard_True );
649 theContext->LocalContext()->HilightPicked( Standard_True );
652 //=======================================================================
653 // function : setLocalPointContext
654 // purpose : Open/close the viewer local context
655 //=======================================================================
656 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
657 Handle(AIS_InteractiveContext) theContext,
664 // Open local context if there is no one
665 if ( !theContext->HasOpenedContext() ) {
666 theContext->ClearCurrents( false );
667 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
669 // load the curve AIS object to the local context with the point selection
670 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
671 if ( !anAIS.IsNull() )
673 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
675 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
676 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
681 if ( theContext->HasOpenedContext() )
682 theContext->CloseAllContexts( Standard_True );
686 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
687 Handle(AIS_InteractiveObject) theObject,
688 const int theX, const int theY,
690 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
692 bool isFullFound = false;
694 if ( theObject.IsNull() || theView.IsNull() )
696 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
697 if ( aShape.IsNull() )
699 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
700 if ( aCompound.IsNull() )
703 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
704 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
705 Standard_Real aParameter;
706 bool isFound = false;
707 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
708 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
709 for ( ; anExp.More(); anExp.Next())
711 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
712 if ( anEdge.IsNull() )
714 Standard_Real aFirst, aLast;
715 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
716 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
717 Handle(Geom_BSplineCurve) aBSplineCurve =
718 Handle(Geom_BSplineCurve)::DownCast( aCurve );
719 if ( !aBSplineCurve.IsNull() ) {
720 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
721 aParameter, aDelta );
723 aCurPoint = aBSplineCurve->Value( aParameter );
724 Standard_Integer anI1, anI2;
725 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
726 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
727 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
731 else { // a curve built on a polyline edge
732 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
733 if ( aGLine.IsNull() )
735 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
738 aCurPoint = aGLine->Value( aParameter );
739 TopoDS_Vertex V1, V2;
740 TopExp::Vertices( anEdge, V1, V2, Standard_True );
741 if ( V1.IsNull() || V2.IsNull() )
743 aCurPoint1 = BRep_Tool::Pnt(V1);
744 aCurPoint2 = BRep_Tool::Pnt(V2);
746 // check that the projected point is on the bounded curve
747 gp_Vec aVec1( aCurPoint1, aCurPoint );
748 gp_Vec aVec2( aCurPoint2, aCurPoint );
749 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
752 if ( isFound && aMinDelta >= aDelta ) {
756 aFoundPnt1 = aCurPoint1;
757 aFoundPnt2 = aCurPoint2;
758 aFoundPoint = aCurPoint;
762 int aX, anY, aX1, anY1, aX2, anY2;
764 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
765 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
766 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
768 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
769 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
771 thePoint = aFoundPoint;
772 thePoint1 = aFoundPnt1;
773 thePoint2 = aFoundPnt2;
779 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
780 const int theX, const int theY,
781 const Handle(Geom_Curve)& theCurve,
782 Standard_Real& theParameter,
785 bool isFound = false;
786 if ( theView.IsNull() )
789 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
791 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
792 Standard_Integer aNbPoint = aProj.NbPoints();
794 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
795 gp_Pnt aNewPoint = aProj.Point( j );
796 theParameter = aProj.Parameter( j );
799 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
801 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
807 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
808 const int theOtherY, const double theTolerance, int& theDelta )
810 int aXDelta = abs( theX - theOtherX );
811 int anYDelta = abs( theY - theOtherY );
813 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
815 return aXDelta < theTolerance && anYDelta < theTolerance;
818 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
820 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
823 //=======================================================================
824 // function : getPoints
826 //=======================================================================
827 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
828 (const TopoDS_Shape &theShape,
832 Handle(TColgp_HArray1OfPnt) aResult;
837 if (theShape.IsNull()) {
841 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
843 if (aShType == TopAbs_VERTEX) {
844 // There is a single point.
845 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
847 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
850 } else if (aShType != TopAbs_WIRE) {
851 // The shape is neither a vertex nor a wire.
856 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
859 // Empty wires are not allowed.
863 // Treat the first edge.
864 TopoDS_Edge anEdge = anExp.Current();
865 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
867 if (aCurve.IsNull()) {
871 // Check the curve type.
872 Handle(Standard_Type) aType = aCurve->DynamicType();
874 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
876 } else if (aType != STANDARD_TYPE(Geom_Line)) {
877 // The curve is neither a line or a BSpline. It is not valid.
881 // Go to the next edge.
882 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
888 // There should be a single BSpline curve in the wire.
893 // Construct a section from poles of BSpline.
894 Handle(Geom_BSplineCurve) aBSplCurve =
895 Handle(Geom_BSplineCurve)::DownCast(aCurve);
897 // Check if the edge is valid. It should not be based on trimmed curve.
898 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
902 TopExp::Vertices(anEdge, aV[0], aV[1]);
904 for (i = 0; i < 2; i++) {
905 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
906 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
908 if (!aPnt.IsEqual(aCP[i], aTol)) {
913 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
915 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
916 TColStd_Array1OfReal aKnots(1, aNbPoints);
917 aBSplCurve->Knots(aKnots);
919 // Don't consider the last point as it coincides with the first
923 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
924 for (i = 1; i <= aNbPoints; ++i)
925 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
929 // This is a polyline.
930 TopTools_ListOfShape aVertices;
931 Standard_Integer aNbVtx = 1;
934 aVertices.Append(aFirstVtx);
936 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
937 anEdge = anExp.Current();
938 aCurve = GetCurve(anEdge);
940 if (aCurve.IsNull()) {
944 aType = aCurve->DynamicType();
946 if (aType != STANDARD_TYPE(Geom_Line)) {
947 // The curve is not a line. It is not valid.
951 // Add the current vertex to the list.
952 aVertices.Append(anExp.CurrentVertex());
955 // Check if the section is closed.
956 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
958 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
960 // Store a last vertex
963 aVertices.Append(aLastVtx);
967 // Fill the array of points.
968 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
971 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
973 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
974 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
976 aResult->SetValue(i, aPnt);
982 //=======================================================================
983 // function : FindPlane
985 //=======================================================================
986 void CurveCreator_Utils::FindPlane
987 (const Handle(TColgp_HArray1OfPnt) &thePoints,
989 Standard_Integer &thePlnStatus)
991 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
992 // The plane can't be defined or is fixed. Nothing to change.
997 const Standard_Real aTolConf = Precision::Confusion();
999 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1000 const gp_Pnt &aPnt = thePoints->Value(i);
1002 switch (thePlnStatus) {
1005 thePlane.SetLocation(aPnt);
1006 thePlnStatus = PLN_ORIGIN;
1010 // Fix origin + OX axis
1011 const gp_Pnt &aPlnLoc = thePlane.Location();
1013 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1015 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1016 gp_Ax3 aXNorm(aPlnLoc, aXDir);
1017 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1019 thePlane.SetPosition(aNewPlnPos);
1020 thePlnStatus = PLN_OX;
1027 gp_Lin aXLin(thePlane.XAxis());
1028 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1030 if (aSqrDist > aTolConf*aTolConf) {
1031 // Compute main axis.
1032 const gp_Pnt &aPlnLoc = thePlane.Location();
1033 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1034 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1035 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1036 aXNorm.Direction());
1038 thePlane.SetPosition(aNewPlnPos);
1039 thePlnStatus = PLN_FIXED;