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