Salome HOME
7b6075c2b3063c1fabe73885852eecc60a78ac3e
[modules/geom.git] / 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 void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
187                                          TopoDS_Shape& theShape )
188 {
189   BRep_Builder aBuilder;
190   TopoDS_Compound aComp;
191   aBuilder.MakeCompound( aComp );
192   for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
193   {
194     int theISection = iSection;
195
196     CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
197     int aPointSize = theCurve->getNbPoints( theISection );
198     if ( aPointSize == 0 )
199       continue;
200
201     bool aSectIsClosed = theCurve->isClosed( theISection );
202     bool isPolyline = aSectType == CurveCreator::Polyline;
203
204     int iPoint = 0;
205     gp_Pnt aPrevPoint, aPoint;
206     // filters the curve points to skip equal points
207     std::vector<gp_Pnt> aPoints;
208     CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
209     aPoints.push_back( aPoint );
210     aPrevPoint = aPoint;
211     iPoint++;
212     for( ; iPoint < aPointSize; iPoint++ ) {
213       CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
214       if ( !isEqualPoints( aPrevPoint, aPoint ) )
215         aPoints.push_back( aPoint );
216       aPrevPoint = aPoint;
217     }
218     int aNbPoints = aPoints.size();
219
220     if ( aNbPoints == 1 ) {
221       aPoint = aPoints.front();
222       TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
223       aBuilder.Add( aComp, aVertex );
224     }
225     else if ( aNbPoints > 1 ) {
226       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aNbPoints);
227       TColgp_Array1OfVec aTangents(1, aNbPoints);
228       Handle(TColStd_HArray1OfBoolean) aTangentFlags = new TColStd_HArray1OfBoolean(1, aNbPoints);
229       gp_Vec aNullVec(0, 0, 0);
230
231       TopoDS_Edge aPointEdge;
232       TopoDS_Vertex aVertex;
233
234       std::vector<gp_Pnt>::const_iterator aPointIt = aPoints.begin(), aPointLast = aPoints.end();
235       aPoint = *aPointIt;
236
237       int aHIndex = 1;
238       aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
239       aBuilder.Add( aComp, aVertex );
240       if ( !isPolyline ) {
241         aHCurvePoints->SetValue( aHIndex, aPoint );
242         aTangents.SetValue( aHIndex, aNullVec );
243         aTangentFlags->SetValue( aHIndex, Standard_False );
244         aHIndex++;
245       }
246
247       aPrevPoint = aPoint;
248       aPointIt++;
249       for( ; aPointIt != aPointLast; aPointIt++ ) {
250         aPoint = *aPointIt;
251         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
252         aBuilder.Add( aComp, aVertex );
253         if ( isPolyline ) {
254           TopoDS_Edge aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
255           aBuilder.Add( aComp, aPointEdge );
256         }
257         else {
258           aHCurvePoints->SetValue( aHIndex, aPoint );
259           aTangents.SetValue( aHIndex, aNullVec );
260           aTangentFlags->SetValue( aHIndex, Standard_False );
261           aHIndex++;
262         }
263         aPrevPoint = aPoint;
264       }
265       if( aSectIsClosed && ( aNbPoints > 2 ) ) {
266         aPoint = aPoints.front();
267         aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
268         aBuilder.Add( aComp, aVertex );
269         if ( isPolyline ) {
270           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
271           aBuilder.Add( aComp, aPointEdge );
272         }
273       }
274       if( !isPolyline ) {
275         // compute BSpline
276         Handle(Geom_BSplineCurve) aBSplineCurve;
277         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
278         // correct the spline degree to be as 3 for non-periodic spline if number of points
279         // less than 3. It is need to have a knot in each spline point. This knots are used
280         // to found a neighbour points when a new point is inserted between two existing.
281         if (!aSectIsClosed ) {
282           if (aHCurvePoints->Length() == 3)
283             aGBC.Load(aTangents, aTangentFlags);
284         }
285
286         aGBC.Perform();
287         if ( aGBC.IsDone() )
288           aBSplineCurve = aGBC.Curve();
289         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
290         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
291         aBuilder.Add( aComp, aWire );
292       }
293     }
294   }
295   theShape = aComp;
296 }
297
298 /**
299  * This is an intermediate structure for curve construction.
300  */
301 struct Section3D
302 {
303   Section3D() : myIsClosed(false), myIsBSpline(false)
304   { }
305
306   bool                        myIsClosed;
307   bool                        myIsBSpline;
308   Handle(TColgp_HArray1OfPnt) myPoints;
309 };
310
311 //=======================================================================
312 // function : constructCurve
313 // purpose  : 
314 //=======================================================================
315 bool CurveCreator_Utils::constructCurve
316                       (const TopoDS_Shape        theShape,
317                              CurveCreator_Curve *theCurve,
318                              gp_Ax3             &theLocalCS)
319 {
320   if (theShape.IsNull()) {
321     return false;
322   }
323
324   // Collect wires or vertices from shape.
325   TopTools_ListOfShape aWOrV;
326   TopAbs_ShapeEnum     aType = theShape.ShapeType();
327
328   if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
329     aWOrV.Append(theShape);
330   } else if (aType == TopAbs_COMPOUND) {
331     TopoDS_Iterator aShIter(theShape);
332
333     for (; aShIter.More(); aShIter.Next()) {
334       const TopoDS_Shape &aSubShape = aShIter.Value();
335
336       aType = aSubShape.ShapeType();
337
338       if (aType == TopAbs_WIRE || aType == TopAbs_VERTEX) {
339         aWOrV.Append(aSubShape);
340       } else {
341         // Only subshapes of types wire or vertex are supported.
342         return false;
343       }
344     }
345   } else {
346     // Only wire (vertex) or compound of wires (vertices) are supported.
347     return false;
348   }
349
350   // Treat each wire or vertex. Get points, compute the working plane.
351   gp_Pln                             aPlane;
352   Standard_Integer                   aPlaneStatus = PLN_FREE;
353   TopTools_ListIteratorOfListOfShape anIter(aWOrV);
354   std::list<Section3D>               aListSec;
355
356   for (; anIter.More(); anIter.Next()) {
357     Section3D aSec3D;
358
359     aSec3D.myPoints = CurveCreator_Utils::getPoints
360       (anIter.Value(), aSec3D.myIsClosed, aSec3D.myIsBSpline);
361
362     if (aSec3D.myPoints.IsNull()) {
363       return false;
364     }
365
366     aListSec.push_back(aSec3D);
367
368     if (aPlaneStatus != PLN_FIXED) {
369       // Compute plane
370       CurveCreator_Utils::FindPlane(aSec3D.myPoints, aPlane, aPlaneStatus);
371     }
372   }
373
374   // Check if it is possible to change a computed coordinate system by
375   // XOY, XOZ or YOZ or parallel to them.
376   gp_Pnt        aO(0., 0., 0.);
377   gp_Dir        aNDir(0., 0., 1.);
378   gp_Dir        aXDir(1., 0., 0.);
379   gp_Ax3        anAxis;
380   Standard_Real aTolAng = Precision::Confusion(); // Angular() is too small.
381
382   switch (aPlaneStatus) {
383     case PLN_ORIGIN:
384       {
385         // Change the location.
386         aO.SetZ(aPlane.Location().Z());
387         anAxis.SetLocation(aO);
388         aPlane.SetPosition(anAxis);
389       }
390       break;
391     case PLN_OX:
392       {
393         // Fixed origin + OX axis
394         const gp_Dir &aPlnX = aPlane.Position().XDirection();
395
396         if (Abs(aPlnX.Z()) <= aTolAng) {
397           // Make a coordinate system parallel to XOY.
398           aO.SetZ(aPlane.Location().Z());
399           anAxis.SetLocation(aO);
400           aPlane.SetPosition(anAxis);
401         } else if (Abs(aPlnX.Y()) <= aTolAng) {
402           // Make a coordinate system parallel to XOZ.
403           aO.SetY(aPlane.Location().Y());
404           aNDir.SetCoord(0., 1., 0.);
405           aXDir.SetCoord(0., 0., 1.);
406           anAxis = gp_Ax3(aO, aNDir, aXDir);
407           aPlane.SetPosition(anAxis);
408         } else if (Abs(aPlnX.X()) <= aTolAng) {
409           // Make a coordinate system parallel to YOZ.
410           aO.SetX(aPlane.Location().X());
411           aNDir.SetCoord(1., 0., 0.);
412           aXDir.SetCoord(0., 1., 0.);
413           anAxis = gp_Ax3(aO, aNDir, aXDir);
414           aPlane.SetPosition(anAxis);
415         }
416       }
417       break;
418     case PLN_FIXED:
419       {
420         const gp_Dir &aPlnN = aPlane.Position().Direction();
421         gp_Dir        aYDir(0., 1., 0.);
422
423         if (aPlnN.IsParallel(aNDir, aTolAng)) {
424           // Make a coordinate system parallel to XOY.
425           aO.SetZ(aPlane.Location().Z());
426           anAxis.SetLocation(aO);
427           aPlane.SetPosition(anAxis);
428         } else if (aPlnN.IsParallel(aYDir, aTolAng)) {
429           // Make a coordinate system parallel to XOZ.
430           aO.SetY(aPlane.Location().Y());
431           aNDir.SetCoord(0., 1., 0.);
432           aXDir.SetCoord(0., 0., 1.);
433           anAxis = gp_Ax3(aO, aNDir, aXDir);
434           aPlane.SetPosition(anAxis);
435         } else if (aPlnN.IsParallel(aXDir, aTolAng)) {
436           // Make a coordinate system parallel to YOZ.
437           aO.SetX(aPlane.Location().X());
438           aNDir.SetCoord(1., 0., 0.);
439           aXDir.SetCoord(0., 1., 0.);
440           anAxis = gp_Ax3(aO, aNDir, aXDir);
441           aPlane.SetPosition(anAxis);
442         }
443       }
444       break;
445     case PLN_FREE:
446     default:
447       // Use XOY plane.
448       aPlane.SetPosition(anAxis);
449       break;
450   }
451
452   // Compute 2d points.
453   std::list<Section3D>::const_iterator aSecIt = aListSec.begin();
454   Standard_Real                        aTolConf2 =
455     Precision::Confusion()*Precision::Confusion();
456   Standard_Real                        aX;
457   Standard_Real                        aY;
458
459   for (; aSecIt != aListSec.end(); ++aSecIt) {
460     Standard_Integer          i;
461     CurveCreator::Coordinates aCoords;
462
463     for (i = aSecIt->myPoints->Lower(); i <= aSecIt->myPoints->Upper(); ++i) {
464       const gp_Pnt &aPnt = aSecIt->myPoints->Value(i);
465
466       if (aPlane.SquareDistance(aPnt) > aTolConf2) {
467         // The point doesn't lie on the plane.
468         return false;
469       }
470
471       ElSLib::Parameters(aPlane, aPnt, aX, aY);
472       aCoords.push_back(aX);
473       aCoords.push_back(aY);
474     }
475
476     // Add a new section to the curve.
477     const std::string               aSecName =
478       CurveCreator_UtilsICurve::getUniqSectionName(theCurve);
479     const CurveCreator::SectionType aSecType = aSecIt->myIsBSpline ?
480       CurveCreator::Spline : CurveCreator::Polyline;
481
482     theCurve->addSectionInternal(aSecName, aSecType,
483                                  aSecIt->myIsClosed, aCoords);
484   }
485
486   // Set the local coordinate system.
487   theLocalCS = aPlane.Position();
488
489   return true;
490 }
491
492 class CompareSectionToPoint
493 {
494 public:
495   CompareSectionToPoint( const int theISection = -1, const int theIPoint = -1 )
496     : mySectionId( theISection ), myPointId( theIPoint ) {};
497   ~CompareSectionToPoint() {}
498
499   bool operator < ( const CompareSectionToPoint& theOther ) const
500   {
501     bool isLess = mySectionId < theOther.mySectionId;
502     if ( !isLess && mySectionId == theOther.mySectionId )
503       isLess = myPointId < theOther.myPointId;
504     return isLess;
505   }
506
507 private:
508   int mySectionId;
509   int myPointId;
510 };
511
512
513 void CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
514                                             const CurveCreator_ICurve* theCurve,
515                                             CurveCreator_ICurve::SectionToPointList& thePoints )
516 {
517   thePoints.clear();
518
519   std::list<float> aSelectedPoints;
520   gp_Pnt aPnt;
521   std::map<CompareSectionToPoint, int> aPointsMap;
522
523   CurveCreator_ICurve::SectionToPointList aPoints;
524   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
525     TopoDS_Vertex aVertex;
526     TopoDS_Shape aShape = theContext->SelectedShape();
527     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
528       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
529
530     if ( aVertex.IsNull() )
531       continue;
532     aPnt = BRep_Tool::Pnt( aVertex );
533
534     CurveCreator_UtilsICurve::findSectionsToPoints( theCurve, aPnt.X(), aPnt.Y(), aPoints );
535     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
536                                                             aLast = aPoints.end();
537     CompareSectionToPoint aPoint;
538     for ( ; anIt != aLast; anIt++ ) {
539       aPoint = CompareSectionToPoint( (*anIt).first, (*anIt).second );
540       if ( aPointsMap.find( aPoint ) != aPointsMap.end() )
541         continue;
542       aPointsMap[aPoint] = 0;
543
544       thePoints.push_back( *anIt );
545     }
546   }
547 }
548
549 void CurveCreator_Utils::setSelectedPoints( Handle(AIS_InteractiveContext) theContext,
550                                             const CurveCreator_ICurve* theCurve,
551                                             const CurveCreator_ICurve::SectionToPointList& thePoints )
552 {
553   if ( !theCurve )
554     return;
555
556   Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
557   if ( anAIS.IsNull() )
558     return;
559   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
560   if ( anAISShape.IsNull() )
561     return;
562
563   //ASL: we convert list of point indices to list of points coordinates
564   int aSize = thePoints.size();
565   std::vector<gp_Pnt> aPntsToSelect( aSize );
566
567   CurveCreator_ICurve::SectionToPointList::const_iterator
568                      aPIt = thePoints.begin(), aPLast = thePoints.end();
569   CurveCreator_ICurve::SectionToPoint aSToPoint;
570   for( int i=0; aPIt != aPLast; aPIt++, i++ )
571   {
572     gp_Pnt aPntToSelect;
573     CurveCreator_UtilsICurve::getPoint( theCurve, aPIt->first, aPIt->second, aPntToSelect );
574     aPntsToSelect[i] = aPntToSelect;
575   }
576
577   theContext->ClearSelected( Standard_False );
578   //ASL: we switch off automatic highlight to improve performance of selection
579   theContext->SetAutomaticHilight( Standard_False );
580
581   Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
582   for( aSelection->Init(); aSelection->More(); aSelection->Next() )
583   {    
584 #if OCC_VERSION_LARGE > 0x06080100
585     const Handle(SelectMgr_SensitiveEntity) aHSenEntity = aSelection->Sensitive();
586     if( aHSenEntity.IsNull() )
587       continue;
588     Handle_SelectBasics_SensitiveEntity aSenEntity = aHSenEntity->BaseSensitive();
589 #else
590     Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
591 #endif
592
593     Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
594
595     gp_Pnt anOwnerPnt = aSenPnt->Point();
596     Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
597
598
599     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
600                                                                    aLast = thePoints.end();
601     bool isFound = false;
602     for( int i=0; i<aSize; i++ )
603     {
604       bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
605                          fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
606       if( isIntersect )
607       {
608         theContext->AddOrRemoveSelected( anOwner, Standard_False );
609         break;
610       }
611     }
612   }
613
614   //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
615   //     and call HilightPicked to draw selected owners
616   theContext->SetAutomaticHilight( Standard_True );
617   theContext->LocalContext()->HilightPicked( Standard_True );
618 }
619
620 //=======================================================================
621 // function : setLocalPointContext
622 // purpose  : Open/close the viewer local context
623 //=======================================================================
624 void CurveCreator_Utils::setLocalPointContext( const CurveCreator_ICurve* theCurve,
625                                                Handle(AIS_InteractiveContext) theContext,
626                                                const bool theOpen )
627 {
628   if ( !theContext )
629     return;
630
631   if ( theOpen ) {
632     // Open local context if there is no one
633     if ( !theContext->HasOpenedContext() ) {
634       theContext->ClearCurrents( false );
635       theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
636     }
637     // load the curve AIS object to the local context with the point selection
638     Handle(AIS_InteractiveObject) anAIS = theCurve->getAISObject();
639     if ( !anAIS.IsNull() )
640     {
641       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
642       {
643         theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
644         theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
645       }
646     }
647   }
648   else {
649     if ( theContext->HasOpenedContext() )
650       theContext->CloseAllContexts();
651   }
652 }
653
654 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
655                                         Handle(AIS_InteractiveObject) theObject,
656                                         const int theX, const int theY,
657                                         gp_Pnt& thePoint,
658                                         gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
659 {
660   bool isFullFound = false;
661
662   if ( theObject.IsNull() || theView.IsNull() )
663     return isFullFound;
664   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
665   if ( aShape.IsNull() )
666     return isFullFound;
667   const TopoDS_Compound& aCompound = TopoDS::Compound( aShape->Shape() );
668   if ( aCompound.IsNull() )
669     return isFullFound;
670
671   gp_Pnt aCurPoint, aCurPoint1, aCurPoint2;
672   gp_Pnt aFoundPoint, aFoundPnt1, aFoundPnt2;
673   Standard_Real aParameter;
674   bool isFound = false;
675   int aDelta, aMinDelta = 2*SCENE_PIXEL_PROJECTION_TOLERANCE*SCENE_PIXEL_PROJECTION_TOLERANCE;
676   TopExp_Explorer anExp( aCompound, TopAbs_EDGE );
677   for ( ; anExp.More(); anExp.Next())
678   {
679     const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
680     if ( anEdge.IsNull() )
681       continue;
682     Standard_Real aFirst, aLast;
683     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
684     if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
685       Handle(Geom_BSplineCurve) aBSplineCurve =
686                           Handle(Geom_BSplineCurve)::DownCast( aCurve );
687       if ( !aBSplineCurve.IsNull() ) {
688         isFound = hasProjectPointOnCurve( theView, theX, theY, aBSplineCurve,
689                                           aParameter, aDelta );
690         if ( isFound ) {
691           aCurPoint = aBSplineCurve->Value( aParameter );
692           Standard_Integer anI1, anI2;
693           aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
694           aCurPoint1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
695           aCurPoint2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
696         }
697       }
698     }
699     else { // a curve built on a polyline edge
700       Handle(Geom_Line) aGLine = Handle(Geom_Line)::DownCast( aCurve );
701       if ( aGLine.IsNull() )
702         continue;
703       isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter,
704                                         aDelta );
705       if ( isFound ) {
706         aCurPoint = aGLine->Value( aParameter );
707         TopoDS_Vertex V1, V2;
708         TopExp::Vertices( anEdge, V1, V2, Standard_True );
709         if ( V1.IsNull() || V2.IsNull() )
710           continue;
711         aCurPoint1 = BRep_Tool::Pnt(V1);
712         aCurPoint2 = BRep_Tool::Pnt(V2);
713
714         // check that the projected point is on the bounded curve
715         gp_Vec aVec1( aCurPoint1, aCurPoint );
716         gp_Vec aVec2( aCurPoint2, aCurPoint );
717         isFound = fabs( aVec1.Angle( aVec2 ) - M_PI ) < LOCAL_SELECTION_TOLERANCE;
718       }
719     }
720     if ( isFound && aMinDelta >= aDelta ) {
721       aMinDelta = aDelta;
722
723       isFullFound = true;
724       aFoundPnt1 = aCurPoint1;
725       aFoundPnt2 = aCurPoint2;
726       aFoundPoint = aCurPoint;
727     }
728   }
729   if ( isFullFound ) {
730     int aX, anY, aX1, anY1, aX2, anY2;
731     int aDelta;
732     CurveCreator_Utils::ConvertPointToClick( aFoundPoint, theView, aX, anY );
733     CurveCreator_Utils::ConvertPointToClick( aFoundPnt1, theView, aX1, anY1 );
734     CurveCreator_Utils::ConvertPointToClick( aFoundPnt2, theView, aX2, anY2 );
735
736     isFullFound = !isEqualPixels( aX, anY, aX1, anY1, SCENE_PIXEL_POINT_TOLERANCE, aDelta ) &&
737                   !isEqualPixels( aX, anY, aX2, anY2, SCENE_PIXEL_POINT_TOLERANCE, aDelta );
738     if ( isFullFound ) {
739       thePoint = aFoundPoint;
740       thePoint1 = aFoundPnt1;
741       thePoint2 = aFoundPnt2;
742     }
743   }
744   return isFullFound;
745 }
746
747 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
748                                                  const int theX, const int theY,
749                                                  const Handle(Geom_Curve)& theCurve,
750                                                  Standard_Real& theParameter,
751                                                  int& theDelta )
752 {
753   bool isFound = false;
754   if ( theView.IsNull() )
755     return isFound;
756
757   gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
758
759   GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
760   Standard_Integer aNbPoint = aProj.NbPoints();
761   if (aNbPoint > 0) {
762     for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
763       gp_Pnt aNewPoint = aProj.Point( j );
764       theParameter = aProj.Parameter( j );
765
766       int aX, anY;
767       CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
768
769       isFound = isEqualPixels( aX, anY, theX, theY, SCENE_PIXEL_PROJECTION_TOLERANCE, theDelta );
770     }
771   }
772   return isFound;
773 }
774
775 bool CurveCreator_Utils::isEqualPixels( const int theX, const int theY, const int theOtherX,
776                                         const int theOtherY, const double theTolerance, int& theDelta )
777 {
778   int aXDelta = abs( theX - theOtherX );
779   int anYDelta = abs( theY - theOtherY );
780
781   theDelta = aXDelta*aXDelta + anYDelta*anYDelta;
782
783   return aXDelta < theTolerance && anYDelta < theTolerance;
784 }
785
786 bool CurveCreator_Utils::isEqualPoints( const gp_Pnt& thePoint, const gp_Pnt& theOtherPoint )
787 {
788   return theOtherPoint.IsEqual( thePoint, LOCAL_SELECTION_TOLERANCE );
789 }
790
791 //=======================================================================
792 // function : getPoints
793 // purpose  : 
794 //=======================================================================
795 Handle(TColgp_HArray1OfPnt) CurveCreator_Utils::getPoints
796                   (const TopoDS_Shape &theShape,
797                          bool         &IsClosed,
798                          bool         &IsBSpline)
799 {
800   Handle(TColgp_HArray1OfPnt) aResult;
801
802   IsClosed  = false;
803   IsBSpline = false;
804
805   if (theShape.IsNull()) {
806     return aResult;
807   }
808
809   const TopAbs_ShapeEnum aShType = theShape.ShapeType();
810
811   if (aShType == TopAbs_VERTEX) {
812     // There is a single point.
813     gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
814
815     aResult = new TColgp_HArray1OfPnt(1, 1, aPnt);
816
817     return aResult;
818   } else if (aShType != TopAbs_WIRE) {
819     // The shape is neither a vertex nor a wire.
820     return aResult;
821   }
822
823   // Treat wire.
824   BRepTools_WireExplorer anExp(TopoDS::Wire(theShape));
825
826   if (!anExp.More()) {
827     // Empty wires are not allowed.
828     return aResult;
829   }
830
831   // Treat the first edge.
832   TopoDS_Edge        anEdge = anExp.Current();
833   Handle(Geom_Curve) aCurve = GetCurve(anEdge);
834
835   if (aCurve.IsNull()) {
836     return aResult;
837   }
838
839   // Check the curve type.
840   Handle(Standard_Type) aType     = aCurve->DynamicType();
841
842   if (aType == STANDARD_TYPE(Geom_BSplineCurve)) {
843     IsBSpline = true;
844   } else if (aType != STANDARD_TYPE(Geom_Line)) {
845     // The curve is neither a line or a BSpline. It is not valid.
846     return aResult;
847   }
848
849   // Go to the next edge.
850   TopoDS_Vertex aFirstVtx = anExp.CurrentVertex();
851
852   anExp.Next();
853
854   if (IsBSpline)
855   {
856     // There should be a single BSpline curve in the wire.
857     if (anExp.More()) {
858       return aResult;
859     }
860
861     // Construct a section from poles of BSpline.
862     Handle(Geom_BSplineCurve) aBSplCurve =
863       Handle(Geom_BSplineCurve)::DownCast(aCurve);
864
865     // Check if the edge is valid. It should not be based on trimmed curve.
866     gp_Pnt aCP[2] = { aBSplCurve->StartPoint(), aBSplCurve->EndPoint() };
867     TopoDS_Vertex aV[2];
868     Standard_Integer i;
869
870     TopExp::Vertices(anEdge, aV[0], aV[1]);
871
872     for (i = 0; i < 2; i++) {
873       gp_Pnt        aPnt = BRep_Tool::Pnt(aV[i]);
874       Standard_Real aTol = BRep_Tool::Tolerance(aV[i]);
875
876       if (!aPnt.IsEqual(aCP[i], aTol)) {
877         return aResult;
878       }
879     }
880
881     IsClosed = aV[0].IsSame(aV[1]) ? true : false;
882     
883     Standard_Integer aNbPoints = aBSplCurve->NbKnots();
884     TColStd_Array1OfReal aKnots(1, aNbPoints);
885     aBSplCurve->Knots(aKnots);
886
887     // Don't consider the last point as it coincides with the first
888     if (IsClosed)
889       --aNbPoints;
890
891     aResult = new TColgp_HArray1OfPnt(1, aNbPoints);
892     for (i = 1; i <= aNbPoints; ++i)
893       aResult->SetValue(i, aBSplCurve->Value( aKnots.Value(i) ));
894   }
895   else
896   {
897     // This is a polyline.
898     TopTools_ListOfShape aVertices;
899     Standard_Integer     aNbVtx = 1;
900
901
902     aVertices.Append(aFirstVtx);
903
904     for (; anExp.More(); anExp.Next(), ++aNbVtx) {
905       anEdge = anExp.Current();
906       aCurve = GetCurve(anEdge);
907
908       if (aCurve.IsNull()) {
909         return aResult;
910       }
911
912       aType = aCurve->DynamicType();
913
914       if (aType != STANDARD_TYPE(Geom_Line)) {
915         // The curve is not a line. It is not valid.
916         return aResult;
917       }
918
919       // Add the current vertex to the list.
920       aVertices.Append(anExp.CurrentVertex());
921     }
922
923     // Check if the section is closed.
924     TopoDS_Vertex aLastVtx = TopExp::LastVertex(anEdge, Standard_True);
925
926     IsClosed = aFirstVtx.IsSame(aLastVtx) ? true : false;
927
928     // Store a last vertex
929     if (!IsClosed)
930     {
931       aVertices.Append(aLastVtx);
932       aNbVtx++;
933     }
934
935     // Fill the array of points.
936     aResult = new TColgp_HArray1OfPnt(1, aNbVtx);
937
938     Standard_Integer i;
939     TopTools_ListIteratorOfListOfShape aVtxIter(aVertices);
940
941     for (i = 1; aVtxIter.More(); aVtxIter.Next(), ++i) {
942       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVtxIter.Value()));
943
944       aResult->SetValue(i, aPnt);
945     }
946   }
947
948   return aResult;
949 }
950 //=======================================================================
951 // function : FindPlane
952 // purpose  : 
953 //=======================================================================
954 void CurveCreator_Utils::FindPlane
955                        (const Handle_TColgp_HArray1OfPnt &thePoints,
956                               gp_Pln                     &thePlane,
957                               Standard_Integer           &thePlnStatus)
958 {
959   if (thePoints.IsNull() || thePlnStatus == PLN_FIXED) {
960     // The plane can't be defined or is fixed. Nothing to change.
961     return;
962   }
963
964   Standard_Integer    i;
965   const Standard_Real aTolConf = Precision::Confusion();
966
967   for (i = thePoints->Lower(); i <= thePoints->Upper(); ++i) {
968     const gp_Pnt &aPnt = thePoints->Value(i);
969
970     switch (thePlnStatus) {
971       case PLN_FREE:
972         // Fix the origin.
973         thePlane.SetLocation(aPnt);
974         thePlnStatus = PLN_ORIGIN;
975         break;
976       case PLN_ORIGIN:
977         {
978           // Fix origin + OX axis
979           const gp_Pnt &aPlnLoc = thePlane.Location();
980
981           if (!aPnt.IsEqual(aPlnLoc, aTolConf)) {
982             // Set the X axis.
983             gp_Dir aXDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
984             gp_Ax3 aXNorm(aPlnLoc, aXDir);
985             gp_Ax3 aNewPlnPos(aPlnLoc, aXNorm.XDirection(), aXNorm.Direction());
986
987             thePlane.SetPosition(aNewPlnPos);
988             thePlnStatus = PLN_OX;
989           }
990         }
991         break;
992       case PLN_OX:
993         {
994           // Fix OY axis
995           gp_Lin aXLin(thePlane.XAxis());
996           Standard_Real aSqrDist = aXLin.SquareDistance(aPnt);
997
998           if (aSqrDist > aTolConf*aTolConf) {
999             // Compute main axis.
1000             const gp_Pnt &aPlnLoc = thePlane.Location();
1001             gp_Dir        aDir(aPnt.XYZ().Subtracted(aPlnLoc.XYZ()));
1002             gp_Ax3        aXNorm(aPlnLoc, aXLin.Direction(), aDir);
1003             gp_Ax3        aNewPlnPos(aPlnLoc, aXNorm.YDirection(),
1004                                      aXNorm.Direction());
1005
1006             thePlane.SetPosition(aNewPlnPos);
1007             thePlnStatus = PLN_FIXED;
1008             return;
1009           }
1010         }
1011         break;
1012       default:
1013         return;
1014     }
1015   }
1016 }