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