Salome HOME
Fix compilation problem with -DSALOME_BUILD_GUI=OFF mode.
[modules/geom.git] / src / CurveCreator / 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::Join) {
101     int *pData = (int *)allocate(2*sizeof(int));
102
103     pData[0] = theIntParam1;
104     pData[1] = theIntParam2;
105     myType   = theType;
106     isOK     = true;
107   }
108
109   return isOK;
110 }
111
112 //=======================================================================
113 // function: Constructor
114 // purpose:
115 //=======================================================================
116 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
117                                   const int theIntParam1,
118                                   const int theIntParam2,
119                                   const int theIntParam3)
120 {
121   bool isOK = false;
122
123   if (theType == CurveCreator_Operation::RemovePoints) {
124     int *pData = (int *)allocate(3*sizeof(int));
125
126     pData[0] = theIntParam1;
127     pData[1] = theIntParam2;
128     pData[2] = theIntParam3;
129     myType   = theType;
130     isOK     = true;
131   }
132
133   return isOK;
134 }
135
136 //=======================================================================
137 // function: Constructor
138 // purpose:
139 //=======================================================================
140 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
141                                   const CurveCreator::Coordinates &theCoords,
142                                   const int theIntParam)
143 {
144   bool isOK = false;
145
146   if (theType == CurveCreator_Operation::AddPoints) {
147     const int aNbCoords = theCoords.size();
148     const size_t aSize =
149       2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
150     int *pIntData = (int *)allocate(aSize);
151
152     *pIntData++ = theIntParam;
153     *pIntData++ = aNbCoords;
154
155     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
156     int i = 0;
157
158     for (; i < aNbCoords; i++) {
159       *pRealData++ = theCoords[i];
160     }
161
162     myType = theType;
163     isOK   = true;
164   }
165
166   return isOK;
167 }
168
169 //=======================================================================
170 // function: Constructor
171 // purpose:
172 //=======================================================================
173 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
174                                   const CurveCreator::Coordinates &theCoords,
175                                   const int theIntParam1,
176                                   const int theIntParam2)
177 {
178   bool isOK = false;
179
180   if (theType == CurveCreator_Operation::AddSection   ||
181       theType == CurveCreator_Operation::InsertPoints ||
182       theType == CurveCreator_Operation::SetCoordinates) {
183     const int aNbCoords = theCoords.size();
184     const size_t aSize =
185       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
186     int *pIntData = (int *)allocate(aSize);
187
188     *pIntData++ = theIntParam1;
189     *pIntData++ = theIntParam2;
190     *pIntData++ = aNbCoords;
191
192     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
193     int i = 0;
194
195     for (; i < aNbCoords; i++) {
196       *pRealData++ = theCoords[i];
197     }
198
199     myType = theType;
200     isOK   = true;
201   }
202
203   return isOK;
204 }
205
206 //=======================================================================
207 // function: Constructor
208 // purpose:
209 //=======================================================================
210 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
211                                   const std::string& theName,
212                                   const CurveCreator::Coordinates &theCoords,
213                                   const int theIntParam1,
214                                   const int theIntParam2)
215 {
216   bool isOK = false;
217   if (theType == CurveCreator_Operation::AddSection ) {
218     const int aNbCoords = theCoords.size();
219     const size_t aSize =
220       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
221     int *pIntData = (int *)allocate(aSize);
222
223     *pIntData++ = theIntParam1;
224     *pIntData++ = theIntParam2;
225     char* aStrPtr = (char*)pIntData;
226     if( !theName.empty() ){
227         strcpy( aStrPtr, theName.c_str() );
228         aStrPtr += theName.length();
229     }
230     else{
231         *aStrPtr = 0;
232     }
233     aStrPtr++;
234     pIntData = (int*)aStrPtr;
235     *pIntData++ = aNbCoords;
236
237     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)aStrPtr;
238     int i = 0;
239
240     for (; i < aNbCoords; i++) {
241       *pRealData++ = theCoords[i];
242     }
243
244     myType = theType;
245     isOK   = true;
246   }
247
248   return isOK;
249 }
250
251 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
252                                   const std::string &theName,
253                                   const int theIntParam1 )
254 {
255     if (theType == CurveCreator_Operation::RenameSection ) {
256         size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
257         int *pIntData = (int *)allocate(aSize);
258         *pIntData = theIntParam1;
259         pIntData++;
260         if( !theName.empty() ){
261             strcpy( (char*)pIntData, theName.c_str() );
262         }
263         else{
264             *((char*)pIntData) = 0;
265         }
266         myType = theType;
267         return true;
268     }
269     return false;
270 }
271
272 //=======================================================================
273 // function: apply
274 // purpose:
275 //=======================================================================
276 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
277 {
278   if (theCurve != NULL) {
279     int *pInt = (int *)myPData;
280
281     switch (myType) {
282       case CurveCreator_Operation::AddPoints:
283         {
284           CurveCreator::Coordinates aCoords;
285
286           getCoords(&pInt[1], aCoords);
287           theCurve->addPoints(aCoords, pInt[0]);
288         }
289         break;
290       case CurveCreator_Operation::RemovePoints:
291         theCurve->removePoints(pInt[0], pInt[1], pInt[2]);
292         break;
293       case CurveCreator_Operation::InsertPoints:
294         {
295           CurveCreator::Coordinates aCoords;
296
297           getCoords(&pInt[2], aCoords);
298           theCurve->insertPoints(aCoords, pInt[0], pInt[1]);
299         }
300         break;
301       case CurveCreator_Operation::SetType:
302         {
303           const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
304
305           theCurve->setType(aType, pInt[1]);
306         }
307         break;
308       case CurveCreator_Operation::Clear:
309         theCurve->clear();
310         break;
311       case CurveCreator_Operation::SetCoordinates:
312         {
313           CurveCreator::Coordinates aCoords;
314
315           getCoords(&pInt[2], aCoords);
316           theCurve->setCoordinates(aCoords, pInt[0], pInt[1]);
317         }
318         break;
319       case CurveCreator_Operation::SetClosed:
320         theCurve->setClosed((pInt[0] != 0), pInt[1]);
321         break;
322       case CurveCreator_Operation::MoveSection:
323         theCurve->moveSection(pInt[0], pInt[1]);
324         break;
325       case CurveCreator_Operation::Join:
326         if (myPData == NULL) {
327           theCurve->join();
328         } else {
329           theCurve->join(pInt[0], pInt[1]);
330         }
331         break;
332       case CurveCreator_Operation::AddSection:
333         {
334           const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
335
336           std::string aName = std::string((char*)&pInt[2]);
337
338           CurveCreator::Coordinates aCoords;
339
340           char* aPtr =  ((char*)&pInt[2]);
341           aPtr += (aName.length()) + 1;
342           getCoords((int*)aPtr, aCoords);
343           theCurve->addSection(aName, aType, (pInt[1] != 0), aCoords);
344         }
345         break;
346       case CurveCreator_Operation::RemoveSection:
347         theCurve->removeSection(pInt[0]);
348         break;
349       case CurveCreator_Operation::RenameSection:
350         {
351             std::string aName = std::string((char*)&pInt[1]);
352             theCurve->setName(aName, pInt[0]);
353         }
354         break;
355       default:
356         break;
357     }
358   }
359 }
360
361 //=======================================================================
362 // function: allocate
363 // purpose:
364 //=======================================================================
365 void *CurveCreator_Operation::allocate(const size_t theSize)
366 {
367   if (myPData != NULL) {
368     clear();
369   }
370
371   myPData = malloc(theSize);
372
373   return myPData;
374 }
375
376 //=======================================================================
377 // function: clear
378 // purpose:
379 //=======================================================================
380 void CurveCreator_Operation::clear()
381 {
382   myType = CurveCreator_Operation::Unknown;
383
384   if (myPData != NULL) {
385     free(myPData);
386     myPData = NULL;
387   }
388 }
389
390 //=======================================================================
391 // function: getCoords
392 // purpose:
393 //=======================================================================
394 void CurveCreator_Operation::getCoords
395           (int *thePInt, CurveCreator::Coordinates &theCoords) const
396 {
397   const int aNbPnts = *thePInt;
398   CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
399
400   for (int i = 0; i < aNbPnts; i++) {
401     theCoords.push_back(pCoord[i]);
402   }
403 }