7fd59977 |
1 | // File: ShapeUpgrade.cxx |
2 | // Created: Thu Nov 12 11:58:22 1998 |
3 | // Author: data exchange team |
4 | // <det@kinox.nnov.matra-dtv.fr> |
5 | // abv 24.05.99 S4174: methods Debug() commented |
6 | |
7 | #include <ShapeUpgrade.ixx> |
8 | #include <TColgp_Array1OfPnt.hxx> |
9 | #include <TColStd_Array1OfReal.hxx> |
10 | #include <BSplCLib.hxx> |
11 | #include <TColStd_Array1OfInteger.hxx> |
12 | |
13 | /* |
14 | // Debug state= True / False. |
15 | static Standard_Boolean Dbg=Standard_False; |
16 | |
17 | void ShapeUpgrade::SetDebug(const Standard_Boolean State) |
18 | { |
19 | Dbg=State; |
20 | } |
21 | |
22 | Standard_Boolean ShapeUpgrade::Debug() |
23 | { |
24 | return Dbg; |
25 | } |
26 | */ |
27 | |
28 | //======================================================================= |
29 | //function : C0BSplineToSequenceOfC1BSplineCurve |
30 | //purpose : |
31 | //======================================================================= |
32 | |
33 | Standard_Boolean ShapeUpgrade::C0BSplineToSequenceOfC1BSplineCurve(const Handle(Geom_BSplineCurve)& BS, |
34 | Handle(TColGeom_HSequenceOfBoundedCurve)& seqBS) |
35 | { |
36 | if (BS.IsNull() || (BS->IsCN (1))) return Standard_False; |
37 | |
38 | seqBS = new TColGeom_HSequenceOfBoundedCurve; |
39 | BS->SetNotPeriodic(); //to have equation NbPoles = NbKnots with Multiplicities - degree - 1 |
40 | |
41 | Standard_Integer deg = BS->Degree(); |
42 | Standard_Integer NbKnots = BS->NbKnots(); |
43 | Standard_Integer NbPoles = BS->NbPoles(); |
44 | TColgp_Array1OfPnt Poles (1, NbPoles); |
45 | TColStd_Array1OfReal Weights (1, NbPoles); |
46 | TColStd_Array1OfReal Knots (1, NbKnots); |
47 | TColStd_Array1OfInteger Mults (1, NbKnots); |
48 | TColStd_Array1OfReal KnotSequence (1, NbPoles + deg + 1); |
49 | |
50 | BS->Poles(Poles); |
51 | if (BS->IsRational()) |
52 | BS->Weights(Weights); |
53 | else |
54 | Weights.Init(1.); |
55 | BS->Knots(Knots); |
56 | BS->Multiplicities(Mults); |
57 | BS->KnotSequence (KnotSequence); |
58 | |
59 | Standard_Integer StartKnotIndex, EndKnotIndex, j; |
60 | |
61 | StartKnotIndex = BS->FirstUKnotIndex(); |
62 | for ( EndKnotIndex = StartKnotIndex + 1; EndKnotIndex <= BS->LastUKnotIndex(); EndKnotIndex++ ) { |
63 | if ( ( Mults (EndKnotIndex) < deg ) && ( EndKnotIndex < BS->LastUKnotIndex() ) ) continue; |
64 | |
65 | Standard_Integer StartFlatIndex = BSplCLib::FlatIndex (deg, StartKnotIndex, Mults, Standard_False); |
66 | // StartFlatIndex += Mults (StartKnotIndex) - 1; |
67 | Standard_Integer EndFlatIndex = BSplCLib::FlatIndex (deg, EndKnotIndex, Mults, Standard_False); |
68 | EndFlatIndex -= Mults (EndKnotIndex) - 1; |
69 | |
70 | TColStd_Array1OfReal TempKnots (1, NbKnots); |
71 | TColStd_Array1OfInteger TempMults (1, NbKnots); |
72 | TempMults.Init (1); |
73 | Standard_Integer TempKnotIndex = 1; |
74 | TempKnots (TempKnotIndex) = KnotSequence (StartFlatIndex - deg); |
75 | |
76 | for ( j = StartFlatIndex - deg + 1; j <= EndFlatIndex + deg; j++ ) |
77 | if (Abs (KnotSequence (j) - KnotSequence (j - 1)) <= gp::Resolution()) |
78 | TempMults (TempKnotIndex) ++; |
79 | else |
80 | TempKnots (++TempKnotIndex) = KnotSequence (j); |
81 | |
82 | Standard_Integer TempStartIndex = 1, TempEndIndex = TempKnotIndex; |
83 | if (TempMults (TempStartIndex) == 1) |
84 | TempMults (++TempStartIndex) ++; |
85 | if (TempMults (TempEndIndex) == 1) |
86 | TempMults (--TempEndIndex) ++; |
87 | |
88 | Standard_Integer NewNbKnots = TempEndIndex - TempStartIndex + 1; |
89 | TColStd_Array1OfInteger newMults (1, NewNbKnots); |
90 | TColStd_Array1OfReal newKnots (1, NewNbKnots); |
91 | for ( j = 1; j <= NewNbKnots; j++ ) { |
92 | newMults (j) = TempMults (j + TempStartIndex - 1); |
93 | newKnots (j) = TempKnots (j + TempStartIndex - 1); |
94 | } |
95 | |
96 | Standard_Integer NewNbPoles = BSplCLib::NbPoles(deg, Standard_False, newMults); |
97 | TColgp_Array1OfPnt newPoles (1, NewNbPoles); |
98 | TColStd_Array1OfReal newWeights (1, NewNbPoles); |
99 | Standard_Integer PoleIndex = StartFlatIndex - deg;//Index of starting pole when splitting B-Spline is an index of starting knot |
100 | for (j = 1; j <= NewNbPoles; j++) { |
101 | newWeights (j) = Weights (j + PoleIndex - 1); |
102 | newPoles (j) = Poles (j + PoleIndex - 1); |
103 | } |
104 | |
105 | Handle(Geom_BSplineCurve) newC = new Geom_BSplineCurve |
106 | (newPoles, newWeights, newKnots, newMults,deg); |
107 | seqBS->Append (newC); |
108 | |
109 | StartKnotIndex = EndKnotIndex; |
110 | } |
111 | return Standard_True; |
112 | } |
113 | |
114 | //======================================================================= |
115 | //function : C0BSplineToSequenceOfC1BSplineCurve |
116 | //purpose : |
117 | //======================================================================= |
118 | |
119 | static Handle(Geom_BSplineCurve) BSplineCurve2dTo3d (const Handle(Geom2d_BSplineCurve)& BS) |
120 | { |
121 | Standard_Integer deg = BS->Degree(); |
122 | Standard_Integer NbKnots = BS->NbKnots(); |
123 | Standard_Integer NbPoles = BS->NbPoles(); |
124 | TColgp_Array1OfPnt2d Poles2d (1,NbPoles); |
125 | TColStd_Array1OfReal Weights (1,NbPoles); |
126 | TColStd_Array1OfReal Knots (1,NbKnots); |
127 | TColStd_Array1OfInteger Mults (1,NbKnots); |
128 | |
129 | BS->Poles(Poles2d); |
130 | if (BS->IsRational()) |
131 | BS->Weights(Weights); |
132 | else |
133 | Weights.Init (1); |
134 | BS->Knots (Knots); |
135 | BS->Multiplicities (Mults); |
136 | |
137 | TColgp_Array1OfPnt Poles3d (1,NbPoles); |
138 | for (Standard_Integer i = 1; i <= NbPoles; i++) |
139 | Poles3d (i) = gp_Pnt (Poles2d (i).X(), Poles2d (i).Y(), 0); |
140 | |
141 | Handle(Geom_BSplineCurve) BS3d = new Geom_BSplineCurve (Poles3d, Weights, |
142 | Knots, Mults, deg, BS->IsPeriodic()); |
143 | return BS3d; |
144 | } |
145 | |
146 | static Handle(Geom2d_BSplineCurve) BSplineCurve3dTo2d (const Handle(Geom_BSplineCurve)& BS) |
147 | { |
148 | Standard_Integer deg = BS->Degree(); |
149 | Standard_Integer NbKnots = BS->NbKnots(); |
150 | Standard_Integer NbPoles = BS->NbPoles(); |
151 | TColgp_Array1OfPnt Poles3d (1, NbPoles); |
152 | TColStd_Array1OfReal Weights (1, NbPoles); |
153 | TColStd_Array1OfReal Knots (1, NbKnots); |
154 | TColStd_Array1OfInteger Mults (1, NbKnots); |
155 | |
156 | BS->Poles(Poles3d); |
157 | if (BS->IsRational()) |
158 | BS->Weights(Weights); |
159 | else |
160 | Weights.Init (1); |
161 | BS->Knots (Knots); |
162 | BS->Multiplicities (Mults); |
163 | |
164 | TColgp_Array1OfPnt2d Poles2d (1,NbPoles); |
165 | for (Standard_Integer i = 1; i <= NbPoles; i++) |
166 | Poles2d (i) = gp_Pnt2d (Poles3d (i).X(), Poles3d (i).Y()); |
167 | |
168 | Handle(Geom2d_BSplineCurve) BS2d = new Geom2d_BSplineCurve (Poles2d, Weights, |
169 | Knots, Mults, deg, BS->IsPeriodic()); |
170 | return BS2d; |
171 | } |
172 | |
173 | Standard_Boolean ShapeUpgrade::C0BSplineToSequenceOfC1BSplineCurve(const Handle(Geom2d_BSplineCurve)& BS, |
174 | Handle(TColGeom2d_HSequenceOfBoundedCurve)& seqBS) |
175 | { |
176 | if (BS.IsNull() || (BS->IsCN (1))) return Standard_False; |
177 | |
178 | Handle(Geom_BSplineCurve) BS3d = BSplineCurve2dTo3d (BS); |
179 | Handle(TColGeom_HSequenceOfBoundedCurve) seqBS3d; |
180 | Standard_Boolean result = C0BSplineToSequenceOfC1BSplineCurve (BS3d, seqBS3d); |
181 | if (result) { |
182 | seqBS = new TColGeom2d_HSequenceOfBoundedCurve; |
183 | for (Standard_Integer i = 1; i <= seqBS3d->Length(); i++) |
184 | seqBS->Append (BSplineCurve3dTo2d (Handle(Geom_BSplineCurve)::DownCast (seqBS3d->Value (i)))); |
185 | } |
186 | return result; |
187 | } |
188 | |