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