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) {
72 //=======================================================================
73 // function: Constructor
75 //=======================================================================
76 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
77 const int theIntParam)
81 if (theType == CurveCreator_Operation::RemoveSection) {
82 int *pData = (int *)allocate(sizeof(int));
84 pData[0] = theIntParam;
92 //=======================================================================
93 // function: Constructor
95 //=======================================================================
96 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
97 const int theIntParam1,
98 const int theIntParam2)
102 if (theType == CurveCreator_Operation::SetType ||
103 theType == CurveCreator_Operation::SetClosed ||
104 theType == CurveCreator_Operation::MoveSection ||
105 theType == CurveCreator_Operation::Join) {
106 int *pData = (int *)allocate(2*sizeof(int));
108 pData[0] = theIntParam1;
109 pData[1] = theIntParam2;
117 //=======================================================================
118 // function: Constructor
120 //=======================================================================
121 bool CurveCreator_Operation::init(const Type theType, const std::list<int> theParamList)
125 if (theType == CurveCreator_Operation::Join)
127 const int aNbPoints = theParamList.size();
131 aNbPoints * (sizeof(int));
133 int *pIntData = (int *)allocate(aSize);
135 *pIntData++ = aNbPoints;
136 std::list<int>::const_iterator anIt = theParamList.begin(), aLast = theParamList.end();
137 for ( ; anIt != aLast; anIt++ )
146 //=======================================================================
147 // function: Constructor
149 //=======================================================================
150 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
151 const CurveCreator::Coordinates &theCoords,
152 const int theIntParam)
156 if (theType == CurveCreator_Operation::AddPoints) {
157 const int aNbCoords = theCoords.size();
159 2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
160 int *pIntData = (int *)allocate(aSize);
162 *pIntData++ = theIntParam;
163 *pIntData++ = aNbCoords;
165 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
168 for (; i < aNbCoords; i++) {
169 *pRealData++ = theCoords[i];
179 //=======================================================================
180 // function: Constructor
182 //=======================================================================
183 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
184 const std::string& theName,
185 const CurveCreator::Coordinates &theCoords,
186 const int theIntParam1,
187 const int theIntParam2)
190 if (theType == CurveCreator_Operation::AddSection ) {
191 const int aNbCoords = theCoords.size();
193 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
194 int *pIntData = (int *)allocate(aSize);
196 *pIntData++ = theIntParam1;
197 *pIntData++ = theIntParam2;
198 char* aStrPtr = (char*)pIntData;
199 if( !theName.empty() ){
200 strcpy( aStrPtr, theName.c_str() );
201 aStrPtr += theName.length();
207 pIntData = (int*)aStrPtr;
208 *pIntData++ = aNbCoords;
210 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
213 for (; i < aNbCoords; i++) {
214 *pRealData++ = theCoords[i];
224 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
225 const std::string &theName,
226 const int theIntParam1 )
228 if (theType == CurveCreator_Operation::RenameSection ) {
229 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
230 int *pIntData = (int *)allocate(aSize);
231 *pIntData = theIntParam1;
233 if( !theName.empty() ){
234 strcpy( (char*)pIntData, theName.c_str() );
237 *((char*)pIntData) = 0;
245 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
246 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
250 if (theType == CurveCreator_Operation::InsertPoints ||
251 theType == CurveCreator_Operation::SetCoordinates ) {
253 const int aNbPoints = theParamList1.size();
255 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
256 theParamList1.begin();
257 const int aNbCoords = anIt->second.size();
260 sizeof(aNbPoints) + sizeof(aNbCoords) +
261 aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
262 int *pIntData = (int *)allocate(aSize);
264 *pIntData++ = aNbPoints;
265 *pIntData++ = aNbCoords;
266 int aSectionId, aPointId;
267 for ( ; anIt != theParamList1.end(); anIt++ ) {
268 aSectionId = anIt->first.first;
269 aPointId = anIt->first.second;
271 *pIntData++ = aSectionId;
272 *pIntData++ = aPointId;
273 *pIntData++ = aNbCoords;
275 const CurveCreator::Coordinates &aCoords = anIt->second;
276 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
277 for (int i = 0; i < aNbCoords; i++) {
278 *pRealData++ = aCoords[i];
280 pIntData = (int *)pRealData;
290 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
291 const CurveCreator_ICurve::SectionToPointList &theParamList1)
295 if (theType == CurveCreator_Operation::RemovePoints) {
296 const int aNbPoints = theParamList1.size();
298 CurveCreator_ICurve::SectionToPointList::const_iterator anIt =
299 theParamList1.begin();
303 aNbPoints * (2*sizeof(int));
304 int *pIntData = (int *)allocate(aSize);
306 *pIntData++ = aNbPoints;
307 int aSectionId, aPointId;
308 for ( ; anIt != theParamList1.end(); anIt++ ) {
309 aSectionId = anIt->first;
310 aPointId = anIt->second;
312 *pIntData++ = aSectionId;
313 *pIntData++ = aPointId;
323 //=======================================================================
326 //=======================================================================
327 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
329 if (theCurve != NULL) {
330 int *pInt = (int *)myPData;
333 case CurveCreator_Operation::AddPoints:
334 case CurveCreator_Operation::InsertPoints:
335 case CurveCreator_Operation::SetCoordinates:
337 int aSectionId, aPointId;
338 CurveCreator::SectionsMap aSectionsMap;
339 CurveCreator::PosPointsList aPoints;
340 CurveCreator::Coordinates aCoords;
342 int nbPoints = pInt[0];
343 int nbCoords = pInt[1];
344 int nbParams = 3+nbCoords;
345 for (int i = 0; i < nbPoints*nbParams; i=i+nbParams) {
348 getCoords(&pInt[4+i], aCoords);
349 aSectionId = pInt[2+i];
350 aPointId = pInt[3+i];
351 if ( aSectionsMap.find( aSectionId ) != aSectionsMap.end() )
352 aPoints = aSectionsMap[aSectionId];
353 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( aPointId, aCoords );
354 aPoints.push_back( aPosPoint );
355 aPoints.sort(compId);
356 aSectionsMap[aSectionId] = aPoints;
359 case CurveCreator_Operation::AddPoints:
360 case CurveCreator_Operation::InsertPoints:
361 theCurve->addPointsInternal( aSectionsMap );
363 case CurveCreator_Operation::SetCoordinates:
364 theCurve->setPointInternal( aSectionsMap );
369 case CurveCreator_Operation::RemovePoints:
371 CurveCreator_ICurve::SectionToPointList aListOfSectionsToPoints;
372 int nbPoints = pInt[0];
373 for (int i = 1; i < nbPoints*2; i=i+2) {
374 aListOfSectionsToPoints.push_back(std::make_pair(pInt[i], pInt[i+1]));
376 theCurve->removePointsInternal(aListOfSectionsToPoints);
379 case CurveCreator_Operation::SetType:
381 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
383 theCurve->setSectionTypeInternal( pInt[1], aType );
386 case CurveCreator_Operation::Clear:
387 theCurve->clearInternal();
389 case CurveCreator_Operation::SetClosed:
390 theCurve->setClosedInternal(pInt[1], (pInt[0] != 0));
392 case CurveCreator_Operation::MoveSection:
393 theCurve->moveSectionInternal(pInt[0], pInt[1]);
395 case CurveCreator_Operation::Join:
398 std::list<int> aListOfSections;
399 int nbSections = pInt[0];
400 for (int i = 1; i < nbSections+1; i++) {
401 aListOfSections.push_back(pInt[i]);
403 theCurve->joinInternal(aListOfSections);
406 case CurveCreator_Operation::AddSection:
408 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
410 std::string aName = std::string((char*)&pInt[2]);
412 CurveCreator::Coordinates aCoords;
414 char* aPtr = ((char*)&pInt[2]);
415 aPtr += (aName.length()) + 1;
416 getCoords((int*)aPtr, aCoords);
417 theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords);
420 case CurveCreator_Operation::RemoveSection:
421 theCurve->removeSectionInternal(pInt[0]);
423 case CurveCreator_Operation::RenameSection:
425 std::string aName = std::string((char*)&pInt[1]);
426 theCurve->setSectionNameInternal(pInt[0], aName);
435 //=======================================================================
436 // function: allocate
438 //=======================================================================
439 void *CurveCreator_Operation::allocate(const size_t theSize)
441 if (myPData != NULL) {
445 myPData = malloc(theSize);
450 //=======================================================================
453 //=======================================================================
454 void CurveCreator_Operation::clear()
456 myType = CurveCreator_Operation::Unknown;
458 if (myPData != NULL) {
464 //=======================================================================
465 // function: getCoords
467 //=======================================================================
468 void CurveCreator_Operation::getCoords
469 (int *thePInt, CurveCreator::Coordinates &theCoords) const
471 const int aNbPnts = *thePInt;
472 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
474 for (int i = 0; i < aNbPnts; i++) {
475 theCoords.push_back(pCoord[i]);