0032402: Coding Rules - eliminate msvc warning C4668 (symbol is not defined as a...
[occt.git] / src / GeomFill / GeomFill_BezierCurves.cxx
1 // Created on: 1993-10-06
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-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
18 #include <Geom_BezierCurve.hxx>
19 #include <Geom_BezierSurface.hxx>
20 #include <GeomFill_BezierCurves.hxx>
21 #include <GeomFill_Coons.hxx>
22 #include <GeomFill_Curved.hxx>
23 #include <GeomFill_Filling.hxx>
24 #include <GeomFill_Stretch.hxx>
25 #include <Precision.hxx>
26 #include <Standard_ConstructionError.hxx>
27 #include <Standard_NotImplemented.hxx>
28 #include <TColgp_Array1OfPnt.hxx>
29 #include <TColgp_Array2OfPnt.hxx>
30 #include <TColStd_Array1OfReal.hxx>
31 #include <TColStd_Array2OfReal.hxx>
32
33 //=======================================================================
34 //function : SetSameWeights
35 //purpose  : Internal Use Only
36 //           This function uses the following property of Rational 
37 //           BezierCurves
38 //              if Wi = Weight(i); Pi = Pole(i); n = NbPoles
39 //              with any a,b,c != 0,      
40 //                                             i    n-i
41 //              The transformation : Wi = a * b  * c    doesn't modify 
42 //              the geometry of the curve.
43 //              Only the length of the derivatives are changed.
44 //=======================================================================
45 static void SetSameWeights(TColStd_Array1OfReal& W1, 
46                     TColStd_Array1OfReal& W2, 
47                     TColStd_Array1OfReal& W3, 
48                     TColStd_Array1OfReal& W4 ) 
49 {
50   Standard_Real Eps = Precision::Confusion();
51
52   Standard_Integer NU = W1.Length();
53   Standard_Integer NV = W2.Length();
54
55   Standard_Real A = ( W1( 1) * W2( 1)) / ( W1( NU) * W2( NV));
56   Standard_Real B = ( W3( 1) * W4( 1)) / ( W3( NU) * W4( NV));
57
58   Standard_Integer i;
59   Standard_Real Alfa = W1( NU) / W2( 1);
60   for ( i=1; i<=NV; i++) {
61     W2(i) *= Alfa;
62   }
63   Standard_Real Beta = W2( NV) / W3( NU);
64   for ( i=1; i<=NU; i++) {
65     W3(i) *= Beta;
66   }
67   Standard_Real Gamma = W3( 1) / W4( NV);
68   for ( i=1; i<=NV; i++) {
69     W4(i) *= Gamma;
70   }
71   
72   if ( Abs(A-B) > Eps) {
73     Standard_Real w = Pow( W1(1)/W4(1), 1./(Standard_Real)(NV-1));
74     Standard_Real x = w;
75     for ( i=NV-1; i>=1; i--) {
76       W4(i) *= x;
77       x *= w;
78     }
79   }
80 }
81
82
83 //=======================================================================
84 //function : Arrange
85 //purpose  : Internal Use Only
86 //           This function is used to prepare the Filling: The Curves
87 //           are arranged in this way:
88 //
89 //                      CC3
90 //                  ----->-----
91 //                 |           |
92 //                 |           |
93 //                 |           |
94 //             CC4 ^           ^ CC2
95 //                 |           |
96 //                 |           |
97 //                  ----->-----
98 //                   CC1 = C1
99 //=======================================================================
100
101 static Standard_Boolean Arrange(const Handle(Geom_BezierCurve)& C1,
102                          const Handle(Geom_BezierCurve)& C2,
103                          const Handle(Geom_BezierCurve)& C3,
104                          const Handle(Geom_BezierCurve)& C4,
105                                Handle(Geom_BezierCurve)& CC1,
106                                Handle(Geom_BezierCurve)& CC2,
107                                Handle(Geom_BezierCurve)& CC3,
108                                Handle(Geom_BezierCurve)& CC4,
109                          const Standard_Real Tol             )
110 {
111   Handle(Geom_BezierCurve) GC[4];
112   Handle(Geom_BezierCurve) Dummy;
113   GC[0] = Handle(Geom_BezierCurve)::DownCast(C1->Copy());
114   GC[1] = Handle(Geom_BezierCurve)::DownCast(C2->Copy());
115   GC[2] = Handle(Geom_BezierCurve)::DownCast(C3->Copy());
116   GC[3] = Handle(Geom_BezierCurve)::DownCast(C4->Copy());
117   
118   Standard_Integer i,j;
119   Standard_Boolean Trouve;
120
121   for (i=1; i<=3; i++) {
122     Trouve = Standard_False;
123     for ( j=i; j<=3 && !Trouve; j++) {
124       if (GC[j]->StartPoint().Distance( GC[i-1]->EndPoint()) < Tol) {
125         Dummy  = GC[i];
126         GC[i]  = GC[j];
127         GC[j]  = Dummy;
128         Trouve = Standard_True;
129       }
130       else if (GC[j]->EndPoint().Distance( GC[i-1]->EndPoint()) < Tol) {
131         GC[j]  = Handle(Geom_BezierCurve)::DownCast(GC[j]->Reversed());
132         Dummy  = GC[i];
133         GC[i]  = GC[j];
134         GC[j]  = Dummy;
135         Trouve = Standard_True;
136       }
137     }
138     if (!Trouve) return Standard_False;
139   }
140
141   CC1 = GC[0];
142   CC2 = GC[1];
143   CC3 = Handle(Geom_BezierCurve)::DownCast( GC[2]->Reversed());
144   CC4 = Handle(Geom_BezierCurve)::DownCast( GC[3]->Reversed());
145
146   return Standard_True;
147 }
148
149
150 //=======================================================================
151 //function : GeomFill_BezierCurves
152 //purpose  : 
153 //=======================================================================
154
155 GeomFill_BezierCurves::GeomFill_BezierCurves()
156 {
157 }
158
159
160 //=======================================================================
161 //function : GeomFill_BezierCurves
162 //purpose  : 
163 //=======================================================================
164
165 GeomFill_BezierCurves::GeomFill_BezierCurves(const Handle(Geom_BezierCurve)& C1, 
166                                        const Handle(Geom_BezierCurve)& C2, 
167                                        const Handle(Geom_BezierCurve)& C3, 
168                                        const Handle(Geom_BezierCurve)& C4, 
169                                        const GeomFill_FillingStyle Type      )
170 {
171   Init( C1, C2, C3, C4, Type);
172 }
173
174
175 //=======================================================================
176 //function : GeomFill_BezierCurves
177 //purpose  : 
178 //=======================================================================
179
180 GeomFill_BezierCurves::GeomFill_BezierCurves(const Handle(Geom_BezierCurve)& C1, 
181                                        const Handle(Geom_BezierCurve)& C2, 
182                                        const Handle(Geom_BezierCurve)& C3, 
183                                        const GeomFill_FillingStyle Type      )
184 {
185   Init( C1, C2, C3, Type);
186 }
187
188
189 //=======================================================================
190 //function : GeomFill_BezierCurves
191 //purpose  : 
192 //=======================================================================
193
194 GeomFill_BezierCurves::GeomFill_BezierCurves(const Handle(Geom_BezierCurve)& C1, 
195                                        const Handle(Geom_BezierCurve)& C2,
196                                        const GeomFill_FillingStyle Type    )
197 {
198   Init( C1, C2, Type);
199 }
200
201
202 //=======================================================================
203 //function : Init
204 //purpose  : 
205 //=======================================================================
206
207 void  GeomFill_BezierCurves::Init(const Handle(Geom_BezierCurve)& C1, 
208                                const Handle(Geom_BezierCurve)& C2, 
209                                const Handle(Geom_BezierCurve)& C3, 
210                                const Handle(Geom_BezierCurve)& C4, 
211                                const GeomFill_FillingStyle Type      )
212 {
213   // On ordonne les courbes
214   Handle(Geom_BezierCurve) CC1, CC2, CC3, CC4;
215   
216   Standard_Real Tol = Precision::Confusion();
217 #ifndef No_Exception
218   Standard_Boolean IsOK =
219 #endif
220     Arrange( C1, C2, C3, C4, CC1, CC2, CC3, CC4, Tol);
221   
222   Standard_ConstructionError_Raise_if 
223     (!IsOK, " GeomFill_BezierCurves: Courbes non jointives");
224
225   // Mise en conformite des degres
226   Standard_Integer DegU = Max( CC1->Degree(), CC3->Degree());
227   Standard_Integer DegV = Max( CC2->Degree(), CC4->Degree());
228
229   if (Type == GeomFill_CoonsStyle) {
230     DegU = Max( DegU, 3);
231     DegV = Max( DegV, 3);
232   }
233
234   if ( CC1->Degree() < DegU )  CC1->Increase(DegU);
235   if ( CC2->Degree() < DegV )  CC2->Increase(DegV);
236   if ( CC3->Degree() < DegU )  CC3->Increase(DegU);
237   if ( CC4->Degree() < DegV )  CC4->Increase(DegV);
238
239   TColgp_Array1OfPnt P1(1,DegU+1);
240   TColgp_Array1OfPnt P3(1,DegU+1);
241   TColgp_Array1OfPnt P2(1,DegV+1);
242   TColgp_Array1OfPnt P4(1,DegV+1);
243   CC1->Poles(P1);
244   CC2->Poles(P2);
245   CC3->Poles(P3);
246   CC4->Poles(P4);
247  
248   // Traitement des courbes rationelles
249   Standard_Boolean isRat = ( CC1->IsRational() || CC2->IsRational() ||
250                              CC3->IsRational() || CC4->IsRational()   );
251
252   TColStd_Array1OfReal W1(1,DegU+1);
253   TColStd_Array1OfReal W3(1,DegU+1);
254   TColStd_Array1OfReal W2(1,DegV+1);
255   TColStd_Array1OfReal W4(1,DegV+1);
256   W1.Init(1.);
257   W2.Init(1.);
258   W3.Init(1.);
259   W4.Init(1.);
260
261   if ( isRat) {
262     if (CC1->IsRational()) {
263       CC1->Weights(W1);
264     }
265     if (CC2->IsRational()) {
266       CC2->Weights(W2);
267     }
268     if (CC3->IsRational()) {
269       CC3->Weights(W3);
270     }
271     if (CC4->IsRational()) {
272       CC4->Weights(W4);
273     }
274   }
275   
276   GeomFill_Filling Caro;
277   if (isRat) {
278     // Mise en conformite des poids aux coins.
279     SetSameWeights( W1, W2, W3, W4);
280     switch (Type)
281       {
282       case GeomFill_StretchStyle :
283         Caro = GeomFill_Stretch( P1, P2, P3, P4, W1, W2, W3, W4); 
284         break;
285       case GeomFill_CoonsStyle   :
286         Caro = GeomFill_Coons  ( P1, P4, P3, P2, W1, W4, W3, W2); 
287         break;
288       case GeomFill_CurvedStyle  :
289         Caro = GeomFill_Curved ( P1, P2, P3, P4, W1, W2, W3, W4); 
290         break;
291       }
292   }
293   else {
294     switch (Type) 
295       {
296       case GeomFill_StretchStyle :
297         Caro = GeomFill_Stretch( P1, P2, P3, P4); 
298         break;
299       case GeomFill_CoonsStyle   :
300         Caro = GeomFill_Coons  ( P1, P4, P3, P2); 
301         break;
302       case GeomFill_CurvedStyle  :
303         Caro = GeomFill_Curved ( P1, P2, P3, P4); 
304         break;
305       }
306   }
307   
308   Standard_Integer NbUPoles = Caro.NbUPoles();
309   Standard_Integer NbVPoles = Caro.NbVPoles();
310   TColgp_Array2OfPnt Poles(1,NbUPoles,1,NbVPoles);
311   
312   Caro.Poles(Poles);
313   
314   if (Caro.isRational()) {
315     TColStd_Array2OfReal Weights(1,NbUPoles, 1,NbVPoles);
316     Caro.Weights(Weights);
317     mySurface = new Geom_BezierSurface(Poles,Weights);
318   }
319   else {
320     mySurface = new Geom_BezierSurface(Poles);
321   }
322 }
323
324
325 //=======================================================================
326 //function : Init
327 //purpose  : 
328 //=======================================================================
329
330 void  GeomFill_BezierCurves::Init(const Handle(Geom_BezierCurve)& C1, 
331                                const Handle(Geom_BezierCurve)& C2, 
332                                const Handle(Geom_BezierCurve)& C3, 
333                                const GeomFill_FillingStyle Type      )
334 {
335   Handle(Geom_BezierCurve) C4;
336   TColgp_Array1OfPnt Poles(1,2);
337   Standard_Real Tol = Precision::Confusion();
338   Tol = Tol * Tol; 
339   if(C1->StartPoint().SquareDistance(C2->StartPoint()) > Tol &&
340      C1->StartPoint().SquareDistance(C2->EndPoint()) > Tol )
341     Poles( 1) = C1->StartPoint();
342   else 
343     Poles( 1) = C1->EndPoint();
344
345   if(C3->StartPoint().SquareDistance(C2->StartPoint()) > Tol &&
346      C3->StartPoint().SquareDistance(C2->EndPoint()) > Tol )
347     Poles( 2) = C3->StartPoint();
348   else 
349     Poles( 2) = C3->EndPoint();
350 //  Poles(1) = C1->StartPoint();
351 //  Poles(2) = C1->StartPoint();
352   C4 = new Geom_BezierCurve(Poles);
353   Init( C1, C2, C3, C4, Type);
354 }
355
356
357 //=======================================================================
358 //function : Init
359 //purpose  : 
360 //=======================================================================
361
362 void  GeomFill_BezierCurves::Init(const Handle(Geom_BezierCurve)& C1, 
363                                const Handle(Geom_BezierCurve)& C2,
364                                const GeomFill_FillingStyle Type     )
365 {
366   Handle(Geom_BezierCurve) 
367     CC1 = Handle(Geom_BezierCurve)::DownCast(C1->Copy());
368   Handle(Geom_BezierCurve) 
369     CC2 = Handle(Geom_BezierCurve)::DownCast(C2->Copy());
370
371   Standard_Integer Deg1 = CC1->Degree();
372   Standard_Integer Deg2 = CC2->Degree();
373
374   Standard_Boolean isRat = ( CC1->IsRational() || CC2->IsRational());
375     
376   if ( Type != GeomFill_CurvedStyle) {
377     Standard_Integer DegU = Max( Deg1, Deg2);
378     
379     if ( CC1->Degree() < DegU )  CC1->Increase(DegU);
380     if ( CC2->Degree() < DegU )  CC2->Increase(DegU);
381     
382     TColgp_Array2OfPnt Poles( 1, DegU+1, 1, 2);
383     TColgp_Array1OfPnt P1(1,DegU+1);
384     TColgp_Array1OfPnt P2(1,DegU+1);
385     CC1->Poles(P1);
386     CC2->Poles(P2);
387     
388     Standard_Integer i;
389     for (i=1; i<=DegU+1; i++) {
390       Poles(i, 1) = P1(i);
391       Poles(i, 2) = P2(i);
392     }
393     if (isRat) {
394       TColStd_Array1OfReal W1(1,DegU+1);
395       TColStd_Array1OfReal W2(1,DegU+1);
396       W1.Init(1.);
397       W2.Init(1.);
398
399       if (CC1->IsRational()) {
400         CC1->Weights(W1);
401       }
402       if (CC2->IsRational()) {
403         CC2->Weights(W2);
404       }
405       TColStd_Array2OfReal Weights(1,DegU+1, 1,2);
406       for ( i=1; i<=DegU+1; i++) {
407         Weights(i, 1) = W1(i);
408         Weights(i, 2) = W2(i);
409       }
410       mySurface = new Geom_BezierSurface(Poles,Weights);
411     }
412     else {
413       mySurface = new Geom_BezierSurface(Poles);
414     }
415   }
416   else {
417     TColgp_Array1OfPnt P1(1,Deg1+1);
418     TColgp_Array1OfPnt P2(1,Deg2+1);
419     
420     Standard_Real Eps = Precision::Confusion();
421     Standard_Boolean IsOK = Standard_False;
422     if ( CC1->StartPoint().IsEqual(CC2->StartPoint(),Eps)) {
423       IsOK = Standard_True;
424     }
425     else if ( CC1->StartPoint().IsEqual(CC2->EndPoint(),Eps)) {
426       CC2->Reverse();
427       IsOK = Standard_True;
428     }
429     else if ( CC1->EndPoint().IsEqual(CC2->StartPoint(),Eps)) {
430       C1->Reverse();
431       IsOK = Standard_True;
432     }
433     else if ( CC1->EndPoint().IsEqual(CC2->EndPoint(),Eps)) {
434       CC1->Reverse();
435       CC2->Reverse();
436       IsOK = Standard_True;
437     }
438
439     if(!IsOK)
440       throw Standard_OutOfRange("GeomFill_BezierCurves: Courbes non jointives");
441
442     CC1->Poles(P1);
443     CC2->Poles(P2);
444
445     TColStd_Array1OfReal W1(1,Deg1+1);
446     TColStd_Array1OfReal W2(1,Deg2+1);
447     W1.Init(1.);
448     W2.Init(1.);
449
450     GeomFill_Filling Caro;
451     if ( isRat) {
452       if (CC1->IsRational()) {
453         CC1->Weights(W1);
454       }
455       if (CC2->IsRational()) {
456         CC2->Weights(W2);
457       }
458       Caro = GeomFill_Curved( P1, P2, W1, W2);
459     }
460     else {
461       Caro = GeomFill_Curved( P1, P2);
462     }
463     
464     Standard_Integer NbUPoles = Caro.NbUPoles();
465     Standard_Integer NbVPoles = Caro.NbVPoles();
466     TColgp_Array2OfPnt Poles(1,NbUPoles,1,NbVPoles);
467     
468     Caro.Poles(Poles);
469     
470     if (Caro.isRational()) {
471       TColStd_Array2OfReal Weights(1,NbUPoles, 1,NbVPoles);
472       Caro.Weights(Weights);
473       mySurface = new Geom_BezierSurface(Poles,Weights);
474     }
475     else {
476       mySurface = new Geom_BezierSurface(Poles);
477     }
478   }
479 }