1 // Copyright (C) 2013-2019 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 <Basics_OCCTVersion.hxx>
22 #include "CurveCreator_Utils.hxx"
23 #include "CurveCreator.hxx"
24 #include "CurveCreator_Curve.hxx"
25 #include "CurveCreator_Section.hxx"
26 #include "CurveCreator_UtilsICurve.hxx"
28 #include <GEOMUtils.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Compound.hxx>
38 #include <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
40 #include <AIS_Shape.hxx>
41 #include <AIS_Line.hxx>
42 #include <AIS_Trihedron.hxx>
44 #if OCC_VERSION_LARGE <= 0x07030000
45 #include <AIS_LocalContext.hxx>
48 #include <Geom_Point.hxx>
49 #include <Geom_BSplineCurve.hxx>
50 #include <Geom_Line.hxx>
51 #include <Geom_Curve.hxx>
52 #include <Geom_TrimmedCurve.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <GeomAPI_ProjectPointOnCurve.hxx>
58 #include <SelectMgr_EntityOwner.hxx>
59 #include <SelectMgr_Selection.hxx>
60 #include <Select3D_SensitivePoint.hxx>
62 #include <BRep_Tool.hxx>
63 #include <BRep_Builder.hxx>
64 #include <BRepBuilderAPI_MakeVertex.hxx>
65 #include <BRepBuilderAPI_MakeEdge.hxx>
66 #include <BRepBuilderAPI_MakeWire.hxx>
67 #include <BRepTools_WireExplorer.hxx>
69 #include <TColgp_HArray1OfPnt.hxx>
70 #include <TColStd_HArray1OfBoolean.hxx>
71 #include <TColStd_Array1OfReal.hxx>
72 #include <TColgp_Array1OfVec.hxx>
73 #include <GeomAPI_Interpolate.hxx>
75 #include <ProjLib.hxx>
80 #include "CurveCreator_ICurve.hxx"
82 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
83 const int SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
84 const int SCENE_PIXEL_POINT_TOLERANCE = 5;
92 * This static function returns the curve of original type from the edge.
94 * \param theEdge the edge
95 * \return the curve of original type. Can be null handle.
97 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
99 Handle(Geom_Curve) aResult;
101 if (theEdge.IsNull()) {
108 aResult = BRep_Tool::Curve(theEdge, aF, aL);
110 if (aResult.IsNull()) {
114 // Get the curve of original type
115 Handle(Standard_Type) aType = aResult->DynamicType();
117 while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
118 Handle(Geom_TrimmedCurve) aTrCurve =
119 Handle(Geom_TrimmedCurve)::DownCast(aResult);
121 aResult = aTrCurve->BasisCurve();
122 aType = aResult->DynamicType();
128 //=======================================================================
129 // function : ConvertClickToPoint()
130 // purpose : Returns the point clicked in 3D view
131 //=======================================================================
132 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
133 Handle(V3d_View) theView,
136 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
140 //=======================================================================
141 // function : ConvertClickToPoint()
142 // purpose : Returns the point clicked in 3D view
143 //=======================================================================
144 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
146 // the 3D point, that is a projection of the pixels to the XYZ view plane
147 //return GEOMUtils::ConvertClickToPoint( x, y, aView );
149 // we need the projection to the XOY plane
150 // 1. find a point in the plane of the eye and the normal to the plane
151 Standard_Real X, Y, Z;
152 Standard_Real Vx, Vy, Vz;
153 aView->ConvertWithProj( x, y, X, Y, Z, Vx, Vy, Vz );
155 // 2. build a ray from the point by the normal to the XOY plane and intersect it
156 // The ray equation is the following : p(x,y,z) = p0(x,y,z) + t*V(x,y,z)
157 // X,Y,Z - defines p0(x,y,z), Vx,Vy,Vz - defines V(x,y,z)
158 // p(x,y,z) - is a searched point, t - should to be calculated by the condition of XOY plane
159 // The system of equations is the following:
160 // p(x) = p0(x)+t*V(x)
161 // p(y) = p0(y)+t*V(y)
162 // p(z) = p0(z)+t*V(z)
165 Standard_Real aXp, aYp, aZp;
166 //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
167 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
168 if ( fabs( Vz ) > aPrec ) {
169 Standard_Real aT = -Z/Vz;
174 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
176 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
178 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
181 /*std::cout << "ConvertClickToPoint: " << std::endl
182 << "XYZ1 = (" << X << ", " << Y << ", " << Z << "); " << std::endl
183 << "Vxyz = (" << Vx << ", " << Vy << ", " << Vz << "); " << std::endl
184 << "Resp = (" << aXp << ", " << aYp << ", " << aZp << "); " << std::endl;*/
186 gp_Pnt ResultPoint( aXp, aYp, aZp );
190 //=======================================================================
191 // function : constructBSpline
193 //=======================================================================
194 bool CurveCreator_Utils::constructBSpline(
195 const Handle(TColgp_HArray1OfPnt)& thePoints,
196 const Standard_Boolean theIsClosed,
197 Handle(Geom_BSplineCurve)& theBSpline)
199 const int aPointCount = thePoints->Length();
200 if (aPointCount <= 1)
205 // Calculate the tangents.
206 TColgp_Array1OfVec aTangents(1, aPointCount);
207 Handle(TColStd_HArray1OfBoolean) aTangentFlags =
208 new TColStd_HArray1OfBoolean(1, aPointCount);
209 GeomAPI_Interpolate aInterpolator(thePoints, theIsClosed, 0);
210 if (aPointCount == 2)
212 aTangentFlags->SetValue(1, Standard_False);
213 aTangentFlags->SetValue(2, Standard_False);
217 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
220 if (aPN != 1 || theIsClosed)
222 const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
223 aTangent = gp_Vec(thePoints->Value(aPN1),
224 thePoints->Value(aPN)).Normalized();
226 if (aPN < aPointCount || theIsClosed)
228 const Standard_Integer aPN2 = (aPN != aPointCount) ? (aPN + 1) : 1;
229 const gp_Vec aTangent2 = aTangent +
230 gp_Vec(thePoints->Value(aPN), thePoints->Value(aPN2)).Normalized();
231 if (aTangent2.SquareMagnitude() >= Precision::SquareConfusion())
233 aTangent = aTangent2.Normalized();
237 aTangent = -aTangent;
240 aTangents.SetValue(aPN, aTangent);
241 aTangentFlags->SetValue(aPN, Standard_True);
246 aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
247 aInterpolator.Perform();
248 const bool aResult = (aInterpolator.IsDone() == Standard_True);
251 theBSpline = aInterpolator.Curve();
256 //=======================================================================
257 // function : constructWire
259 //=======================================================================
260 TopoDS_Wire CurveCreator_Utils::ConstructWire(
261 Handle(TColgp_HArray1OfPnt) thePoints,
262 const bool theIsPolyline,
263 const bool theIsClosed)
266 BRep_Builder aBuilder;
267 aBuilder.MakeWire(aWire);
268 const int aPointCount = thePoints->Length();
271 const TopoDS_Vertex aFirstVertex =
272 BRepBuilderAPI_MakeVertex(thePoints->Value(1));
273 TopoDS_Vertex aVertex = aFirstVertex;
274 for (Standard_Integer aPN = 1; aPN < aPointCount; ++aPN)
276 const TopoDS_Vertex aVertex2 =
277 BRepBuilderAPI_MakeVertex(thePoints->Value(aPN + 1));
278 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aVertex2));
281 if (theIsClosed && aPointCount > 1)
283 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aFirstVertex));
288 Handle(Geom_BSplineCurve) aBSpline;
289 if (constructBSpline(thePoints, theIsClosed, aBSpline))
291 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aBSpline));
297 //=======================================================================
298 // function : constructShape
300 //=======================================================================
301 void CurveCreator_Utils::constructShape(
302 const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape)
304 BRep_Builder aBuilder;
305 TopoDS_Compound aShape;
306 aBuilder.MakeCompound(aShape);
307 const int aSectionCount = theCurve->getNbSections();
308 for (int aSectionI = 0; aSectionI < aSectionCount; ++aSectionI)
310 const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
311 if (aTmpPointCount == 0)
316 // Get the different points.
317 Handle(TColgp_HArray1OfPnt) aPoints = theCurve->GetDifferentPoints( aSectionI );
318 const int aPointCount = aPoints->Length();
319 const bool isClosed = theCurve->isClosed(aSectionI);
321 // Add the vertices to the shape.
322 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
324 aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex(aPoints->Value(aPN)));
327 // Add the wire to the shape.
328 const bool isPolyline =
329 (theCurve->getSectionType(aSectionI) == CurveCreator::Polyline);
330 const TopoDS_Wire aWire = ConstructWire(aPoints, isPolyline, isClosed);
333 aBuilder.Add(aShape, aWire);
340 * This is an intermediate structure for curve construction.
344 Section3D() : myIsClosed(false), myIsBSpline(false)
349 Handle(TColgp_HArray1OfPnt) myPoints;
352 //=======================================================================
353 // function : constructCurve
355 //=======================================================================
356 bool CurveCreator_Utils::constructCurve
357 (const TopoDS_Shape theShape,
358 CurveCreator_Curve *theCurve,
361 if (theShape.IsNull()) {
365 // Collect wires or vertices from shape.
366 TopTools_ListOfShape aWOrV;
367 TopAbs_ShapeEnum aType = theShape.ShapeType();
369 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
370 aWOrV.Append(theShape);
371 } else if (aType == TopAbs_COMPOUND) {
372 TopoDS_Iterator aShIter(theShape);
374 for (; aShIter.More(); aShIter.Next()) {
375 const TopoDS_Shape &aSubShape = aShIter.Value();
377 aType = aSubShape.ShapeType();
379 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
380 aWOrV.Append(aSubShape);
382 // Only subshapes of types wire or vertex are supported.
387 // Only wire (vertex) or compound of wires (vertices) are supported.
391 // Treat each wire or vertex. Get points, compute the working plane.
393 Standard_Integer aPlaneStatus = PLN_FREE;
394 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
395 std::list<Section3D> aListSec;
397 for (; anIter.More(); anIter.Next()) {
400 aSec3D.myPoints = CurveCreator_Utils::getPoints
401 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
403 if (aSec3D.myPoints.IsNull()) {
407 aListSec.push_back(aSec3D);
409 if (aPlaneStatus != PLN_FIXED) {
411 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
415 // Check if it is possible to change a computed coordinate system by
416 // XOY, XOZ or YOZ or parallel to them.
417 gp_Pnt aO(0., 0., 0.);
418 gp_Dir aNDir(0., 0., 1.);
419 gp_Dir aXDir(1., 0., 0.);
421 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
423 switch (aPlaneStatus) {
426 // Change the location.
427 aO.SetZ(aPlane.Location().Z());
428 anAxis.SetLocation(aO);
429 aPlane.SetPosition(anAxis);
434 // Fixed origin + OX axis
435 const gp_Dir &aPlnX = aPlane.Position().XDirection();
437 if (Abs(aPlnX.Z()) <= aTolAng) {
438 // Make a coordinate system parallel to XOY.
439 aO.SetZ(aPlane.Location().Z());
440 anAxis.SetLocation(aO);
441 aPlane.SetPosition(anAxis);
442 } else if (Abs(aPlnX.Y()) <= aTolAng) {
443 // Make a coordinate system parallel to XOZ.
444 aO.SetY(aPlane.Location().Y());
445 aNDir.SetCoord(0., 1., 0.);
446 aXDir.SetCoord(0., 0., 1.);
447 anAxis = gp_Ax3(aO, aNDir, aXDir);
448 aPlane.SetPosition(anAxis);
449 } else if (Abs(aPlnX.X()) <= aTolAng) {
450 // Make a coordinate system parallel to YOZ.
451 aO.SetX(aPlane.Location().X());
452 aNDir.SetCoord(1., 0., 0.);
453 aXDir.SetCoord(0., 1., 0.);
454 anAxis = gp_Ax3(aO, aNDir, aXDir);
455 aPlane.SetPosition(anAxis);
461 const gp_Dir &aPlnN = aPlane.Position().Direction();
462 gp_Dir aYDir(0., 1., 0.);
464 if (aPlnN.IsParallel(aNDir, aTolAng)) {
465 // Make a coordinate system parallel to XOY.
466 aO.SetZ(aPlane.Location().Z());
467 anAxis.SetLocation(aO);
468 aPlane.SetPosition(anAxis);
469 } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
470 // Make a coordinate system parallel to XOZ.
471 aO.SetY(aPlane.Location().Y());
472 aNDir.SetCoord(0., 1., 0.);
473 aXDir.SetCoord(0., 0., 1.);
474 anAxis = gp_Ax3(aO, aNDir, aXDir);
475 aPlane.SetPosition(anAxis);
476 } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
477 // Make a coordinate system parallel to YOZ.
478 aO.SetX(aPlane.Location().X());
479 aNDir.SetCoord(1., 0., 0.);
480 aXDir.SetCoord(0., 1., 0.);
481 anAxis = gp_Ax3(aO, aNDir, aXDir);
482 aPlane.SetPosition(anAxis);
489 aPlane.SetPosition(anAxis);
493 // Compute 2d points.
494 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
495 Standard_Real aTolConf2 =
496 Precision::Confusion()*Precision::Confusion();
500 for (; aSecIt != aListSec.end(); ++aSecIt) {
502 CurveCreator::Coordinates aCoords;
504 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
505 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
507 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
508 // The point doesn't lie on the plane.
512 ElSLib::Parameters(aPlane, aPnt, aX, aY);
513 aCoords.push_back(aX);
514 aCoords.push_back(aY);
517 // Add a new section to the curve.
518 const std::string aSecName =
519 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
520 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
521 CurveCreator::Spline : CurveCreator::Polyline;
523 theCurve->addSectionInternal(aSecName, aSecType,
524 aSecIt->myIsClosed, aCoords);
527 // Set the local coordinate system.
528 theLocalCS = aPlane.Position();
533 class CompareSectionToPoint
536 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
537 : mySectionId( theISection ), myPointId( theIPoint ) {};
538 ~CompareSectionToPoint() {}
540 bool operator < ( const CompareSectionToPoint& theOther ) const
542 bool isLess = mySectionId < theOther.mySectionId;
543 if ( !isLess && mySectionId == theOther.mySectionId )
544 isLess = myPointId < theOther.myPointId;
554 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
555 const CurveCreator_ICurve* theCurve,
556 CurveCreator_ICurve::SectionToPointList& thePoints )
560 std::list<double> aSelectedPoints;
562 std::map<CompareSectionToPoint, int> aPointsMap;
564 CurveCreator_ICurve::SectionToPointList aPoints;
565 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
566 TopoDS_Vertex aVertex;
567 TopoDS_Shape aShape = theContext->SelectedShape();
568 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
569 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
571 if ( aVertex.IsNull() )
573 aPnt = BRep_Tool::Pnt( aVertex );
575 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
576 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
577 aLast = aPoints.end();
578 CompareSectionToPoint aPoint;
579 for ( ; anIt != aLast; anIt++ ) {
580 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
581 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
583 aPointsMap[aPoint] = 0;
585 thePoints.push_back( *anIt );
590 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
591 const CurveCreator_ICurve* theCurve,
592 const CurveCreator_ICurve::SectionToPointList& thePoints )
597 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
598 if ( anAIS.IsNull() )
600 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
601 if ( anAISShape.IsNull() )
604 //ASL: we convert list of point indices to list of points coordinates
605 int aSize = thePoints.size();
606 std::vector<gp_Pnt> aPntsToSelect( aSize );
608 CurveCreator_ICurve::SectionToPointList::const_iterator
609 aPIt = thePoints.begin(), aPLast = thePoints.end();
610 CurveCreator_ICurve::SectionToPoint aSToPoint;
611 for( int i=0; aPIt != aPLast; aPIt++, i++ )
614 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
615 aPntsToSelect[i] = aPntToSelect;
618 theContext->ClearSelected( Standard_False );
619 //ASL: we switch off automatic highlight to improve performance of selection
620 theContext->SetAutomaticHilight( Standard_False );
622 Handle(SelectMgr_Selection) aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
624 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
625 aLast = thePoints.end();
626 bool isFound = false;
627 for( int i=0; i<aSize; i++ )
629 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
631 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
632 if( aHSenEntity.IsNull() )
634 Handle(SelectBasics_SensitiveEntity) aSenEntity = aHSenEntity->BaseSensitive();
636 Handle(Select3D_SensitivePoint) aSenPnt = Handle(Select3D_SensitivePoint)::DownCast( aSenEntity );
638 gp_Pnt anOwnerPnt = aSenPnt->Point();
639 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast( aSenPnt->OwnerId() );
641 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
642 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
645 theContext->AddOrRemoveSelected( anOwner, Standard_False );
651 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
652 // and call HilightPicked to draw selected owners
653 theContext->SetAutomaticHilight( Standard_True );
654 #if OCC_VERSION_LARGE <= 0x07030000
655 theContext->LocalContext()->HilightPicked( Standard_True );
659 //=======================================================================
660 // function : setLocalPointContext
661 // purpose : Open/close the viewer local context
662 //=======================================================================
663 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
664 Handle(AIS_InteractiveContext) theContext,
671 #if OCC_VERSION_LARGE <= 0x07030000
672 // Open local context if there is no one
673 if ( !theContext->HasOpenedContext() ) {
675 theContext->ClearCurrents( false );
676 #if OCC_VERSION_LARGE <= 0x07030000
677 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
680 // load the curve AIS object to the local context with the point selection
681 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
682 if ( !anAIS.IsNull() )
684 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
686 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
687 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
692 #if OCC_VERSION_LARGE <= 0x07030000
693 if ( theContext->HasOpenedContext() )
694 theContext->CloseAllContexts( Standard_True );
696 theContext->Deactivate();
697 theContext->Activate(0);
702 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
703 Handle(AIS_InteractiveObject) theObject,
704 const int theX, const int theY,
706 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
708 bool isFullFound = false;
710 if ( theObject.IsNull() || theView.IsNull() )
712 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
713 if ( aShape.IsNull() )
715 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
716 if ( aCompound.IsNull() )
719 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
720 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
721 Standard_Real aParameter;
722 bool isFound = false;
723 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
724 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
725 for ( ; anExp.More(); anExp.Next())
727 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
728 if ( anEdge.IsNull() )
730 Standard_Real aFirst, aLast;
731 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
732 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
733 Handle(Geom_BSplineCurve) aBSplineCurve =
734 Handle(Geom_BSplineCurve)::DownCast( aCurve );
735 if ( !aBSplineCurve.IsNull() ) {
736 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
737 aParameter, aDelta );
739 aCurPoint = aBSplineCurve->Value( aParameter );
740 Standard_Integer anI1, anI2;
741 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
742 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
743 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
747 else { // a curve built on a polyline edge
748 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
749 if ( aGLine.IsNull() )
751 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
754 aCurPoint = aGLine->Value( aParameter );
755 TopoDS_Vertex V1, V2;
756 TopExp::Vertices( anEdge, V1, V2, Standard_True );
757 if ( V1.IsNull() || V2.IsNull() )
759 aCurPoint1 = BRep_Tool::Pnt(V1);
760 aCurPoint2 = BRep_Tool::Pnt(V2);
762 // check that the projected point is on the bounded curve
763 gp_Vec aVec1( aCurPoint1, aCurPoint );
764 gp_Vec aVec2( aCurPoint2, aCurPoint );
765 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
768 if ( isFound && aMinDelta >= aDelta ) {
772 aFoundPnt1 = aCurPoint1;
773 aFoundPnt2 = aCurPoint2;
774 aFoundPoint = aCurPoint;
778 int aX, anY, aX1, anY1, aX2, anY2;
780 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
781 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
782 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
784 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
785 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
787 thePoint = aFoundPoint;
788 thePoint1 = aFoundPnt1;
789 thePoint2 = aFoundPnt2;
795 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
796 const int theX, const int theY,
797 const Handle(Geom_Curve)& theCurve,
798 Standard_Real& theParameter,
801 bool isFound = false;
802 if ( theView.IsNull() )
805 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
807 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
808 Standard_Integer aNbPoint = aProj.NbPoints();
810 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
811 gp_Pnt aNewPoint = aProj.Point( j );
812 theParameter = aProj.Parameter( j );
815 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
817 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
823 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
824 const int theOtherY, const double theTolerance, int& theDelta )
826 int aXDelta = abs( theX - theOtherX );
827 int anYDelta = abs( theY - theOtherY );
829 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
831 return aXDelta < theTolerance && anYDelta < theTolerance;
834 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
836 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
839 //=======================================================================
840 // function : getPoints
842 //=======================================================================
843 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
844 (const TopoDS_Shape &theShape,
848 Handle(TColgp_HArray1OfPnt) aResult;
853 if (theShape.IsNull()) {
857 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
859 if (aShType == TopAbs_VERTEX) {
860 // There is a single point.
861 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
863 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
866 } else if (aShType != TopAbs_WIRE) {
867 // The shape is neither a vertex nor a wire.
872 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
875 // Empty wires are not allowed.
879 // Treat the first edge.
880 TopoDS_Edge anEdge = anExp.Current();
881 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
883 if (aCurve.IsNull()) {
887 // Check the curve type.
888 Handle(Standard_Type) aType = aCurve->DynamicType();
890 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
892 } else if (aType != STANDARD_TYPE(Geom_Line)) {
893 // The curve is neither a line or a BSpline. It is not valid.
897 // Go to the next edge.
898 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
904 // There should be a single BSpline curve in the wire.
909 // Construct a section from poles of BSpline.
910 Handle(Geom_BSplineCurve) aBSplCurve =
911 Handle(Geom_BSplineCurve)::DownCast(aCurve);
913 // Check if the edge is valid. It should not be based on trimmed curve.
914 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
918 TopExp::Vertices(anEdge, aV[0], aV[1]);
920 for (i = 0; i < 2; i++) {
921 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
922 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
924 if (!aPnt.IsEqual(aCP[i], aTol)) {
929 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
931 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
932 TColStd_Array1OfReal aKnots(1, aNbPoints);
933 aBSplCurve->Knots(aKnots);
935 // Don't consider the last point as it coincides with the first
939 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
940 for (i = 1; i <= aNbPoints; ++i)
941 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
945 // This is a polyline.
946 TopTools_ListOfShape aVertices;
947 Standard_Integer aNbVtx = 1;
950 aVertices.Append(aFirstVtx);
952 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
953 anEdge = anExp.Current();
954 aCurve = GetCurve(anEdge);
956 if (aCurve.IsNull()) {
960 aType = aCurve->DynamicType();
962 if (aType != STANDARD_TYPE(Geom_Line)) {
963 // The curve is not a line. It is not valid.
967 // Add the current vertex to the list.
968 aVertices.Append(anExp.CurrentVertex());
971 // Check if the section is closed.
972 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
974 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
976 // Store a last vertex
979 aVertices.Append(aLastVtx);
983 // Fill the array of points.
984 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
987 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
989 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
990 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
992 aResult->SetValue(i, aPnt);
998 //=======================================================================
999 // function : FindPlane
1001 //=======================================================================
1002 void CurveCreator_Utils::FindPlane
1003 (const Handle(TColgp_HArray1OfPnt) &thePoints,
1005 Standard_Integer &thePlnStatus)
1007 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1008 // The plane can't be defined or is fixed. Nothing to change.
1013 const Standard_Real aTolConf = Precision::Confusion();
1015 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1016 const gp_Pnt &aPnt = thePoints->Value(i);
1018 switch (thePlnStatus) {
1021 thePlane.SetLocation(aPnt);
1022 thePlnStatus = PLN_ORIGIN;
1026 // Fix origin + OX axis
1027 const gp_Pnt &aPlnLoc = thePlane.Location();
1029 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1031 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1032 gp_Ax3 aXNorm(aPlnLoc, aXDir);
1033 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1035 thePlane.SetPosition(aNewPlnPos);
1036 thePlnStatus = PLN_OX;
1043 gp_Lin aXLin(thePlane.XAxis());
1044 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1046 if (aSqrDist > aTolConf*aTolConf) {
1047 // Compute main axis.
1048 const gp_Pnt &aPlnLoc = thePlane.Location();
1049 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1050 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1051 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1052 aXNorm.Direction());
1054 thePlane.SetPosition(aNewPlnPos);
1055 thePlnStatus = PLN_FIXED;