Salome HOME
fa9cdb071f753de652069a14ed1307fd8bb4c980
[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 #include "CurveCreator.hxx"
26
27 #include <string>
28 #include <stdlib.h>
29 #include <string.h>
30
31 //=======================================================================
32 // function: Constructor
33 // purpose:
34 //=======================================================================
35 CurveCreator_Operation::CurveCreator_Operation()
36 : myType  (CurveCreator_Operation::Unknown),
37   myPData (NULL)
38 {
39 }
40
41 //=======================================================================
42 // function: Destructor
43 // purpose:
44 //=======================================================================
45 CurveCreator_Operation::~CurveCreator_Operation()
46 {
47   clear();
48 }
49
50 bool compId(CurveCreator_PosPoint* p1, CurveCreator_PosPoint* p2)
51 {
52   return p1->myID < p2->myID;
53 }
54
55 //=======================================================================
56 // function: Constructor
57 // purpose:
58 //=======================================================================
59 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
60 {
61   bool isOK = false;
62
63   if (theType == CurveCreator_Operation::Clear ||
64       theType == CurveCreator_Operation::Join) {
65     clear();
66     myType = theType;
67     isOK   = true;
68   }
69
70   return isOK;
71 }
72
73 //=======================================================================
74 // function: Constructor
75 // purpose:
76 //=======================================================================
77 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
78                                   const int theIntParam)
79 {
80   bool isOK = false;
81
82   if (theType == CurveCreator_Operation::RemoveSection) {
83     int *pData = (int *)allocate(sizeof(int));
84
85     pData[0] = theIntParam;
86     myType   = theType;
87     isOK     = true;
88   }
89
90   return isOK;
91 }
92
93 //=======================================================================
94 // function: Constructor
95 // purpose:
96 //=======================================================================
97 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
98                                   const int theIntParam1,
99                                   const int theIntParam2)
100 {
101   bool isOK = false;
102
103   if (theType == CurveCreator_Operation::SetType     ||
104       theType == CurveCreator_Operation::SetClosed   ||
105       theType == CurveCreator_Operation::MoveSection ||
106       theType == CurveCreator_Operation::RemovePoints ||
107       theType == CurveCreator_Operation::Join) {
108     int *pData = (int *)allocate(2*sizeof(int));
109
110     pData[0] = theIntParam1;
111     pData[1] = theIntParam2;
112     myType   = theType;
113     isOK     = true;
114   }
115
116   return isOK;
117 }
118
119 //=======================================================================
120 // function: Constructor
121 // purpose:
122 //=======================================================================
123 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
124                                   const int theIntParam1,
125                                   const int theIntParam2,
126                                   const int theIntParam3)
127 {
128   bool isOK = false;
129
130   if (theType == CurveCreator_Operation::RemovePoints) {
131     int *pData = (int *)allocate(3*sizeof(int));
132
133     pData[0] = theIntParam1;
134     pData[1] = theIntParam2;
135     pData[2] = theIntParam3;
136     myType   = theType;
137     isOK     = true;
138   }
139
140   return isOK;
141 }
142
143 //=======================================================================
144 // function: Constructor
145 // purpose:
146 //=======================================================================
147 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
148                                   const CurveCreator::Coordinates &theCoords,
149                                   const int theIntParam)
150 {
151   bool isOK = false;
152
153   if (theType == CurveCreator_Operation::AddPoints) {
154     const int aNbCoords = theCoords.size();
155     const size_t aSize =
156       2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
157     int *pIntData = (int *)allocate(aSize);
158
159     *pIntData++ = theIntParam;
160     *pIntData++ = aNbCoords;
161
162     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
163     int i = 0;
164
165     for (; i < aNbCoords; i++) {
166       *pRealData++ = theCoords[i];
167     }
168
169     myType = theType;
170     isOK   = true;
171   }
172
173   return isOK;
174 }
175
176 //=======================================================================
177 // function: Constructor
178 // purpose:
179 //=======================================================================
180 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
181                                   const CurveCreator::Coordinates &theCoords,
182                                   const int theIntParam1,
183                                   const int theIntParam2)
184 {
185   bool isOK = false;
186
187   if (theType == CurveCreator_Operation::AddSection) {
188     const int aNbCoords = theCoords.size();
189     const size_t aSize =
190       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
191     int *pIntData = (int *)allocate(aSize);
192
193     *pIntData++ = theIntParam1;
194     *pIntData++ = theIntParam2;
195     *pIntData++ = aNbCoords;
196
197     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
198     int i = 0;
199
200     for (; i < aNbCoords; i++) {
201       *pRealData++ = theCoords[i];
202     }
203
204     myType = theType;
205     isOK   = true;
206   }
207
208   return isOK;
209 }
210
211 //=======================================================================
212 // function: Constructor
213 // purpose:
214 //=======================================================================
215 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
216                                   const std::string& theName,
217                                   const CurveCreator::Coordinates &theCoords,
218                                   const int theIntParam1,
219                                   const int theIntParam2)
220 {
221   bool isOK = false;
222   if (theType == CurveCreator_Operation::AddSection ) {
223     const int aNbCoords = theCoords.size();
224     const size_t aSize =
225       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
226     int *pIntData = (int *)allocate(aSize);
227
228     *pIntData++ = theIntParam1;
229     *pIntData++ = theIntParam2;
230     char* aStrPtr = (char*)pIntData;
231     if( !theName.empty() ){
232         strcpy( aStrPtr, theName.c_str() );
233         aStrPtr += theName.length();
234     }
235     else{
236         *aStrPtr = 0;
237     }
238     aStrPtr++;
239     pIntData = (int*)aStrPtr;
240     *pIntData++ = aNbCoords;
241
242     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)aStrPtr;
243     int i = 0;
244
245     for (; i < aNbCoords; i++) {
246       *pRealData++ = theCoords[i];
247     }
248
249     myType = theType;
250     isOK   = true;
251   }
252
253   return isOK;
254 }
255
256 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
257                                   const std::string &theName,
258                                   const int theIntParam1 )
259 {
260     if (theType == CurveCreator_Operation::RenameSection ) {
261         size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
262         int *pIntData = (int *)allocate(aSize);
263         *pIntData = theIntParam1;
264         pIntData++;
265         if( !theName.empty() ){
266             strcpy( (char*)pIntData, theName.c_str() );
267         }
268         else{
269             *((char*)pIntData) = 0;
270         }
271         myType = theType;
272         return true;
273     }
274     return false;
275 }
276
277 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
278                                   const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
279 {
280   bool isOK = false;
281
282   if (theType == CurveCreator_Operation::InsertPoints ||
283       theType == CurveCreator_Operation::SetCoordinates ) {
284
285     const int aNbPoints = theParamList1.size();
286
287     CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = 
288       theParamList1.begin();
289     const int aNbCoords = anIt->second.size();
290
291     const size_t aSize =
292       sizeof(aNbPoints) + sizeof(aNbCoords) + 
293       aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
294     int *pIntData = (int *)allocate(aSize);
295
296     *pIntData++ = aNbPoints;
297     *pIntData++ = aNbCoords;
298     int aSectionId, aPointId;
299     for ( ; anIt != theParamList1.end(); anIt++ ) {
300       aSectionId = anIt->first.first;
301       aPointId = anIt->first.second;
302
303       *pIntData++ = aSectionId;
304       *pIntData++ = aPointId;
305       *pIntData++ = aNbCoords;
306
307       const CurveCreator::Coordinates &aCoords = anIt->second;
308       CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
309       for (int i = 0; i < aNbCoords; i++) {
310         *pRealData++ = aCoords[i];
311       }
312       pIntData = (int *)pRealData;
313     }
314
315     myType = theType;
316     isOK   = true;
317   }
318
319   return isOK;
320 }
321
322 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
323                                   const CurveCreator_ICurve::SectionToPointList &theParamList1)
324 {
325   bool isOK = false;
326
327   if (theType == CurveCreator_Operation::RemovePoints) {
328     const int aNbPoints = theParamList1.size();
329
330     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = 
331       theParamList1.begin();
332
333     const size_t aSize =
334       sizeof(aNbPoints) +
335       aNbPoints * (2*sizeof(int));
336     int *pIntData = (int *)allocate(aSize);
337
338     *pIntData++ = aNbPoints;
339     int aSectionId, aPointId;
340     for ( ; anIt != theParamList1.end(); anIt++ ) {
341       aSectionId = anIt->first;
342       aPointId = anIt->second;
343
344       *pIntData++ = aSectionId;
345       *pIntData++ = aPointId;
346     }
347
348     myType = theType;
349     isOK   = true;
350   }
351
352   return isOK;
353 }
354
355 //=======================================================================
356 // function: apply
357 // purpose:
358 //=======================================================================
359 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
360 {
361   if (theCurve != NULL) {
362     int *pInt = (int *)myPData;
363
364     switch (myType) {
365       case CurveCreator_Operation::AddPoints:
366       case CurveCreator_Operation::InsertPoints:
367       case CurveCreator_Operation::SetCoordinates:
368         {
369           int aSectionId, aPointId;
370           CurveCreator::SectionsMap aSectionsMap;
371           CurveCreator::PosPointsList aPoints;
372           CurveCreator::Coordinates aCoords;
373
374           int nbPoints = pInt[0];
375           int nbCoords = pInt[1];
376           int nbParams = 3+nbCoords;
377           for (int i = 0; i < nbPoints*nbParams; i=i+nbParams) {
378             aCoords.clear();
379             aPoints.clear();
380             getCoords(&pInt[4+i], aCoords);
381             aSectionId = pInt[2+i];
382             aPointId = pInt[3+i];
383             if ( aSectionsMap.find( aSectionId ) != aSectionsMap.end() )
384               aPoints = aSectionsMap[aSectionId];
385             CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( aPointId, aCoords );
386             aPoints.push_back( aPosPoint );
387             aPoints.sort(compId);
388             aSectionsMap[aSectionId] = aPoints;
389           }
390           switch (myType) {
391             case CurveCreator_Operation::AddPoints:
392             case CurveCreator_Operation::InsertPoints:
393               theCurve->addPointsInternal( aSectionsMap );
394               break;
395             case CurveCreator_Operation::SetCoordinates:
396               theCurve->setPointInternal( aSectionsMap );
397               break;
398           }
399         }
400         break;
401       case CurveCreator_Operation::RemovePoints:
402         {
403           CurveCreator_ICurve::SectionToPointList aListOfSectionsToPoints;
404           int nbPoints = pInt[0];
405           for (int i = 1; i < nbPoints*2; i=i+2) {
406             aListOfSectionsToPoints.push_back(std::make_pair(pInt[i], pInt[i+1]));
407           }
408           theCurve->removePointsInternal(aListOfSectionsToPoints);
409         }
410         break;
411       case CurveCreator_Operation::SetType:
412         {
413           const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
414
415           theCurve->setSectionTypeInternal( pInt[1], aType );
416         }
417         break;
418       case CurveCreator_Operation::Clear:
419         theCurve->clearInternal();
420         break;
421       case CurveCreator_Operation::SetClosed:
422         theCurve->setClosedInternal((pInt[0] != 0), pInt[1]);
423         break;
424       case CurveCreator_Operation::MoveSection:
425         theCurve->moveSectionInternal(pInt[0], pInt[1]);
426         break;
427       case CurveCreator_Operation::Join:
428         if (myPData == NULL) {
429           theCurve->joinInternal();
430         } else {
431           theCurve->joinInternal(pInt[0], pInt[1]);
432         }
433         break;
434       case CurveCreator_Operation::AddSection:
435         {
436           const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
437
438           std::string aName = std::string((char*)&pInt[2]);
439
440           CurveCreator::Coordinates aCoords;
441
442           char* aPtr =  ((char*)&pInt[2]);
443           aPtr += (aName.length()) + 1;
444           getCoords((int*)aPtr, aCoords);
445           theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords);
446         }
447         break;
448       case CurveCreator_Operation::RemoveSection:
449         theCurve->removeSectionInternal(pInt[0]);
450         break;
451       case CurveCreator_Operation::RenameSection:
452         {
453             std::string aName = std::string((char*)&pInt[1]);
454             theCurve->setSectionNameInternal(pInt[0], aName);
455         }
456         break;
457       default:
458         break;
459     }
460   }
461 }
462
463 //=======================================================================
464 // function: allocate
465 // purpose:
466 //=======================================================================
467 void *CurveCreator_Operation::allocate(const size_t theSize)
468 {
469   if (myPData != NULL) {
470     clear();
471   }
472
473   myPData = malloc(theSize);
474
475   return myPData;
476 }
477
478 //=======================================================================
479 // function: clear
480 // purpose:
481 //=======================================================================
482 void CurveCreator_Operation::clear()
483 {
484   myType = CurveCreator_Operation::Unknown;
485
486   if (myPData != NULL) {
487     free(myPData);
488     myPData = NULL;
489   }
490 }
491
492 //=======================================================================
493 // function: getCoords
494 // purpose:
495 //=======================================================================
496 void CurveCreator_Operation::getCoords
497           (int *thePInt, CurveCreator::Coordinates &theCoords) const
498 {
499   const int aNbPnts = *thePInt;
500   CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
501
502   for (int i = 0; i < aNbPnts; i++) {
503     theCoords.push_back(pCoord[i]);
504   }
505 }