]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Curve.cxx
Salome HOME
CurveCreator was updated
[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 std::string CurveCreator_Curve::getSectionName(const int theISection) const
147 {
148     return mySections.at(theISection)->myName;
149 }
150
151 //=======================================================================
152 // function: setType
153 // purpose:
154 //=======================================================================
155 void CurveCreator_Curve::setType
156     (const CurveCreator::Type theType, const int theISection)
157 {
158   if (theISection == -1) {
159     int i = 0;
160     const int aNbSections = getNbSections();
161
162     for (; i < aNbSections; i++) {
163       mySections[i]->myType = theType;
164     }
165   } else {
166     mySections.at(theISection)->myType = theType;
167   }
168 }
169
170 //=======================================================================
171 // function: addPoints
172 // purpose:
173 //=======================================================================
174 void CurveCreator_Curve::addPoints
175    (const CurveCreator::Coordinates &thePoints, const int theISection)
176 {
177   CurveCreator_Section *aSection =
178     (theISection == -1 ? mySections.back() : mySections.at(theISection));
179
180   aSection->myPoints.insert(aSection->myPoints.end(),
181                             thePoints.begin(), thePoints.end());
182 }
183
184 //=======================================================================
185 // function: addSection
186 // purpose:
187 //=======================================================================
188 void CurveCreator_Curve::addSection
189                   (const std::string& theName,
190                    const CurveCreator::Type theType,
191                    const bool theIsClosed,
192                    const CurveCreator::Coordinates &thePoints)
193 {
194   CurveCreator_Section *aSection = new CurveCreator_Section;
195
196   std::string aName = theName;
197   if( aName.empty() ){
198       aName = getUnicSectionName();
199   }
200   aSection->myName     = aName;
201   aSection->myType     = theType;
202   aSection->myIsClosed = theIsClosed;
203   aSection->myPoints   = thePoints;
204   mySections.push_back(aSection);
205 }
206
207 //=======================================================================
208 // function: removeSection
209 // purpose:
210 //=======================================================================
211 void CurveCreator_Curve::removeSection(const int theISection)
212 {
213   if (theISection == -1) {
214     delete mySections.back();
215     mySections.pop_back();
216   } else {
217     Sections::iterator anIterRm = mySections.begin() + theISection;
218
219     delete *anIterRm;
220     mySections.erase(anIterRm);
221   }
222 }
223
224 //=======================================================================
225 // function: insertPoints
226 // purpose:
227 //=======================================================================
228 void CurveCreator_Curve::insertPoints
229                    (const CurveCreator::Coordinates &thePoints,
230                     const int theISection,
231                     const int theIPnt)
232 {
233   if (theIPnt == -1) {
234     // Add points to the end of section points.
235     addPoints(thePoints, theISection);
236   } else {
237     CurveCreator_Section *aSection =
238       (theISection == -1 ? mySections.back() : mySections.at(theISection));
239
240     aSection->myPoints.insert(aSection->myPoints.begin() + toICoord(theIPnt),
241                              thePoints.begin(), thePoints.end());
242   }
243 }
244
245 void CurveCreator_Curve::movePoint(const int theISection, const int theIPointFrom, const int theNewIndex)
246 {
247     CurveCreator::Coordinates aCoords = getCoordinates(theISection, theIPointFrom );
248     insertPoints(aCoords, theISection, theNewIndex+1);
249     int aRemPntIndx = theIPointFrom;
250     if( theNewIndex < theIPointFrom )
251         aRemPntIndx++;
252     removePoints(theISection, aRemPntIndx, 1 );
253 }
254
255 //=======================================================================
256 // function: removePoints
257 // purpose:
258 //=======================================================================
259 void CurveCreator_Curve::removePoints(const int theISection,
260                                       const int theIPnt,
261                                       const int theNbPoints)
262 {
263   CurveCreator_Section *aSection = mySections.at(theISection);
264   CurveCreator::Coordinates::iterator anIterBegin =
265     aSection->myPoints.begin() + toICoord(theIPnt);
266   CurveCreator::Coordinates::iterator anIterEnd =
267     (theNbPoints == -1 ?
268     aSection->myPoints.end() : anIterBegin + toICoord(theNbPoints));
269
270   aSection->myPoints.erase(anIterBegin, anIterEnd);
271 }
272
273 //=======================================================================
274 // function: clear
275 // purpose:
276 //=======================================================================
277 void CurveCreator_Curve::clear()
278 {
279   // Delete all allocated data.
280   int i = 0;
281   const int aNbSections = getNbSections();
282
283   for (; i < aNbSections; i++) {
284     delete mySections[i];
285   }
286
287   mySections.clear();
288 }
289
290 //=======================================================================
291 // function: setCoordinates
292 // purpose:
293 //=======================================================================
294 void CurveCreator_Curve::setCoordinates
295                      (const CurveCreator::Coordinates &theCoords,
296                       const int theISection,
297                       const int theIPnt)
298 {
299   if (theCoords.size() == myDimension) {
300     CurveCreator_Section *aSection = mySections.at(theISection);
301     int i;
302
303     for (i = 0; i < myDimension; i++) {
304       aSection->myPoints.at(toICoord(theIPnt) + i) = theCoords[i];
305     }
306   }
307 }
308
309 //=======================================================================
310 // function: setClosed
311 // purpose:
312 //=======================================================================
313 void CurveCreator_Curve::setClosed(const bool theIsClosed,
314                                    const int theISection)
315 {
316   if (theISection == -1) {
317     int aSize = mySections.size();
318     int i;
319
320     for (i = 0; i < aSize; i++) {
321       mySections[i]->myIsClosed = theIsClosed;
322     }
323   } else {
324     mySections.at(theISection)->myIsClosed = theIsClosed;
325   }
326 }
327
328 /** Set name of the specified section.
329  */
330 void CurveCreator_Curve::setName( const std::string& theName, const int theISection )
331 {
332     if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
333         mySections.at(theISection)->myName = theName;
334     }
335 }
336
337 //=======================================================================
338 // function: moveSection
339 // purpose:
340 //=======================================================================
341 void CurveCreator_Curve::moveSection(const int theISection,
342                                      const int theNewIndex)
343 {
344   if (theISection != theNewIndex) {
345     CurveCreator_Section *aSection = mySections.at(theISection);
346
347     // Remove section
348     Sections::iterator anIter = mySections.begin() + theISection;
349
350     mySections.erase(anIter);
351
352     // Insert section.
353     anIter = mySections.begin() + theNewIndex;
354     mySections.insert(anIter, aSection);
355   }
356 }
357
358 //=======================================================================
359 // function: join
360 // purpose:
361 //=======================================================================
362 void CurveCreator_Curve::join(const int theISectionTo,
363                               const int theISectionFrom)
364 {
365   if (theISectionTo != theISectionFrom) {
366     CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
367     CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
368
369     aSection1->myPoints.insert(aSection1->myPoints.end(),
370       aSection2->myPoints.begin(), aSection2->myPoints.end());
371
372     removeSection(theISectionFrom);
373   }
374 }
375
376 //=======================================================================
377 // function: join
378 // purpose:
379 //=======================================================================
380 void CurveCreator_Curve::join()
381 {
382   const int aSize = mySections.size();
383
384   if (aSize > 1) {
385     CurveCreator_Section *aSection1 = mySections[0];
386     int i;
387
388     for (i = 1; i < aSize; i++) {
389       CurveCreator_Section *aSection2 = mySections[i];
390
391       aSection1->myPoints.insert(aSection1->myPoints.end(),
392         aSection2->myPoints.begin(), aSection2->myPoints.end());
393       delete aSection2;
394     }
395
396     // Just erace section pointers as they were deleted before.
397     mySections.erase(mySections.begin() + 1, mySections.end());
398   }
399 }
400
401 //=======================================================================
402 // function: toICoord
403 // purpose:
404 //=======================================================================
405 int CurveCreator_Curve::toICoord(const int theIPnt) const
406 {
407   return theIPnt*myDimension;
408 }
409
410 //=======================================================================
411 // function: getUnicSectionName
412 // purpose: return unic section name
413 //=======================================================================
414 std::string CurveCreator_Curve::getUnicSectionName()
415 {
416     for( int i = 0 ; i < 1000000 ; i++ ){
417         char aBuffer[255];
418         sprintf( aBuffer, "Section_%d", i+1 );
419         std::string aName(aBuffer);
420         int j;
421         for( j = 0 ; j < mySections.size() ; j++ ){
422             if( mySections[j]->myName == aName )
423               break;
424         }
425         if( j == mySections.size() )
426             return aName;
427     }
428     return "";
429 }