1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <Convert_TorusToBSplineSurface.ixx>
24 #include <gp_Trsf.hxx>
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;
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)
42 Standard_Real deltaU = U2 - U1;
43 Standard_Real deltaV = V2 - V1;
45 Standard_Integer i, j;
47 // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
49 nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
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);
55 Standard_Integer nbVP = 2 * nbVSpans + 1;
57 Standard_Real x[MaxNbVPoles];
58 Standard_Real z[MaxNbVPoles];
60 x[0] = R + r * Cos( V1);
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);
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]);
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),
82 Poles(2*i+1,j+1) = gp_Pnt( x[j] * Cos(UStart+2*AlfaU),
83 x[j] * Sin(UStart+2*AlfaU),
91 //=======================================================================
92 //function : Convert_TorusToBSplineSurface
94 //=======================================================================
96 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface
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)
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");
112 isuperiodic = Standard_False;
113 isvperiodic = Standard_False;
115 Standard_Integer i,j;
116 // construction of the torus in the reference mark xOy.
118 // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
120 nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
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);
126 nbUPoles = 2 * nbUSpans + 1;
127 nbVPoles = 2 * nbVSpans + 1;
128 nbUKnots = nbUSpans + 1;
129 nbVKnots = nbVSpans + 1;
131 Standard_Real R = T.MajorRadius();
132 Standard_Real r = T.MinorRadius();
134 ComputePoles( R, r, U1, U2, V1, V2, poles);
136 for ( i = 1; i<= nbUKnots; i++) {
137 uknots(i) = U1 + (i-1) * 2 * AlfaU;
140 umults(1)++; umults(nbUKnots)++;
141 for ( i = 1; i<= nbVKnots; i++) {
142 vknots(i) = V1 + (i-1) * 2 * AlfaV;
145 vmults(1)++; vmults(nbVKnots)++;
148 // Replace the bspline in the reference of the torus.
149 // and calculate the weight of the bspline.
150 Standard_Real W1, W2;
152 Trsf.SetTransformation( T.Position(), gp::XOY());
154 for ( i = 1; i <= nbUPoles; i++) {
155 if ( i % 2 == 0) W1 = Cos(AlfaU);
158 for ( j = 1; j <= nbVPoles; j++) {
159 if ( j % 2 == 0) W2 = Cos(AlfaV);
162 weights( i, j) = W1 * W2;
163 poles( i, j).Transform( Trsf);
169 //=======================================================================
170 //function : Convert_TorusToBSplineSurface
172 //=======================================================================
174 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface
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)
184 Standard_Real delta = Param2 - Param1;
186 Standard_DomainError_Raise_if( (delta>2*M_PI) || (delta<0.),
187 "Convert_TorusToBSplineSurface");
189 Standard_Integer i, j;
190 Standard_Real deltaU, deltaV;
192 isuperiodic = !UTrim;
195 Standard_Real R = T.MajorRadius();
196 Standard_Real r = T.MinorRadius();
198 Standard_Real W1, W2, CosU, CosV;
201 ComputePoles(R, r, 0, 2.*M_PI, Param1, Param2, poles);
206 deltaV = Param2 - Param1;
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;
213 for ( i = 1; i <= nbUKnots; i++) {
214 uknots(i) = ( i-1) * 2. * M_PI /3.;
217 for ( i = 1; i <= nbVKnots; i++) {
218 vknots(i) = Param1 + (i-1) * 2 * AlfaV;
221 vmults(1)++; vmults(nbVKnots)++;
223 CosU = 0.5; // = Cos(pi /3)
227 ComputePoles(R, r, Param1, Param2, 0., 2.*M_PI, poles);
232 deltaU = Param2 - Param1;
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;
239 for ( i = 1; i <= nbVKnots; i++) {
240 vknots(i) = ( i-1) * 2. * M_PI /3.;
243 for ( i = 1; i <= nbUKnots; i++) {
244 uknots(i) = Param1 + (i-1) * 2 * AlfaU;
247 umults(1)++; umults(nbUKnots)++;
249 CosV = 0.5; // = Cos(pi /3)
253 // Replace the bspline in the reference of the torus.
254 // and calculate the weight of the bspline.
256 Trsf.SetTransformation( T.Position(), gp::XOY());
258 for ( i = 1; i <= nbUPoles; i++) {
259 if ( i % 2 == 0) W1 = CosU;
262 for ( j = 1; j <= nbVPoles; j++) {
263 if ( j % 2 == 0) W2 = CosV;
266 weights( i, j) = W1 * W2;
267 poles( i, j).Transform( Trsf);
274 //=======================================================================
275 //function : Convert_TorusToBSplineSurface
277 //=======================================================================
279 Convert_TorusToBSplineSurface::Convert_TorusToBSplineSurface
281 : Convert_ElementarySurfaceToBSplineSurface (MaxNbUPoles, MaxNbVPoles,
282 MaxNbUKnots, MaxNbVKnots,
283 TheUDegree , TheVDegree )
285 isuperiodic = Standard_True;
286 isvperiodic = Standard_True;
288 Standard_Real W1, W2;
289 Standard_Integer i, j;
296 // Construction of the Torus in the reference mark xOy.
298 Standard_Real R = T.MajorRadius();
299 Standard_Real r = T.MinorRadius();
301 ComputePoles( R, r, 0., 2.*M_PI, 0., 2.*M_PI, poles);
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;
311 // Replace the bspline in the mark of the torus.
312 // and calculate the weight of the bspline.
314 Trsf.SetTransformation( T.Position(), gp::XOY());
316 for ( i = 1; i <= nbUPoles; i++) {
317 if ( i % 2 == 0) W1 = 0.5;
320 for ( j = 1; j <= nbVPoles; j++) {
321 if ( j % 2 == 0) W2 = 0.5;
324 weights( i, j) = W1 * W2;
325 poles( i, j).Transform( Trsf);