0024059: Eliminate compiler warning C4701 in MSVC++ with warning level 4
[occt.git] / src / Convert / Convert_TorusToBSplineSurface.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 //JCV 16/10/91
20
21 #include <Convert_TorusToBSplineSurface.ixx>
22
23 #include <gp.hxx>
24 #include <gp_Trsf.hxx>
25
26 static const Standard_Integer TheUDegree  = 2;
27 static const Standard_Integer TheVDegree  = 2;
28 static const Standard_Integer MaxNbUKnots = 4;
29 static const Standard_Integer MaxNbVKnots = 4;
30 static const Standard_Integer MaxNbUPoles = 7;
31 static const Standard_Integer MaxNbVPoles = 7;
32
33
34 static void ComputePoles ( const Standard_Real R,
35                            const Standard_Real r,
36                            const Standard_Real U1,
37                            const Standard_Real U2,
38                            const Standard_Real V1,
39                            const Standard_Real V2,
40                                  TColgp_Array2OfPnt& Poles)
41 {
42   Standard_Real deltaU = U2 - U1;
43   Standard_Real deltaV = V2 - V1;
44
45   Standard_Integer i, j;
46
47   // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
48   Standard_Integer 
49     nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
50   Standard_Integer  
51     nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / M_PI) + 1;
52   Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
53   Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
54
55   Standard_Integer nbVP = 2 * nbVSpans + 1;
56
57   Standard_Real x[MaxNbVPoles];
58   Standard_Real z[MaxNbVPoles];
59   
60   x[0] = R + r * Cos( V1);
61   z[0] =     r * Sin( V1);
62
63   Standard_Real VStart = V1;
64   for ( i = 1; i <= nbVSpans; i++) {
65     x[2*i-1] = R + r * Cos( VStart + AlfaV) / Cos( AlfaV);
66     z[2*i-1] =     r * Sin( VStart + AlfaV) / Cos( AlfaV);
67     x[2*i]   = R + r * Cos( VStart + 2 * AlfaV);
68     z[2*i]   =     r * Sin( VStart + 2 * AlfaV);
69     VStart += 2*AlfaV;
70   }
71
72   Standard_Real UStart = U1;
73   for ( j = 0; j <= nbVP-1; j++) {
74     Poles( 1, j+1) = gp_Pnt(x[j]*Cos(UStart),x[j]*Sin(UStart),z[j]);
75   }
76
77   for ( i = 1; i <= nbUSpans; i++) {
78     for ( j = 0; j<= nbVP-1; j++) {
79       Poles( 2*i, j+1) = gp_Pnt( x[j] * Cos(UStart+AlfaU) / Cos(AlfaU),
80                                  x[j] * Sin(UStart+AlfaU) / Cos(AlfaU),
81                                  z[j] );
82       Poles(2*i+1,j+1) = gp_Pnt( x[j] * Cos(UStart+2*AlfaU),
83                                  x[j] * Sin(UStart+2*AlfaU),
84                                  z[j] );
85     }
86     UStart += 2*AlfaU;
87   }
88 }
89
90
91 //=======================================================================
92 //function : Convert_TorusToBSplineSurface
93 //purpose  : 
94 //=======================================================================
95
96 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
97   (const gp_Torus& T, 
98    const Standard_Real U1,
99    const Standard_Real U2,
100    const Standard_Real V1,
101    const Standard_Real V2)
102 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles, 
103                                              MaxNbUKnots, MaxNbVKnots,
104                                              TheUDegree , TheVDegree)
105 {
106   Standard_Real deltaU = U2 - U1;
107   Standard_Real deltaV = V2 - V1;
108   Standard_DomainError_Raise_if( (deltaU>2*M_PI) || (deltaU<0.) ||
109                                  (deltaV>2*M_PI) || (deltaV<0.),
110                                 "Convert_TorusToBSplineSurface");
111
112   isuperiodic = Standard_False;
113   isvperiodic = Standard_False;
114
115   Standard_Integer i,j;
116   // construction of the torus in the reference mark xOy.
117
118   // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
119   Standard_Integer 
120     nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
121   Standard_Integer  
122     nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / M_PI) + 1;
123   Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
124   Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
125
126   nbUPoles = 2 * nbUSpans + 1;
127   nbVPoles = 2 * nbVSpans + 1;
128   nbUKnots = nbUSpans + 1;
129   nbVKnots = nbVSpans + 1;
130
131   Standard_Real R = T.MajorRadius();
132   Standard_Real r = T.MinorRadius();
133
134   ComputePoles( R, r, U1, U2, V1, V2, poles); 
135   
136   for ( i = 1; i<= nbUKnots; i++) {
137     uknots(i) = U1 + (i-1) * 2 * AlfaU;
138     umults(i) = 2;
139   }
140   umults(1)++; umults(nbUKnots)++;
141   for ( i = 1; i<= nbVKnots; i++) {
142     vknots(i) = V1 + (i-1) * 2 * AlfaV;
143     vmults(i) = 2;
144   }
145   vmults(1)++; vmults(nbVKnots)++;
146
147
148   // Replace the bspline in the reference of the torus.
149   // and calculate the weight of the bspline.
150   Standard_Real W1, W2;
151   gp_Trsf Trsf;
152   Trsf.SetTransformation( T.Position(), gp::XOY());
153
154   for ( i = 1; i <= nbUPoles; i++) {
155     if ( i % 2 == 0)  W1 = Cos(AlfaU);
156     else              W1 = 1.;
157
158     for ( j = 1; j <= nbVPoles; j++) {
159       if ( j % 2 == 0)  W2 = Cos(AlfaV);
160       else              W2 = 1.;
161
162       weights( i, j) = W1 * W2;
163       poles( i, j).Transform( Trsf);
164     }
165   }
166 }
167
168
169 //=======================================================================
170 //function : Convert_TorusToBSplineSurface
171 //purpose  : 
172 //=======================================================================
173
174 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
175   (const gp_Torus& T, 
176    const Standard_Real    Param1,
177    const Standard_Real    Param2,
178    const Standard_Boolean UTrim  )
179 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles,
180                                              MaxNbUKnots, MaxNbVKnots,
181                                              TheUDegree , TheVDegree)
182 {
183 #ifndef No_Exception
184   Standard_Real delta = Param2 - Param1;
185 #endif
186   Standard_DomainError_Raise_if( (delta>2*M_PI) || (delta<0.),
187                                 "Convert_TorusToBSplineSurface");
188
189   Standard_Integer i, j;
190   Standard_Real deltaU, deltaV;
191
192   isuperiodic = !UTrim;
193   isvperiodic =  UTrim;
194
195   Standard_Real R = T.MajorRadius();
196   Standard_Real r = T.MinorRadius();
197   
198   Standard_Real W1, W2, CosU, CosV;
199   
200   if ( isuperiodic) {
201     ComputePoles(R, r, 0, 2.*M_PI, Param1, Param2, poles);
202     
203     nbUPoles = 6;
204     nbUKnots = 4;
205     
206     deltaV = Param2 - Param1;
207     Standard_Integer  
208       nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / M_PI) + 1;
209     Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
210     nbVPoles = 2 * nbVSpans + 1;
211     nbVKnots = nbVSpans + 1;
212     
213     for ( i = 1; i <= nbUKnots; i++) {
214       uknots(i) = ( i-1) * 2. * M_PI /3.;
215       umults(i) = 2;
216     }
217     for ( i = 1; i <= nbVKnots; i++) {
218       vknots(i) = Param1 + (i-1) * 2 * AlfaV;
219       vmults(i) = 2;
220     }
221     vmults(1)++; vmults(nbVKnots)++;
222     
223     CosU = 0.5;       // = Cos(pi /3)
224     CosV = Cos(AlfaV);
225   }
226   else {
227     ComputePoles(R, r, Param1, Param2, 0., 2.*M_PI, poles);
228     
229     nbVPoles = 6;
230     nbVKnots = 4;
231     
232     deltaU = Param2 - Param1;
233     Standard_Integer  
234       nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
235     Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
236     nbUPoles = 2 * nbUSpans + 1;
237     nbUKnots = nbUSpans + 1;
238     
239     for ( i = 1; i <= nbVKnots; i++) {
240       vknots(i) = ( i-1) * 2. * M_PI /3.;
241       vmults(i) = 2;
242     }
243     for ( i = 1; i <= nbUKnots; i++) {
244       uknots(i) = Param1 + (i-1) * 2 * AlfaU;
245       umults(i) = 2;
246     }
247     umults(1)++; umults(nbUKnots)++;
248
249     CosV = 0.5;       // = Cos(pi /3)
250     CosU = Cos(AlfaU);
251   }
252
253   // Replace the bspline in the reference of the torus.
254   // and calculate the weight of the bspline.
255   gp_Trsf Trsf;
256   Trsf.SetTransformation( T.Position(), gp::XOY());
257
258   for ( i = 1; i <= nbUPoles; i++) {
259     if ( i % 2 == 0)  W1 = CosU;
260     else              W1 = 1.;
261
262     for ( j = 1; j <= nbVPoles; j++) {
263       if ( j % 2 == 0)  W2 = CosV;
264       else              W2 = 1.;
265
266       weights( i, j) = W1 * W2;
267       poles( i, j).Transform( Trsf);
268     }
269   }
270 }
271
272
273
274 //=======================================================================
275 //function : Convert_TorusToBSplineSurface
276 //purpose  : 
277 //=======================================================================
278
279 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
280   (const gp_Torus& T )
281 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles, 
282                                              MaxNbUKnots, MaxNbVKnots,
283                                              TheUDegree , TheVDegree  )
284 {
285   isuperiodic = Standard_True;
286   isvperiodic = Standard_True;
287
288   Standard_Real W1, W2;
289   Standard_Integer i, j;
290
291   nbUPoles = 6;
292   nbVPoles = 6;
293   nbUKnots = 4;
294   nbVKnots = 4;
295
296   // Construction of the Torus in the reference mark xOy.
297   
298   Standard_Real R = T.MajorRadius();
299   Standard_Real r = T.MinorRadius();
300
301   ComputePoles( R, r, 0., 2.*M_PI, 0., 2.*M_PI, poles);
302
303   uknots( 1) = vknots( 1) = 0.;
304   uknots( 2) = vknots( 2) = 2. * M_PI / 3.;
305   uknots( 3) = vknots( 3) = 4. * M_PI / 3.;
306   uknots( 4) = vknots( 4) = 2. * M_PI;
307   for ( i = 1; i <= 4; i++) {
308     umults( i) = vmults( i) = 2;
309   }
310
311   // Replace the bspline in the mark of the torus.
312   // and calculate the weight of the bspline.
313   gp_Trsf Trsf;
314   Trsf.SetTransformation( T.Position(), gp::XOY());
315
316   for ( i = 1; i <= nbUPoles; i++) {
317     if ( i % 2 == 0)  W1 = 0.5;
318     else              W1 = 1.;
319
320     for ( j = 1; j <= nbVPoles; j++) {
321       if ( j % 2 == 0)  W2 = 0.5;
322       else              W2 = 1.;
323
324       weights( i, j) = W1 * W2;
325       poles( i, j).Transform( Trsf);
326     }
327   }
328 }