0027234: Code duplication: Convert_CompBezierCurvesToBSplineCurve* in ShapeConstruct
[occt.git] / src / Convert / Convert_CylinderToBSplineSurface.cxx
CommitLineData
b311480e 1// Copyright (c) 1995-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15//JCV 16/10/91
16
42cf5bc1 17#include <Convert_CylinderToBSplineSurface.hxx>
7fd59977 18#include <gp.hxx>
42cf5bc1 19#include <gp_Cylinder.hxx>
7fd59977 20#include <gp_Trsf.hxx>
42cf5bc1 21#include <Standard_DomainError.hxx>
7fd59977 22
23static const Standard_Integer TheUDegree = 2;
24static const Standard_Integer TheVDegree = 1;
25static const Standard_Integer TheNbUKnots = 5;
26static const Standard_Integer TheNbVKnots = 2;
27static const Standard_Integer TheNbUPoles = 9;
28static const Standard_Integer TheNbVPoles = 2;
29
30
31static void ComputePoles( const Standard_Real R,
32 const Standard_Real U1,
33 const Standard_Real U2,
34 const Standard_Real V1,
35 const Standard_Real V2,
36 TColgp_Array2OfPnt& Poles)
37{
38 Standard_Real deltaU = U2 - U1;
39
40 Standard_Integer i;
41
0d969553 42 // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
7fd59977 43 Standard_Integer
c6541a0c 44 nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
7fd59977 45 Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
46
47 Standard_Real UStart = U1;
48 Poles(1,1) = gp_Pnt(R*Cos(UStart),R*Sin(UStart),V1);
49 Poles(1,2) = gp_Pnt(R*Cos(UStart),R*Sin(UStart),V2);
50
51 for ( i = 1; i <= nbUSpans; i++) {
52 Poles( 2*i, 1) = gp_Pnt( R * Cos(UStart+AlfaU) / Cos(AlfaU),
53 R * Sin(UStart+AlfaU) / Cos(AlfaU),
54 V1 );
55 Poles( 2*i, 2) = gp_Pnt( R * Cos(UStart+AlfaU) / Cos(AlfaU),
56 R * Sin(UStart+AlfaU) / Cos(AlfaU),
57 V2 );
58 Poles(2*i+1,1) = gp_Pnt( R * Cos(UStart+2*AlfaU),
59 R * Sin(UStart+2*AlfaU),
60 V1 );
61 Poles(2*i+1,2) = gp_Pnt( R * Cos(UStart+2*AlfaU),
62 R * Sin(UStart+2*AlfaU),
63 V2 );
64 UStart += 2*AlfaU;
65 }
66}
67
68
69//=======================================================================
70//function : Convert_CylinderToBSplineSurface
71//purpose :
72//=======================================================================
73
74Convert_CylinderToBSplineSurface::Convert_CylinderToBSplineSurface
75 (const gp_Cylinder& Cyl,
76 const Standard_Real U1 ,
77 const Standard_Real U2 ,
78 const Standard_Real V1 ,
79 const Standard_Real V2 )
80: Convert_ElementarySurfaceToBSplineSurface (TheNbUPoles, TheNbVPoles,
81 TheNbUKnots, TheNbVKnots,
82 TheUDegree , TheVDegree )
83{
84 Standard_Real deltaU = U2 - U1;
85 Standard_DomainError_Raise_if( (Abs(V2-V1) <= Abs(Epsilon(V1))) ||
c6541a0c 86 (deltaU > 2*M_PI) ||
7fd59977 87 (deltaU < 0. ),
88 "Convert_CylinderToBSplineSurface");
89
90 isuperiodic = Standard_False;
91 isvperiodic = Standard_False;
92
93 Standard_Integer i,j;
0d969553 94 // construction of the cylinder in the reference mark xOy.
7fd59977 95
0d969553 96 // Number of spans : maximum opening = 150 degrees ( = PI / 1.2 rds)
7fd59977 97 Standard_Integer
c6541a0c 98 nbUSpans = (Standard_Integer)IntegerPart( 1.2 * deltaU / M_PI) + 1;
7fd59977 99 Standard_Real AlfaU = deltaU / ( nbUSpans * 2);
100
101 nbUPoles = 2 * nbUSpans + 1;
102 nbUKnots = nbUSpans + 1;
103
104 nbVPoles = 2;
105 nbVKnots = 2;
106
107 Standard_Real R = Cyl.Radius();
108
109 ComputePoles( R, U1, U2, V1, V2, poles);
110
111 for ( i = 1; i<= nbUKnots; i++) {
112 uknots(i) = U1 + (i-1) * 2 * AlfaU;
113 umults(i) = 2;
114 }
115 umults(1)++; umults(nbUKnots)++;
116 vknots(1) = V1; vmults(1) = 2;
117 vknots(2) = V2; vmults(2) = 2;
118
0d969553
Y
119 // Replace bspline in the mark of the sphere.
120 // and calculate the weight of the bspline.
7fd59977 121 Standard_Real W1;
122 gp_Trsf Trsf;
123 Trsf.SetTransformation( Cyl.Position(), gp::XOY());
124
125 for ( i = 1; i <= nbUPoles; i++) {
126 if ( i % 2 == 0) W1 = Cos(AlfaU);
127 else W1 = 1.;
128
129 for ( j = 1; j <= nbVPoles; j++) {
130 weights( i, j) = W1;
131 poles( i, j).Transform( Trsf);
132 }
133 }
134}
135
136
137//=======================================================================
138//function : Convert_CylinderToBSplineSurface
139//purpose :
140//=======================================================================
141
142Convert_CylinderToBSplineSurface::Convert_CylinderToBSplineSurface
143 (const gp_Cylinder& Cyl,
144 const Standard_Real V1 ,
145 const Standard_Real V2 )
146: Convert_ElementarySurfaceToBSplineSurface (TheNbUPoles, TheNbVPoles,
147 TheNbUKnots, TheNbVKnots,
148 TheUDegree , TheVDegree)
149{
150 Standard_DomainError_Raise_if( Abs(V2-V1) <= Abs(Epsilon(V1)),
151 "Convert_CylinderToBSplineSurface");
152
153 Standard_Integer i,j;
154
155 isuperiodic = Standard_True;
156 isvperiodic = Standard_False;
157
0d969553 158 // construction of the cylinder in the reference mark xOy.
7fd59977 159
160 Standard_Real R = Cyl.Radius();
161
c6541a0c 162 ComputePoles( R, 0., 2.*M_PI, V1, V2, poles);
7fd59977 163
164 nbUPoles = 6;
165 nbUKnots = 4;
166 nbVPoles = 2;
167 nbVKnots = 2;
168
169 for ( i = 1; i <= nbUKnots; i++) {
c6541a0c 170 uknots(i) = ( i-1) * 2. * M_PI /3.;
7fd59977 171 umults(i) = 2;
172 }
173 vknots(1) = V1; vmults(1) = 2;
174 vknots(2) = V2; vmults(2) = 2;
175
0d969553
Y
176 // Replace the bspline inn the mark of the cone.
177 // and calculate the weight of the bspline.
7fd59977 178 Standard_Real W;
179 gp_Trsf Trsf;
180 Trsf.SetTransformation( Cyl.Position(), gp::XOY());
181
182 for ( i = 1; i <= nbUPoles; i++) {
183 if ( i % 2 == 0) W = 0.5; // = Cos(pi /3)
184 else W = 1.;
185
186 for ( j = 1; j <= nbVPoles; j++) {
187 weights( i, j) = W;
188 poles( i, j).Transform( Trsf);
189 }
190 }
191}
192