1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: CurveCreator_Operation.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_Operation.hxx"
24 #include "CurveCreator_Curve.hxx"
25 #include "CurveCreator.hxx"
31 //=======================================================================
32 // function: Constructor
34 //=======================================================================
35 CurveCreator_Operation::CurveCreator_Operation()
36 : myType (CurveCreator_Operation::Unknown),
41 //=======================================================================
42 // function: Destructor
44 //=======================================================================
45 CurveCreator_Operation::~CurveCreator_Operation()
50 bool compId(CurveCreator_PosPoint* p1, CurveCreator_PosPoint* p2)
52 return p1->myID < p2->myID;
55 //=======================================================================
56 // function: Constructor
58 //=======================================================================
59 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
63 if (theType == CurveCreator_Operation::Clear ||
64 theType == CurveCreator_Operation::Join) {
73 //=======================================================================
74 // function: Constructor
76 //=======================================================================
77 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
78 const int theIntParam)
82 if (theType == CurveCreator_Operation::RemoveSection) {
83 int *pData = (int *)allocate(sizeof(int));
85 pData[0] = theIntParam;
93 //=======================================================================
94 // function: Constructor
96 //=======================================================================
97 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
98 const int theIntParam1,
99 const int theIntParam2)
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));
110 pData[0] = theIntParam1;
111 pData[1] = theIntParam2;
119 //=======================================================================
120 // function: Constructor
122 //=======================================================================
123 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
124 const int theIntParam1,
125 const int theIntParam2,
126 const int theIntParam3)
130 if (theType == CurveCreator_Operation::RemovePoints) {
131 int *pData = (int *)allocate(3*sizeof(int));
133 pData[0] = theIntParam1;
134 pData[1] = theIntParam2;
135 pData[2] = theIntParam3;
143 //=======================================================================
144 // function: Constructor
146 //=======================================================================
147 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
148 const CurveCreator::Coordinates &theCoords,
149 const int theIntParam)
153 if (theType == CurveCreator_Operation::AddPoints) {
154 const int aNbCoords = theCoords.size();
156 2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
157 int *pIntData = (int *)allocate(aSize);
159 *pIntData++ = theIntParam;
160 *pIntData++ = aNbCoords;
162 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
165 for (; i < aNbCoords; i++) {
166 *pRealData++ = theCoords[i];
176 //=======================================================================
177 // function: Constructor
179 //=======================================================================
180 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
181 const CurveCreator::Coordinates &theCoords,
182 const int theIntParam1,
183 const int theIntParam2)
187 if (theType == CurveCreator_Operation::AddSection) {
188 const int aNbCoords = theCoords.size();
190 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
191 int *pIntData = (int *)allocate(aSize);
193 *pIntData++ = theIntParam1;
194 *pIntData++ = theIntParam2;
195 *pIntData++ = aNbCoords;
197 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
200 for (; i < aNbCoords; i++) {
201 *pRealData++ = theCoords[i];
211 //=======================================================================
212 // function: Constructor
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)
222 if (theType == CurveCreator_Operation::AddSection ) {
223 const int aNbCoords = theCoords.size();
225 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
226 int *pIntData = (int *)allocate(aSize);
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();
239 pIntData = (int*)aStrPtr;
240 *pIntData++ = aNbCoords;
242 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)aStrPtr;
245 for (; i < aNbCoords; i++) {
246 *pRealData++ = theCoords[i];
256 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
257 const std::string &theName,
258 const int theIntParam1 )
260 if (theType == CurveCreator_Operation::RenameSection ) {
261 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
262 int *pIntData = (int *)allocate(aSize);
263 *pIntData = theIntParam1;
265 if( !theName.empty() ){
266 strcpy( (char*)pIntData, theName.c_str() );
269 *((char*)pIntData) = 0;
277 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
278 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
282 if (theType == CurveCreator_Operation::InsertPoints ||
283 theType == CurveCreator_Operation::SetCoordinates ) {
285 const int aNbPoints = theParamList1.size();
287 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
288 theParamList1.begin();
289 const int aNbCoords = anIt->second.size();
292 sizeof(aNbPoints) + sizeof(aNbCoords) +
293 aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
294 int *pIntData = (int *)allocate(aSize);
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;
303 *pIntData++ = aSectionId;
304 *pIntData++ = aPointId;
305 *pIntData++ = aNbCoords;
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];
312 pIntData = (int *)pRealData;
322 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
323 const CurveCreator_ICurve::SectionToPointList &theParamList1)
327 if (theType == CurveCreator_Operation::RemovePoints) {
328 const int aNbPoints = theParamList1.size();
330 CurveCreator_ICurve::SectionToPointList::const_iterator anIt =
331 theParamList1.begin();
335 aNbPoints * (2*sizeof(int));
336 int *pIntData = (int *)allocate(aSize);
338 *pIntData++ = aNbPoints;
339 int aSectionId, aPointId;
340 for ( ; anIt != theParamList1.end(); anIt++ ) {
341 aSectionId = anIt->first;
342 aPointId = anIt->second;
344 *pIntData++ = aSectionId;
345 *pIntData++ = aPointId;
355 //=======================================================================
358 //=======================================================================
359 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
361 if (theCurve != NULL) {
362 int *pInt = (int *)myPData;
365 case CurveCreator_Operation::AddPoints:
366 case CurveCreator_Operation::InsertPoints:
367 case CurveCreator_Operation::SetCoordinates:
369 int aSectionId, aPointId;
370 CurveCreator::SectionsMap aSectionsMap;
371 CurveCreator::PosPointsList aPoints;
372 CurveCreator::Coordinates aCoords;
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) {
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;
391 case CurveCreator_Operation::AddPoints:
392 case CurveCreator_Operation::InsertPoints:
393 theCurve->addPointsInternal( aSectionsMap );
395 case CurveCreator_Operation::SetCoordinates:
396 theCurve->setPointInternal( aSectionsMap );
401 case CurveCreator_Operation::RemovePoints:
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]));
408 theCurve->removePointsInternal(aListOfSectionsToPoints);
411 case CurveCreator_Operation::SetType:
413 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
415 theCurve->setSectionTypeInternal( pInt[1], aType );
418 case CurveCreator_Operation::Clear:
419 theCurve->clearInternal();
421 case CurveCreator_Operation::SetClosed:
422 theCurve->setClosedInternal((pInt[0] != 0), pInt[1]);
424 case CurveCreator_Operation::MoveSection:
425 theCurve->moveSectionInternal(pInt[0], pInt[1]);
427 case CurveCreator_Operation::Join:
428 if (myPData == NULL) {
429 theCurve->joinInternal();
431 theCurve->joinInternal(pInt[0], pInt[1]);
434 case CurveCreator_Operation::AddSection:
436 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
438 std::string aName = std::string((char*)&pInt[2]);
440 CurveCreator::Coordinates aCoords;
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);
448 case CurveCreator_Operation::RemoveSection:
449 theCurve->removeSectionInternal(pInt[0]);
451 case CurveCreator_Operation::RenameSection:
453 std::string aName = std::string((char*)&pInt[1]);
454 theCurve->setSectionNameInternal(pInt[0], aName);
463 //=======================================================================
464 // function: allocate
466 //=======================================================================
467 void *CurveCreator_Operation::allocate(const size_t theSize)
469 if (myPData != NULL) {
473 myPData = malloc(theSize);
478 //=======================================================================
481 //=======================================================================
482 void CurveCreator_Operation::clear()
484 myType = CurveCreator_Operation::Unknown;
486 if (myPData != NULL) {
492 //=======================================================================
493 // function: getCoords
495 //=======================================================================
496 void CurveCreator_Operation::getCoords
497 (int *thePInt, CurveCreator::Coordinates &theCoords) const
499 const int aNbPnts = *thePInt;
500 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
502 for (int i = 0; i < aNbPnts; i++) {
503 theCoords.push_back(pCoord[i]);