0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / StepToGeom / StepToGeom_MakeBSplineCurve.pxx
1 // Created on: 1993-07-02
2 // Created by: Martine LANGLOIS
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 {
18   Handle(StepGeom_BSplineCurveWithKnots)   BSCW;
19   Handle(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve) BSCWR;
20
21   if (SC->IsKind(STANDARD_TYPE(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve))) {
22     BSCWR = 
23       Handle(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve)
24         ::DownCast(SC);
25     BSCW = 
26       Handle(StepGeom_BSplineCurveWithKnots)
27         ::DownCast(BSCWR->BSplineCurveWithKnots());
28   }
29   else
30     BSCW = Handle(StepGeom_BSplineCurveWithKnots)::DownCast(SC);
31
32   const Standard_Integer Deg = BSCW->Degree();
33   const Standard_Integer NbPoles = BSCW->NbControlPointsList();
34   const Standard_Integer NbKnots = BSCW->NbKnotMultiplicities();
35
36   //aKnotMultiplicities = new TColStd_HArray1OfInteger(1,NbKnots);
37   const Handle(TColStd_HArray1OfInteger)& aKnotMultiplicities = BSCW->KnotMultiplicities();
38   //aKnots = new TColStd_HArray1OfReal(1,NbKnots);
39   const Handle(TColStd_HArray1OfReal)& aKnots = BSCW->Knots();
40   
41   // Count number of unique knots
42   Standard_Integer i;
43   Standard_Integer NbUniqueKnots = 0;
44   Standard_Real lastKnot = RealFirst();
45   for (i=1; i<=NbKnots; ++i) {
46     if (aKnots->Value(i) - lastKnot > Epsilon (Abs(lastKnot))) {
47       NbUniqueKnots++;
48       lastKnot = aKnots->Value(i);
49     }
50   }
51   
52   TColStd_Array1OfReal Kn(1,NbUniqueKnots);
53   TColStd_Array1OfInteger Mult(1,NbUniqueKnots);
54   lastKnot = aKnots->Value(1);
55   Kn.SetValue(1, aKnots->Value(1));
56   Mult.SetValue(1, aKnotMultiplicities->Value(1));
57   Standard_Integer pos = 1;
58   for (i=2; i<=NbKnots; i++) {
59     if (aKnots->Value(i) - lastKnot > Epsilon (Abs(lastKnot))) {
60       pos++;
61       Kn.SetValue(pos, aKnots->Value(i));
62       Mult.SetValue(pos, aKnotMultiplicities->Value(i));
63       lastKnot = aKnots->Value(i);
64     }
65     else {
66       // Knot not unique, increase multiplicity
67       Standard_Integer curMult = Mult.Value(pos);
68       Mult.SetValue(pos, curMult + aKnotMultiplicities->Value(i));
69     }
70   }
71
72   Standard_Integer aFMulDiff = 0,aLMulDiff = 0;
73   for (i=1; i<=NbUniqueKnots; ++i) {
74     Standard_Integer aCurrentVal = Mult.Value(i);
75     if (aCurrentVal > Deg + 1)
76     {
77       if (i == 1)       aFMulDiff = aCurrentVal - Deg - 1;
78       if (i == NbKnots) aLMulDiff = aCurrentVal - Deg - 1;
79 #ifdef STEPTOGEOM_DEB
80       cout << "\nWrong multiplicity " << aCurrentVal <<  " on " << i 
81            << " knot!" << "\nChanged to " << Deg + 1 << endl;
82 #endif
83       aCurrentVal = Deg + 1;
84     }
85     Mult.SetValue(i,aCurrentVal);
86   }
87   
88   //aControlPointsList = new StepGeom_HArray1OfCartesianPoint(1,NbPoles);
89   const Handle(StepGeom_HArray1OfCartesianPoint)& aControlPointsList = BSCW->ControlPointsList();
90   Standard_Integer aSumMulDiff = aFMulDiff + aLMulDiff;
91   Array1OfPnt_gen Poles(1,NbPoles - aSumMulDiff);
92   CartesianPoint_gen P;
93   
94   for (i = 1 + aFMulDiff; i<= NbPoles - aLMulDiff; ++i)
95   {
96     if (StepToGeom_MakeCartesianPoint_gen::Convert(aControlPointsList->Value(i),P))
97       Poles.SetValue(i - aFMulDiff,P->Pnt_fonc());
98     else
99       return Standard_False;
100   }
101   
102   // --- Does the Curve descriptor LOOKS like a periodic descriptor ? ---
103
104   Standard_Integer SumMult = 0;
105   for (i=1; i<=NbUniqueKnots; i++) {
106     SumMult += Mult.Value(i);
107   }
108   
109   Standard_Boolean shouldBePeriodic;
110   if (SumMult == (NbPoles + Deg + 1)) {
111     shouldBePeriodic = Standard_False;
112   }
113   else if ((Mult.Value(1) == 
114             Mult.Value(NbUniqueKnots)) &&
115            ((SumMult - Mult.Value(1)) == NbPoles)) {
116     shouldBePeriodic = Standard_True;
117   }
118   else {  // --- What is that ??? ---
119     shouldBePeriodic = Standard_False;
120     //cout << "Strange BSpline Curve Descriptor" << endl;
121   }
122   
123   if (SC->IsKind(STANDARD_TYPE(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve))) {
124     const Handle(TColStd_HArray1OfReal)& aWeight = BSCWR->WeightsData();
125     TColStd_Array1OfReal W(1,NbPoles);
126     for (i=1; i<=NbPoles; i++)
127       W.SetValue(i,aWeight->Value(i));
128     CC = new BSplineCurve_gen(Poles, W, Kn, Mult, Deg, shouldBePeriodic);
129   }
130   else
131     CC = new BSplineCurve_gen(Poles, Kn, Mult, Deg, shouldBePeriodic);
132
133   // abv 04.07.00 CAX-IF TRJ4: trj4_k1_top-md-203.stp #716 (face #581):
134   // force periodicity on closed curves
135   if ( SC->ClosedCurve() && CC->Degree() >1 && CC->IsClosed() ) {
136     CC->SetPeriodic();
137   }
138   return Standard_True;
139 }