0024988: Wrong result done by projection algorithm
[occt.git] / src / ProjLib / ProjLib_ProjectOnSurface.cxx
1 // Created on: 1994-09-15
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <ProjLib_ProjectOnSurface.ixx>
18
19 #include <AppCont_Function.hxx>
20 #include <Approx_FitAndDivide.hxx>
21 #include <AppParCurves_MultiCurve.hxx>
22 #include <Standard_NoSuchObject.hxx>
23 #include <Extrema_POnSurf.hxx>
24 #include <Precision.hxx>
25 #include <BSplCLib.hxx>
26 #include <PLib.hxx>
27 #include <Adaptor3d_HCurve.hxx>
28 #include <Geom_BSplineCurve.hxx>
29 #include <TColgp_Array1OfPnt.hxx>
30 #include <TColStd_Array1OfReal.hxx>
31 #include <TColStd_Array1OfInteger.hxx>
32 #include <Extrema_ExtPS.hxx>
33
34
35 //=======================================================================
36 //function : OnSurface_Value
37 //purpose  : Evaluate current point of the projected curve
38 //=======================================================================
39
40 static gp_Pnt OnSurface_Value(const Standard_Real U,
41                               const Handle(Adaptor3d_HCurve)& myCurve,
42                               Extrema_ExtPS * myExtPS)
43 {
44   // on essaie de rendre le point solution le plus proche.
45   myExtPS->Perform(myCurve->Value(U));
46   
47   Standard_Real Dist2Min = RealLast();
48   Standard_Integer Index = 0;
49   
50   for ( Standard_Integer i = 1; i <= myExtPS->NbExt(); i++) {
51     if ( myExtPS->SquareDistance(i) < Dist2Min) {
52       Index = i;
53       Dist2Min = myExtPS->SquareDistance(Index);
54     }
55   }
56   if ( Index == 0 ) {
57     cout << " Extrema non trouve pour U = " << U << endl;
58     return gp_Pnt(0.,0.,0.);
59   }
60   else {
61     return (myExtPS->Point(Index)).Value();
62   }
63 }
64
65 //=======================================================================
66 //function : OnSurface_D1
67 //purpose  : 
68 //=======================================================================
69
70 static Standard_Boolean OnSurface_D1(const Standard_Real , // U,
71                                      gp_Pnt& ,       // P,
72                                      gp_Vec&       , // V,
73                                      const  Handle(Adaptor3d_HCurve)& , //  myCurve,
74                                      Extrema_ExtPS *) // myExtPS)
75 {
76   return Standard_False;
77 }
78
79
80 //=======================================================================
81 //  class  : ProjLib_OnSurface
82 //purpose  : Use to approximate the projection on a plane
83 //=======================================================================
84
85 class ProjLib_OnSurface : public AppCont_Function
86
87 {
88   Handle(Adaptor3d_HCurve)       myCurve;
89   Extrema_ExtPS *myExtPS;
90
91   public :
92
93   ProjLib_OnSurface(const Handle(Adaptor3d_HCurve)   & C, 
94                     const Handle(Adaptor3d_HSurface) & S)
95  : myCurve(C)
96   {
97     myNbPnt = 1;
98     myNbPnt2d = 0;
99     Standard_Real U = myCurve->FirstParameter();
100     gp_Pnt P = myCurve->Value(U);
101     Standard_Real Tol = Precision::PConfusion();
102     myExtPS = new Extrema_ExtPS(P,S->Surface(),Tol,Tol);
103   }
104
105   ~ProjLib_OnSurface() { delete myExtPS; }
106
107   Standard_Real FirstParameter() const
108     {return myCurve->FirstParameter();}
109
110   Standard_Real LastParameter() const
111     {return myCurve->LastParameter();}
112
113   Standard_Boolean Value(const Standard_Real   theT,
114                          NCollection_Array1<gp_Pnt2d>& /*thePnt2d*/,
115                          NCollection_Array1<gp_Pnt>&   thePnt) const
116   {
117       thePnt(1) = OnSurface_Value(theT, myCurve, myExtPS);
118       return Standard_True;
119   }
120
121   Standard_Boolean D1(const Standard_Real   theT,
122                       NCollection_Array1<gp_Vec2d>& /*theVec2d*/,
123                       NCollection_Array1<gp_Vec>&   theVec) const
124   {
125     gp_Pnt aPnt;
126     return OnSurface_D1(theT, aPnt, theVec(1), myCurve, myExtPS);
127   }
128 };
129
130
131 //=====================================================================//
132 //                                                                     //
133 //  D E S C R I P T I O N   O F   T H E   C L A S S  :                 // 
134 //                                                                     //
135 //         P r o j L i b _ A p p r o x P r o j e c t O n P l a n e     //
136 //                                                                     //
137 //=====================================================================//
138
139
140 //=======================================================================
141 //function : ProjLib_ProjectOnSurface
142 //purpose  : 
143 //=======================================================================
144
145 ProjLib_ProjectOnSurface::ProjLib_ProjectOnSurface() :
146 myIsDone(Standard_False) 
147 {
148 }
149
150 //=======================================================================
151 //function : ProjLib_ProjectOnSurface
152 //purpose  : 
153 //=======================================================================
154
155 ProjLib_ProjectOnSurface::ProjLib_ProjectOnSurface
156 (const Handle(Adaptor3d_HSurface)& S ) :
157 myIsDone(Standard_False) 
158 {
159   mySurface = S;
160 }
161
162 void ProjLib_ProjectOnSurface::Load(const Handle(Adaptor3d_HCurve)& C,
163                                     const Standard_Real  Tolerance) 
164 {
165   myTolerance = Tolerance ;
166   myCurve = C;
167   myIsDone = Standard_False ; 
168   if (!mySurface.IsNull()) { 
169       
170     ProjLib_OnSurface F(myCurve, mySurface);
171
172     Standard_Integer Deg1, Deg2;
173     Deg1 = 8; Deg2 = 8;
174     
175     Approx_FitAndDivide Fit(F,Deg1,Deg2,Precision::Approximation(),
176                             Precision::PApproximation(),Standard_True);
177     Standard_Integer i;
178     Standard_Integer NbCurves = Fit.NbMultiCurves();
179     Standard_Integer MaxDeg = 0;
180     
181     // Pour transformer la MultiCurve en BSpline, il faut que toutes 
182     // les Bezier la constituant aient le meme degre -> Calcul de MaxDeg
183     Standard_Integer NbPoles  = 1;
184     for (i = 1; i <= NbCurves; i++) {
185       Standard_Integer Deg = Fit.Value(i).Degree();
186       MaxDeg = Max ( MaxDeg, Deg);
187     }
188     NbPoles = MaxDeg * NbCurves + 1;               //Poles sur la BSpline
189     TColgp_Array1OfPnt  Poles( 1, NbPoles);
190     
191     TColgp_Array1OfPnt TempPoles( 1, MaxDeg + 1);  //pour augmentation du degre
192     
193     TColStd_Array1OfReal Knots( 1, NbCurves + 1);  //Noeuds de la BSpline
194     
195     Standard_Integer Compt = 1;
196     for (i = 1; i <= Fit.NbMultiCurves(); i++) {
197       Fit.Parameters(i, Knots(i), Knots(i+1)); 
198       
199       AppParCurves_MultiCurve MC = Fit.Value( i);   //Charge la Ieme Curve
200       TColgp_Array1OfPnt LocalPoles( 1, MC.Degree() + 1);//Recupere les poles
201       MC.Curve(1, Poles);
202       
203       //Augmentation eventuelle du degre
204       Standard_Integer Inc = MaxDeg - MC.Degree();
205       if ( Inc > 0) {
206         BSplCLib::IncreaseDegree( Inc, LocalPoles, PLib::NoWeights(),
207                                  TempPoles, PLib::NoWeights());
208           //mise a jour des poles de la PCurve
209           for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
210             Poles.SetValue( Compt, TempPoles( j));
211             Compt++;
212           }
213         }
214       else {
215         //mise a jour des poles de la PCurve
216         for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
217           Poles.SetValue( Compt, LocalPoles( j));
218           Compt++;
219         }
220       } 
221       
222       Compt--;
223     }
224     
225     //mise a jour des fields de ProjLib_Approx
226     
227     Standard_Integer NbKnots = NbCurves + 1;
228     
229     TColStd_Array1OfInteger  Mults( 1, NbKnots);
230     Mults.SetValue( 1, MaxDeg + 1);
231     for ( i = 2; i <= NbCurves; i++) {
232       Mults.SetValue( i, MaxDeg);
233     }
234     Mults.SetValue(NbKnots, MaxDeg + 1);
235     myResult = 
236       new Geom_BSplineCurve(Poles,
237                             Knots,
238                             Mults,
239                             MaxDeg,
240                             Standard_False) ;
241     myIsDone = Standard_True ;
242   }
243 }
244
245 void ProjLib_ProjectOnSurface::Delete()
246 {}
247
248 //=======================================================================
249 //function : BSpline
250 //purpose  : 
251 //=======================================================================
252
253 Handle(Geom_BSplineCurve) ProjLib_ProjectOnSurface::BSpline() const 
254 {
255   Standard_NoSuchObject_Raise_if
256     (!myIsDone,
257      "ProjLib_ProjectOnSurface:BSpline");
258   return myResult ;
259 }
260
261 //=======================================================================
262 //function : IsDone
263 //purpose  : 
264 //=======================================================================
265
266 Standard_Boolean ProjLib_ProjectOnSurface::IsDone() const 
267 {
268   return myIsDone;
269 }