Salome HOME
Using stl container instead of Qt.
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Operation.cxx
1 // Copyright (C) 2013  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.
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_Operation.cxx
21 // Author:      Sergey KHROMOV
22
23 #include "CurveCreator_Operation.hxx"
24 #include "CurveCreator_Curve.hxx"
25
26 #include <string>
27 #include <stdlib.h>
28 #include <string.h>
29
30 //=======================================================================
31 // function: Constructor
32 // purpose:
33 //=======================================================================
34 CurveCreator_Operation::CurveCreator_Operation()
35 : myType  (CurveCreator_Operation::Unknown),
36   myPData (NULL)
37 {
38 }
39
40 //=======================================================================
41 // function: Destructor
42 // purpose:
43 //=======================================================================
44 CurveCreator_Operation::~CurveCreator_Operation()
45 {
46   clear();
47 }
48
49 //=======================================================================
50 // function: Constructor
51 // purpose:
52 //=======================================================================
53 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
54 {
55   bool isOK = false;
56
57   if (theType == CurveCreator_Operation::Clear ||
58       theType == CurveCreator_Operation::Join) {
59     clear();
60     myType = theType;
61     isOK   = true;
62   }
63
64   return isOK;
65 }
66
67 //=======================================================================
68 // function: Constructor
69 // purpose:
70 //=======================================================================
71 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
72                                   const int theIntParam)
73 {
74   bool isOK = false;
75
76   if (theType == CurveCreator_Operation::RemoveSection) {
77     int *pData = (int *)allocate(sizeof(int));
78
79     pData[0] = theIntParam;
80     myType   = theType;
81     isOK     = true;
82   }
83
84   return isOK;
85 }
86
87 //=======================================================================
88 // function: Constructor
89 // purpose:
90 //=======================================================================
91 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
92                                   const int theIntParam1,
93                                   const int theIntParam2)
94 {
95   bool isOK = false;
96
97   if (theType == CurveCreator_Operation::SetType     ||
98       theType == CurveCreator_Operation::SetClosed   ||
99       theType == CurveCreator_Operation::MoveSection ||
100       theType == CurveCreator_Operation::RemovePoints ||
101       theType == CurveCreator_Operation::Join) {
102     int *pData = (int *)allocate(2*sizeof(int));
103
104     pData[0] = theIntParam1;
105     pData[1] = theIntParam2;
106     myType   = theType;
107     isOK     = true;
108   }
109
110   return isOK;
111 }
112
113 //=======================================================================
114 // function: Constructor
115 // purpose:
116 //=======================================================================
117 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
118                                   const int theIntParam1,
119                                   const int theIntParam2,
120                                   const int theIntParam3)
121 {
122   bool isOK = false;
123
124   if (theType == CurveCreator_Operation::RemovePoints) {
125     int *pData = (int *)allocate(3*sizeof(int));
126
127     pData[0] = theIntParam1;
128     pData[1] = theIntParam2;
129     pData[2] = theIntParam3;
130     myType   = theType;
131     isOK     = true;
132   }
133
134   return isOK;
135 }
136
137 //=======================================================================
138 // function: Constructor
139 // purpose:
140 //=======================================================================
141 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
142                                   const CurveCreator::Coordinates &theCoords,
143                                   const int theIntParam)
144 {
145   bool isOK = false;
146
147   if (theType == CurveCreator_Operation::AddPoints) {
148     const int aNbCoords = theCoords.size();
149     const size_t aSize =
150       2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
151     int *pIntData = (int *)allocate(aSize);
152
153     *pIntData++ = theIntParam;
154     *pIntData++ = aNbCoords;
155
156     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
157     int i = 0;
158
159     for (; i < aNbCoords; i++) {
160       *pRealData++ = theCoords[i];
161     }
162
163     myType = theType;
164     isOK   = true;
165   }
166
167   return isOK;
168 }
169
170 //=======================================================================
171 // function: Constructor
172 // purpose:
173 //=======================================================================
174 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
175                                   const CurveCreator::Coordinates &theCoords,
176                                   const int theIntParam1,
177                                   const int theIntParam2)
178 {
179   bool isOK = false;
180
181   if (theType == CurveCreator_Operation::AddSection   ||
182       theType == CurveCreator_Operation::InsertPoints ||
183       theType == CurveCreator_Operation::SetCoordinates) {
184     const int aNbCoords = theCoords.size();
185     const size_t aSize =
186       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
187     int *pIntData = (int *)allocate(aSize);
188
189     *pIntData++ = theIntParam1;
190     *pIntData++ = theIntParam2;
191     *pIntData++ = aNbCoords;
192
193     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
194     int i = 0;
195
196     for (; i < aNbCoords; i++) {
197       *pRealData++ = theCoords[i];
198     }
199
200     myType = theType;
201     isOK   = true;
202   }
203
204   return isOK;
205 }
206
207 //=======================================================================
208 // function: Constructor
209 // purpose:
210 //=======================================================================
211 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
212                                   const std::string& theName,
213                                   const CurveCreator::Coordinates &theCoords,
214                                   const int theIntParam1,
215                                   const int theIntParam2)
216 {
217   bool isOK = false;
218   if (theType == CurveCreator_Operation::AddSection ) {
219     const int aNbCoords = theCoords.size();
220     const size_t aSize =
221       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
222     int *pIntData = (int *)allocate(aSize);
223
224     *pIntData++ = theIntParam1;
225     *pIntData++ = theIntParam2;
226     char* aStrPtr = (char*)pIntData;
227     if( !theName.empty() ){
228         strcpy( aStrPtr, theName.c_str() );
229         aStrPtr += theName.length();
230     }
231     else{
232         *aStrPtr = 0;
233     }
234     aStrPtr++;
235     pIntData = (int*)aStrPtr;
236     *pIntData++ = aNbCoords;
237
238     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)aStrPtr;
239     int i = 0;
240
241     for (; i < aNbCoords; i++) {
242       *pRealData++ = theCoords[i];
243     }
244
245     myType = theType;
246     isOK   = true;
247   }
248
249   return isOK;
250 }
251
252 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
253                                   const std::string &theName,
254                                   const int theIntParam1 )
255 {
256     if (theType == CurveCreator_Operation::RenameSection ) {
257         size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
258         int *pIntData = (int *)allocate(aSize);
259         *pIntData = theIntParam1;
260         pIntData++;
261         if( !theName.empty() ){
262             strcpy( (char*)pIntData, theName.c_str() );
263         }
264         else{
265             *((char*)pIntData) = 0;
266         }
267         myType = theType;
268         return true;
269     }
270     return false;
271 }
272
273 //=======================================================================
274 // function: apply
275 // purpose:
276 //=======================================================================
277 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
278 {
279   if (theCurve != NULL) {
280     int *pInt = (int *)myPData;
281
282     switch (myType) {
283       case CurveCreator_Operation::AddPoints:
284       case CurveCreator_Operation::InsertPoints:
285         {
286           CurveCreator::Coordinates aCoords;
287
288           getCoords(&pInt[2], aCoords);
289           std::vector<int> anISections, anIPnts;
290           anISections.push_back( pInt[0] );
291           anIPnts.push_back( pInt[1] );
292           theCurve->addPointsInternal(aCoords, anISections, anIPnts);
293         }
294         break;
295       case CurveCreator_Operation::RemovePoints:
296         {
297           std::vector<int> anISections, anIPnts;
298           anISections.push_back( pInt[0] );
299           anIPnts.push_back( pInt[1] );
300           theCurve->removePointsInternal(anISections, anIPnts);
301         }
302         break;
303       case CurveCreator_Operation::SetType:
304         {
305           const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
306
307           theCurve->setSectionTypeInternal( pInt[1], aType );
308         }
309         break;
310       case CurveCreator_Operation::Clear:
311         theCurve->clearInternal();
312         break;
313       case CurveCreator_Operation::SetCoordinates:
314         {
315           CurveCreator::Coordinates aCoords;
316
317           getCoords(&pInt[2], aCoords);
318           theCurve->setPointInternal(pInt[0], pInt[1], aCoords);
319         }
320         break;
321       case CurveCreator_Operation::SetClosed:
322         theCurve->setClosedInternal((pInt[0] != 0), pInt[1]);
323         break;
324       case CurveCreator_Operation::MoveSection:
325         theCurve->moveSectionInternal(pInt[0], pInt[1]);
326         break;
327       case CurveCreator_Operation::Join:
328         if (myPData == NULL) {
329           theCurve->joinInternal();
330         } else {
331           theCurve->joinInternal(pInt[0], pInt[1]);
332         }
333         break;
334       case CurveCreator_Operation::AddSection:
335         {
336           const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
337
338           std::string aName = std::string((char*)&pInt[2]);
339
340           CurveCreator::Coordinates aCoords;
341
342           char* aPtr =  ((char*)&pInt[2]);
343           aPtr += (aName.length()) + 1;
344           getCoords((int*)aPtr, aCoords);
345           theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords);
346         }
347         break;
348       case CurveCreator_Operation::RemoveSection:
349         theCurve->removeSectionInternal(pInt[0]);
350         break;
351       case CurveCreator_Operation::RenameSection:
352         {
353             std::string aName = std::string((char*)&pInt[1]);
354             theCurve->setSectionNameInternal(pInt[0], aName);
355         }
356         break;
357       default:
358         break;
359     }
360   }
361 }
362
363 //=======================================================================
364 // function: allocate
365 // purpose:
366 //=======================================================================
367 void *CurveCreator_Operation::allocate(const size_t theSize)
368 {
369   if (myPData != NULL) {
370     clear();
371   }
372
373   myPData = malloc(theSize);
374
375   return myPData;
376 }
377
378 //=======================================================================
379 // function: clear
380 // purpose:
381 //=======================================================================
382 void CurveCreator_Operation::clear()
383 {
384   myType = CurveCreator_Operation::Unknown;
385
386   if (myPData != NULL) {
387     free(myPData);
388     myPData = NULL;
389   }
390 }
391
392 //=======================================================================
393 // function: getCoords
394 // purpose:
395 //=======================================================================
396 void CurveCreator_Operation::getCoords
397           (int *thePInt, CurveCreator::Coordinates &theCoords) const
398 {
399   const int aNbPnts = *thePInt;
400   CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
401
402   for (int i = 0; i < aNbPnts; i++) {
403     theCoords.push_back(pCoord[i]);
404   }
405 }