Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / CurveCreator / CurveCreator_ICurve.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        CurveCreator_ICurve.cxx
21 // Author:      Sergey KHROMOV
22
23 #include "CurveCreator_ICurve.hxx"
24 #include "CurveCreator_Section.hxx"
25 #include "CurveCreator_Listener.hxx"
26
27 #include <stdio.h>
28
29 //=======================================================================
30 // function: Constructor
31 // purpose:
32 //=======================================================================
33 CurveCreator_ICurve::CurveCreator_ICurve
34                 (const CurveCreator::Dimension theDimension)
35 : myIsLocked  (false),
36   myDimension (theDimension),
37   myListener(NULL)
38 {
39 }
40
41 //=======================================================================
42 // function: Destructor
43 // purpose:
44 //=======================================================================
45 CurveCreator_ICurve::~CurveCreator_ICurve()
46 {
47   // Delete all allocated data.
48   clear();
49 }
50
51 //=======================================================================
52 // function: isLocked
53 // purpose:
54 //=======================================================================
55 bool CurveCreator_ICurve::isLocked() const
56 {
57   return myIsLocked;
58 }
59
60 //=======================================================================
61 // function: getDimension
62 // purpose:
63 //=======================================================================
64 CurveCreator::Dimension CurveCreator_ICurve::getDimension() const
65 {
66   return myDimension;
67 }
68
69 //=======================================================================
70 // function: getNbPoints
71 // purpose:
72 //=======================================================================
73 int CurveCreator_ICurve::getNbPoints(const int theISection) const
74 {
75   int aNbCoords = 0;
76
77   if (theISection == -1) {
78     int i = 0;
79     const int aNbSections = getNbSections();
80
81     for (; i < aNbSections; i++) {
82       aNbCoords += mySections[i]->myPoints.size();
83     }
84   } else {
85     aNbCoords = mySections.at(theISection)->myPoints.size();
86   }
87
88   return aNbCoords/myDimension;
89 }
90
91 //=======================================================================
92 // function: getNbSections
93 // purpose:
94 //=======================================================================
95 int CurveCreator_ICurve::getNbSections() const
96 {
97   return mySections.size();
98 }
99
100 //=======================================================================
101 // function: getCoordinates
102 // purpose:
103 //=======================================================================
104 CurveCreator::Coordinates CurveCreator_ICurve::getCoordinates
105                             (const int theISection, const int theIPnt) const
106 {
107   CurveCreator_Section *aSection = mySections.at(theISection);
108   CurveCreator::Coordinates::const_iterator
109     anIter = aSection->myPoints.begin() + toICoord(theIPnt);
110   CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
111
112   return aResult;
113 }
114
115 //=======================================================================
116 // function: getType
117 // purpose:
118 //=======================================================================
119 CurveCreator::Type CurveCreator_ICurve::getType(const int theISection) const
120 {
121   return mySections.at(theISection)->myType;
122 }
123
124 //=======================================================================
125 // function: getPoints
126 // purpose:
127 //=======================================================================
128 const CurveCreator::Coordinates &CurveCreator_ICurve::getPoints
129                   (const int theISection) const
130 {
131   return mySections.at(theISection)->myPoints;
132 }
133
134 //=======================================================================
135 // function: isClosed
136 // purpose:
137 //=======================================================================
138 bool CurveCreator_ICurve::isClosed(const int theISection) const
139 {
140   return mySections.at(theISection)->myIsClosed;
141 }
142
143 std::string CurveCreator_ICurve::getSectionName(const int theISection) const
144 {
145     return mySections.at(theISection)->myName;
146 }
147
148 //=======================================================================
149 // function: setType
150 // purpose:
151 //=======================================================================
152 void CurveCreator_ICurve::setType
153     (const CurveCreator::Type theType, const int theISection)
154 {
155   if (theISection == -1) {
156     int i = 0;
157     const int aNbSections = getNbSections();
158
159     for (; i < aNbSections; i++) {
160       mySections[i]->myType = theType;
161     }
162     if( myListener )
163       myListener->curveChanged();
164   } else {
165     if( mySections.at(theISection)->myType != theType ){
166       mySections.at(theISection)->myType = theType;
167       if( myListener )
168         myListener->sectionTypeChanged(theISection);
169     }
170   }
171 }
172
173 //=======================================================================
174 // function: addSection
175 // purpose:
176 //=======================================================================
177 void CurveCreator_ICurve::addSection
178                   (const std::string& theName,
179                    const CurveCreator::Type theType,
180                    const bool theIsClosed,
181                    const CurveCreator::Coordinates &thePoints)
182 {
183   CurveCreator_Section *aSection = new CurveCreator_Section;
184
185   std::string aName = theName;
186   if( aName.empty() ){
187       aName = getUnicSectionName();
188   }
189   aSection->myName     = aName;
190   aSection->myType     = theType;
191   aSection->myIsClosed = theIsClosed;
192   aSection->myPoints   = thePoints;
193   mySections.push_back(aSection);
194   if( myListener )
195     myListener->sectionAdded( -1 );
196 }
197
198 //=======================================================================
199 // function: removeSection
200 // purpose:
201 //=======================================================================
202 void CurveCreator_ICurve::removeSection(const int theISection)
203 {
204   if (theISection == -1) {
205     delete mySections.back();
206     mySections.pop_back();
207   } else {
208     Sections::iterator anIterRm = mySections.begin() + theISection;
209
210     delete *anIterRm;
211     mySections.erase(anIterRm);
212   }
213   if( myListener )
214     myListener->sectionRemoved(theISection);
215 }
216
217 //=======================================================================
218 // function: insertPoints
219 // purpose:
220 //=======================================================================
221 void CurveCreator_ICurve::insertPoints
222                    (const CurveCreator::Coordinates &thePoints,
223                     const int theISection,
224                     const int theIPnt)
225 {
226   if (theIPnt == -1) {
227     // Add points to the end of section points.
228     addPoints(thePoints, theISection);
229   } else {
230     CurveCreator_Section *aSection =
231       (theISection == -1 ? mySections.back() : mySections.at(theISection));
232
233     aSection->myPoints.insert(aSection->myPoints.begin() + toICoord(theIPnt),
234                              thePoints.begin(), thePoints.end());
235     if( myListener )
236       myListener->pointInserted( theISection, theIPnt );
237   }
238 }
239
240 void CurveCreator_ICurve::movePoint(const int theISection, const int theIPointFrom, const int theNewIndex)
241 {
242     CurveCreator::Coordinates aCoords = getCoordinates(theISection, theIPointFrom );
243     insertPoints(aCoords, theISection, theNewIndex+1);
244     int aRemPntIndx = theIPointFrom;
245     if( theNewIndex < theIPointFrom )
246         aRemPntIndx++;
247     removePoints(theISection, aRemPntIndx, 1 );
248 }
249
250 //=======================================================================
251 // function: removePoints
252 // purpose:
253 //=======================================================================
254 void CurveCreator_ICurve::removePoints(const int theISection,
255                                       const int theIPnt,
256                                       const int theNbPoints)
257 {
258   CurveCreator_Section *aSection = mySections.at(theISection);
259   CurveCreator::Coordinates::iterator anIterBegin =
260     aSection->myPoints.begin() + toICoord(theIPnt);
261   CurveCreator::Coordinates::iterator anIterEnd =
262     (theNbPoints == -1 ?
263     aSection->myPoints.end() : anIterBegin + toICoord(theNbPoints));
264
265   aSection->myPoints.erase(anIterBegin, anIterEnd);
266   if( myListener )
267     myListener->pointRemoved(theISection, theIPnt, theNbPoints );
268 }
269
270 //=======================================================================
271 // function: clear
272 // purpose:
273 //=======================================================================
274 void CurveCreator_ICurve::clear()
275 {
276   // Delete all allocated data.
277   int i = 0;
278   const int aNbSections = getNbSections();
279
280   for (; i < aNbSections; i++) {
281     delete mySections[i];
282   }
283
284   mySections.clear();
285   if( myListener )
286     myListener->curveChanged();
287 }
288
289 //=======================================================================
290 // function: setCoordinates
291 // purpose:
292 //=======================================================================
293 void CurveCreator_ICurve::setCoordinates
294                      (const CurveCreator::Coordinates &theCoords,
295                       const int theISection,
296                       const int theIPnt)
297 {
298   if (theCoords.size() == myDimension) {
299     CurveCreator_Section *aSection = mySections.at(theISection);
300     int i;
301
302     for (i = 0; i < myDimension; i++) {
303       aSection->myPoints.at(toICoord(theIPnt) + i) = theCoords[i];
304     }
305
306     if( myListener )
307       myListener->pointChanged( theISection, theIPnt );
308   }
309 }
310
311 //=======================================================================
312 // function: setClosed
313 // purpose:
314 //=======================================================================
315 void CurveCreator_ICurve::setClosed(const bool theIsClosed,
316                                    const int theISection)
317 {
318   if (theISection == -1) {
319     int aSize = mySections.size();
320     int i;
321
322     for (i = 0; i < aSize; i++) {
323       mySections[i]->myIsClosed = theIsClosed;
324       if( myListener ){
325         myListener->sectionClosed( theISection, theIsClosed );
326       }
327     }
328   } else {
329     mySections.at(theISection)->myIsClosed = theIsClosed;
330     if( myListener ){
331       myListener->sectionClosed( theISection, theIsClosed );
332     }
333   }
334 }
335
336 /** Set name of the specified section.
337  */
338 void CurveCreator_ICurve::setName( const std::string& theName, const int theISection )
339 {
340     if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
341         mySections.at(theISection)->myName = theName;
342     }
343 }
344
345 //=======================================================================
346 // function: moveSection
347 // purpose:
348 //=======================================================================
349 void CurveCreator_ICurve::moveSection(const int theISection,
350                                      const int theNewIndex)
351 {
352   if (theISection != theNewIndex) {
353     CurveCreator_Section *aSection = mySections.at(theISection);
354
355     // Remove section
356     Sections::iterator anIter = mySections.begin() + theISection;
357
358     mySections.erase(anIter);
359
360     // Insert section.
361     anIter = mySections.begin() + theNewIndex;
362     mySections.insert(anIter, aSection);
363   }
364 }
365
366 //=======================================================================
367 // function: join
368 // purpose:
369 //=======================================================================
370 void CurveCreator_ICurve::join(const int theISectionTo,
371                               const int theISectionFrom)
372 {
373   if (theISectionTo != theISectionFrom) {
374     CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
375     CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
376
377     aSection1->myPoints.insert(aSection1->myPoints.end(),
378     aSection2->myPoints.begin(), aSection2->myPoints.end());
379
380     removeSection(theISectionFrom);
381     if( myListener )
382       myListener->curveChanged();
383   }
384 }
385
386 //=======================================================================
387 // function: join
388 // purpose:
389 //=======================================================================
390 void CurveCreator_ICurve::join()
391 {
392   const int aSize = mySections.size();
393
394   if (aSize > 1) {
395     CurveCreator_Section *aSection1 = mySections[0];
396     int i;
397
398     for (i = 1; i < aSize; i++) {
399       CurveCreator_Section *aSection2 = mySections[i];
400
401       aSection1->myPoints.insert(aSection1->myPoints.end(),
402         aSection2->myPoints.begin(), aSection2->myPoints.end());
403       delete aSection2;
404     }
405
406     // Just erace section pointers as they were deleted before.
407     mySections.erase(mySections.begin() + 1, mySections.end());
408     if( myListener )
409       myListener->curveChanged();
410   }
411 }
412
413 //=======================================================================
414 // function: toICoord
415 // purpose:
416 //=======================================================================
417 int CurveCreator_ICurve::toICoord(const int theIPnt) const
418 {
419   return theIPnt*myDimension;
420 }
421
422 //=======================================================================
423 // function: getUnicSectionName
424 // purpose: return unic section name
425 //=======================================================================
426 std::string CurveCreator_ICurve::getUnicSectionName()
427 {
428     for( int i = 0 ; i < 1000000 ; i++ ){
429         char aBuffer[255];
430         sprintf( aBuffer, "Section_%d", i+1 );
431         std::string aName(aBuffer);
432         int j;
433         for( j = 0 ; j < mySections.size() ; j++ ){
434             if( mySections[j]->myName == aName )
435               break;
436         }
437         if( j == mySections.size() )
438             return aName;
439     }
440     return "";
441 }
442
443 //=======================================================================
444 // function: setListener
445 // purpose: set curve changes listener
446 //=======================================================================
447 void CurveCreator_ICurve::setListener( CurveCreator_Listener*   theListener )
448 {
449   myListener = theListener;
450 }
451
452 //=======================================================================
453 // function: setListener
454 // purpose: set curve changes listener
455 //=======================================================================
456 void CurveCreator_ICurve::removeListener()
457 {
458   myListener = NULL;
459 }