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