Salome HOME
970595d4f4c1cf7019e321eb41a67472c0cf5d11
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IPolyline2D.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23
24 #include "GEOMImpl_IPolyline2D.hxx"
25
26 #include <TColStd_HArray1OfInteger.hxx>
27
28
29 //=============================================================================
30 /*!
31  *  SetCoords
32  */
33 //=============================================================================
34 void GEOMImpl_IPolyline2D::SetCoords
35                   (const std::list <std::list <double> > &theValue)
36 {
37   const Standard_Integer aNbSec = theValue.size();
38
39   if (aNbSec > 0) {
40     // Compute the total number of points and fill the array of start indices.
41     Standard_Integer                                i;
42     Standard_Integer                                aNbCoords = 0;
43     Handle(TColStd_HArray1OfInteger)                anIndices =
44       new TColStd_HArray1OfInteger(1, aNbSec);
45     Handle(TColStd_HArray1OfReal)                   aCoords;
46     std::list <std::list <double> >::const_iterator aSecIter = theValue.begin();
47
48     for (i = 1; aSecIter != theValue.end(); ++aSecIter, ++i) {
49       anIndices->SetValue(i, aNbCoords + 1);
50       aNbCoords += aSecIter->size();
51     }
52
53     if (aNbCoords > 0) {
54       // Fill the array of coordinates.
55       std::list<double>::const_iterator aCIter;
56
57       aCoords  = new TColStd_HArray1OfReal(1, aNbCoords);
58       aSecIter = theValue.begin();
59
60       for (i = 1; aSecIter != theValue.end(); ++aSecIter) {
61         for (aCIter = aSecIter->begin(); aCIter != aSecIter->end(); ++aCIter) {
62           aCoords->SetValue(i++, *aCIter);
63         }
64       }
65     }
66
67     // Store the coordinates.
68     if (aCoords.IsNull() == Standard_False) {
69       _func->SetRealArray(POLY_ARG_COORDS, aCoords);
70     }
71
72     _func->SetIntegerArray(POLY_ARG_START_INDICES, anIndices);
73   }
74 }
75
76 //=============================================================================
77 /*!
78  *  GetCoords
79  */
80 //=============================================================================
81 void GEOMImpl_IPolyline2D::GetCoords(std::list <std::list <double> > &theValue)
82 {
83   theValue.clear();
84
85   Handle(TColStd_HArray1OfReal)    aCoords   =
86     _func->GetRealArray(POLY_ARG_COORDS);
87   Handle(TColStd_HArray1OfInteger) anIndices =
88     _func->GetIntegerArray(POLY_ARG_START_INDICES);
89
90   if (anIndices.IsNull() == Standard_False) {
91     const Standard_Integer aNbSec = anIndices->Length();
92
93     // Create an empty sections.
94     theValue.resize(aNbSec);
95
96     if (aCoords.IsNull() == Standard_False) {
97       Standard_Integer i;
98       Standard_Integer j;
99       std::list <std::list <double> >::iterator anIt = theValue.begin();
100
101       for (i = anIndices->Lower(); i <= anIndices->Upper(); ++i, ++anIt) {
102         const Standard_Integer iCoord1 = anIndices->Value(i);
103         const Standard_Integer iCoord2 = i + 1 > anIndices->Upper() ?
104           aCoords->Upper() + 1 : anIndices->Value(i + 1);
105
106         for (j = iCoord1; j < iCoord2; ++j) {
107           anIt->push_back(aCoords->Value(j));
108         }
109       }
110     }
111   }
112 }