1 // Copyright (C) 2013-2023 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 #include <Geom_Point.hxx>
45 #include <Geom_BSplineCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <Geom_Curve.hxx>
48 #include <Geom_TrimmedCurve.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <GeomAPI_ProjectPointOnCurve.hxx>
54 #include <SelectMgr_EntityOwner.hxx>
55 #include <SelectMgr_Selection.hxx>
56 #include <Select3D_SensitivePoint.hxx>
58 #include <BRep_Tool.hxx>
59 #include <BRep_Builder.hxx>
60 #include <BRepBuilderAPI_MakeVertex.hxx>
61 #include <BRepBuilderAPI_MakeEdge.hxx>
62 #include <BRepBuilderAPI_MakeWire.hxx>
63 #include <BRepTools_WireExplorer.hxx>
65 #include <TColgp_HArray1OfPnt.hxx>
66 #include <TColStd_HArray1OfBoolean.hxx>
67 #include <TColStd_Array1OfReal.hxx>
68 #include <TColgp_Array1OfVec.hxx>
69 #include <GeomAPI_Interpolate.hxx>
71 #include <ProjLib.hxx>
76 #include "CurveCreator_ICurve.hxx"
78 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
79 const int SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
80 const int SCENE_PIXEL_POINT_TOLERANCE = 5;
88 * This static function returns the curve of original type from the edge.
90 * \param theEdge the edge
91 * \return the curve of original type. Can be null handle.
93 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
95 Handle(Geom_Curve) aResult;
97 if (theEdge.IsNull()) {
104 aResult = BRep_Tool::Curve(theEdge, aF, aL);
106 if (aResult.IsNull()) {
110 // Get the curve of original type
111 Handle(Standard_Type) aType = aResult->DynamicType();
113 while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
114 Handle(Geom_TrimmedCurve) aTrCurve =
115 Handle(Geom_TrimmedCurve)::DownCast(aResult);
117 aResult = aTrCurve->BasisCurve();
118 aType = aResult->DynamicType();
124 //=======================================================================
125 // function : ConvertClickToPoint()
126 // purpose : Returns the point clicked in 3D view
127 //=======================================================================
128 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
129 Handle(V3d_View) theView,
132 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
136 //=======================================================================
137 // function : ConvertClickToPoint()
138 // purpose : Returns the point clicked in 3D view
139 //=======================================================================
140 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
142 // the 3D point, that is a projection of the pixels to the XYZ view plane
143 //return GEOMUtils::ConvertClickToPoint( x, y, aView );
145 // we need the projection to the XOY plane
146 // 1. find a point in the plane of the eye and the normal to the plane
147 Standard_Real X, Y, Z;
148 Standard_Real Vx, Vy, Vz;
149 aView->ConvertWithProj( x, y, X, Y, Z, Vx, Vy, Vz );
151 // 2. build a ray from the point by the normal to the XOY plane and intersect it
152 // The ray equation is the following : p(x,y,z) = p0(x,y,z) + t*V(x,y,z)
153 // X,Y,Z - defines p0(x,y,z), Vx,Vy,Vz - defines V(x,y,z)
154 // p(x,y,z) - is a searched point, t - should to be calculated by the condition of XOY plane
155 // The system of equations is the following:
156 // p(x) = p0(x)+t*V(x)
157 // p(y) = p0(y)+t*V(y)
158 // p(z) = p0(z)+t*V(z)
161 Standard_Real aXp, aYp, aZp;
162 //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
163 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
164 if ( fabs( Vz ) > aPrec ) {
165 Standard_Real aT = -Z/Vz;
170 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
172 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
174 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
177 /*std::cout << "ConvertClickToPoint: " << std::endl
178 << "XYZ1 = (" << X << ", " << Y << ", " << Z << "); " << std::endl
179 << "Vxyz = (" << Vx << ", " << Vy << ", " << Vz << "); " << std::endl
180 << "Resp = (" << aXp << ", " << aYp << ", " << aZp << "); " << std::endl;*/
182 gp_Pnt ResultPoint( aXp, aYp, aZp );
186 //=======================================================================
187 // function : constructBSpline
189 //=======================================================================
190 bool CurveCreator_Utils::constructBSpline(
191 const Handle(TColgp_HArray1OfPnt)& thePoints,
192 const Standard_Boolean theIsClosed,
193 Handle(Geom_BSplineCurve)& theBSpline)
195 const int aPointCount = thePoints->Length();
196 if (aPointCount <= 1)
201 // Calculate the tangents.
202 TColgp_Array1OfVec aTangents(1, aPointCount);
203 Handle(TColStd_HArray1OfBoolean) aTangentFlags =
204 new TColStd_HArray1OfBoolean(1, aPointCount);
205 GeomAPI_Interpolate aInterpolator(thePoints, theIsClosed, 0);
206 if (aPointCount == 2)
208 aTangentFlags->SetValue(1, Standard_False);
209 aTangentFlags->SetValue(2, Standard_False);
213 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
216 if (aPN != 1 || theIsClosed)
218 const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
219 aTangent = gp_Vec(thePoints->Value(aPN1),
220 thePoints->Value(aPN)).Normalized();
222 if (aPN < aPointCount || theIsClosed)
224 const Standard_Integer aPN2 = (aPN != aPointCount) ? (aPN + 1) : 1;
225 const gp_Vec aTangent2 = aTangent +
226 gp_Vec(thePoints->Value(aPN), thePoints->Value(aPN2)).Normalized();
227 if (aTangent2.SquareMagnitude() >= Precision::SquareConfusion())
229 aTangent = aTangent2.Normalized();
233 aTangent = -aTangent;
236 aTangents.SetValue(aPN, aTangent);
237 aTangentFlags->SetValue(aPN, Standard_True);
242 aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
243 aInterpolator.Perform();
244 const bool aResult = (aInterpolator.IsDone() == Standard_True);
247 theBSpline = aInterpolator.Curve();
252 //=======================================================================
253 // function : constructWire
255 //=======================================================================
256 TopoDS_Wire CurveCreator_Utils::ConstructWire(
257 Handle(TColgp_HArray1OfPnt) thePoints,
258 const bool theIsPolyline,
259 const bool theIsClosed)
262 BRep_Builder aBuilder;
263 aBuilder.MakeWire(aWire);
264 const int aPointCount = thePoints->Length();
267 const TopoDS_Vertex aFirstVertex =
268 BRepBuilderAPI_MakeVertex(thePoints->Value(1));
269 TopoDS_Vertex aVertex = aFirstVertex;
270 for (Standard_Integer aPN = 1; aPN < aPointCount; ++aPN)
272 const TopoDS_Vertex aVertex2 =
273 BRepBuilderAPI_MakeVertex(thePoints->Value(aPN + 1));
274 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aVertex2));
277 if (theIsClosed && aPointCount > 1)
279 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aFirstVertex));
284 Handle(Geom_BSplineCurve) aBSpline;
285 if (constructBSpline(thePoints, theIsClosed, aBSpline))
287 aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aBSpline));
293 //=======================================================================
294 // function : constructShape
296 //=======================================================================
297 void CurveCreator_Utils::constructShape(
298 const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape,
299 NCollection_IndexedDataMap<int, TopoDS_Shape>* theSect2Shape )
301 BRep_Builder aBuilder;
302 TopoDS_Compound aShape;
303 aBuilder.MakeCompound(aShape);
305 const int aSectionCount = theCurve->getNbSections();
306 for (int aSectionI = 0; aSectionI < aSectionCount; ++aSectionI)
308 const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
309 if (aTmpPointCount == 0)
314 // Get the different points.
315 Handle(TColgp_HArray1OfPnt) aPoints = theCurve->GetDifferentPoints( aSectionI );
316 const int aPointCount = aPoints->Length();
317 const bool isClosed = theCurve->isClosed(aSectionI);
319 TopoDS_Compound ShapeWireWithV;
321 aBuilder.MakeCompound(ShapeWireWithV);
323 // Add the vertices to the shape.
324 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
327 aBuilder.MakeVertex(V,aPoints->Value(aPN),Precision::Confusion());
328 aBuilder.Add(aShape, V);
330 aBuilder.Add(ShapeWireWithV, V);
333 // Add the wire to the shape.
334 const bool isPolyline =
335 (theCurve->getSectionType(aSectionI) == CurveCreator::Polyline);
336 const TopoDS_Wire aWire = ConstructWire(aPoints, isPolyline, isClosed);
339 aBuilder.Add(aShape, aWire);
342 aBuilder.Add(ShapeWireWithV, aWire);
343 (*theSect2Shape).Add(aSectionI, ShapeWireWithV);
351 * This is an intermediate structure for curve construction.
355 Section3D() : myIsClosed(false), myIsBSpline(false)
360 Handle(TColgp_HArray1OfPnt) myPoints;
363 Quantity_Color CurveCreator_Utils::getRandColor()
365 float aHue = ( rand()%1000 ) * 0.001f;
368 aColor.setHsl( (int)(aHue*255.), 200, 128 );
369 int r = aColor.red();
370 int g = aColor.green();
371 int b = aColor.blue();
373 double r1 = r / 255.0;
374 double g1 = g / 255.0;
375 double b1 = b / 255.0;
376 return Quantity_Color( r1, g1, b1, Quantity_TOC_RGB );
379 Quantity_Color CurveCreator_Utils::colorConv(QColor color)
381 return Quantity_Color( color.red() / 255.,
382 color.green() / 255., color.blue() / 255., Quantity_TOC_RGB );
385 QColor CurveCreator_Utils::colorConv(Quantity_Color color)
387 return QColor( (int)( color.Red() * 255 ), (int)( color.Green() * 255 ), (int)( color.Blue() * 255 ) );
390 //=======================================================================
391 // function : constructCurve
393 //=======================================================================
394 bool CurveCreator_Utils::constructCurve
395 (const TopoDS_Shape theShape,
396 CurveCreator_Curve *theCurve,
399 if (theShape.IsNull()) {
403 // Collect wires or vertices from shape.
404 TopTools_ListOfShape aWOrV;
405 TopAbs_ShapeEnum aType = theShape.ShapeType();
407 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
408 aWOrV.Append(theShape);
409 } else if (aType == TopAbs_COMPOUND) {
410 TopoDS_Iterator aShIter(theShape);
412 for (; aShIter.More(); aShIter.Next()) {
413 const TopoDS_Shape &aSubShape = aShIter.Value();
415 aType = aSubShape.ShapeType();
417 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
418 aWOrV.Append(aSubShape);
420 // Only subshapes of types wire or vertex are supported.
425 // Only wire (vertex) or compound of wires (vertices) are supported.
429 // Treat each wire or vertex. Get points, compute the working plane.
431 Standard_Integer aPlaneStatus = PLN_FREE;
432 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
433 std::list<Section3D> aListSec;
435 for (; anIter.More(); anIter.Next()) {
438 aSec3D.myPoints = CurveCreator_Utils::getPoints
439 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
441 if (aSec3D.myPoints.IsNull()) {
445 aListSec.push_back(aSec3D);
447 if (aPlaneStatus != PLN_FIXED) {
449 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
453 // Check if it is possible to change a computed coordinate system by
454 // XOY, XOZ or YOZ or parallel to them.
455 gp_Pnt aO(0., 0., 0.);
456 gp_Dir aNDir(0., 0., 1.);
457 gp_Dir aXDir(1., 0., 0.);
459 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
461 switch (aPlaneStatus) {
464 // Change the location.
465 aO.SetZ(aPlane.Location().Z());
466 anAxis.SetLocation(aO);
467 aPlane.SetPosition(anAxis);
472 // Fixed origin + OX axis
473 const gp_Dir &aPlnX = aPlane.Position().XDirection();
475 if (Abs(aPlnX.Z()) <= aTolAng) {
476 // Make a coordinate system parallel to XOY.
477 aO.SetZ(aPlane.Location().Z());
478 anAxis.SetLocation(aO);
479 aPlane.SetPosition(anAxis);
480 } else if (Abs(aPlnX.Y()) <= aTolAng) {
481 // Make a coordinate system parallel to XOZ.
482 aO.SetY(aPlane.Location().Y());
483 aNDir.SetCoord(0., 1., 0.);
484 aXDir.SetCoord(0., 0., 1.);
485 anAxis = gp_Ax3(aO, aNDir, aXDir);
486 aPlane.SetPosition(anAxis);
487 } else if (Abs(aPlnX.X()) <= aTolAng) {
488 // Make a coordinate system parallel to YOZ.
489 aO.SetX(aPlane.Location().X());
490 aNDir.SetCoord(1., 0., 0.);
491 aXDir.SetCoord(0., 1., 0.);
492 anAxis = gp_Ax3(aO, aNDir, aXDir);
493 aPlane.SetPosition(anAxis);
499 const gp_Dir &aPlnN = aPlane.Position().Direction();
500 gp_Dir aYDir(0., 1., 0.);
502 if (aPlnN.IsParallel(aNDir, aTolAng)) {
503 // Make a coordinate system parallel to XOY.
504 aO.SetZ(aPlane.Location().Z());
505 anAxis.SetLocation(aO);
506 aPlane.SetPosition(anAxis);
507 } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
508 // Make a coordinate system parallel to XOZ.
509 aO.SetY(aPlane.Location().Y());
510 aNDir.SetCoord(0., 1., 0.);
511 aXDir.SetCoord(0., 0., 1.);
512 anAxis = gp_Ax3(aO, aNDir, aXDir);
513 aPlane.SetPosition(anAxis);
514 } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
515 // Make a coordinate system parallel to YOZ.
516 aO.SetX(aPlane.Location().X());
517 aNDir.SetCoord(1., 0., 0.);
518 aXDir.SetCoord(0., 1., 0.);
519 anAxis = gp_Ax3(aO, aNDir, aXDir);
520 aPlane.SetPosition(anAxis);
527 aPlane.SetPosition(anAxis);
531 // Compute 2d points.
532 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
533 Standard_Real aTolConf2 =
534 Precision::Confusion()*Precision::Confusion();
538 for (; aSecIt != aListSec.end(); ++aSecIt) {
540 CurveCreator::Coordinates aCoords;
542 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
543 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
545 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
546 // The point doesn't lie on the plane.
550 ElSLib::Parameters(aPlane, aPnt, aX, aY);
551 aCoords.push_back(aX);
552 aCoords.push_back(aY);
555 // Add a new section to the curve.
556 const std::string aSecName =
557 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
558 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
559 CurveCreator::Spline : CurveCreator::Polyline;
561 theCurve->addSectionInternal(aSecName, aSecType,
562 aSecIt->myIsClosed, aCoords, Quantity_NOC_RED);
565 // Set the local coordinate system.
566 theLocalCS = aPlane.Position();
571 class CompareSectionToPoint
574 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
575 : mySectionId( theISection ), myPointId( theIPoint ) {};
576 ~CompareSectionToPoint() {}
578 bool operator < ( const CompareSectionToPoint& theOther ) const
580 bool isLess = mySectionId < theOther.mySectionId;
581 if ( !isLess && mySectionId == theOther.mySectionId )
582 isLess = myPointId < theOther.myPointId;
592 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
593 const CurveCreator_ICurve* theCurve,
594 CurveCreator_ICurve::SectionToPointList& thePoints )
598 std::list<double> aSelectedPoints;
600 std::map<CompareSectionToPoint, int> aPointsMap;
602 CurveCreator_ICurve::SectionToPointList aPoints;
603 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
604 TopoDS_Vertex aVertex;
605 TopoDS_Shape aShape = theContext->SelectedShape();
606 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
607 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
609 if ( aVertex.IsNull() )
611 aPnt = BRep_Tool::Pnt( aVertex );
613 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
614 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
615 aLast = aPoints.end();
616 CompareSectionToPoint aPoint;
617 for ( ; anIt != aLast; anIt++ ) {
618 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
619 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
621 aPointsMap[aPoint] = 0;
623 thePoints.push_back( *anIt );
628 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
629 const CurveCreator_ICurve* theCurve,
630 const CurveCreator_ICurve::SectionToPointList& thePoints )
635 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
636 if ( anAIS.IsNull() )
638 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
639 if ( anAISShape.IsNull() )
642 //ASL: we convert list of point indices to list of points coordinates
643 int aSize = thePoints.size();
644 std::vector<gp_Pnt> aPntsToSelect( aSize );
646 CurveCreator_ICurve::SectionToPointList::const_iterator
647 aPIt = thePoints.begin(), aPLast = thePoints.end();
648 CurveCreator_ICurve::SectionToPoint aSToPoint;
649 for( int i=0; aPIt != aPLast; aPIt++, i++ )
652 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
653 aPntsToSelect[i] = aPntToSelect;
656 theContext->ClearSelected( Standard_False );
657 //ASL: we switch off automatic highlight to improve performance of selection
658 theContext->SetAutomaticHilight( Standard_False );
660 Handle(SelectMgr_Selection) aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
661 const NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>& selected = aSelection->Entities();
663 /*CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
664 aLast = thePoints.end();*/
665 //bool isFound = false;
666 for( int i=0; i<aSize; i++ )
668 for ( NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator selIter( selected );
669 selIter.More(); selIter.Next() )
671 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = selIter.Value();
672 if( aHSenEntity.IsNull() )
674 Handle(SelectBasics_SensitiveEntity) aSenEntity = aHSenEntity->BaseSensitive();
676 Handle(Select3D_SensitivePoint) aSenPnt = Handle(Select3D_SensitivePoint)::DownCast( aSenEntity );
678 gp_Pnt anOwnerPnt = aSenPnt->Point();
679 Handle(SelectMgr_EntityOwner) anOwner = aSenPnt->OwnerId();
681 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
682 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
685 theContext->AddOrRemoveSelected( anOwner, Standard_False );
691 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
692 // and call HilightPicked to draw selected owners
693 theContext->SetAutomaticHilight( Standard_True );
696 //=======================================================================
697 // function : setLocalPointContext
698 // purpose : Open/close the viewer local context
699 //=======================================================================
700 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
701 Handle(AIS_InteractiveContext) theContext,
708 theContext->ClearCurrents( false ); // todo: deprecated OCCT API
709 // load the curve AIS object to the local context with the point selection
710 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
711 if ( !anAIS.IsNull() )
713 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
715 theContext->Load( anAIS, -1/*selection mode*/);
716 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
721 theContext->Deactivate();
722 theContext->Activate(0);
726 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
727 Handle(AIS_InteractiveObject) theObject,
728 const int theX, const int theY,
730 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
732 bool isFullFound = false;
734 if ( theObject.IsNull() || theView.IsNull() )
736 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
737 if ( aShape.IsNull() )
739 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
740 if ( aCompound.IsNull() )
743 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
744 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
745 Standard_Real aParameter;
746 bool isFound = false;
747 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
748 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
749 for ( ; anExp.More(); anExp.Next())
751 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
752 if ( anEdge.IsNull() )
754 Standard_Real aFirst, aLast;
755 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
756 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
757 Handle(Geom_BSplineCurve) aBSplineCurve =
758 Handle(Geom_BSplineCurve)::DownCast( aCurve );
759 if ( !aBSplineCurve.IsNull() ) {
760 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
761 aParameter, aDelta );
763 aCurPoint = aBSplineCurve->Value( aParameter );
764 Standard_Integer anI1, anI2;
765 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
766 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
767 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
771 else { // a curve built on a polyline edge
772 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
773 if ( aGLine.IsNull() )
775 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
778 aCurPoint = aGLine->Value( aParameter );
779 TopoDS_Vertex V1, V2;
780 TopExp::Vertices( anEdge, V1, V2, Standard_True );
781 if ( V1.IsNull() || V2.IsNull() )
783 aCurPoint1 = BRep_Tool::Pnt(V1);
784 aCurPoint2 = BRep_Tool::Pnt(V2);
786 // check that the projected point is on the bounded curve
787 gp_Vec aVec1( aCurPoint1, aCurPoint );
788 gp_Vec aVec2( aCurPoint2, aCurPoint );
789 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
792 if ( isFound && aMinDelta >= aDelta ) {
796 aFoundPnt1 = aCurPoint1;
797 aFoundPnt2 = aCurPoint2;
798 aFoundPoint = aCurPoint;
802 int aX, anY, aX1, anY1, aX2, anY2;
804 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
805 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
806 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
808 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
809 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
811 thePoint = aFoundPoint;
812 thePoint1 = aFoundPnt1;
813 thePoint2 = aFoundPnt2;
819 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
820 const int theX, const int theY,
821 const Handle(Geom_Curve)& theCurve,
822 Standard_Real& theParameter,
825 bool isFound = false;
826 if ( theView.IsNull() )
829 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
831 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
832 Standard_Integer aNbPoint = aProj.NbPoints();
834 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
835 gp_Pnt aNewPoint = aProj.Point( j );
836 theParameter = aProj.Parameter( j );
839 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
841 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
847 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
848 const int theOtherY, const double theTolerance, int& theDelta )
850 int aXDelta = abs( theX - theOtherX );
851 int anYDelta = abs( theY - theOtherY );
853 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
855 return aXDelta < theTolerance && anYDelta < theTolerance;
858 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
860 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
863 //=======================================================================
864 // function : getPoints
866 //=======================================================================
867 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
868 (const TopoDS_Shape &theShape,
872 Handle(TColgp_HArray1OfPnt) aResult;
877 if (theShape.IsNull()) {
881 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
883 if (aShType == TopAbs_VERTEX) {
884 // There is a single point.
885 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
887 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
890 } else if (aShType != TopAbs_WIRE) {
891 // The shape is neither a vertex nor a wire.
896 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
899 // Empty wires are not allowed.
903 // Treat the first edge.
904 TopoDS_Edge anEdge = anExp.Current();
905 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
907 if (aCurve.IsNull()) {
911 // Check the curve type.
912 Handle(Standard_Type) aType = aCurve->DynamicType();
914 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
916 } else if (aType != STANDARD_TYPE(Geom_Line)) {
917 // The curve is neither a line or a BSpline. It is not valid.
921 // Go to the next edge.
922 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
928 // There should be a single BSpline curve in the wire.
933 // Construct a section from poles of BSpline.
934 Handle(Geom_BSplineCurve) aBSplCurve =
935 Handle(Geom_BSplineCurve)::DownCast(aCurve);
937 // Check if the edge is valid. It should not be based on trimmed curve.
938 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
942 TopExp::Vertices(anEdge, aV[0], aV[1]);
944 for (i = 0; i < 2; i++) {
945 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
946 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
948 if (!aPnt.IsEqual(aCP[i], aTol)) {
953 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
955 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
956 TColStd_Array1OfReal aKnots(1, aNbPoints);
957 aBSplCurve->Knots(aKnots);
959 // Don't consider the last point as it coincides with the first
963 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
964 for (i = 1; i <= aNbPoints; ++i)
965 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
969 // This is a polyline.
970 TopTools_ListOfShape aVertices;
971 Standard_Integer aNbVtx = 1;
974 aVertices.Append(aFirstVtx);
976 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
977 anEdge = anExp.Current();
978 aCurve = GetCurve(anEdge);
980 if (aCurve.IsNull()) {
984 aType = aCurve->DynamicType();
986 if (aType != STANDARD_TYPE(Geom_Line)) {
987 // The curve is not a line. It is not valid.
991 // Add the current vertex to the list.
992 aVertices.Append(anExp.CurrentVertex());
995 // Check if the section is closed.
996 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
998 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
1000 // Store a last vertex
1003 aVertices.Append(aLastVtx);
1007 // Fill the array of points.
1008 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
1011 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
1013 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
1014 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
1016 aResult->SetValue(i, aPnt);
1022 //=======================================================================
1023 // function : FindPlane
1025 //=======================================================================
1026 void CurveCreator_Utils::FindPlane
1027 (const Handle(TColgp_HArray1OfPnt) &thePoints,
1029 Standard_Integer &thePlnStatus)
1031 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1032 // The plane can't be defined or is fixed. Nothing to change.
1037 const Standard_Real aTolConf = Precision::Confusion();
1039 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1040 const gp_Pnt &aPnt = thePoints->Value(i);
1042 switch (thePlnStatus) {
1045 thePlane.SetLocation(aPnt);
1046 thePlnStatus = PLN_ORIGIN;
1050 // Fix origin + OX axis
1051 const gp_Pnt &aPlnLoc = thePlane.Location();
1053 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1055 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1056 gp_Ax3 aXNorm(aPlnLoc, aXDir);
1057 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1059 thePlane.SetPosition(aNewPlnPos);
1060 thePlnStatus = PLN_OX;
1067 gp_Lin aXLin(thePlane.XAxis());
1068 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1070 if (aSqrDist > aTolConf*aTolConf) {
1071 // Compute main axis.
1072 const gp_Pnt &aPlnLoc = thePlane.Location();
1073 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1074 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1075 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1076 aXNorm.Direction());
1078 thePlane.SetPosition(aNewPlnPos);
1079 thePlnStatus = PLN_FIXED;