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 //=======================================================================
187 // function : constructBSpline
189 // The algorithm builds the cubic B-spline passing through the points that the
190 // tangent vector in each given point P is calculated by the following way:
191 // if point P is preceded by a point A and is followed by a point B then
192 // the tangent vector is equal to (P - A) / |P - A| + (B - P) / |B - P|;
193 // if point P is preceded by a point A but is not followed by any point then
194 // the tangent vector is equal to P - A;
195 // if point P is followed by a point B but is not preceded by any point then
196 // the tangent vector is equal to B - P.
197 //=======================================================================
198 static bool constructBSpline(
199 const Handle(TColgp_HArray1OfPnt)& thePoints,
200 const Standard_Boolean theIsClosed,
201 Handle(Geom_BSplineCurve)& theBSpline)
203 const int aPointCount = thePoints->Length();
204 if (aPointCount <= 1)
209 // Calculate the tangents.
210 TColgp_Array1OfVec aTangents(1, aPointCount);
211 Handle(TColStd_HArray1OfBoolean) aTangentFlags =
212 new TColStd_HArray1OfBoolean(1, aPointCount);
213 GeomAPI_Interpolate aInterpolator(thePoints, theIsClosed, 0);
214 if (aPointCount == 2)
216 aTangentFlags->SetValue(1, Standard_False);
217 aTangentFlags->SetValue(2, Standard_False);
221 for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
224 if (aPN != 1 || theIsClosed)
226 const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
227 aTangent = gp_Vec(thePoints->Value(aPN1),
228 thePoints->Value(aPN)).Normalized();
230 if (aPN < aPointCount || theIsClosed)
232 const Standard_Integer aPN2 = (aPN != aPointCount) ? (aPN + 1) : 1;
233 const gp_Vec aTangent2 = aTangent +
234 gp_Vec(thePoints->Value(aPN), thePoints->Value(aPN2)).Normalized();
235 if (aTangent2.SquareMagnitude() >= Precision::SquareConfusion())
237 aTangent = aTangent2.Normalized();
241 aTangent = -aTangent;
244 aTangents.SetValue(aPN, aTangent);
245 aTangentFlags->SetValue(aPN, Standard_True);
250 aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
251 aInterpolator.Perform();
252 const bool aResult = (aInterpolator.IsDone() == Standard_True);
255 theBSpline = aInterpolator.Curve();
260 //=======================================================================
261 // function : constructShape
263 //=======================================================================
264 void CurveCreator_Utils::constructShape(
265 const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape)
267 BRep_Builder aBuilder;
268 TopoDS_Compound aShape;
269 aBuilder.MakeCompound(aShape);
270 const int aSectionCount = theCurve->getNbSections();
271 for (int aSectionI = 0; aSectionI < aSectionCount; ++aSectionI)
273 const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
274 if (aTmpPointCount == 0)
279 // Get the different points.
280 std::vector<gp_Pnt> aTmpPoints;
282 CurveCreator_UtilsICurve::getPoint(theCurve, aSectionI, 0, aFirstPoint);
283 gp_Pnt aPoint = aFirstPoint;
284 aTmpPoints.push_back(aPoint);
285 for (int aPI = 1; aPI < aTmpPointCount; ++aPI)
288 CurveCreator_UtilsICurve::getPoint(theCurve, aSectionI, aPI, aPoint2);
289 if (!isEqualPoints(aPoint, aPoint2))
292 aTmpPoints.push_back(aPoint);
295 const bool isClosed = theCurve->isClosed(aSectionI);
296 int aPointCount = aTmpPoints.size();
299 while (aPointCount > 1 &&
300 isEqualPoints(aFirstPoint, aTmpPoints[aPointCount - 1]))
306 // Add the vertices to the shape.
307 Handle(TColgp_HArray1OfPnt) aPoints =
308 new TColgp_HArray1OfPnt(1, aPointCount);
309 for (Standard_Integer aPI = 0; aPI < aPointCount; ++aPI)
311 aPoints->SetValue(aPI + 1, aTmpPoints[aPI]);
312 aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex(aTmpPoints[aPI]));
314 if (aPointCount == 1)
319 // Add the edges to the shape.
320 const bool isPolyline =
321 (theCurve->getSectionType(aSectionI) == CurveCreator::Polyline);
324 for (Standard_Integer aPN = 1; aPN < aPointCount; ++aPN)
326 aBuilder.Add(aShape, BRepBuilderAPI_MakeEdge(
327 BRepBuilderAPI_MakeVertex(aPoints->Value(aPN)),
328 BRepBuilderAPI_MakeVertex(aPoints->Value(aPN + 1))));
332 aBuilder.Add(aShape, BRepBuilderAPI_MakeEdge(
333 BRepBuilderAPI_MakeVertex(aPoints->Value(aPointCount)),
334 BRepBuilderAPI_MakeVertex(aPoints->Value(1))));
339 Handle(Geom_BSplineCurve) aBSpline;
340 if (constructBSpline(aPoints, isClosed, aBSpline))
343 BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aBSpline)));
351 * This is an intermediate structure for curve construction.
355 Section3D() : myIsClosed(false), myIsBSpline(false)
360 Handle(TColgp_HArray1OfPnt) myPoints;
363 //=======================================================================
364 // function : constructCurve
366 //=======================================================================
367 bool CurveCreator_Utils::constructCurve
368 (const TopoDS_Shape theShape,
369 CurveCreator_Curve *theCurve,
372 if (theShape.IsNull()) {
376 // Collect wires or vertices from shape.
377 TopTools_ListOfShape aWOrV;
378 TopAbs_ShapeEnum aType = theShape.ShapeType();
380 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
381 aWOrV.Append(theShape);
382 } else if (aType == TopAbs_COMPOUND) {
383 TopoDS_Iterator aShIter(theShape);
385 for (; aShIter.More(); aShIter.Next()) {
386 const TopoDS_Shape &aSubShape = aShIter.Value();
388 aType = aSubShape.ShapeType();
390 if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
391 aWOrV.Append(aSubShape);
393 // Only subshapes of types wire or vertex are supported.
398 // Only wire (vertex) or compound of wires (vertices) are supported.
402 // Treat each wire or vertex. Get points, compute the working plane.
404 Standard_Integer aPlaneStatus = PLN_FREE;
405 TopTools_ListIteratorOfListOfShape anIter(aWOrV);
406 std::list<Section3D> aListSec;
408 for (; anIter.More(); anIter.Next()) {
411 aSec3D.myPoints = CurveCreator_Utils::getPoints
412 (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
414 if (aSec3D.myPoints.IsNull()) {
418 aListSec.push_back(aSec3D);
420 if (aPlaneStatus != PLN_FIXED) {
422 CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
426 // Check if it is possible to change a computed coordinate system by
427 // XOY, XOZ or YOZ or parallel to them.
428 gp_Pnt aO(0., 0., 0.);
429 gp_Dir aNDir(0., 0., 1.);
430 gp_Dir aXDir(1., 0., 0.);
432 Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
434 switch (aPlaneStatus) {
437 // Change the location.
438 aO.SetZ(aPlane.Location().Z());
439 anAxis.SetLocation(aO);
440 aPlane.SetPosition(anAxis);
445 // Fixed origin + OX axis
446 const gp_Dir &aPlnX = aPlane.Position().XDirection();
448 if (Abs(aPlnX.Z()) <= aTolAng) {
449 // Make a coordinate system parallel to XOY.
450 aO.SetZ(aPlane.Location().Z());
451 anAxis.SetLocation(aO);
452 aPlane.SetPosition(anAxis);
453 } else if (Abs(aPlnX.Y()) <= aTolAng) {
454 // Make a coordinate system parallel to XOZ.
455 aO.SetY(aPlane.Location().Y());
456 aNDir.SetCoord(0., 1., 0.);
457 aXDir.SetCoord(0., 0., 1.);
458 anAxis = gp_Ax3(aO, aNDir, aXDir);
459 aPlane.SetPosition(anAxis);
460 } else if (Abs(aPlnX.X()) <= aTolAng) {
461 // Make a coordinate system parallel to YOZ.
462 aO.SetX(aPlane.Location().X());
463 aNDir.SetCoord(1., 0., 0.);
464 aXDir.SetCoord(0., 1., 0.);
465 anAxis = gp_Ax3(aO, aNDir, aXDir);
466 aPlane.SetPosition(anAxis);
472 const gp_Dir &aPlnN = aPlane.Position().Direction();
473 gp_Dir aYDir(0., 1., 0.);
475 if (aPlnN.IsParallel(aNDir, 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 (aPlnN.IsParallel(aYDir, 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 (aPlnN.IsParallel(aXDir, 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);
500 aPlane.SetPosition(anAxis);
504 // Compute 2d points.
505 std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
506 Standard_Real aTolConf2 =
507 Precision::Confusion()*Precision::Confusion();
511 for (; aSecIt != aListSec.end(); ++aSecIt) {
513 CurveCreator::Coordinates aCoords;
515 for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
516 const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
518 if (aPlane.SquareDistance(aPnt) > aTolConf2) {
519 // The point doesn't lie on the plane.
523 ElSLib::Parameters(aPlane, aPnt, aX, aY);
524 aCoords.push_back(aX);
525 aCoords.push_back(aY);
528 // Add a new section to the curve.
529 const std::string aSecName =
530 CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
531 const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
532 CurveCreator::Spline : CurveCreator::Polyline;
534 theCurve->addSectionInternal(aSecName, aSecType,
535 aSecIt->myIsClosed, aCoords);
538 // Set the local coordinate system.
539 theLocalCS = aPlane.Position();
544 class CompareSectionToPoint
547 CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
548 : mySectionId( theISection ), myPointId( theIPoint ) {};
549 ~CompareSectionToPoint() {}
551 bool operator < ( const CompareSectionToPoint& theOther ) const
553 bool isLess = mySectionId < theOther.mySectionId;
554 if ( !isLess && mySectionId == theOther.mySectionId )
555 isLess = myPointId < theOther.myPointId;
565 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
566 const CurveCreator_ICurve* theCurve,
567 CurveCreator_ICurve::SectionToPointList& thePoints )
571 std::list<float> aSelectedPoints;
573 std::map<CompareSectionToPoint, int> aPointsMap;
575 CurveCreator_ICurve::SectionToPointList aPoints;
576 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
577 TopoDS_Vertex aVertex;
578 TopoDS_Shape aShape = theContext->SelectedShape();
579 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
580 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
582 if ( aVertex.IsNull() )
584 aPnt = BRep_Tool::Pnt( aVertex );
586 CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
587 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
588 aLast = aPoints.end();
589 CompareSectionToPoint aPoint;
590 for ( ; anIt != aLast; anIt++ ) {
591 aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
592 if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
594 aPointsMap[aPoint] = 0;
596 thePoints.push_back( *anIt );
601 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
602 const CurveCreator_ICurve* theCurve,
603 const CurveCreator_ICurve::SectionToPointList& thePoints )
608 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
609 if ( anAIS.IsNull() )
611 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
612 if ( anAISShape.IsNull() )
615 //ASL: we convert list of point indices to list of points coordinates
616 int aSize = thePoints.size();
617 std::vector<gp_Pnt> aPntsToSelect( aSize );
619 CurveCreator_ICurve::SectionToPointList::const_iterator
620 aPIt = thePoints.begin(), aPLast = thePoints.end();
621 CurveCreator_ICurve::SectionToPoint aSToPoint;
622 for( int i=0; aPIt != aPLast; aPIt++, i++ )
625 CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
626 aPntsToSelect[i] = aPntToSelect;
629 theContext->ClearSelected( Standard_False );
630 //ASL: we switch off automatic highlight to improve performance of selection
631 theContext->SetAutomaticHilight( Standard_False );
633 Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
634 for( aSelection->Init(); aSelection->More(); aSelection->Next() )
636 #if OCC_VERSION_LARGE > 0x06080100
637 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
638 if( aHSenEntity.IsNull() )
640 Handle_SelectBasics_SensitiveEntity aSenEntity = aHSenEntity->BaseSensitive();
642 Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
645 Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
647 gp_Pnt anOwnerPnt = aSenPnt->Point();
648 Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
651 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
652 aLast = thePoints.end();
653 bool isFound = false;
654 for( int i=0; i<aSize; i++ )
656 bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
657 fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
660 theContext->AddOrRemoveSelected( anOwner, Standard_False );
666 //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
667 // and call HilightPicked to draw selected owners
668 theContext->SetAutomaticHilight( Standard_True );
669 theContext->LocalContext()->HilightPicked( Standard_True );
672 //=======================================================================
673 // function : setLocalPointContext
674 // purpose : Open/close the viewer local context
675 //=======================================================================
676 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
677 Handle(AIS_InteractiveContext) theContext,
684 // Open local context if there is no one
685 if ( !theContext->HasOpenedContext() ) {
686 theContext->ClearCurrents( false );
687 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
689 // load the curve AIS object to the local context with the point selection
690 Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
691 if ( !anAIS.IsNull() )
693 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
695 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
696 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
701 if ( theContext->HasOpenedContext() )
702 theContext->CloseAllContexts();
706 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
707 Handle(AIS_InteractiveObject) theObject,
708 const int theX, const int theY,
710 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
712 bool isFullFound = false;
714 if ( theObject.IsNull() || theView.IsNull() )
716 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
717 if ( aShape.IsNull() )
719 const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
720 if ( aCompound.IsNull() )
723 gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
724 gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
725 Standard_Real aParameter;
726 bool isFound = false;
727 int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
728 TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
729 for ( ; anExp.More(); anExp.Next())
731 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
732 if ( anEdge.IsNull() )
734 Standard_Real aFirst, aLast;
735 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
736 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
737 Handle(Geom_BSplineCurve) aBSplineCurve =
738 Handle(Geom_BSplineCurve)::DownCast( aCurve );
739 if ( !aBSplineCurve.IsNull() ) {
740 isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
741 aParameter, aDelta );
743 aCurPoint = aBSplineCurve->Value( aParameter );
744 Standard_Integer anI1, anI2;
745 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
746 aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
747 aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
751 else { // a curve built on a polyline edge
752 Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
753 if ( aGLine.IsNull() )
755 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
758 aCurPoint = aGLine->Value( aParameter );
759 TopoDS_Vertex V1, V2;
760 TopExp::Vertices( anEdge, V1, V2, Standard_True );
761 if ( V1.IsNull() || V2.IsNull() )
763 aCurPoint1 = BRep_Tool::Pnt(V1);
764 aCurPoint2 = BRep_Tool::Pnt(V2);
766 // check that the projected point is on the bounded curve
767 gp_Vec aVec1( aCurPoint1, aCurPoint );
768 gp_Vec aVec2( aCurPoint2, aCurPoint );
769 isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
772 if ( isFound && aMinDelta >= aDelta ) {
776 aFoundPnt1 = aCurPoint1;
777 aFoundPnt2 = aCurPoint2;
778 aFoundPoint = aCurPoint;
782 int aX, anY, aX1, anY1, aX2, anY2;
784 CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
785 CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
786 CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
788 isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
789 !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
791 thePoint = aFoundPoint;
792 thePoint1 = aFoundPnt1;
793 thePoint2 = aFoundPnt2;
799 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
800 const int theX, const int theY,
801 const Handle(Geom_Curve)& theCurve,
802 Standard_Real& theParameter,
805 bool isFound = false;
806 if ( theView.IsNull() )
809 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
811 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
812 Standard_Integer aNbPoint = aProj.NbPoints();
814 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
815 gp_Pnt aNewPoint = aProj.Point( j );
816 theParameter = aProj.Parameter( j );
819 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
821 isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
827 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
828 const int theOtherY, const double theTolerance, int& theDelta )
830 int aXDelta = abs( theX - theOtherX );
831 int anYDelta = abs( theY - theOtherY );
833 theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
835 return aXDelta < theTolerance && anYDelta < theTolerance;
838 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
840 return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
843 //=======================================================================
844 // function : getPoints
846 //=======================================================================
847 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
848 (const TopoDS_Shape &theShape,
852 Handle(TColgp_HArray1OfPnt) aResult;
857 if (theShape.IsNull()) {
861 const TopAbs_ShapeEnum aShType = theShape.ShapeType();
863 if (aShType == TopAbs_VERTEX) {
864 // There is a single point.
865 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
867 aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
870 } else if (aShType != TopAbs_WIRE) {
871 // The shape is neither a vertex nor a wire.
876 BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
879 // Empty wires are not allowed.
883 // Treat the first edge.
884 TopoDS_Edge anEdge = anExp.Current();
885 Handle(Geom_Curve) aCurve = GetCurve(anEdge);
887 if (aCurve.IsNull()) {
891 // Check the curve type.
892 Handle(Standard_Type) aType = aCurve->DynamicType();
894 if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
896 } else if (aType != STANDARD_TYPE(Geom_Line)) {
897 // The curve is neither a line or a BSpline. It is not valid.
901 // Go to the next edge.
902 TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
908 // There should be a single BSpline curve in the wire.
913 // Construct a section from poles of BSpline.
914 Handle(Geom_BSplineCurve) aBSplCurve =
915 Handle(Geom_BSplineCurve)::DownCast(aCurve);
917 // Check if the edge is valid. It should not be based on trimmed curve.
918 gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
922 TopExp::Vertices(anEdge, aV[0], aV[1]);
924 for (i = 0; i < 2; i++) {
925 gp_Pnt aPnt = BRep_Tool::Pnt(aV[i]);
926 Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
928 if (!aPnt.IsEqual(aCP[i], aTol)) {
933 IsClosed = aV[0].IsSame(aV[1]) ? true : false;
935 Standard_Integer aNbPoints = aBSplCurve->NbKnots();
936 TColStd_Array1OfReal aKnots(1, aNbPoints);
937 aBSplCurve->Knots(aKnots);
939 // Don't consider the last point as it coincides with the first
943 aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
944 for (i = 1; i <= aNbPoints; ++i)
945 aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
949 // This is a polyline.
950 TopTools_ListOfShape aVertices;
951 Standard_Integer aNbVtx = 1;
954 aVertices.Append(aFirstVtx);
956 for (; anExp.More(); anExp.Next(), ++aNbVtx) {
957 anEdge = anExp.Current();
958 aCurve = GetCurve(anEdge);
960 if (aCurve.IsNull()) {
964 aType = aCurve->DynamicType();
966 if (aType != STANDARD_TYPE(Geom_Line)) {
967 // The curve is not a line. It is not valid.
971 // Add the current vertex to the list.
972 aVertices.Append(anExp.CurrentVertex());
975 // Check if the section is closed.
976 TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
978 IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
980 // Store a last vertex
983 aVertices.Append(aLastVtx);
987 // Fill the array of points.
988 aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
991 TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
993 for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
994 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
996 aResult->SetValue(i, aPnt);
1002 //=======================================================================
1003 // function : FindPlane
1005 //=======================================================================
1006 void CurveCreator_Utils::FindPlane
1007 (const Handle_TColgp_HArray1OfPnt &thePoints,
1009 Standard_Integer &thePlnStatus)
1011 if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1012 // The plane can't be defined or is fixed. Nothing to change.
1017 const Standard_Real aTolConf = Precision::Confusion();
1019 for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1020 const gp_Pnt &aPnt = thePoints->Value(i);
1022 switch (thePlnStatus) {
1025 thePlane.SetLocation(aPnt);
1026 thePlnStatus = PLN_ORIGIN;
1030 // Fix origin + OX axis
1031 const gp_Pnt &aPlnLoc = thePlane.Location();
1033 if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1035 gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1036 gp_Ax3 aXNorm(aPlnLoc, aXDir);
1037 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1039 thePlane.SetPosition(aNewPlnPos);
1040 thePlnStatus = PLN_OX;
1047 gp_Lin aXLin(thePlane.XAxis());
1048 Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1050 if (aSqrDist > aTolConf*aTolConf) {
1051 // Compute main axis.
1052 const gp_Pnt &aPlnLoc = thePlane.Location();
1053 gp_Dir aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1054 gp_Ax3 aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1055 gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1056 aXNorm.Direction());
1058 thePlane.SetPosition(aNewPlnPos);
1059 thePlnStatus = PLN_FIXED;