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