0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / RWStepGeom / RWStepGeom_RWBSplineCurveWithKnots.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_Check.hxx>
16 #include <Interface_EntityIterator.hxx>
17 #include <Interface_ShareTool.hxx>
18 #include <RWStepGeom_RWBSplineCurveWithKnots.hxx>
19 #include <StepData_Logical.hxx>
20 #include <StepData_StepReaderData.hxx>
21 #include <StepData_StepWriter.hxx>
22 #include <StepGeom_BSplineCurveForm.hxx>
23 #include <StepGeom_BSplineCurveWithKnots.hxx>
24 #include <StepGeom_CartesianPoint.hxx>
25 #include <StepGeom_HArray1OfCartesianPoint.hxx>
26 #include <StepGeom_KnotType.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TColStd_HArray1OfInteger.hxx>
29 #include <TColStd_HArray1OfReal.hxx>
30
31 // --- Enum : KnotType ---
32 static TCollection_AsciiString ktUniformKnots(".UNIFORM_KNOTS.");
33 static TCollection_AsciiString ktQuasiUniformKnots(".QUASI_UNIFORM_KNOTS.");
34 static TCollection_AsciiString ktPiecewiseBezierKnots(".PIECEWISE_BEZIER_KNOTS.");
35 static TCollection_AsciiString ktUnspecified(".UNSPECIFIED.");
36
37
38         // --- Enum : BSplineCurveForm ---
39 static TCollection_AsciiString bscfEllipticArc(".ELLIPTIC_ARC.");
40 static TCollection_AsciiString bscfPolylineForm(".POLYLINE_FORM.");
41 static TCollection_AsciiString bscfParabolicArc(".PARABOLIC_ARC.");
42 static TCollection_AsciiString bscfCircularArc(".CIRCULAR_ARC.");
43 static TCollection_AsciiString bscfUnspecified(".UNSPECIFIED.");
44 static TCollection_AsciiString bscfHyperbolicArc(".HYPERBOLIC_ARC.");
45
46 RWStepGeom_RWBSplineCurveWithKnots::RWStepGeom_RWBSplineCurveWithKnots () {}
47
48 void RWStepGeom_RWBSplineCurveWithKnots::ReadStep
49         (const Handle(StepData_StepReaderData)& data,
50          const Standard_Integer num,
51          Handle(Interface_Check)& ach,
52          const Handle(StepGeom_BSplineCurveWithKnots)& ent) const
53 {
54
55
56         // --- Number of Parameter Control ---
57
58         if (!data->CheckNbParams(num,9,ach,"b_spline_curve_with_knots")) return;
59
60         // --- inherited field : name ---
61
62         Handle(TCollection_HAsciiString) aName;
63         //szv#4:S4163:12Mar99 `Standard_Boolean stat1 =` not needed
64         data->ReadString (num,1,"name",ach,aName);
65
66         // --- inherited field : degree ---
67
68         Standard_Integer aDegree;
69         //szv#4:S4163:12Mar99 `Standard_Boolean stat2 =` not needed
70         data->ReadInteger (num,2,"degree",ach,aDegree);
71
72         // --- inherited field : controlPointsList ---
73
74         Handle(StepGeom_HArray1OfCartesianPoint) aControlPointsList;
75         Handle(StepGeom_CartesianPoint) anent3;
76         Standard_Integer nsub3;
77         if (data->ReadSubList (num,3,"control_points_list",ach,nsub3)) {
78           Standard_Integer nb3 = data->NbParams(nsub3);
79     if(nb3 <1)
80       ach->AddFail("Number of control points of the b_spline_curve_form is equal to 0");
81     else
82     {
83       aControlPointsList = new StepGeom_HArray1OfCartesianPoint (1, nb3);
84       for (Standard_Integer i3 = 1; i3 <= nb3; i3 ++) {
85         //szv#4:S4163:12Mar99 `Standard_Boolean stat3 =` not needed
86         if (data->ReadEntity (nsub3, i3,"cartesian_point", ach,
87           STANDARD_TYPE(StepGeom_CartesianPoint), anent3))
88           aControlPointsList->SetValue(i3, anent3);
89       }
90     }
91         }
92
93         // --- inherited field : curveForm ---
94
95         StepGeom_BSplineCurveForm aCurveForm = StepGeom_bscfPolylineForm;
96         if (data->ParamType(num,4) == Interface_ParamEnum) {
97           Standard_CString text = data->ParamCValue(num,4);
98           if      (bscfEllipticArc.IsEqual(text)) aCurveForm = StepGeom_bscfEllipticArc;
99           else if (bscfPolylineForm.IsEqual(text)) aCurveForm = StepGeom_bscfPolylineForm;
100           else if (bscfParabolicArc.IsEqual(text)) aCurveForm = StepGeom_bscfParabolicArc;
101           else if (bscfCircularArc.IsEqual(text)) aCurveForm = StepGeom_bscfCircularArc;
102           else if (bscfUnspecified.IsEqual(text)) aCurveForm = StepGeom_bscfUnspecified;
103           else if (bscfHyperbolicArc.IsEqual(text)) aCurveForm = StepGeom_bscfHyperbolicArc;
104           else ach->AddFail("Enumeration b_spline_curve_form has not an allowed value");
105         }
106         else ach->AddFail("Parameter #4 (curve_form) is not an enumeration");
107
108         // --- inherited field : closedCurve ---
109
110         StepData_Logical aClosedCurve;
111         //szv#4:S4163:12Mar99 `Standard_Boolean stat5 =` not needed
112         data->ReadLogical (num,5,"closed_curve",ach,aClosedCurve);
113
114         // --- inherited field : selfIntersect ---
115
116         StepData_Logical aSelfIntersect;
117         //szv#4:S4163:12Mar99 `Standard_Boolean stat6 =` not needed
118         data->ReadLogical (num,6,"self_intersect",ach,aSelfIntersect);
119
120         // --- own field : knotMultiplicities ---
121
122         Handle(TColStd_HArray1OfInteger) aKnotMultiplicities;
123         Standard_Integer aKnotMultiplicitiesItem;
124         Standard_Integer nsub7;
125         if (data->ReadSubList (num,7,"knot_multiplicities",ach,nsub7)) {
126           Standard_Integer nb7 = data->NbParams(nsub7);
127           aKnotMultiplicities = new TColStd_HArray1OfInteger (1, nb7);
128           for (Standard_Integer i7 = 1; i7 <= nb7; i7 ++) {
129             //szv#4:S4163:12Mar99 `Standard_Boolean stat7 =` not needed
130             if (data->ReadInteger (nsub7,i7,"knot_multiplicities",ach,aKnotMultiplicitiesItem))
131               aKnotMultiplicities->SetValue(i7,aKnotMultiplicitiesItem);
132           }
133         }
134
135         // --- own field : knots ---
136
137         Handle(TColStd_HArray1OfReal) aKnots;
138         Standard_Real aKnotsItem;
139         Standard_Integer nsub8;
140         if (data->ReadSubList (num,8,"knots",ach,nsub8)) {
141           Standard_Integer nb8 = data->NbParams(nsub8);
142           aKnots = new TColStd_HArray1OfReal (1, nb8);
143           for (Standard_Integer i8 = 1; i8 <= nb8; i8 ++) {
144             //szv#4:S4163:12Mar99 `Standard_Boolean stat8 =` not needed
145             if (data->ReadReal (nsub8,i8,"knots",ach,aKnotsItem))
146               aKnots->SetValue(i8,aKnotsItem);
147           }
148         }
149
150         // --- own field : knotSpec ---
151
152         StepGeom_KnotType aKnotSpec = StepGeom_ktUniformKnots;
153         if (data->ParamType(num,9) == Interface_ParamEnum) {
154           Standard_CString text = data->ParamCValue(num,9);
155           if      (ktUniformKnots.IsEqual(text)) aKnotSpec = StepGeom_ktUniformKnots;
156           else if (ktQuasiUniformKnots.IsEqual(text)) aKnotSpec = StepGeom_ktQuasiUniformKnots;
157           else if (ktPiecewiseBezierKnots.IsEqual(text)) aKnotSpec = StepGeom_ktPiecewiseBezierKnots;
158           else if (ktUnspecified.IsEqual(text)) aKnotSpec = StepGeom_ktUnspecified;
159           else ach->AddFail("Enumeration knot_type has not an allowed value");
160         }
161         else ach->AddFail("Parameter #9 (knot_spec) is not an enumeration");
162
163         //--- Initialisation of the read entity ---
164
165
166         ent->Init(aName, aDegree, aControlPointsList, aCurveForm, aClosedCurve, aSelfIntersect, aKnotMultiplicities, aKnots, aKnotSpec);
167 }
168
169
170 void RWStepGeom_RWBSplineCurveWithKnots::WriteStep
171         (StepData_StepWriter& SW,
172          const Handle(StepGeom_BSplineCurveWithKnots)& ent) const
173 {
174
175         // --- inherited field name ---
176
177         SW.Send(ent->Name());
178
179         // --- inherited field degree ---
180
181         SW.Send(ent->Degree());
182
183         // --- inherited field controlPointsList ---
184
185         SW.OpenSub();
186         for (Standard_Integer i3 = 1;  i3 <= ent->NbControlPointsList();  i3 ++) {
187           SW.Send(ent->ControlPointsListValue(i3));
188         }
189         SW.CloseSub();
190
191         // --- inherited field curveForm ---
192
193         switch(ent->CurveForm()) {
194           case StepGeom_bscfEllipticArc : SW.SendEnum (bscfEllipticArc); break;
195           case StepGeom_bscfPolylineForm : SW.SendEnum (bscfPolylineForm); break;
196           case StepGeom_bscfParabolicArc : SW.SendEnum (bscfParabolicArc); break;
197           case StepGeom_bscfCircularArc : SW.SendEnum (bscfCircularArc); break;
198           case StepGeom_bscfUnspecified : SW.SendEnum (bscfUnspecified); break;
199           case StepGeom_bscfHyperbolicArc : SW.SendEnum (bscfHyperbolicArc); break;
200         }
201
202         // --- inherited field closedCurve ---
203
204         SW.SendLogical(ent->ClosedCurve());
205
206         // --- inherited field selfIntersect ---
207
208         SW.SendLogical(ent->SelfIntersect());
209
210         // --- own field : knotMultiplicities ---
211
212         SW.OpenSub();
213         for (Standard_Integer i7 = 1;  i7 <= ent->NbKnotMultiplicities();  i7 ++) {
214           SW.Send(ent->KnotMultiplicitiesValue(i7));
215         }
216         SW.CloseSub();
217
218         // --- own field : knots ---
219
220         SW.OpenSub();
221         for (Standard_Integer i8 = 1;  i8 <= ent->NbKnots();  i8 ++) {
222           SW.Send(ent->KnotsValue(i8));
223         }
224         SW.CloseSub();
225
226         // --- own field : knotSpec ---
227
228         switch(ent->KnotSpec()) {
229           case StepGeom_ktUniformKnots : SW.SendEnum (ktUniformKnots); break;
230           case StepGeom_ktQuasiUniformKnots : SW.SendEnum (ktQuasiUniformKnots); break;
231           case StepGeom_ktPiecewiseBezierKnots : SW.SendEnum (ktPiecewiseBezierKnots); break;
232           case StepGeom_ktUnspecified : SW.SendEnum (ktUnspecified); break;
233         }
234 }
235
236
237 void RWStepGeom_RWBSplineCurveWithKnots::Share(const Handle(StepGeom_BSplineCurveWithKnots)& ent, Interface_EntityIterator& iter) const
238 {
239
240         Standard_Integer nbElem1 = ent->NbControlPointsList();
241         for (Standard_Integer is1=1; is1<=nbElem1; is1 ++) {
242           iter.GetOneItem(ent->ControlPointsListValue(is1));
243         }
244
245 }
246
247 void RWStepGeom_RWBSplineCurveWithKnots::Check
248 (const Handle(StepGeom_BSplineCurveWithKnots)& ent,
249  const Interface_ShareTool& ,
250  Handle(Interface_Check)& ach) const
251 {
252   Standard_Integer nbCPL  = ent->NbControlPointsList();
253   Standard_Integer dgBSC  = ent->Degree();
254   Standard_Integer nbMult = ent->NbKnotMultiplicities();
255   Standard_Integer nbKno  = ent->NbKnots();
256   Standard_Integer sumMult = 0;
257 //  std::cout << "BSplineCurveWithKnots: nbMult=" << nbMult << " nbKno= " << 
258 //    nbKno << " nbCPL= " << nbCPL << " degree= " << dgBSC << std::endl;
259   if(nbMult != nbKno) {
260     ach->AddFail("ERROR: No.of KnotMultiplicities not equal No.of Knots");
261   }
262   Standard_Integer i;//svv Jan 10 2000: porting on DEC 
263   for (i=1; i<=nbMult-1; i++) {
264     sumMult = sumMult + ent->KnotMultiplicitiesValue(i);
265   }
266   Standard_Integer sumNonP = nbCPL + dgBSC + 1;
267   Standard_Integer mult1 = ent->KnotMultiplicitiesValue(1);
268   Standard_Integer multN = ent->KnotMultiplicitiesValue(nbMult);
269 //  std::cout << "BSplineCurveWithKnots: mult1=" << mult1 << " multN= " <<
270 //    multN << " sumMult= " << sumMult << std::endl;
271   if((sumMult + multN) == sumNonP) {
272   }
273   else if((sumMult == nbCPL) && (mult1 == multN)) {
274   }
275   else {
276     ach->AddFail("ERROR: wrong number of Knot Multiplicities");
277   }
278   for(i=2; i<=nbKno; i++) {
279     Standard_Real distKn  = ent->KnotsValue(i-1) - ent->KnotsValue(i);
280     if(Abs(distKn) <= RealEpsilon())
281       ach->AddWarning("WARNING: Curve contains identical KnotsValues");
282     else if(distKn > RealEpsilon())
283       ach->AddFail("ERROR: Curve contains descending KnotsValues");
284   }
285 }
286