0022312: Translation of french commentaries in OCCT files
[occt.git] / src / Convert / Convert_TorusToBSplineSurface.cxx
1 //File Convert_TorusToBSplineSurface.cxx
2 //JCV 16/10/91
3
4 #include <Convert_TorusToBSplineSurface.ixx>
5
6 #include <gp.hxx>
7 #include <gp_Trsf.hxx>
8
9 static const Standard_Integer TheUDegree  = 2;
10 static const Standard_Integer TheVDegree  = 2;
11 static const Standard_Integer MaxNbUKnots = 4;
12 static const Standard_Integer MaxNbVKnots = 4;
13 static const Standard_Integer MaxNbUPoles = 7;
14 static const Standard_Integer MaxNbVPoles = 7;
15
16
17 static void ComputePoles ( const Standard_Real R,
18                            const Standard_Real r,
19                            const Standard_Real U1,
20                            const Standard_Real U2,
21                            const Standard_Real V1,
22                            const Standard_Real V2,
23                                  TColgp_Array2OfPnt& Poles)
24 {
25   Standard_Real deltaU = U2 - U1;
26   Standard_Real deltaV = V2 - V1;
27
28   Standard_Integer i, j;
29
30   // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
31   Standard_Integer 
32     nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / PI) + 1;
33   Standard_Integer  
34     nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / PI) + 1;
35   Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
36   Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
37
38   Standard_Integer nbVP = 2 * nbVSpans + 1;
39
40   Standard_Real x[MaxNbVPoles];
41   Standard_Real z[MaxNbVPoles];
42   
43   x[0] = R + r * Cos( V1);
44   z[0] =     r * Sin( V1);
45
46   Standard_Real VStart = V1;
47   for ( i = 1; i <= nbVSpans; i++) {
48     x[2*i-1] = R + r * Cos( VStart + AlfaV) / Cos( AlfaV);
49     z[2*i-1] =     r * Sin( VStart + AlfaV) / Cos( AlfaV);
50     x[2*i]   = R + r * Cos( VStart + 2 * AlfaV);
51     z[2*i]   =     r * Sin( VStart + 2 * AlfaV);
52     VStart += 2*AlfaV;
53   }
54
55   Standard_Real UStart = U1;
56   for ( j = 0; j <= nbVP-1; j++) {
57     Poles( 1, j+1) = gp_Pnt(x[j]*Cos(UStart),x[j]*Sin(UStart),z[j]);
58   }
59
60   for ( i = 1; i <= nbUSpans; i++) {
61     for ( j = 0; j<= nbVP-1; j++) {
62       Poles( 2*i, j+1) = gp_Pnt( x[j] * Cos(UStart+AlfaU) / Cos(AlfaU),
63                                  x[j] * Sin(UStart+AlfaU) / Cos(AlfaU),
64                                  z[j] );
65       Poles(2*i+1,j+1) = gp_Pnt( x[j] * Cos(UStart+2*AlfaU),
66                                  x[j] * Sin(UStart+2*AlfaU),
67                                  z[j] );
68     }
69     UStart += 2*AlfaU;
70   }
71 }
72
73
74 //=======================================================================
75 //function : Convert_TorusToBSplineSurface
76 //purpose  : 
77 //=======================================================================
78
79 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
80   (const gp_Torus& T, 
81    const Standard_Real U1,
82    const Standard_Real U2,
83    const Standard_Real V1,
84    const Standard_Real V2)
85 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles, 
86                                              MaxNbUKnots, MaxNbVKnots,
87                                              TheUDegree , TheVDegree)
88 {
89   Standard_Real deltaU = U2 - U1;
90   Standard_Real deltaV = V2 - V1;
91   Standard_DomainError_Raise_if( (deltaU>2*PI) || (deltaU<0.) ||
92                                  (deltaV>2*PI) || (deltaV<0.),
93                                 "Convert_TorusToBSplineSurface");
94
95   isuperiodic = Standard_False;
96   isvperiodic = Standard_False;
97
98   Standard_Integer i,j;
99   // construction of the torus in the reference mark xOy.
100
101   // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
102   Standard_Integer 
103     nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / PI) + 1;
104   Standard_Integer  
105     nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / PI) + 1;
106   Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
107   Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
108
109   nbUPoles = 2 * nbUSpans + 1;
110   nbVPoles = 2 * nbVSpans + 1;
111   nbUKnots = nbUSpans + 1;
112   nbVKnots = nbVSpans + 1;
113
114   Standard_Real R = T.MajorRadius();
115   Standard_Real r = T.MinorRadius();
116
117   ComputePoles( R, r, U1, U2, V1, V2, poles); 
118   
119   for ( i = 1; i<= nbUKnots; i++) {
120     uknots(i) = U1 + (i-1) * 2 * AlfaU;
121     umults(i) = 2;
122   }
123   umults(1)++; umults(nbUKnots)++;
124   for ( i = 1; i<= nbVKnots; i++) {
125     vknots(i) = V1 + (i-1) * 2 * AlfaV;
126     vmults(i) = 2;
127   }
128   vmults(1)++; vmults(nbVKnots)++;
129
130
131   // Replace the bspline in the reference of the torus.
132   // and calculate the weight of the bspline.
133   Standard_Real W1, W2;
134   gp_Trsf Trsf;
135   Trsf.SetTransformation( T.Position(), gp::XOY());
136
137   for ( i = 1; i <= nbUPoles; i++) {
138     if ( i % 2 == 0)  W1 = Cos(AlfaU);
139     else              W1 = 1.;
140
141     for ( j = 1; j <= nbVPoles; j++) {
142       if ( j % 2 == 0)  W2 = Cos(AlfaV);
143       else              W2 = 1.;
144
145       weights( i, j) = W1 * W2;
146       poles( i, j).Transform( Trsf);
147     }
148   }
149 }
150
151
152 //=======================================================================
153 //function : Convert_TorusToBSplineSurface
154 //purpose  : 
155 //=======================================================================
156
157 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
158   (const gp_Torus& T, 
159    const Standard_Real    Param1,
160    const Standard_Real    Param2,
161    const Standard_Boolean UTrim  )
162 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles,
163                                              MaxNbUKnots, MaxNbVKnots,
164                                              TheUDegree , TheVDegree)
165 {
166 #ifndef No_Exception
167   Standard_Real delta = Param2 - Param1;
168 #endif
169   Standard_DomainError_Raise_if( (delta>2*PI) || (delta<0.),
170                                 "Convert_TorusToBSplineSurface");
171
172   Standard_Integer i, j;
173   Standard_Real deltaU, deltaV;
174
175   isuperiodic = !UTrim;
176   isvperiodic =  UTrim;
177
178   Standard_Real R = T.MajorRadius();
179   Standard_Real r = T.MinorRadius();
180   
181   Standard_Real W1, W2, CosU, CosV;
182   
183   if ( isuperiodic) {
184     ComputePoles(R, r, 0, 2.*PI, Param1, Param2, poles);
185     
186     nbUPoles = 6;
187     nbUKnots = 4;
188     
189     deltaV = Param2 - Param1;
190     Standard_Integer  
191       nbVSpans = (Standard_Integer)IntegerPart( 1.2 * deltaV / PI) + 1;
192     Standard_Real AlfaV = deltaV / ( nbVSpans * 2);
193     nbVPoles = 2 * nbVSpans + 1;
194     nbVKnots = nbVSpans + 1;
195     
196     for ( i = 1; i <= nbUKnots; i++) {
197       uknots(i) = ( i-1) * 2. * PI /3.;
198       umults(i) = 2;
199     }
200     for ( i = 1; i <= nbVKnots; i++) {
201       vknots(i) = Param1 + (i-1) * 2 * AlfaV;
202       vmults(i) = 2;
203     }
204     vmults(1)++; vmults(nbVKnots)++;
205     
206     CosU = 0.5;       // = Cos(pi /3)
207     CosV = Cos(AlfaV);
208   }
209   else {
210     ComputePoles(R, r, Param1, Param2, 0., 2.*PI, poles);
211     
212     nbVPoles = 6;
213     nbVKnots = 4;
214     
215     deltaU = Param2 - Param1;
216     Standard_Integer  
217       nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / PI) + 1;
218     Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
219     nbUPoles = 2 * nbUSpans + 1;
220     nbUKnots = nbUSpans + 1;
221     
222     for ( i = 1; i <= nbVKnots; i++) {
223       vknots(i) = ( i-1) * 2. * PI /3.;
224       vmults(i) = 2;
225     }
226     for ( i = 1; i <= nbUKnots; i++) {
227       uknots(i) = Param1 + (i-1) * 2 * AlfaU;
228       umults(i) = 2;
229     }
230     umults(1)++; umults(nbUKnots)++;
231
232     CosV = 0.5;       // = Cos(pi /3)
233     CosU = Cos(AlfaU);
234   }
235
236   // Replace the bspline in the reference of the torus.
237   // and calculate the weight of the bspline.
238   gp_Trsf Trsf;
239   Trsf.SetTransformation( T.Position(), gp::XOY());
240
241   for ( i = 1; i <= nbUPoles; i++) {
242     if ( i % 2 == 0)  W1 = CosU;
243     else              W1 = 1.;
244
245     for ( j = 1; j <= nbVPoles; j++) {
246       if ( j % 2 == 0)  W2 = CosV;
247       else              W2 = 1.;
248
249       weights( i, j) = W1 * W2;
250       poles( i, j).Transform( Trsf);
251     }
252   }
253 }
254
255
256
257 //=======================================================================
258 //function : Convert_TorusToBSplineSurface
259 //purpose  : 
260 //=======================================================================
261
262 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface 
263   (const gp_Torus& T )
264 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles, 
265                                              MaxNbUKnots, MaxNbVKnots,
266                                              TheUDegree , TheVDegree  )
267 {
268   isuperiodic = Standard_True;
269   isvperiodic = Standard_True;
270
271   Standard_Real W1, W2;
272   Standard_Integer i, j;
273
274   nbUPoles = 6;
275   nbVPoles = 6;
276   nbUKnots = 4;
277   nbVKnots = 4;
278
279   // Construction of the Torus in the reference mark xOy.
280   
281   Standard_Real R = T.MajorRadius();
282   Standard_Real r = T.MinorRadius();
283
284   ComputePoles( R, r, 0., 2.*PI, 0., 2.*PI, poles);
285
286   uknots( 1) = vknots( 1) = 0.;
287   uknots( 2) = vknots( 2) = 2. * PI / 3.;
288   uknots( 3) = vknots( 3) = 4. * PI / 3.;
289   uknots( 4) = vknots( 4) = 2. * PI;
290   for ( i = 1; i <= 4; i++) {
291     umults( i) = vmults( i) = 2;
292   }
293
294   // Replace the bspline in the mark of the torus.
295   // and calculate the weight of the bspline.
296   gp_Trsf Trsf;
297   Trsf.SetTransformation( T.Position(), gp::XOY());
298
299   for ( i = 1; i <= nbUPoles; i++) {
300     if ( i % 2 == 0)  W1 = 0.5;
301     else              W1 = 1.;
302
303     for ( j = 1; j <= nbVPoles; j++) {
304       if ( j % 2 == 0)  W2 = 0.5;
305       else              W2 = 1.;
306
307       weights( i, j) = W1 * W2;
308       poles( i, j).Transform( Trsf);
309     }
310   }
311 }