]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Utils.cxx
Salome HOME
52fab0c1c4fbf4dbf2b6b47ad861d4bc7d35d322
[modules/geom.git] / src / CurveCreator / CurveCreator_Utils.cxx
1 // Copyright (C) 2013-2015  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 "CurveCreator_Utils.hxx"
21 #include "CurveCreator.hxx"
22 #include "CurveCreator_Curve.hxx"
23 #include "CurveCreator_UtilsICurve.hxx"
24
25 #include <Basics_OCCTVersion.hxx>
26
27 #include <GEOMUtils.hxx>
28
29 #include <gp_Pln.hxx>
30
31 #include <TopoDS.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS_Wire.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Compound.hxx>
36
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>
43
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>
49
50 #include <TopExp.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>
57
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>
64
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>
70
71 #include <ProjLib.hxx>
72 #include <ElSLib.hxx>
73
74 #include <math.h>
75
76 #include "CurveCreator_ICurve.hxx"
77
78 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
79 const int    SCENE_PIXEL_PROJECTION_TOLERANCE = 10;
80 const int    SCENE_PIXEL_POINT_TOLERANCE = 5;
81
82 #define PLN_FREE   0
83 #define PLN_ORIGIN 1
84 #define PLN_OX     2
85 #define PLN_FIXED  3
86
87 /**
88  * This static function returns the curve of original type from the edge.
89  *
90  * \param theEdge the edge
91  * \return the curve of original type. Can be null handle.
92  */
93 static Handle(Geom_Curve) GetCurve(const TopoDS_Edge &theEdge)
94 {
95   Handle(Geom_Curve) aResult;
96
97   if (theEdge.IsNull()) {
98     return aResult;
99   }
100
101   Standard_Real aF;
102   Standard_Real aL;
103
104   aResult = BRep_Tool::Curve(theEdge, aF, aL);
105
106   if (aResult.IsNull()) {
107     return aResult;
108   }
109
110   // Get the curve of original type
111   Handle(Standard_Type) aType = aResult->DynamicType();
112
113   while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
114     Handle(Geom_TrimmedCurve) aTrCurve =
115       Handle(Geom_TrimmedCurve)::DownCast(aResult);
116
117     aResult = aTrCurve->BasisCurve();
118     aType  = aResult->DynamicType();
119   }
120
121   return aResult;
122 }
123
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,
130                                               int& x, int& y )
131 {
132   theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
133 }
134
135
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 )
141 {
142   // the 3D point, that is a projection of the pixels to the XYZ view plane
143   //return GEOMUtils::ConvertClickToPoint( x, y, aView );
144
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 );
150
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)
159   // p(z) = 0
160
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;
166     aXp = X + aT*Vx;
167     aYp = Y + aT*Vy;
168     aZp = Z + aT*Vz;
169   }
170   else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
171     aXp = aYp = aZp = 0;
172     if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
173       aYp = Y;
174     else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
175       aXp = X;
176   }
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;*/
181
182   gp_Pnt ResultPoint( aXp, aYp, aZp );
183   return ResultPoint;
184 }
185
186 //=======================================================================
187 // function : constructBSpline
188 // purpose  :
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)
202 {
203   const int aPointCount = thePoints->Length();
204   if (aPointCount <= 1)
205   {
206     return false;
207   }
208
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)
215   {
216     aTangentFlags->SetValue(1, Standard_False);
217     aTangentFlags->SetValue(2, Standard_False);
218   }
219   else
220   {
221     for (Standard_Integer aPN = 1; aPN <= aPointCount; ++aPN)
222     {
223       gp_Vec aTangent;
224       if (aPN != 1 || theIsClosed)
225       {
226         const Standard_Integer aPN1 = (aPN != 1) ? (aPN - 1) : aPointCount;
227         aTangent = gp_Vec(thePoints->Value(aPN1),
228           thePoints->Value(aPN)).Normalized();
229       }
230       if (aPN < aPointCount || theIsClosed)
231       {
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())
236         {
237           aTangent = aTangent2.Normalized();
238         }
239         else
240         {
241           aTangent = -aTangent;
242         }
243       }
244       aTangents.SetValue(aPN, aTangent);
245       aTangentFlags->SetValue(aPN, Standard_True);
246     }
247   }
248
249   // Interpolate.
250   aInterpolator.Load(aTangents, aTangentFlags, Standard_False);
251   aInterpolator.Perform();
252   const bool aResult = (aInterpolator.IsDone() == Standard_True);
253   if (aResult)
254   {
255     theBSpline = aInterpolator.Curve();
256   }
257   return aResult;
258 }
259
260 //=======================================================================
261 // function : constructShape
262 // purpose  :
263 //=======================================================================
264 void CurveCreator_Utils::constructShape(
265   const CurveCreator_ICurve* theCurve, TopoDS_Shape& theShape)
266 {
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)
272   {
273     const int aTmpPointCount = theCurve->getNbPoints(aSectionI);
274     if (aTmpPointCount == 0)
275     {
276       continue;
277     }
278
279     // Get the different points.
280     std::vector<gp_Pnt> aTmpPoints;
281     gp_Pnt aFirstPoint;
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)
286     {
287       gp_Pnt aPoint2;
288       CurveCreator_UtilsICurve::getPoint(theCurve, aSectionI, aPI, aPoint2);
289       if (!isEqualPoints(aPoint, aPoint2))
290       {
291         aPoint = aPoint2;
292         aTmpPoints.push_back(aPoint);
293       }
294     }
295     const bool isClosed = theCurve->isClosed(aSectionI);
296     int aPointCount = aTmpPoints.size();
297     if (isClosed)
298     {
299       while (aPointCount > 1 &&
300         isEqualPoints(aFirstPoint, aTmpPoints[aPointCount - 1]))
301       {
302         --aPointCount;
303       }
304     }
305
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)
310     {
311       aPoints->SetValue(aPI + 1, aTmpPoints[aPI]);
312       aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex(aTmpPoints[aPI]));
313     }
314     if (aPointCount == 1)
315     {
316       continue;
317     }
318
319     // Add the edges to the shape.
320     const bool isPolyline =
321       (theCurve->getSectionType(aSectionI) == CurveCreator::Polyline);
322     if (isPolyline)
323     {
324       for (Standard_Integer aPN = 1; aPN < aPointCount; ++aPN)
325       {
326         aBuilder.Add(aShape, BRepBuilderAPI_MakeEdge(
327           BRepBuilderAPI_MakeVertex(aPoints->Value(aPN)),
328           BRepBuilderAPI_MakeVertex(aPoints->Value(aPN + 1))));
329       }
330       if (isClosed)
331       {
332         aBuilder.Add(aShape, BRepBuilderAPI_MakeEdge(
333           BRepBuilderAPI_MakeVertex(aPoints->Value(aPointCount)),
334           BRepBuilderAPI_MakeVertex(aPoints->Value(1))));
335       }
336     }
337     else
338     {
339       Handle(Geom_BSplineCurve) aBSpline;
340       if (constructBSpline(aPoints, isClosed, aBSpline))
341       {
342         aBuilder.Add(aShape,
343           BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aBSpline)));
344       }
345     }
346   }
347   theShape = aShape;
348 }
349
350 /**
351  * This is an intermediate structure for curve construction.
352  */
353 struct Section3D
354 {
355   Section3D() : myIsClosed(false), myIsBSpline(false)
356   { }
357
358   bool                        myIsClosed;
359   bool                        myIsBSpline;
360   Handle(TColgp_HArray1OfPnt) myPoints;
361 };
362
363 //=======================================================================
364 // function : constructCurve
365 // purpose  : 
366 //=======================================================================
367 bool CurveCreator_Utils::constructCurve
368                       (const TopoDS_Shape        theShape,
369                              CurveCreator_Curve *theCurve,
370                              gp_Ax3             &theLocalCS)
371 {
372   if (theShape.IsNull()) {
373     return false;
374   }
375
376   // Collect wires or vertices from shape.
377   TopTools_ListOfShape aWOrV;
378   TopAbs_ShapeEnum     aType = theShape.ShapeType();
379
380   if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
381     aWOrV.Append(theShape);
382   } else if (aType == TopAbs_COMPOUND) {
383     TopoDS_Iterator aShIter(theShape);
384
385     for (; aShIter.More(); aShIter.Next()) {
386       const TopoDS_Shape &aSubShape = aShIter.Value();
387
388       aType = aSubShape.ShapeType();
389
390       if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
391         aWOrV.Append(aSubShape);
392       } else {
393         // Only subshapes of types wire or vertex are supported.
394         return false;
395       }
396     }
397   } else {
398     // Only wire (vertex) or compound of wires (vertices) are supported.
399     return false;
400   }
401
402   // Treat each wire or vertex. Get points, compute the working plane.
403   gp_Pln                             aPlane;
404   Standard_Integer                   aPlaneStatus = PLN_FREE;
405   TopTools_ListIteratorOfListOfShape anIter(aWOrV);
406   std::list<Section3D>               aListSec;
407
408   for (; anIter.More(); anIter.Next()) {
409     Section3D aSec3D;
410
411     aSec3D.myPoints = CurveCreator_Utils::getPoints
412       (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
413
414     if (aSec3D.myPoints.IsNull()) {
415       return false;
416     }
417
418     aListSec.push_back(aSec3D);
419
420     if (aPlaneStatus != PLN_FIXED) {
421       // Compute plane
422       CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
423     }
424   }
425
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.);
431   gp_Ax3        anAxis;
432   Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
433
434   switch (aPlaneStatus) {
435     case PLN_ORIGIN:
436       {
437         // Change the location.
438         aO.SetZ(aPlane.Location().Z());
439         anAxis.SetLocation(aO);
440         aPlane.SetPosition(anAxis);
441       }
442       break;
443     case PLN_OX:
444       {
445         // Fixed origin + OX axis
446         const gp_Dir &aPlnX = aPlane.Position().XDirection();
447
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);
467         }
468       }
469       break;
470     case PLN_FIXED:
471       {
472         const gp_Dir &aPlnN = aPlane.Position().Direction();
473         gp_Dir        aYDir(0., 1., 0.);
474
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);
494         }
495       }
496       break;
497     case PLN_FREE:
498     default:
499       // Use XOY plane.
500       aPlane.SetPosition(anAxis);
501       break;
502   }
503
504   // Compute 2d points.
505   std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
506   Standard_Real                        aTolConf2 =
507     Precision::Confusion()*Precision::Confusion();
508   Standard_Real                        aX;
509   Standard_Real                        aY;
510
511   for (; aSecIt != aListSec.end(); ++aSecIt) {
512     Standard_Integer          i;
513     CurveCreator::Coordinates aCoords;
514
515     for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
516       const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
517
518       if (aPlane.SquareDistance(aPnt) > aTolConf2) {
519         // The point doesn't lie on the plane.
520         return false;
521       }
522
523       ElSLib::Parameters(aPlane, aPnt, aX, aY);
524       aCoords.push_back(aX);
525       aCoords.push_back(aY);
526     }
527
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;
533
534     theCurve->addSectionInternal(aSecName, aSecType,
535                                  aSecIt->myIsClosed, aCoords);
536   }
537
538   // Set the local coordinate system.
539   theLocalCS = aPlane.Position();
540
541   return true;
542 }
543
544 class CompareSectionToPoint
545 {
546 public:
547   CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
548     : mySectionId( theISection ), myPointId( theIPoint ) {};
549   ~CompareSectionToPoint() {}
550
551   bool operator < ( const CompareSectionToPoint& theOther ) const
552   {
553     bool isLess = mySectionId < theOther.mySectionId;
554     if ( !isLess && mySectionId == theOther.mySectionId )
555       isLess = myPointId < theOther.myPointId;
556     return isLess;
557   }
558
559 private:
560   int mySectionId;
561   int myPointId;
562 };
563
564
565 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
566                                             const CurveCreator_ICurve* theCurve,
567                                             CurveCreator_ICurve::SectionToPointList& thePoints )
568 {
569   thePoints.clear();
570
571   std::list<float> aSelectedPoints;
572   gp_Pnt aPnt;
573   std::map<CompareSectionToPoint, int> aPointsMap;
574
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() );
581
582     if ( aVertex.IsNull() )
583       continue;
584     aPnt = BRep_Tool::Pnt( aVertex );
585
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() )
593         continue;
594       aPointsMap[aPoint] = 0;
595
596       thePoints.push_back( *anIt );
597     }
598   }
599 }
600
601 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
602                                             const CurveCreator_ICurve* theCurve,
603                                             const CurveCreator_ICurve::SectionToPointList& thePoints )
604 {
605   if ( !theCurve )
606     return;
607
608   Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
609   if ( anAIS.IsNull() )
610     return;
611   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
612   if ( anAISShape.IsNull() )
613     return;
614
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 );
618
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++ )
623   {
624     gp_Pnt aPntToSelect;
625     CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
626     aPntsToSelect[i] = aPntToSelect;
627   }
628
629   theContext->ClearSelected( Standard_False );
630   //ASL: we switch off automatic highlight to improve performance of selection
631   theContext->SetAutomaticHilight( Standard_False );
632
633   Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
634   for( aSelection->Init(); aSelection->More(); aSelection->Next() )
635   {    
636 #if OCC_VERSION_LARGE > 0x06080100
637     const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
638     if( aHSenEntity.IsNull() )
639       continue;
640     Handle_SelectBasics_SensitiveEntity aSenEntity = aHSenEntity->BaseSensitive();
641 #else
642     Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
643 #endif
644
645     Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
646
647     gp_Pnt anOwnerPnt = aSenPnt->Point();
648     Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
649
650
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++ )
655     {
656       bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
657                          fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
658       if( isIntersect )
659       {
660         theContext->AddOrRemoveSelected( anOwner, Standard_False );
661         break;
662       }
663     }
664   }
665
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 );
670 }
671
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,
678                                                const bool theOpen )
679 {
680   if ( !theContext )
681     return;
682
683   if ( theOpen ) {
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*/ );
688     }
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() )
692     {
693       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
694       {
695         theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
696         theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
697       }
698     }
699   }
700   else {
701     if ( theContext->HasOpenedContext() )
702       theContext->CloseAllContexts();
703   }
704 }
705
706 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
707                                         Handle(AIS_InteractiveObject) theObject,
708                                         const int theX, const int theY,
709                                         gp_Pnt& thePoint,
710                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
711 {
712   bool isFullFound = false;
713
714   if ( theObject.IsNull() || theView.IsNull() )
715     return isFullFound;
716   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
717   if ( aShape.IsNull() )
718     return isFullFound;
719   const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
720   if ( aCompound.IsNull() )
721     return isFullFound;
722
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())
730   {
731     const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
732     if ( anEdge.IsNull() )
733       continue;
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 );
742         if ( isFound ) {
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 ) );
748         }
749       }
750     }
751     else { // a curve built on a polyline edge
752       Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
753       if ( aGLine.IsNull() )
754         continue;
755       isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
756                                         aDelta );
757       if ( isFound ) {
758         aCurPoint = aGLine->Value( aParameter );
759         TopoDS_Vertex V1, V2;
760         TopExp::Vertices( anEdge, V1, V2, Standard_True );
761         if ( V1.IsNull() || V2.IsNull() )
762           continue;
763         aCurPoint1 = BRep_Tool::Pnt(V1);
764         aCurPoint2 = BRep_Tool::Pnt(V2);
765
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;
770       }
771     }
772     if ( isFound && aMinDelta >= aDelta ) {
773       aMinDelta = aDelta;
774
775       isFullFound = true;
776       aFoundPnt1 = aCurPoint1;
777       aFoundPnt2 = aCurPoint2;
778       aFoundPoint = aCurPoint;
779     }
780   }
781   if ( isFullFound ) {
782     int aX, anY, aX1, anY1, aX2, anY2;
783     int aDelta;
784     CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
785     CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
786     CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
787
788     isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
789                   !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
790     if ( isFullFound ) {
791       thePoint = aFoundPoint;
792       thePoint1 = aFoundPnt1;
793       thePoint2 = aFoundPnt2;
794     }
795   }
796   return isFullFound;
797 }
798
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,
803                                                  int& theDelta )
804 {
805   bool isFound = false;
806   if ( theView.IsNull() )
807     return isFound;
808
809   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
810
811   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
812   Standard_Integer aNbPoint = aProj.NbPoints();
813   if (aNbPoint > 0) {
814     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
815       gp_Pnt aNewPoint = aProj.Point( j );
816       theParameter = aProj.Parameter( j );
817
818       int aX, anY;
819       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
820
821       isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
822     }
823   }
824   return isFound;
825 }
826
827 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
828                                         const int theOtherY, const double theTolerance, int& theDelta )
829 {
830   int aXDelta = abs( theX - theOtherX );
831   int anYDelta = abs( theY - theOtherY );
832
833   theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
834
835   return aXDelta < theTolerance && anYDelta < theTolerance;
836 }
837
838 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
839 {
840   return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
841 }
842
843 //=======================================================================
844 // function : getPoints
845 // purpose  : 
846 //=======================================================================
847 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
848                   (const TopoDS_Shape &theShape,
849                          bool         &IsClosed,
850                          bool         &IsBSpline)
851 {
852   Handle(TColgp_HArray1OfPnt) aResult;
853
854   IsClosed  = false;
855   IsBSpline = false;
856
857   if (theShape.IsNull()) {
858     return aResult;
859   }
860
861   const TopAbs_ShapeEnum aShType = theShape.ShapeType();
862
863   if (aShType == TopAbs_VERTEX) {
864     // There is a single point.
865     gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
866
867     aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
868
869     return aResult;
870   } else if (aShType != TopAbs_WIRE) {
871     // The shape is neither a vertex nor a wire.
872     return aResult;
873   }
874
875   // Treat wire.
876   BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
877
878   if (!anExp.More()) {
879     // Empty wires are not allowed.
880     return aResult;
881   }
882
883   // Treat the first edge.
884   TopoDS_Edge        anEdge = anExp.Current();
885   Handle(Geom_Curve) aCurve = GetCurve(anEdge);
886
887   if (aCurve.IsNull()) {
888     return aResult;
889   }
890
891   // Check the curve type.
892   Handle(Standard_Type) aType     = aCurve->DynamicType();
893
894   if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
895     IsBSpline = true;
896   } else if (aType != STANDARD_TYPE(Geom_Line)) {
897     // The curve is neither a line or a BSpline. It is not valid.
898     return aResult;
899   }
900
901   // Go to the next edge.
902   TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
903
904   anExp.Next();
905
906   if (IsBSpline)
907   {
908     // There should be a single BSpline curve in the wire.
909     if (anExp.More()) {
910       return aResult;
911     }
912
913     // Construct a section from poles of BSpline.
914     Handle(Geom_BSplineCurve) aBSplCurve =
915       Handle(Geom_BSplineCurve)::DownCast(aCurve);
916
917     // Check if the edge is valid. It should not be based on trimmed curve.
918     gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
919     TopoDS_Vertex aV[2];
920     Standard_Integer i;
921
922     TopExp::Vertices(anEdge, aV[0], aV[1]);
923
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]);
927
928       if (!aPnt.IsEqual(aCP[i], aTol)) {
929         return aResult;
930       }
931     }
932
933     IsClosed = aV[0].IsSame(aV[1]) ? true : false;
934     
935     Standard_Integer aNbPoints = aBSplCurve->NbKnots();
936     TColStd_Array1OfReal aKnots(1, aNbPoints);
937     aBSplCurve->Knots(aKnots);
938
939     // Don't consider the last point as it coincides with the first
940     if (IsClosed)
941       --aNbPoints;
942
943     aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
944     for (i = 1; i <= aNbPoints; ++i)
945       aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
946   }
947   else
948   {
949     // This is a polyline.
950     TopTools_ListOfShape aVertices;
951     Standard_Integer     aNbVtx = 1;
952
953
954     aVertices.Append(aFirstVtx);
955
956     for (; anExp.More(); anExp.Next(), ++aNbVtx) {
957       anEdge = anExp.Current();
958       aCurve = GetCurve(anEdge);
959
960       if (aCurve.IsNull()) {
961         return aResult;
962       }
963
964       aType = aCurve->DynamicType();
965
966       if (aType != STANDARD_TYPE(Geom_Line)) {
967         // The curve is not a line. It is not valid.
968         return aResult;
969       }
970
971       // Add the current vertex to the list.
972       aVertices.Append(anExp.CurrentVertex());
973     }
974
975     // Check if the section is closed.
976     TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
977
978     IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
979
980     // Store a last vertex
981     if (!IsClosed)
982     {
983       aVertices.Append(aLastVtx);
984       aNbVtx++;
985     }
986
987     // Fill the array of points.
988     aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
989
990     Standard_Integer i;
991     TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
992
993     for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
994       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
995
996       aResult->SetValue(i, aPnt);
997     }
998   }
999
1000   return aResult;
1001 }
1002 //=======================================================================
1003 // function : FindPlane
1004 // purpose  : 
1005 //=======================================================================
1006 void CurveCreator_Utils::FindPlane
1007                        (const Handle_TColgp_HArray1OfPnt &thePoints,
1008                               gp_Pln                     &thePlane,
1009                               Standard_Integer           &thePlnStatus)
1010 {
1011   if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
1012     // The plane can't be defined or is fixed. Nothing to change.
1013     return;
1014   }
1015
1016   Standard_Integer    i;
1017   const Standard_Real aTolConf = Precision::Confusion();
1018
1019   for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
1020     const gp_Pnt &aPnt = thePoints->Value(i);
1021
1022     switch (thePlnStatus) {
1023       case PLN_FREE:
1024         // Fix the origin.
1025         thePlane.SetLocation(aPnt);
1026         thePlnStatus = PLN_ORIGIN;
1027         break;
1028       case PLN_ORIGIN:
1029         {
1030           // Fix origin + OX axis
1031           const gp_Pnt &aPlnLoc = thePlane.Location();
1032
1033           if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
1034             // Set the X axis.
1035             gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1036             gp_Ax3 aXNorm(aPlnLoc, aXDir);
1037             gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
1038
1039             thePlane.SetPosition(aNewPlnPos);
1040             thePlnStatus = PLN_OX;
1041           }
1042         }
1043         break;
1044       case PLN_OX:
1045         {
1046           // Fix OY axis
1047           gp_Lin aXLin(thePlane.XAxis());
1048           Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
1049
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());
1057
1058             thePlane.SetPosition(aNewPlnPos);
1059             thePlnStatus = PLN_FIXED;
1060             return;
1061           }
1062         }
1063         break;
1064       default:
1065         return;
1066     }
1067   }
1068 }