1 // Copyright (C) 2013-2020 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 ) );
623 const NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>& selected = aSelection->Entities();
625 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
626 aLast = thePoints.end();
627 bool isFound = false;
628 for( int i=0; i<aSize; i++ )
630 for ( NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator selIter( selected );
631 selIter.More(); selIter.Next() )
633 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = selIter.Value();
634 if( aHSenEntity.IsNull() )
636 Handle(SelectBasics_SensitiveEntity) aSenEntity = aHSenEntity->BaseSensitive();
638 Handle(Select3D_SensitivePoint) aSenPnt = Handle(Select3D_SensitivePoint)::DownCast( aSenEntity );
640 gp_Pnt anOwnerPnt = aSenPnt->Point();
641 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast( aSenPnt->OwnerId() );
643 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
644 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
647 theContext->AddOrRemoveSelected( anOwner, Standard_False );
653 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
654 // and call HilightPicked to draw selected owners
655 theContext->SetAutomaticHilight( Standard_True );
656 #if OCC_VERSION_LARGE <= 0x07030000
657 theContext->LocalContext()->HilightPicked( Standard_True );
661 //=======================================================================
662 // function : setLocalPointContext
663 // purpose : Open/close the viewer local context
664 //=======================================================================
665 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
666 Handle(AIS_InteractiveContext) theContext,
673 #if OCC_VERSION_LARGE <= 0x07030000
674 // Open local context if there is no one
675 if ( !theContext->HasOpenedContext() ) {
677 theContext->ClearCurrents( false );
678 #if OCC_VERSION_LARGE <= 0x07030000
679 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
682 // load the curve AIS object to the local context with the point selection
683 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
684 if ( !anAIS.IsNull() )
686 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
688 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
689 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
694 #if OCC_VERSION_LARGE <= 0x07030000
695 if ( theContext->HasOpenedContext() )
696 theContext->CloseAllContexts( Standard_True );
698 theContext->Deactivate();
699 theContext->Activate(0);
704 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
705 Handle(AIS_InteractiveObject) theObject,
706 const int theX, const int theY,
708 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
710 bool isFullFound = false;
712 if ( theObject.IsNull() || theView.IsNull() )
714 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
715 if ( aShape.IsNull() )
717 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
718 if ( aCompound.IsNull() )
721 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
722 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
723 Standard_Real aParameter;
724 bool isFound = false;
725 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
726 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
727 for ( ; anExp.More(); anExp.Next())
729 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
730 if ( anEdge.IsNull() )
732 Standard_Real aFirst, aLast;
733 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
734 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
735 Handle(Geom_BSplineCurve) aBSplineCurve =
736 Handle(Geom_BSplineCurve)::DownCast( aCurve );
737 if ( !aBSplineCurve.IsNull() ) {
738 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
739 aParameter, aDelta );
741 aCurPoint = aBSplineCurve->Value( aParameter );
742 Standard_Integer anI1, anI2;
743 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
744 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
745 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
749 else { // a curve built on a polyline edge
750 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
751 if ( aGLine.IsNull() )
753 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
756 aCurPoint = aGLine->Value( aParameter );
757 TopoDS_Vertex V1, V2;
758 TopExp::Vertices( anEdge, V1, V2, Standard_True );
759 if ( V1.IsNull() || V2.IsNull() )
761 aCurPoint1 = BRep_Tool::Pnt(V1);
762 aCurPoint2 = BRep_Tool::Pnt(V2);
764 // check that the projected point is on the bounded curve
765 gp_Vec aVec1( aCurPoint1, aCurPoint );
766 gp_Vec aVec2( aCurPoint2, aCurPoint );
767 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
770 if ( isFound && aMinDelta >= aDelta ) {
774 aFoundPnt1 = aCurPoint1;
775 aFoundPnt2 = aCurPoint2;
776 aFoundPoint = aCurPoint;
780 int aX, anY, aX1, anY1, aX2, anY2;
782 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
783 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
784 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
786 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
787 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
789 thePoint = aFoundPoint;
790 thePoint1 = aFoundPnt1;
791 thePoint2 = aFoundPnt2;
797 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
798 const int theX, const int theY,
799 const Handle(Geom_Curve)& theCurve,
800 Standard_Real& theParameter,
803 bool isFound = false;
804 if ( theView.IsNull() )
807 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
809 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
810 Standard_Integer aNbPoint = aProj.NbPoints();
812 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
813 gp_Pnt aNewPoint = aProj.Point( j );
814 theParameter = aProj.Parameter( j );
817 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
819 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
825 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
826 const int theOtherY, const double theTolerance, int& theDelta )
828 int aXDelta = abs( theX - theOtherX );
829 int anYDelta = abs( theY - theOtherY );
831 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
833 return aXDelta < theTolerance && anYDelta < theTolerance;
836 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
838 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
841 //=======================================================================
842 // function : getPoints
844 //=======================================================================
845 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
846 (const TopoDS_Shape &theShape,
850 Handle(TColgp_HArray1OfPnt) aResult;
855 if (theShape.IsNull()) {
859 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
861 if (aShType == TopAbs_VERTEX) {
862 // There is a single point.
863 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
865 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
868 } else if (aShType != TopAbs_WIRE) {
869 // The shape is neither a vertex nor a wire.
874 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
877 // Empty wires are not allowed.
881 // Treat the first edge.
882 TopoDS_Edge anEdge = anExp.Current();
883 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
885 if (aCurve.IsNull()) {
889 // Check the curve type.
890 Handle(Standard_Type) aType = aCurve->DynamicType();
892 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
894 } else if (aType != STANDARD_TYPE(Geom_Line)) {
895 // The curve is neither a line or a BSpline. It is not valid.
899 // Go to the next edge.
900 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
906 // There should be a single BSpline curve in the wire.
911 // Construct a section from poles of BSpline.
912 Handle(Geom_BSplineCurve) aBSplCurve =
913 Handle(Geom_BSplineCurve)::DownCast(aCurve);
915 // Check if the edge is valid. It should not be based on trimmed curve.
916 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
920 TopExp::Vertices(anEdge, aV[0], aV[1]);
922 for (i = 0; i < 2; i++) {
923 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
924 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
926 if (!aPnt.IsEqual(aCP[i], aTol)) {
931 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
933 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
934 TColStd_Array1OfReal aKnots(1, aNbPoints);
935 aBSplCurve->Knots(aKnots);
937 // Don't consider the last point as it coincides with the first
941 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
942 for (i = 1; i <= aNbPoints; ++i)
943 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
947 // This is a polyline.
948 TopTools_ListOfShape aVertices;
949 Standard_Integer aNbVtx = 1;
952 aVertices.Append(aFirstVtx);
954 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
955 anEdge = anExp.Current();
956 aCurve = GetCurve(anEdge);
958 if (aCurve.IsNull()) {
962 aType = aCurve->DynamicType();
964 if (aType != STANDARD_TYPE(Geom_Line)) {
965 // The curve is not a line. It is not valid.
969 // Add the current vertex to the list.
970 aVertices.Append(anExp.CurrentVertex());
973 // Check if the section is closed.
974 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
976 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
978 // Store a last vertex
981 aVertices.Append(aLastVtx);
985 // Fill the array of points.
986 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
989 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
991 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
992 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
994 aResult->SetValue(i, aPnt);
1000 //=======================================================================
1001 // function : FindPlane
1003 //=======================================================================
1004 void CurveCreator_Utils::FindPlane
1005 (const Handle(TColgp_HArray1OfPnt) &thePoints,
1007 Standard_Integer &thePlnStatus)
1009 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1010 // The plane can't be defined or is fixed. Nothing to change.
1015 const Standard_Real aTolConf = Precision::Confusion();
1017 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1018 const gp_Pnt &aPnt = thePoints->Value(i);
1020 switch (thePlnStatus) {
1023 thePlane.SetLocation(aPnt);
1024 thePlnStatus = PLN_ORIGIN;
1028 // Fix origin + OX axis
1029 const gp_Pnt &aPlnLoc = thePlane.Location();
1031 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1033 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1034 gp_Ax3 aXNorm(aPlnLoc, aXDir);
1035 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1037 thePlane.SetPosition(aNewPlnPos);
1038 thePlnStatus = PLN_OX;
1045 gp_Lin aXLin(thePlane.XAxis());
1046 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1048 if (aSqrDist > aTolConf*aTolConf) {
1049 // Compute main axis.
1050 const gp_Pnt &aPlnLoc = thePlane.Location();
1051 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1052 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1053 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1054 aXNorm.Direction());
1056 thePlane.SetPosition(aNewPlnPos);
1057 thePlnStatus = PLN_FIXED;