]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Curve.cxx
Salome HOME
CurveCreator implementation
[modules/geom.git] / src / CurveCreator / CurveCreator_Curve.cxx
1 // Copyright (C) 2007-2013  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.
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 // File:        CurveCreator_Curve.cxx
24 // Created:     Thu Jun  20 9:54:07 2013
25 // Author:      Sergey KHROMOV
26 //
27
28
29 #include <CurveCreator_Curve.hxx>
30 #include <CurveCreator_Section.hxx>
31
32
33 //=======================================================================
34 // function: Constructor
35 // purpose:
36 //=======================================================================
37 CurveCreator_Curve::CurveCreator_Curve
38                 (const CurveCreator::Dimension theDimension)
39 : myIsLocked  (false),
40   myDimension (theDimension)
41 {
42 }
43
44 //=======================================================================
45 // function: Destructor
46 // purpose:
47 //=======================================================================
48 CurveCreator_Curve::~CurveCreator_Curve()
49 {
50   // Delete all allocated data.
51   clear();
52 }
53
54 //=======================================================================
55 // function: isLocked
56 // purpose:
57 //=======================================================================
58 bool CurveCreator_Curve::isLocked() const
59 {
60   return myIsLocked;
61 }
62
63 //=======================================================================
64 // function: getDimension
65 // purpose:
66 //=======================================================================
67 CurveCreator::Dimension CurveCreator_Curve::getDimension() const
68 {
69   return myDimension;
70 }
71
72 //=======================================================================
73 // function: getNbPoints
74 // purpose:
75 //=======================================================================
76 int CurveCreator_Curve::getNbPoints(const int theISection) const
77 {
78   int aNbCoords = 0;
79
80   if (theISection == -1) {
81     int i = 0;
82     const int aNbSections = getNbSections();
83
84     for (; i < aNbSections; i++) {
85       aNbCoords += mySections[i]->myPoints.size();
86     }
87   } else {
88     aNbCoords = mySections.at(theISection)->myPoints.size();
89   }
90
91   return aNbCoords/myDimension;
92 }
93
94 //=======================================================================
95 // function: getNbSections
96 // purpose:
97 //=======================================================================
98 int CurveCreator_Curve::getNbSections() const
99 {
100   return mySections.size();
101 }
102
103 //=======================================================================
104 // function: getCoordinates
105 // purpose:
106 //=======================================================================
107 CurveCreator::Coordinates CurveCreator_Curve::getCoordinates
108                             (const int theISection, const int theIPnt) const
109 {
110   CurveCreator_Section *aSection = mySections.at(theISection);
111   CurveCreator::Coordinates::const_iterator
112     anIter = aSection->myPoints.begin() + toICoord(theIPnt);
113   CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
114
115   return aResult;
116 }
117
118 //=======================================================================
119 // function: getType
120 // purpose:
121 //=======================================================================
122 CurveCreator::Type CurveCreator_Curve::getType(const int theISection) const
123 {
124   return mySections.at(theISection)->myType;
125 }
126
127 //=======================================================================
128 // function: getPoints
129 // purpose:
130 //=======================================================================
131 const CurveCreator::Coordinates &CurveCreator_Curve::getPoints
132                   (const int theISection) const
133 {
134   return mySections.at(theISection)->myPoints;
135 }
136
137 //=======================================================================
138 // function: isClosed
139 // purpose:
140 //=======================================================================
141 bool CurveCreator_Curve::isClosed(const int theISection) const
142 {
143   return mySections.at(theISection)->myIsClosed;
144 }
145
146 //=======================================================================
147 // function: setType
148 // purpose:
149 //=======================================================================
150 void CurveCreator_Curve::setType
151     (const CurveCreator::Type theType, const int theISection)
152 {
153   if (theISection == -1) {
154     int i = 0;
155     const int aNbSections = getNbSections();
156
157     for (; i < aNbSections; i++) {
158       mySections[i]->myType = theType;
159     }
160   } else {
161     mySections.at(theISection)->myType = theType;
162   }
163 }
164
165 //=======================================================================
166 // function: addPoints
167 // purpose:
168 //=======================================================================
169 void CurveCreator_Curve::addPoints
170    (const CurveCreator::Coordinates &thePoints, const int theISection)
171 {
172   CurveCreator_Section *aSection =
173     (theISection == -1 ? mySections.back() : mySections.at(theISection));
174
175   aSection->myPoints.insert(aSection->myPoints.end(),
176                             thePoints.begin(), thePoints.end());
177 }
178
179 //=======================================================================
180 // function: addSection
181 // purpose:
182 //=======================================================================
183 void CurveCreator_Curve::addSection
184                   (const CurveCreator::Type theType,
185                    const bool theIsClosed,
186                    const CurveCreator::Coordinates &thePoints)
187 {
188   CurveCreator_Section *aSection = new CurveCreator_Section;
189
190   aSection->myType     = theType;
191   aSection->myIsClosed = theIsClosed;
192   aSection->myPoints   = thePoints;
193   mySections.push_back(aSection);
194 }
195
196 //=======================================================================
197 // function: removeSection
198 // purpose:
199 //=======================================================================
200 void CurveCreator_Curve::removeSection(const int theISection)
201 {
202   if (theISection == -1) {
203     delete mySections.back();
204     mySections.pop_back();
205   } else {
206     Sections::iterator anIterRm = mySections.begin() + theISection;
207
208     delete *anIterRm;
209     mySections.erase(anIterRm);
210   }
211 }
212
213 //=======================================================================
214 // function: insertPoints
215 // purpose:
216 //=======================================================================
217 void CurveCreator_Curve::insertPoints
218                    (const CurveCreator::Coordinates &thePoints,
219                     const int theISection,
220                     const int theIPnt)
221 {
222   if (theIPnt == -1) {
223     // Add points to the end of section points.
224     addPoints(thePoints, theISection);
225   } else {
226     CurveCreator_Section *aSection =
227       (theISection == -1 ? mySections.back() : mySections.at(theISection));
228
229     aSection->myPoints.insert(aSection->myPoints.begin() + toICoord(theIPnt),
230                              thePoints.begin(), thePoints.end());
231   }
232 }
233
234 //=======================================================================
235 // function: removePoints
236 // purpose:
237 //=======================================================================
238 void CurveCreator_Curve::removePoints(const int theISection,
239                                       const int theIPnt,
240                                       const int theNbPoints)
241 {
242   CurveCreator_Section *aSection = mySections.at(theISection);
243   CurveCreator::Coordinates::iterator anIterBegin =
244     aSection->myPoints.begin() + toICoord(theIPnt);
245   CurveCreator::Coordinates::iterator anIterEnd =
246     (theNbPoints == -1 ?
247     aSection->myPoints.end() : anIterBegin + toICoord(theNbPoints));
248
249   aSection->myPoints.erase(anIterBegin, anIterEnd);
250 }
251
252 //=======================================================================
253 // function: clear
254 // purpose:
255 //=======================================================================
256 void CurveCreator_Curve::clear()
257 {
258   // Delete all allocated data.
259   int i = 0;
260   const int aNbSections = getNbSections();
261
262   for (; i < aNbSections; i++) {
263     delete mySections[i];
264   }
265
266   mySections.clear();
267 }
268
269 //=======================================================================
270 // function: setCoordinates
271 // purpose:
272 //=======================================================================
273 void CurveCreator_Curve::setCoordinates
274                      (const CurveCreator::Coordinates &theCoords,
275                       const int theISection,
276                       const int theIPnt)
277 {
278   if (theCoords.size() == myDimension) {
279     CurveCreator_Section *aSection = mySections.at(theISection);
280     int i;
281
282     for (i = 0; i < myDimension; i++) {
283       aSection->myPoints.at(toICoord(theIPnt) + i) = theCoords[i];
284     }
285   }
286 }
287
288 //=======================================================================
289 // function: setClosed
290 // purpose:
291 //=======================================================================
292 void CurveCreator_Curve::setClosed(const bool theIsClosed,
293                                    const int theISection)
294 {
295   if (theISection == -1) {
296     int aSize = mySections.size();
297     int i;
298
299     for (i = 0; i < aSize; i++) {
300       mySections[i]->myIsClosed = theIsClosed;
301     }
302   } else {
303     mySections.at(theISection)->myIsClosed = theIsClosed;
304   }
305 }
306
307 //=======================================================================
308 // function: moveSection
309 // purpose:
310 //=======================================================================
311 void CurveCreator_Curve::moveSection(const int theISection,
312                                      const int theNewIndex)
313 {
314   if (theISection != theNewIndex) {
315     CurveCreator_Section *aSection = mySections.at(theISection);
316
317     // Remove section
318     Sections::iterator anIter = mySections.begin() + theISection;
319
320     mySections.erase(anIter);
321
322     // Insert section.
323     anIter = mySections.begin() + theNewIndex;
324     mySections.insert(anIter, aSection);
325   }
326 }
327
328 //=======================================================================
329 // function: join
330 // purpose:
331 //=======================================================================
332 void CurveCreator_Curve::join(const int theISectionTo,
333                               const int theISectionFrom)
334 {
335   if (theISectionTo != theISectionFrom) {
336     CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
337     CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
338
339     aSection1->myPoints.insert(aSection1->myPoints.end(),
340       aSection2->myPoints.begin(), aSection2->myPoints.end());
341
342     removeSection(theISectionFrom);
343   }
344 }
345
346 //=======================================================================
347 // function: join
348 // purpose:
349 //=======================================================================
350 void CurveCreator_Curve::join()
351 {
352   const int aSize = mySections.size();
353
354   if (aSize > 1) {
355     CurveCreator_Section *aSection1 = mySections[0];
356     int i;
357
358     for (i = 1; i < aSize; i++) {
359       CurveCreator_Section *aSection2 = mySections[i];
360
361       aSection1->myPoints.insert(aSection1->myPoints.end(),
362         aSection2->myPoints.begin(), aSection2->myPoints.end());
363       delete aSection2;
364     }
365
366     // Just erace section pointers as they were deleted before.
367     mySections.erase(mySections.begin() + 1, mySections.end());
368   }
369 }
370
371 //=======================================================================
372 // function: toICoord
373 // purpose:
374 //=======================================================================
375 int CurveCreator_Curve::toICoord(const int theIPnt) const
376 {
377   return theIPnt*myDimension;
378 }