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 std::string& theName,
182 const CurveCreator::Coordinates &theCoords,
183 const int theIntParam1,
184 const int theIntParam2)
187 if (theType == CurveCreator_Operation::AddSection ) {
188 const int aNbCoords = theCoords.size();
190 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
191 int *pIntData = (int *)allocate(aSize);
193 *pIntData++ = theIntParam1;
194 *pIntData++ = theIntParam2;
195 char* aStrPtr = (char*)pIntData;
196 if( !theName.empty() ){
197 strcpy( aStrPtr, theName.c_str() );
198 aStrPtr += theName.length();
204 pIntData = (int*)aStrPtr;
205 *pIntData++ = aNbCoords;
207 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
210 for (; i < aNbCoords; i++) {
211 *pRealData++ = theCoords[i];
221 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
222 const std::string &theName,
223 const int theIntParam1 )
225 if (theType == CurveCreator_Operation::RenameSection ) {
226 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
227 int *pIntData = (int *)allocate(aSize);
228 *pIntData = theIntParam1;
230 if( !theName.empty() ){
231 strcpy( (char*)pIntData, theName.c_str() );
234 *((char*)pIntData) = 0;
242 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
243 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
247 if (theType == CurveCreator_Operation::InsertPoints ||
248 theType == CurveCreator_Operation::SetCoordinates ) {
250 const int aNbPoints = theParamList1.size();
252 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
253 theParamList1.begin();
254 const int aNbCoords = anIt->second.size();
257 sizeof(aNbPoints) + sizeof(aNbCoords) +
258 aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
259 int *pIntData = (int *)allocate(aSize);
261 *pIntData++ = aNbPoints;
262 *pIntData++ = aNbCoords;
263 int aSectionId, aPointId;
264 for ( ; anIt != theParamList1.end(); anIt++ ) {
265 aSectionId = anIt->first.first;
266 aPointId = anIt->first.second;
268 *pIntData++ = aSectionId;
269 *pIntData++ = aPointId;
270 *pIntData++ = aNbCoords;
272 const CurveCreator::Coordinates &aCoords = anIt->second;
273 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
274 for (int i = 0; i < aNbCoords; i++) {
275 *pRealData++ = aCoords[i];
277 pIntData = (int *)pRealData;
287 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
288 const CurveCreator_ICurve::SectionToPointList &theParamList1)
292 if (theType == CurveCreator_Operation::RemovePoints) {
293 const int aNbPoints = theParamList1.size();
295 CurveCreator_ICurve::SectionToPointList::const_iterator anIt =
296 theParamList1.begin();
300 aNbPoints * (2*sizeof(int));
301 int *pIntData = (int *)allocate(aSize);
303 *pIntData++ = aNbPoints;
304 int aSectionId, aPointId;
305 for ( ; anIt != theParamList1.end(); anIt++ ) {
306 aSectionId = anIt->first;
307 aPointId = anIt->second;
309 *pIntData++ = aSectionId;
310 *pIntData++ = aPointId;
320 //=======================================================================
323 //=======================================================================
324 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
326 if (theCurve != NULL) {
327 int *pInt = (int *)myPData;
330 case CurveCreator_Operation::AddPoints:
331 case CurveCreator_Operation::InsertPoints:
332 case CurveCreator_Operation::SetCoordinates:
334 int aSectionId, aPointId;
335 CurveCreator::SectionsMap aSectionsMap;
336 CurveCreator::PosPointsList aPoints;
337 CurveCreator::Coordinates aCoords;
339 int nbPoints = pInt[0];
340 int nbCoords = pInt[1];
341 int nbParams = 3+nbCoords;
342 for (int i = 0; i < nbPoints*nbParams; i=i+nbParams) {
345 getCoords(&pInt[4+i], aCoords);
346 aSectionId = pInt[2+i];
347 aPointId = pInt[3+i];
348 if ( aSectionsMap.find( aSectionId ) != aSectionsMap.end() )
349 aPoints = aSectionsMap[aSectionId];
350 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( aPointId, aCoords );
351 aPoints.push_back( aPosPoint );
352 aPoints.sort(compId);
353 aSectionsMap[aSectionId] = aPoints;
356 case CurveCreator_Operation::AddPoints:
357 case CurveCreator_Operation::InsertPoints:
358 theCurve->addPointsInternal( aSectionsMap );
360 case CurveCreator_Operation::SetCoordinates:
361 theCurve->setPointInternal( aSectionsMap );
366 case CurveCreator_Operation::RemovePoints:
368 CurveCreator_ICurve::SectionToPointList aListOfSectionsToPoints;
369 int nbPoints = pInt[0];
370 for (int i = 1; i < nbPoints*2; i=i+2) {
371 aListOfSectionsToPoints.push_back(std::make_pair(pInt[i], pInt[i+1]));
373 theCurve->removePointsInternal(aListOfSectionsToPoints);
376 case CurveCreator_Operation::SetType:
378 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
380 theCurve->setSectionTypeInternal( pInt[1], aType );
383 case CurveCreator_Operation::Clear:
384 theCurve->clearInternal();
386 case CurveCreator_Operation::SetClosed:
387 theCurve->setClosedInternal(pInt[1], (pInt[0] != 0));
389 case CurveCreator_Operation::MoveSection:
390 theCurve->moveSectionInternal(pInt[0], pInt[1]);
392 case CurveCreator_Operation::Join:
393 if (myPData == NULL) {
394 theCurve->joinInternal();
396 theCurve->joinInternal(pInt[0], pInt[1]);
399 case CurveCreator_Operation::AddSection:
401 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
403 std::string aName = std::string((char*)&pInt[2]);
405 CurveCreator::Coordinates aCoords;
407 char* aPtr = ((char*)&pInt[2]);
408 aPtr += (aName.length()) + 1;
409 getCoords((int*)aPtr, aCoords);
410 theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords);
413 case CurveCreator_Operation::RemoveSection:
414 theCurve->removeSectionInternal(pInt[0]);
416 case CurveCreator_Operation::RenameSection:
418 std::string aName = std::string((char*)&pInt[1]);
419 theCurve->setSectionNameInternal(pInt[0], aName);
428 //=======================================================================
429 // function: allocate
431 //=======================================================================
432 void *CurveCreator_Operation::allocate(const size_t theSize)
434 if (myPData != NULL) {
438 myPData = malloc(theSize);
443 //=======================================================================
446 //=======================================================================
447 void CurveCreator_Operation::clear()
449 myType = CurveCreator_Operation::Unknown;
451 if (myPData != NULL) {
457 //=======================================================================
458 // function: getCoords
460 //=======================================================================
461 void CurveCreator_Operation::getCoords
462 (int *thePInt, CurveCreator::Coordinates &theCoords) const
464 const int aNbPnts = *thePInt;
465 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
467 for (int i = 0; i < aNbPnts; i++) {
468 theCoords.push_back(pCoord[i]);