0027104: DownCast() cannot return null for mismatched handle
[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   Handle(StepGeom_BSplineCurveWithKnots)   BSCW;
18   Handle(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve) BSCWR;
19
20   if (SC->IsKind(STANDARD_TYPE(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve))) {
21     BSCWR = Handle(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve)::DownCast(SC);
22     BSCW = BSCWR->BSplineCurveWithKnots();
23   }
24   else
25     BSCW = Handle(StepGeom_BSplineCurveWithKnots)::DownCast(SC);
26
27   const Standard_Integer Deg = BSCW->Degree();
28   const Standard_Integer NbPoles = BSCW->NbControlPointsList();
29   const Standard_Integer NbKnots = BSCW->NbKnotMultiplicities();
30
31   //aKnotMultiplicities = new TColStd_HArray1OfInteger(1,NbKnots);
32   const Handle(TColStd_HArray1OfInteger)& aKnotMultiplicities = BSCW->KnotMultiplicities();
33   //aKnots = new TColStd_HArray1OfReal(1,NbKnots);
34   const Handle(TColStd_HArray1OfReal)& aKnots = BSCW->Knots();
35   
36   // Count number of unique knots
37   Standard_Integer i;
38   Standard_Integer NbUniqueKnots = 0;
39   Standard_Real lastKnot = RealFirst();
40   for (i=1; i<=NbKnots; ++i) {
41     if (aKnots->Value(i) - lastKnot > Epsilon (Abs(lastKnot))) {
42       NbUniqueKnots++;
43       lastKnot = aKnots->Value(i);
44     }
45   }
46   
47   TColStd_Array1OfReal Kn(1,NbUniqueKnots);
48   TColStd_Array1OfInteger Mult(1,NbUniqueKnots);
49   lastKnot = aKnots->Value(1);
50   Kn.SetValue(1, aKnots->Value(1));
51   Mult.SetValue(1, aKnotMultiplicities->Value(1));
52   Standard_Integer pos = 1;
53   for (i=2; i<=NbKnots; i++) {
54     if (aKnots->Value(i) - lastKnot > Epsilon (Abs(lastKnot))) {
55       pos++;
56       Kn.SetValue(pos, aKnots->Value(i));
57       Mult.SetValue(pos, aKnotMultiplicities->Value(i));
58       lastKnot = aKnots->Value(i);
59     }
60     else {
61       // Knot not unique, increase multiplicity
62       Standard_Integer curMult = Mult.Value(pos);
63       Mult.SetValue(pos, curMult + aKnotMultiplicities->Value(i));
64     }
65   }
66
67   Standard_Integer aFMulDiff = 0,aLMulDiff = 0;
68   for (i=1; i<=NbUniqueKnots; ++i) {
69     Standard_Integer aCurrentVal = Mult.Value(i);
70     if (aCurrentVal > Deg + 1)
71     {
72       if (i == 1)       aFMulDiff = aCurrentVal - Deg - 1;
73       if (i == NbUniqueKnots) aLMulDiff = aCurrentVal - Deg - 1;
74 #ifdef OCCT_DEBUG
75       cout << "\nWrong multiplicity " << aCurrentVal <<  " on " << i 
76            << " knot!" << "\nChanged to " << Deg + 1 << endl;
77 #endif
78       aCurrentVal = Deg + 1;
79     }
80     Mult.SetValue(i,aCurrentVal);
81   }
82   
83   //aControlPointsList = new StepGeom_HArray1OfCartesianPoint(1,NbPoles);
84   const Handle(StepGeom_HArray1OfCartesianPoint)& aControlPointsList = BSCW->ControlPointsList();
85   Standard_Integer aSumMulDiff = aFMulDiff + aLMulDiff;
86   Array1OfPnt_gen Poles(1,NbPoles - aSumMulDiff);
87   
88   for (i = 1 + aFMulDiff; i<= NbPoles - aLMulDiff; ++i)
89   {
90     CartesianPoint_gen P = MakeCartesianPoint_gen (aControlPointsList->Value(i));
91     if (! P.IsNull())
92       Poles.SetValue(i - aFMulDiff,P->Pnt_fonc());
93     else
94       return 0;
95   }
96   
97   // --- Does the Curve descriptor LOOKS like a periodic descriptor ? ---
98
99   Standard_Integer SumMult = 0;
100   for (i=1; i<=NbUniqueKnots; i++) {
101     SumMult += Mult.Value(i);
102   }
103   
104   Standard_Boolean shouldBePeriodic;
105   if (SumMult == (NbPoles + Deg + 1)) {
106     shouldBePeriodic = Standard_False;
107   }
108   else if ((Mult.Value(1) == 
109             Mult.Value(NbUniqueKnots)) &&
110            ((SumMult - Mult.Value(1)) == NbPoles)) {
111     shouldBePeriodic = Standard_True;
112   }
113   else {  // --- What is that ??? ---
114     shouldBePeriodic = Standard_False;
115     //cout << "Strange BSpline Curve Descriptor" << endl;
116   }
117
118   BSplineCurve_retour CC;
119   if (SC->IsKind(STANDARD_TYPE(StepGeom_BSplineCurveWithKnotsAndRationalBSplineCurve))) {
120     const Handle(TColStd_HArray1OfReal)& aWeight = BSCWR->WeightsData();
121     TColStd_Array1OfReal W(1,NbPoles);
122     for (i=1; i<=NbPoles; i++)
123       W.SetValue(i,aWeight->Value(i));
124     CC = new BSplineCurve_gen(Poles, W, Kn, Mult, Deg, shouldBePeriodic);
125   }
126   else
127     CC = new BSplineCurve_gen(Poles, Kn, Mult, Deg, shouldBePeriodic);
128
129   // abv 04.07.00 CAX-IF TRJ4: trj4_k1_top-md-203.stp #716 (face #581):
130   // force periodicity on closed curves
131   if ( SC->ClosedCurve() && CC->Degree() >1 && CC->IsClosed() ) {
132     CC->SetPeriodic();
133   }
134   return CC;