]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Utils.cxx
Salome HOME
433f02ee54fa2566678bc7f749f7b6c2cb40e24f
[modules/geom.git] / src / CurveCreator / CurveCreator_Utils.cxx
1 // Copyright (C) 2013-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Basics_OCCTVersion.hxx>
21
22 #include "CurveCreator_Utils.hxx"
23 #include "CurveCreator.hxx"
24 #include "CurveCreator_Curve.hxx"
25 #include "CurveCreator_Section.hxx"
26 #include "CurveCreator_UtilsICurve.hxx"
27
28 #include <GEOMUtils.hxx>
29
30 #include <gp_Pln.hxx>
31
32 #include <TopoDS.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Compound.hxx>
37
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>
43
44 #if OCC_VERSION_LARGE <= 0x07030000
45 #include <AIS_LocalContext.hxx>
46 #endif
47
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>
53
54 #include <TopExp.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>
61
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>
68
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>
74
75 #include <ProjLib.hxx>
76 #include <ElSLib.hxx>
77
78 #include <math.h>
79
80 #include "CurveCreator_ICurve.hxx"
81
82 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
83 const int    SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
84 const int    SCENE_PIXEL_POINT_TOLERANCE = 5;
85
86 #define PLN_FREE   0
87 #define PLN_ORIGIN 1
88 #define PLN_OX     2
89 #define PLN_FIXED  3
90
91 /**
92  * This static function returns the curve of original type from the edge.
93  *
94  * \param theEdge the edge
95  * \return the curve of original type. Can be null handle.
96  */
97 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
98 {
99   Handle(Geom_Curve) aResult;
100
101   if (theEdge.IsNull()) {
102     return aResult;
103   }
104
105   Standard_Real aF;
106   Standard_Real aL;
107
108   aResult = BRep_Tool::Curve(theEdge, aF, aL);
109
110   if (aResult.IsNull()) {
111     return aResult;
112   }
113
114   // Get the curve of original type
115   Handle(Standard_Type) aType = aResult->DynamicType();
116
117   while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
118     Handle(Geom_TrimmedCurve) aTrCurve =
119       Handle(Geom_TrimmedCurve)::DownCast(aResult);
120
121     aResult = aTrCurve->BasisCurve();
122     aType  = aResult->DynamicType();
123   }
124
125   return aResult;
126 }
127
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,
134                                               int& x, int& y )
135 {
136   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
137 }
138
139
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 )
145 {
146   // the 3D point, that is a projection of the pixels to the XYZ view plane
147   //return GEOMUtils::ConvertClickToPoint( x, y, aView );
148
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 );
154
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)
163   // p(z) = 0
164
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;
170     aXp = X + aT*Vx;
171     aYp = Y + aT*Vy;
172     aZp = Z + aT*Vz;
173   }
174   else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
175     aXp = aYp = aZp = 0;
176     if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
177       aYp = Y;
178     else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
179       aXp = X;
180   }
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;*/
185
186   gp_Pnt ResultPoint( aXp, aYp, aZp );
187   return ResultPoint;
188 }
189
190 //=======================================================================
191 // function : constructBSpline
192 // purpose  :
193 //=======================================================================
194 bool CurveCreator_Utils::constructBSpline(
195   const Handle(TColgp_HArray1OfPnt)& thePoints,
196   const Standard_Boolean theIsClosed,
197   Handle(Geom_BSplineCurve)& theBSpline)
198 {
199   const int aPointCount = thePoints->Length();
200   if (aPointCount <= 1)
201   {
202     return false;
203   }
204
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)
211   {
212     aTangentFlags->SetValue(1, Standard_False);
213     aTangentFlags->SetValue(2, Standard_False);
214   }
215   else
216   {
217     for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
218     {
219       gp_Vec aTangent;
220       if (aPN != 1 || theIsClosed)
221       {
222         const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
223         aTangent = gp_Vec(thePoints->Value(aPN1),
224           thePoints->Value(aPN)).Normalized();
225       }
226       if (aPN < aPointCount || theIsClosed)
227       {
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())
232         {
233           aTangent = aTangent2.Normalized();
234         }
235         else
236         {
237           aTangent = -aTangent;
238         }
239       }
240       aTangents.SetValue(aPN, aTangent);
241       aTangentFlags->SetValue(aPN, Standard_True);
242     }
243   }
244
245   // Interpolate.
246   aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
247   aInterpolator.Perform();
248   const bool aResult = (aInterpolator.IsDone() == Standard_True);
249   if (aResult)
250   {
251     theBSpline = aInterpolator.Curve();
252   }
253   return aResult;
254 }
255
256 //=======================================================================
257 // function : constructWire
258 // purpose  :
259 //=======================================================================
260 TopoDS_Wire CurveCreator_Utils::ConstructWire(
261   Handle(TColgp_HArray1OfPnt) thePoints,
262   const bool theIsPolyline,
263   const bool theIsClosed)
264 {
265   TopoDS_Wire aWire;
266   BRep_Builder aBuilder;
267   aBuilder.MakeWire(aWire);
268   const int aPointCount = thePoints->Length();
269   if (theIsPolyline)
270   {
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)
275     {
276       const TopoDS_Vertex aVertex2 =
277         BRepBuilderAPI_MakeVertex(thePoints->Value(aPN + 1));
278       aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aVertex2));
279       aVertex = aVertex2;
280     }
281     if (theIsClosed && aPointCount > 1)
282     {
283       aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aVertex, aFirstVertex));
284     }
285   }
286   else
287   {
288     Handle(Geom_BSplineCurve) aBSpline;
289     if (constructBSpline(thePoints, theIsClosed, aBSpline))
290     {
291       aBuilder.Add(aWire, BRepBuilderAPI_MakeEdge(aBSpline));
292     }
293   }
294   return aWire;
295 }
296
297 //=======================================================================
298 // function : constructShape
299 // purpose  :
300 //=======================================================================
301 void CurveCreator_Utils::constructShape(
302   const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape)
303 {
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)
309   {
310     const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
311     if (aTmpPointCount == 0)
312     {
313       continue;
314     }
315
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);
320
321     // Add the vertices to the shape.
322     for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
323     {
324       aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex(aPoints->Value(aPN)));
325     }
326
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);
331     if (!aWire.IsNull())
332     {
333       aBuilder.Add(aShape, aWire);
334     }
335   }
336   theShape = aShape;
337 }
338
339 /**
340  * This is an intermediate structure for curve construction.
341  */
342 struct Section3D
343 {
344   Section3D() : myIsClosed(false), myIsBSpline(false)
345   { }
346
347   bool                        myIsClosed;
348   bool                        myIsBSpline;
349   Handle(TColgp_HArray1OfPnt) myPoints;
350 };
351
352 //=======================================================================
353 // function : constructCurve
354 // purpose  : 
355 //=======================================================================
356 bool CurveCreator_Utils::constructCurve
357                       (const TopoDS_Shape        theShape,
358                              CurveCreator_Curve *theCurve,
359                              gp_Ax3             &theLocalCS)
360 {
361   if (theShape.IsNull()) {
362     return false;
363   }
364
365   // Collect wires or vertices from shape.
366   TopTools_ListOfShape aWOrV;
367   TopAbs_ShapeEnum     aType = theShape.ShapeType();
368
369   if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
370     aWOrV.Append(theShape);
371   } else if (aType == TopAbs_COMPOUND) {
372     TopoDS_Iterator aShIter(theShape);
373
374     for (; aShIter.More(); aShIter.Next()) {
375       const TopoDS_Shape &aSubShape = aShIter.Value();
376
377       aType = aSubShape.ShapeType();
378
379       if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
380         aWOrV.Append(aSubShape);
381       } else {
382         // Only subshapes of types wire or vertex are supported.
383         return false;
384       }
385     }
386   } else {
387     // Only wire (vertex) or compound of wires (vertices) are supported.
388     return false;
389   }
390
391   // Treat each wire or vertex. Get points, compute the working plane.
392   gp_Pln                             aPlane;
393   Standard_Integer                   aPlaneStatus = PLN_FREE;
394   TopTools_ListIteratorOfListOfShape anIter(aWOrV);
395   std::list<Section3D>               aListSec;
396
397   for (; anIter.More(); anIter.Next()) {
398     Section3D aSec3D;
399
400     aSec3D.myPoints = CurveCreator_Utils::getPoints
401       (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
402
403     if (aSec3D.myPoints.IsNull()) {
404       return false;
405     }
406
407     aListSec.push_back(aSec3D);
408
409     if (aPlaneStatus != PLN_FIXED) {
410       // Compute plane
411       CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
412     }
413   }
414
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.);
420   gp_Ax3        anAxis;
421   Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
422
423   switch (aPlaneStatus) {
424     case PLN_ORIGIN:
425       {
426         // Change the location.
427         aO.SetZ(aPlane.Location().Z());
428         anAxis.SetLocation(aO);
429         aPlane.SetPosition(anAxis);
430       }
431       break;
432     case PLN_OX:
433       {
434         // Fixed origin + OX axis
435         const gp_Dir &aPlnX = aPlane.Position().XDirection();
436
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);
456         }
457       }
458       break;
459     case PLN_FIXED:
460       {
461         const gp_Dir &aPlnN = aPlane.Position().Direction();
462         gp_Dir        aYDir(0., 1., 0.);
463
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);
483         }
484       }
485       break;
486     case PLN_FREE:
487     default:
488       // Use XOY plane.
489       aPlane.SetPosition(anAxis);
490       break;
491   }
492
493   // Compute 2d points.
494   std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
495   Standard_Real                        aTolConf2 =
496     Precision::Confusion()*Precision::Confusion();
497   Standard_Real                        aX;
498   Standard_Real                        aY;
499
500   for (; aSecIt != aListSec.end(); ++aSecIt) {
501     Standard_Integer          i;
502     CurveCreator::Coordinates aCoords;
503
504     for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
505       const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
506
507       if (aPlane.SquareDistance(aPnt) > aTolConf2) {
508         // The point doesn't lie on the plane.
509         return false;
510       }
511
512       ElSLib::Parameters(aPlane, aPnt, aX, aY);
513       aCoords.push_back(aX);
514       aCoords.push_back(aY);
515     }
516
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;
522
523     theCurve->addSectionInternal(aSecName, aSecType,
524                                  aSecIt->myIsClosed, aCoords);
525   }
526
527   // Set the local coordinate system.
528   theLocalCS = aPlane.Position();
529
530   return true;
531 }
532
533 class CompareSectionToPoint
534 {
535 public:
536   CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
537     : mySectionId( theISection ), myPointId( theIPoint ) {};
538   ~CompareSectionToPoint() {}
539
540   bool operator < ( const CompareSectionToPoint& theOther ) const
541   {
542     bool isLess = mySectionId < theOther.mySectionId;
543     if ( !isLess && mySectionId == theOther.mySectionId )
544       isLess = myPointId < theOther.myPointId;
545     return isLess;
546   }
547
548 private:
549   int mySectionId;
550   int myPointId;
551 };
552
553
554 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
555                                             const CurveCreator_ICurve* theCurve,
556                                             CurveCreator_ICurve::SectionToPointList& thePoints )
557 {
558   thePoints.clear();
559
560   std::list<double> aSelectedPoints;
561   gp_Pnt aPnt;
562   std::map<CompareSectionToPoint, int> aPointsMap;
563
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() );
570
571     if ( aVertex.IsNull() )
572       continue;
573     aPnt = BRep_Tool::Pnt( aVertex );
574
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() )
582         continue;
583       aPointsMap[aPoint] = 0;
584
585       thePoints.push_back( *anIt );
586     }
587   }
588 }
589
590 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
591                                             const CurveCreator_ICurve* theCurve,
592                                             const CurveCreator_ICurve::SectionToPointList& thePoints )
593 {
594   if ( !theCurve )
595     return;
596
597   Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
598   if ( anAIS.IsNull() )
599     return;
600   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
601   if ( anAISShape.IsNull() )
602     return;
603
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 );
607
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++ )
612   {
613     gp_Pnt aPntToSelect;
614     CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
615     aPntsToSelect[i] = aPntToSelect;
616   }
617
618   theContext->ClearSelected( Standard_False );
619   //ASL: we switch off automatic highlight to improve performance of selection
620   theContext->SetAutomaticHilight( Standard_False );
621
622   Handle(SelectMgr_Selection) aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
623   const NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>& selected = aSelection->Entities();
624
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++ )
629   {
630   for ( NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator selIter( selected );
631         selIter.More(); selIter.Next() )
632   {    
633     const Handle(SelectMgr_SensitiveEntity) aHSenEntity = selIter.Value();
634     if( aHSenEntity.IsNull() )
635       continue;
636     Handle(SelectBasics_SensitiveEntity) aSenEntity = aHSenEntity->BaseSensitive();
637
638     Handle(Select3D_SensitivePoint) aSenPnt = Handle(Select3D_SensitivePoint)::DownCast( aSenEntity );
639
640     gp_Pnt anOwnerPnt = aSenPnt->Point();
641     Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast( aSenPnt->OwnerId() );
642
643       bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
644                          fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
645       if( isIntersect )
646       {
647         theContext->AddOrRemoveSelected( anOwner, Standard_False );
648         break;
649       }
650     }
651   }
652
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 );
658 #endif
659 }
660
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,
667                                                const bool theOpen )
668 {
669   if ( !theContext )
670     return;
671
672   if ( theOpen ) {
673 #if OCC_VERSION_LARGE <= 0x07030000
674     // Open local context if there is no one
675     if ( !theContext->HasOpenedContext() ) {
676 #endif
677       theContext->ClearCurrents( false );
678 #if OCC_VERSION_LARGE <= 0x07030000
679       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
680     }
681 #endif
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() )
685     {
686       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
687       {
688         theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
689         theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
690       }
691     }
692   }
693   else {
694 #if OCC_VERSION_LARGE <= 0x07030000
695     if ( theContext->HasOpenedContext() )
696       theContext->CloseAllContexts( Standard_True );
697 #else
698     theContext->Deactivate();
699     theContext->Activate(0);
700 #endif
701   }
702 }
703
704 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
705                                         Handle(AIS_InteractiveObject) theObject,
706                                         const int theX, const int theY,
707                                         gp_Pnt& thePoint,
708                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
709 {
710   bool isFullFound = false;
711
712   if ( theObject.IsNull() || theView.IsNull() )
713     return isFullFound;
714   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
715   if ( aShape.IsNull() )
716     return isFullFound;
717   const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
718   if ( aCompound.IsNull() )
719     return isFullFound;
720
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())
728   {
729     const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
730     if ( anEdge.IsNull() )
731       continue;
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 );
740         if ( isFound ) {
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 ) );
746         }
747       }
748     }
749     else { // a curve built on a polyline edge
750       Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
751       if ( aGLine.IsNull() )
752         continue;
753       isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
754                                         aDelta );
755       if ( isFound ) {
756         aCurPoint = aGLine->Value( aParameter );
757         TopoDS_Vertex V1, V2;
758         TopExp::Vertices( anEdge, V1, V2, Standard_True );
759         if ( V1.IsNull() || V2.IsNull() )
760           continue;
761         aCurPoint1 = BRep_Tool::Pnt(V1);
762         aCurPoint2 = BRep_Tool::Pnt(V2);
763
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;
768       }
769     }
770     if ( isFound && aMinDelta >= aDelta ) {
771       aMinDelta = aDelta;
772
773       isFullFound = true;
774       aFoundPnt1 = aCurPoint1;
775       aFoundPnt2 = aCurPoint2;
776       aFoundPoint = aCurPoint;
777     }
778   }
779   if ( isFullFound ) {
780     int aX, anY, aX1, anY1, aX2, anY2;
781     int aDelta;
782     CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
783     CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
784     CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
785
786     isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
787                   !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
788     if ( isFullFound ) {
789       thePoint = aFoundPoint;
790       thePoint1 = aFoundPnt1;
791       thePoint2 = aFoundPnt2;
792     }
793   }
794   return isFullFound;
795 }
796
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,
801                                                  int& theDelta )
802 {
803   bool isFound = false;
804   if ( theView.IsNull() )
805     return isFound;
806
807   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
808
809   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
810   Standard_Integer aNbPoint = aProj.NbPoints();
811   if (aNbPoint > 0) {
812     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
813       gp_Pnt aNewPoint = aProj.Point( j );
814       theParameter = aProj.Parameter( j );
815
816       int aX, anY;
817       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
818
819       isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
820     }
821   }
822   return isFound;
823 }
824
825 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
826                                         const int theOtherY, const double theTolerance, int& theDelta )
827 {
828   int aXDelta = abs( theX - theOtherX );
829   int anYDelta = abs( theY - theOtherY );
830
831   theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
832
833   return aXDelta < theTolerance && anYDelta < theTolerance;
834 }
835
836 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
837 {
838   return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
839 }
840
841 //=======================================================================
842 // function : getPoints
843 // purpose  : 
844 //=======================================================================
845 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
846                   (const TopoDS_Shape &theShape,
847                          bool         &IsClosed,
848                          bool         &IsBSpline)
849 {
850   Handle(TColgp_HArray1OfPnt) aResult;
851
852   IsClosed  = false;
853   IsBSpline = false;
854
855   if (theShape.IsNull()) {
856     return aResult;
857   }
858
859   const TopAbs_ShapeEnum aShType = theShape.ShapeType();
860
861   if (aShType == TopAbs_VERTEX) {
862     // There is a single point.
863     gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
864
865     aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
866
867     return aResult;
868   } else if (aShType != TopAbs_WIRE) {
869     // The shape is neither a vertex nor a wire.
870     return aResult;
871   }
872
873   // Treat wire.
874   BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
875
876   if (!anExp.More()) {
877     // Empty wires are not allowed.
878     return aResult;
879   }
880
881   // Treat the first edge.
882   TopoDS_Edge        anEdge = anExp.Current();
883   Handle(Geom_Curve) aCurve = GetCurve(anEdge);
884
885   if (aCurve.IsNull()) {
886     return aResult;
887   }
888
889   // Check the curve type.
890   Handle(Standard_Type) aType     = aCurve->DynamicType();
891
892   if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
893     IsBSpline = true;
894   } else if (aType != STANDARD_TYPE(Geom_Line)) {
895     // The curve is neither a line or a BSpline. It is not valid.
896     return aResult;
897   }
898
899   // Go to the next edge.
900   TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
901
902   anExp.Next();
903
904   if (IsBSpline)
905   {
906     // There should be a single BSpline curve in the wire.
907     if (anExp.More()) {
908       return aResult;
909     }
910
911     // Construct a section from poles of BSpline.
912     Handle(Geom_BSplineCurve) aBSplCurve =
913       Handle(Geom_BSplineCurve)::DownCast(aCurve);
914
915     // Check if the edge is valid. It should not be based on trimmed curve.
916     gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
917     TopoDS_Vertex aV[2];
918     Standard_Integer i;
919
920     TopExp::Vertices(anEdge, aV[0], aV[1]);
921
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]);
925
926       if (!aPnt.IsEqual(aCP[i], aTol)) {
927         return aResult;
928       }
929     }
930
931     IsClosed = aV[0].IsSame(aV[1]) ? true : false;
932     
933     Standard_Integer aNbPoints = aBSplCurve->NbKnots();
934     TColStd_Array1OfReal aKnots(1, aNbPoints);
935     aBSplCurve->Knots(aKnots);
936
937     // Don't consider the last point as it coincides with the first
938     if (IsClosed)
939       --aNbPoints;
940
941     aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
942     for (i = 1; i <= aNbPoints; ++i)
943       aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
944   }
945   else
946   {
947     // This is a polyline.
948     TopTools_ListOfShape aVertices;
949     Standard_Integer     aNbVtx = 1;
950
951
952     aVertices.Append(aFirstVtx);
953
954     for (; anExp.More(); anExp.Next(), ++aNbVtx) {
955       anEdge = anExp.Current();
956       aCurve = GetCurve(anEdge);
957
958       if (aCurve.IsNull()) {
959         return aResult;
960       }
961
962       aType = aCurve->DynamicType();
963
964       if (aType != STANDARD_TYPE(Geom_Line)) {
965         // The curve is not a line. It is not valid.
966         return aResult;
967       }
968
969       // Add the current vertex to the list.
970       aVertices.Append(anExp.CurrentVertex());
971     }
972
973     // Check if the section is closed.
974     TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
975
976     IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
977
978     // Store a last vertex
979     if (!IsClosed)
980     {
981       aVertices.Append(aLastVtx);
982       aNbVtx++;
983     }
984
985     // Fill the array of points.
986     aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
987
988     Standard_Integer i;
989     TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
990
991     for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
992       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
993
994       aResult->SetValue(i, aPnt);
995     }
996   }
997
998   return aResult;
999 }
1000 //=======================================================================
1001 // function : FindPlane
1002 // purpose  : 
1003 //=======================================================================
1004 void CurveCreator_Utils::FindPlane
1005                        (const Handle(TColgp_HArray1OfPnt) &thePoints,
1006                               gp_Pln                     &thePlane,
1007                               Standard_Integer           &thePlnStatus)
1008 {
1009   if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1010     // The plane can't be defined or is fixed. Nothing to change.
1011     return;
1012   }
1013
1014   Standard_Integer    i;
1015   const Standard_Real aTolConf = Precision::Confusion();
1016
1017   for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1018     const gp_Pnt &aPnt = thePoints->Value(i);
1019
1020     switch (thePlnStatus) {
1021       case PLN_FREE:
1022         // Fix the origin.
1023         thePlane.SetLocation(aPnt);
1024         thePlnStatus = PLN_ORIGIN;
1025         break;
1026       case PLN_ORIGIN:
1027         {
1028           // Fix origin + OX axis
1029           const gp_Pnt &aPlnLoc = thePlane.Location();
1030
1031           if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1032             // Set the X axis.
1033             gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1034             gp_Ax3 aXNorm(aPlnLoc, aXDir);
1035             gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1036
1037             thePlane.SetPosition(aNewPlnPos);
1038             thePlnStatus = PLN_OX;
1039           }
1040         }
1041         break;
1042       case PLN_OX:
1043         {
1044           // Fix OY axis
1045           gp_Lin aXLin(thePlane.XAxis());
1046           Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1047
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());
1055
1056             thePlane.SetPosition(aNewPlnPos);
1057             thePlnStatus = PLN_FIXED;
1058             return;
1059           }
1060         }
1061         break;
1062       default:
1063         return;
1064     }
1065   }
1066 }