1 // Copyright (C) 2013-2015 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_UtilsICurve.hxx"
25 #include <Basics_OCCTVersion.hxx>
27 #include <GEOMUtils.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS_Wire.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Compound.hxx>
37 #include <AIS_ListOfInteractive.hxx>
38 #include <AIS_ListIteratorOfListOfInteractive.hxx>
39 #include <AIS_Shape.hxx>
40 #include <AIS_Line.hxx>
41 #include <AIS_Trihedron.hxx>
42 #include <AIS_LocalContext.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 Quantity_Parameter 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 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
187 TopoDS_Shape& theShape )
189 BRep_Builder aBuilder;
190 TopoDS_Compound aComp;
191 aBuilder.MakeCompound( aComp );
192 for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
194 int theISection = iSection;
196 CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
197 int aPointSize = theCurve->getNbPoints( theISection );
198 if ( aPointSize == 0 )
201 bool aSectIsClosed = theCurve->isClosed( theISection );
202 bool isPolyline = aSectType == CurveCreator::Polyline;
205 gp_Pnt aPrevPoint, aPoint;
206 // filters the curve points to skip equal points
207 std::vector<gp_Pnt> aPoints;
208 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
209 aPoints.push_back( aPoint );
212 for( ; iPoint < aPointSize; iPoint++ ) {
213 CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
214 if ( !isEqualPoints( aPrevPoint, aPoint ) )
215 aPoints.push_back( aPoint );
218 int aNbPoints = aPoints.size();
220 if ( aNbPoints == 1 ) {
221 aPoint = aPoints.front();
222 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
223 aBuilder.Add( aComp, aVertex );
225 else if ( aNbPoints > 1 ) {
226 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aNbPoints);
227 TColgp_Array1OfVec aTangents(1, aNbPoints);
228 Handle(TColStd_HArray1OfBoolean) aTangentFlags = new TColStd_HArray1OfBoolean(1, aNbPoints);
229 gp_Vec aNullVec(0, 0, 0);
231 TopoDS_Edge aPointEdge;
232 TopoDS_Vertex aVertex;
234 std::vector<gp_Pnt>::const_iterator aPointIt = aPoints.begin(), aPointLast = aPoints.end();
238 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
239 aBuilder.Add( aComp, aVertex );
241 aHCurvePoints->SetValue( aHIndex, aPoint );
242 aTangents.SetValue( aHIndex, aNullVec );
243 aTangentFlags->SetValue( aHIndex, Standard_False );
249 for( ; aPointIt != aPointLast; aPointIt++ ) {
251 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
252 aBuilder.Add( aComp, aVertex );
254 TopoDS_Edge aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
255 aBuilder.Add( aComp, aPointEdge );
258 aHCurvePoints->SetValue( aHIndex, aPoint );
259 aTangents.SetValue( aHIndex, aNullVec );
260 aTangentFlags->SetValue( aHIndex, Standard_False );
265 if( aSectIsClosed && ( aNbPoints > 2 ) ) {
266 aPoint = aPoints.front();
267 aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
268 aBuilder.Add( aComp, aVertex );
270 aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
271 aBuilder.Add( aComp, aPointEdge );
276 Handle(Geom_BSplineCurve) aBSplineCurve;
277 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
278 // correct the spline degree to be as 3 for non-periodic spline if number of points
279 // less than 3. It is need to have a knot in each spline point. This knots are used
280 // to found a neighbour points when a new point is inserted between two existing.
281 if (!aSectIsClosed ) {
282 if (aHCurvePoints->Length() == 3)
283 aGBC.Load(aTangents, aTangentFlags);
288 aBSplineCurve = aGBC.Curve();
289 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
290 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
291 aBuilder.Add( aComp, aWire );
299 * This is an intermediate structure for curve construction.
303 Section3D() : myIsClosed(false), myIsBSpline(false)
308 Handle(TColgp_HArray1OfPnt) myPoints;
311 //=======================================================================
312 // function : constructCurve
314 //=======================================================================
315 bool CurveCreator_Utils::constructCurve
316 (const TopoDS_Shape theShape,
317 CurveCreator_Curve *theCurve,
320 if (theShape.IsNull()) {
324 // Collect wires or vertices from shape.
325 TopTools_ListOfShape aWOrV;
326 TopAbs_ShapeEnum aType = theShape.ShapeType();
328 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
329 aWOrV.Append(theShape);
330 } else if (aType == TopAbs_COMPOUND) {
331 TopoDS_Iterator aShIter(theShape);
333 for (; aShIter.More(); aShIter.Next()) {
334 const TopoDS_Shape &aSubShape = aShIter.Value();
336 aType = aSubShape.ShapeType();
338 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
339 aWOrV.Append(aSubShape);
341 // Only subshapes of types wire or vertex are supported.
346 // Only wire (vertex) or compound of wires (vertices) are supported.
350 // Treat each wire or vertex. Get points, compute the working plane.
352 Standard_Integer aPlaneStatus = PLN_FREE;
353 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
354 std::list<Section3D> aListSec;
356 for (; anIter.More(); anIter.Next()) {
359 aSec3D.myPoints = CurveCreator_Utils::getPoints
360 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
362 if (aSec3D.myPoints.IsNull()) {
366 aListSec.push_back(aSec3D);
368 if (aPlaneStatus != PLN_FIXED) {
370 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
374 // Check if it is possible to change a computed coordinate system by
375 // XOY, XOZ or YOZ or parallel to them.
376 gp_Pnt aO(0., 0., 0.);
377 gp_Dir aNDir(0., 0., 1.);
378 gp_Dir aXDir(1., 0., 0.);
380 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
382 switch (aPlaneStatus) {
385 // Change the location.
386 aO.SetZ(aPlane.Location().Z());
387 anAxis.SetLocation(aO);
388 aPlane.SetPosition(anAxis);
393 // Fixed origin + OX axis
394 const gp_Dir &aPlnX = aPlane.Position().XDirection();
396 if (Abs(aPlnX.Z()) <= aTolAng) {
397 // Make a coordinate system parallel to XOY.
398 aO.SetZ(aPlane.Location().Z());
399 anAxis.SetLocation(aO);
400 aPlane.SetPosition(anAxis);
401 } else if (Abs(aPlnX.Y()) <= aTolAng) {
402 // Make a coordinate system parallel to XOZ.
403 aO.SetY(aPlane.Location().Y());
404 aNDir.SetCoord(0., 1., 0.);
405 aXDir.SetCoord(0., 0., 1.);
406 anAxis = gp_Ax3(aO, aNDir, aXDir);
407 aPlane.SetPosition(anAxis);
408 } else if (Abs(aPlnX.X()) <= aTolAng) {
409 // Make a coordinate system parallel to YOZ.
410 aO.SetX(aPlane.Location().X());
411 aNDir.SetCoord(1., 0., 0.);
412 aXDir.SetCoord(0., 1., 0.);
413 anAxis = gp_Ax3(aO, aNDir, aXDir);
414 aPlane.SetPosition(anAxis);
420 const gp_Dir &aPlnN = aPlane.Position().Direction();
421 gp_Dir aYDir(0., 1., 0.);
423 if (aPlnN.IsParallel(aNDir, aTolAng)) {
424 // Make a coordinate system parallel to XOY.
425 aO.SetZ(aPlane.Location().Z());
426 anAxis.SetLocation(aO);
427 aPlane.SetPosition(anAxis);
428 } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
429 // Make a coordinate system parallel to XOZ.
430 aO.SetY(aPlane.Location().Y());
431 aNDir.SetCoord(0., 1., 0.);
432 aXDir.SetCoord(0., 0., 1.);
433 anAxis = gp_Ax3(aO, aNDir, aXDir);
434 aPlane.SetPosition(anAxis);
435 } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
436 // Make a coordinate system parallel to YOZ.
437 aO.SetX(aPlane.Location().X());
438 aNDir.SetCoord(1., 0., 0.);
439 aXDir.SetCoord(0., 1., 0.);
440 anAxis = gp_Ax3(aO, aNDir, aXDir);
441 aPlane.SetPosition(anAxis);
448 aPlane.SetPosition(anAxis);
452 // Compute 2d points.
453 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
454 Standard_Real aTolConf2 =
455 Precision::Confusion()*Precision::Confusion();
459 for (; aSecIt != aListSec.end(); ++aSecIt) {
461 CurveCreator::Coordinates aCoords;
463 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
464 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
466 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
467 // The point doesn't lie on the plane.
471 ElSLib::Parameters(aPlane, aPnt, aX, aY);
472 aCoords.push_back(aX);
473 aCoords.push_back(aY);
476 // Add a new section to the curve.
477 const std::string aSecName =
478 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
479 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
480 CurveCreator::Spline : CurveCreator::Polyline;
482 theCurve->addSectionInternal(aSecName, aSecType,
483 aSecIt->myIsClosed, aCoords);
486 // Set the local coordinate system.
487 theLocalCS = aPlane.Position();
492 class CompareSectionToPoint
495 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
496 : mySectionId( theISection ), myPointId( theIPoint ) {};
497 ~CompareSectionToPoint() {}
499 bool operator < ( const CompareSectionToPoint& theOther ) const
501 bool isLess = mySectionId < theOther.mySectionId;
502 if ( !isLess && mySectionId == theOther.mySectionId )
503 isLess = myPointId < theOther.myPointId;
513 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
514 const CurveCreator_ICurve* theCurve,
515 CurveCreator_ICurve::SectionToPointList& thePoints )
519 std::list<float> aSelectedPoints;
521 std::map<CompareSectionToPoint, int> aPointsMap;
523 CurveCreator_ICurve::SectionToPointList aPoints;
524 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
525 TopoDS_Vertex aVertex;
526 TopoDS_Shape aShape = theContext->SelectedShape();
527 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
528 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
530 if ( aVertex.IsNull() )
532 aPnt = BRep_Tool::Pnt( aVertex );
534 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
535 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
536 aLast = aPoints.end();
537 CompareSectionToPoint aPoint;
538 for ( ; anIt != aLast; anIt++ ) {
539 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
540 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
542 aPointsMap[aPoint] = 0;
544 thePoints.push_back( *anIt );
549 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
550 const CurveCreator_ICurve* theCurve,
551 const CurveCreator_ICurve::SectionToPointList& thePoints )
556 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
557 if ( anAIS.IsNull() )
559 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
560 if ( anAISShape.IsNull() )
563 //ASL: we convert list of point indices to list of points coordinates
564 int aSize = thePoints.size();
565 std::vector<gp_Pnt> aPntsToSelect( aSize );
567 CurveCreator_ICurve::SectionToPointList::const_iterator
568 aPIt = thePoints.begin(), aPLast = thePoints.end();
569 CurveCreator_ICurve::SectionToPoint aSToPoint;
570 for( int i=0; aPIt != aPLast; aPIt++, i++ )
573 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
574 aPntsToSelect[i] = aPntToSelect;
577 theContext->ClearSelected( Standard_False );
578 //ASL: we switch off automatic highlight to improve performance of selection
579 theContext->SetAutomaticHilight( Standard_False );
581 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
582 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
584 #if OCC_VERSION_LARGE > 0x06080100
585 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
586 if( aHSenEntity.IsNull() )
588 Handle_SelectBasics_SensitiveEntity aSenEntity = aHSenEntity->BaseSensitive();
590 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
593 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
595 gp_Pnt anOwnerPnt = aSenPnt->Point();
596 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
599 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
600 aLast = thePoints.end();
601 bool isFound = false;
602 for( int i=0; i<aSize; i++ )
604 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
605 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
608 theContext->AddOrRemoveSelected( anOwner, Standard_False );
614 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
615 // and call HilightPicked to draw selected owners
616 theContext->SetAutomaticHilight( Standard_True );
617 theContext->LocalContext()->HilightPicked( Standard_True );
620 //=======================================================================
621 // function : setLocalPointContext
622 // purpose : Open/close the viewer local context
623 //=======================================================================
624 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
625 Handle(AIS_InteractiveContext) theContext,
632 // Open local context if there is no one
633 if ( !theContext->HasOpenedContext() ) {
634 theContext->ClearCurrents( false );
635 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
637 // load the curve AIS object to the local context with the point selection
638 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
639 if ( !anAIS.IsNull() )
641 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
643 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
644 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
649 if ( theContext->HasOpenedContext() )
650 theContext->CloseAllContexts();
654 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
655 Handle(AIS_InteractiveObject) theObject,
656 const int theX, const int theY,
658 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
660 bool isFullFound = false;
662 if ( theObject.IsNull() || theView.IsNull() )
664 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
665 if ( aShape.IsNull() )
667 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
668 if ( aCompound.IsNull() )
671 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
672 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
673 Standard_Real aParameter;
674 bool isFound = false;
675 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
676 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
677 for ( ; anExp.More(); anExp.Next())
679 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
680 if ( anEdge.IsNull() )
682 Standard_Real aFirst, aLast;
683 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
684 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
685 Handle(Geom_BSplineCurve) aBSplineCurve =
686 Handle(Geom_BSplineCurve)::DownCast( aCurve );
687 if ( !aBSplineCurve.IsNull() ) {
688 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
689 aParameter, aDelta );
691 aCurPoint = aBSplineCurve->Value( aParameter );
692 Standard_Integer anI1, anI2;
693 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
694 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
695 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
699 else { // a curve built on a polyline edge
700 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
701 if ( aGLine.IsNull() )
703 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
706 aCurPoint = aGLine->Value( aParameter );
707 TopoDS_Vertex V1, V2;
708 TopExp::Vertices( anEdge, V1, V2, Standard_True );
709 if ( V1.IsNull() || V2.IsNull() )
711 aCurPoint1 = BRep_Tool::Pnt(V1);
712 aCurPoint2 = BRep_Tool::Pnt(V2);
714 // check that the projected point is on the bounded curve
715 gp_Vec aVec1( aCurPoint1, aCurPoint );
716 gp_Vec aVec2( aCurPoint2, aCurPoint );
717 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
720 if ( isFound && aMinDelta >= aDelta ) {
724 aFoundPnt1 = aCurPoint1;
725 aFoundPnt2 = aCurPoint2;
726 aFoundPoint = aCurPoint;
730 int aX, anY, aX1, anY1, aX2, anY2;
732 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
733 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
734 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
736 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
737 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
739 thePoint = aFoundPoint;
740 thePoint1 = aFoundPnt1;
741 thePoint2 = aFoundPnt2;
747 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
748 const int theX, const int theY,
749 const Handle(Geom_Curve)& theCurve,
750 Standard_Real& theParameter,
753 bool isFound = false;
754 if ( theView.IsNull() )
757 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
759 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
760 Standard_Integer aNbPoint = aProj.NbPoints();
762 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
763 gp_Pnt aNewPoint = aProj.Point( j );
764 theParameter = aProj.Parameter( j );
767 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
769 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
775 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
776 const int theOtherY, const double theTolerance, int& theDelta )
778 int aXDelta = abs( theX - theOtherX );
779 int anYDelta = abs( theY - theOtherY );
781 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
783 return aXDelta < theTolerance && anYDelta < theTolerance;
786 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
788 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
791 //=======================================================================
792 // function : getPoints
794 //=======================================================================
795 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
796 (const TopoDS_Shape &theShape,
800 Handle(TColgp_HArray1OfPnt) aResult;
805 if (theShape.IsNull()) {
809 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
811 if (aShType == TopAbs_VERTEX) {
812 // There is a single point.
813 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
815 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
818 } else if (aShType != TopAbs_WIRE) {
819 // The shape is neither a vertex nor a wire.
824 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
827 // Empty wires are not allowed.
831 // Treat the first edge.
832 TopoDS_Edge anEdge = anExp.Current();
833 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
835 if (aCurve.IsNull()) {
839 // Check the curve type.
840 Handle(Standard_Type) aType = aCurve->DynamicType();
842 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
844 } else if (aType != STANDARD_TYPE(Geom_Line)) {
845 // The curve is neither a line or a BSpline. It is not valid.
849 // Go to the next edge.
850 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
856 // There should be a single BSpline curve in the wire.
861 // Construct a section from poles of BSpline.
862 Handle(Geom_BSplineCurve) aBSplCurve =
863 Handle(Geom_BSplineCurve)::DownCast(aCurve);
865 // Check if the edge is valid. It should not be based on trimmed curve.
866 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
870 TopExp::Vertices(anEdge, aV[0], aV[1]);
872 for (i = 0; i < 2; i++) {
873 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
874 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
876 if (!aPnt.IsEqual(aCP[i], aTol)) {
881 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
883 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
884 TColStd_Array1OfReal aKnots(1, aNbPoints);
885 aBSplCurve->Knots(aKnots);
887 // Don't consider the last point as it coincides with the first
891 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
892 for (i = 1; i <= aNbPoints; ++i)
893 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
897 // This is a polyline.
898 TopTools_ListOfShape aVertices;
899 Standard_Integer aNbVtx = 1;
902 aVertices.Append(aFirstVtx);
904 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
905 anEdge = anExp.Current();
906 aCurve = GetCurve(anEdge);
908 if (aCurve.IsNull()) {
912 aType = aCurve->DynamicType();
914 if (aType != STANDARD_TYPE(Geom_Line)) {
915 // The curve is not a line. It is not valid.
919 // Add the current vertex to the list.
920 aVertices.Append(anExp.CurrentVertex());
923 // Check if the section is closed.
924 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
926 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
928 // Store a last vertex
931 aVertices.Append(aLastVtx);
935 // Fill the array of points.
936 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
939 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
941 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
942 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
944 aResult->SetValue(i, aPnt);
950 //=======================================================================
951 // function : FindPlane
953 //=======================================================================
954 void CurveCreator_Utils::FindPlane
955 (const Handle_TColgp_HArray1OfPnt &thePoints,
957 Standard_Integer &thePlnStatus)
959 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
960 // The plane can't be defined or is fixed. Nothing to change.
965 const Standard_Real aTolConf = Precision::Confusion();
967 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
968 const gp_Pnt &aPnt = thePoints->Value(i);
970 switch (thePlnStatus) {
973 thePlane.SetLocation(aPnt);
974 thePlnStatus = PLN_ORIGIN;
978 // Fix origin + OX axis
979 const gp_Pnt &aPlnLoc = thePlane.Location();
981 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
983 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
984 gp_Ax3 aXNorm(aPlnLoc, aXDir);
985 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
987 thePlane.SetPosition(aNewPlnPos);
988 thePlnStatus = PLN_OX;
995 gp_Lin aXLin(thePlane.XAxis());
996 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
998 if (aSqrDist > aTolConf*aTolConf) {
999 // Compute main axis.
1000 const gp_Pnt &aPlnLoc = thePlane.Location();
1001 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1002 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1003 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1004 aXNorm.Direction());
1006 thePlane.SetPosition(aNewPlnPos);
1007 thePlnStatus = PLN_FIXED;