]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Operation.cxx
Salome HOME
Images neccessary for CurveEditor were added
[modules/geom.git] / src / CurveCreator / CurveCreator_Operation.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_Operation.cxx
24 // Created:     Wed Jun  26 13:06:56 2013
25 // Author:      Sergey KHROMOV
26 //
27
28
29 #include <CurveCreator_Operation.hxx>
30 #include <CurveCreator_Curve.hxx>
31 #include <stdlib.h>
32
33
34 //=======================================================================
35 // function: Constructor
36 // purpose:
37 //=======================================================================
38 CurveCreator_Operation::CurveCreator_Operation()
39 : myType  (CurveCreator_Operation::Unknown),
40   myPData (NULL)
41 {
42 }
43
44 //=======================================================================
45 // function: Destructor
46 // purpose:
47 //=======================================================================
48 CurveCreator_Operation::~CurveCreator_Operation()
49 {
50   clear();
51 }
52
53 //=======================================================================
54 // function: Constructor
55 // purpose:
56 //=======================================================================
57 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
58 {
59   bool isOK = false;
60
61   if (theType == CurveCreator_Operation::Clear ||
62       theType == CurveCreator_Operation::Join) {
63     clear();
64     myType = theType;
65     isOK   = true;
66   }
67
68   return isOK;
69 }
70
71 //=======================================================================
72 // function: Constructor
73 // purpose:
74 //=======================================================================
75 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
76                                   const int theIntParam)
77 {
78   bool isOK = false;
79
80   if (theType == CurveCreator_Operation::RemoveSection) {
81     int *pData = (int *)allocate(sizeof(int));
82
83     pData[0] = theIntParam;
84     myType   = theType;
85     isOK     = true;
86   }
87
88   return isOK;
89 }
90
91 //=======================================================================
92 // function: Constructor
93 // purpose:
94 //=======================================================================
95 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
96                                   const int theIntParam1,
97                                   const int theIntParam2)
98 {
99   bool isOK = false;
100
101   if (theType == CurveCreator_Operation::SetType     ||
102       theType == CurveCreator_Operation::SetClosed   ||
103       theType == CurveCreator_Operation::MoveSection ||
104       theType == CurveCreator_Operation::Join) {
105     int *pData = (int *)allocate(2*sizeof(int));
106
107     pData[0] = theIntParam1;
108     pData[1] = theIntParam2;
109     myType   = theType;
110     isOK     = true;
111   }
112
113   return isOK;
114 }
115
116 //=======================================================================
117 // function: Constructor
118 // purpose:
119 //=======================================================================
120 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
121                                   const int theIntParam1,
122                                   const int theIntParam2,
123                                   const int theIntParam3)
124 {
125   bool isOK = false;
126
127   if (theType == CurveCreator_Operation::RemovePoints) {
128     int *pData = (int *)allocate(3*sizeof(int));
129
130     pData[0] = theIntParam1;
131     pData[1] = theIntParam2;
132     pData[2] = theIntParam3;
133     myType   = theType;
134     isOK     = true;
135   }
136
137   return isOK;
138 }
139
140 //=======================================================================
141 // function: Constructor
142 // purpose:
143 //=======================================================================
144 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
145                                   const CurveCreator::Coordinates &theCoords,
146                                   const int theIntParam)
147 {
148   bool isOK = false;
149
150   if (theType == CurveCreator_Operation::AddPoints) {
151     const int aNbCoords = theCoords.size();
152     const size_t aSize =
153       2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
154     int *pIntData = (int *)allocate(aSize);
155
156     *pIntData++ = theIntParam;
157     *pIntData++ = aNbCoords;
158
159     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
160     int i = 0;
161
162     for (; i < aNbCoords; i++) {
163       *pRealData++ = theCoords[i];
164     }
165
166     myType = theType;
167     isOK   = true;
168   }
169
170   return isOK;
171 }
172
173 //=======================================================================
174 // function: Constructor
175 // purpose:
176 //=======================================================================
177 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
178                                   const CurveCreator::Coordinates &theCoords,
179                                   const int theIntParam1,
180                                   const int theIntParam2)
181 {
182   bool isOK = false;
183
184   if (theType == CurveCreator_Operation::AddSection   ||
185       theType == CurveCreator_Operation::InsertPoints ||
186       theType == CurveCreator_Operation::SetCoordinates) {
187     const int aNbCoords = theCoords.size();
188     const size_t aSize =
189       3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
190     int *pIntData = (int *)allocate(aSize);
191
192     *pIntData++ = theIntParam1;
193     *pIntData++ = theIntParam2;
194     *pIntData++ = aNbCoords;
195
196     CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
197     int i = 0;
198
199     for (; i < aNbCoords; i++) {
200       *pRealData++ = theCoords[i];
201     }
202
203     myType = theType;
204     isOK   = true;
205   }
206
207   return isOK;
208 }
209
210 //=======================================================================
211 // function: apply
212 // purpose:
213 //=======================================================================
214 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
215 {
216   if (theCurve != NULL) {
217     int *pInt = (int *)myPData;
218
219     switch (myType) {
220       case CurveCreator_Operation::AddPoints:
221         {
222           CurveCreator::Coordinates aCoords;
223
224           getCoords(&pInt[1], aCoords);
225           theCurve->addPoints(aCoords, pInt[0]);
226         }
227         break;
228       case CurveCreator_Operation::RemovePoints:
229         theCurve->removePoints(pInt[0], pInt[1], pInt[2]);
230         break;
231       case CurveCreator_Operation::InsertPoints:
232         {
233           CurveCreator::Coordinates aCoords;
234
235           getCoords(&pInt[2], aCoords);
236           theCurve->insertPoints(aCoords, pInt[0], pInt[1]);
237         }
238         break;
239       case CurveCreator_Operation::SetType:
240         {
241           const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
242
243           theCurve->setType(aType, pInt[1]);
244         }
245         break;
246       case CurveCreator_Operation::Clear:
247         theCurve->clear();
248         break;
249       case CurveCreator_Operation::SetCoordinates:
250         {
251           CurveCreator::Coordinates aCoords;
252
253           getCoords(&pInt[2], aCoords);
254           theCurve->setCoordinates(aCoords, pInt[0], pInt[1]);
255         }
256         break;
257       case CurveCreator_Operation::SetClosed:
258         theCurve->setClosed((pInt[0] != 0), pInt[1]);
259         break;
260       case CurveCreator_Operation::MoveSection:
261         theCurve->moveSection(pInt[0], pInt[1]);
262         break;
263       case CurveCreator_Operation::Join:
264         if (myPData == NULL) {
265           theCurve->join();
266         } else {
267           theCurve->join(pInt[0], pInt[1]);
268         }
269         break;
270       case CurveCreator_Operation::AddSection:
271         {
272           const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
273           CurveCreator::Coordinates aCoords;
274
275           getCoords(&pInt[2], aCoords);
276           theCurve->addSection(aType, (pInt[1] != 0), aCoords);
277         }
278         break;
279       case CurveCreator_Operation::RemoveSection:
280         theCurve->removeSection(pInt[0]);
281         break;
282       default:
283         break;
284     }
285   }
286 }
287
288 //=======================================================================
289 // function: allocate
290 // purpose:
291 //=======================================================================
292 void *CurveCreator_Operation::allocate(const size_t theSize)
293 {
294   if (myPData != NULL) {
295     clear();
296   }
297
298   myPData = malloc(theSize);
299
300   return myPData;
301 }
302
303 //=======================================================================
304 // function: clear
305 // purpose:
306 //=======================================================================
307 void CurveCreator_Operation::clear()
308 {
309   myType = CurveCreator_Operation::Unknown;
310
311   if (myPData != NULL) {
312     free(myPData);
313     myPData = NULL;
314   }
315 }
316
317 //=======================================================================
318 // function: getCoords
319 // purpose:
320 //=======================================================================
321 void CurveCreator_Operation::getCoords
322           (int *thePInt, CurveCreator::Coordinates &theCoords) const
323 {
324   const int aNbPnts = *thePInt;
325   CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
326
327   for (int i = 0; i < aNbPnts; i++) {
328     theCoords.push_back(pCoord[i]);
329   }
330 }