1 // Copyright (C) 2013-2024 CEA, EDF, 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, or (at your option) any later version.
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"
26 #include "CurveCreator_Section.hxx"
27 #include "CurveCreator_Utils.hxx"
34 //=======================================================================
35 // function: Constructor
37 //=======================================================================
38 CurveCreator_Operation::CurveCreator_Operation()
39 : myType (CurveCreator_Operation::Unknown),
44 //=======================================================================
45 // function: Destructor
47 //=======================================================================
48 CurveCreator_Operation::~CurveCreator_Operation()
53 bool compId(CurveCreator_PosPoint* p1, CurveCreator_PosPoint* p2)
55 return p1->myID < p2->myID;
58 //=======================================================================
59 // function: Constructor
61 //=======================================================================
62 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
66 if (theType == CurveCreator_Operation::Clear) {
75 //=======================================================================
76 // function: Constructor
78 //=======================================================================
79 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
80 const int theIntParam)
84 if (theType == CurveCreator_Operation::RemoveSection) {
85 int *pData = (int *)allocate(sizeof(int));
87 pData[0] = theIntParam;
95 //=======================================================================
96 // function: Constructor
98 //=======================================================================
99 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
100 const int theIntParam1,
101 const int theIntParam2)
105 if (theType == CurveCreator_Operation::SetType ||
106 theType == CurveCreator_Operation::SetClosed ||
107 theType == CurveCreator_Operation::MoveSection ||
108 theType == CurveCreator_Operation::Join) {
109 int *pData = (int *)allocate(2*sizeof(int));
111 pData[0] = theIntParam1;
112 pData[1] = theIntParam2;
120 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
121 const int theIntParam1,
122 const int theIntParam2[3])
126 if (theType == CurveCreator_Operation::SetColorSection)
128 int *pData = (int *)allocate(4*sizeof(int));
130 pData[0] = theIntParam1;
131 pData[1] = theIntParam2[0];
132 pData[2] = theIntParam2[1];
133 pData[3] = theIntParam2[2];
142 //=======================================================================
143 // function: Constructor
145 //=======================================================================
146 bool CurveCreator_Operation::init(const Type theType, const std::list<int> theParamList)
150 if (theType == CurveCreator_Operation::Join)
152 const int aNbPoints = theParamList.size();
156 aNbPoints * (sizeof(int));
158 int *pIntData = (int *)allocate(aSize);
160 *pIntData++ = aNbPoints;
161 std::list<int>::const_iterator anIt = theParamList.begin(), aLast = theParamList.end();
162 for ( ; anIt != aLast; anIt++ )
171 //=======================================================================
172 // function: Constructor
174 //=======================================================================
175 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
176 const CurveCreator::Coordinates &theCoords,
177 const int theIntParam)
181 if (theType == CurveCreator_Operation::AddPoints) {
182 const int aNbCoords = theCoords.size();
184 2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
185 int *pIntData = (int *)allocate(aSize);
187 *pIntData++ = theIntParam;
188 *pIntData++ = aNbCoords;
190 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
193 for (; i < aNbCoords; i++) {
194 *pRealData++ = theCoords[i];
204 //=======================================================================
205 // function: Constructor
207 //=======================================================================
208 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
209 const std::string& theName,
210 const CurveCreator::Coordinates &theCoords,
211 const int theIntParam1,
212 const int theIntParam2)
215 if (theType == CurveCreator_Operation::AddSection ) {
216 const int aNbCoords = theCoords.size();
218 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
219 int *pIntData = (int *)allocate(aSize);
221 *pIntData++ = theIntParam1;
222 *pIntData++ = theIntParam2;
223 char* aStrPtr = (char*)pIntData;
224 if( !theName.empty() ){
225 strcpy( aStrPtr, theName.c_str() );
226 aStrPtr += theName.length();
232 pIntData = (int*)aStrPtr;
233 *pIntData++ = aNbCoords;
235 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
238 for (; i < aNbCoords; i++) {
239 *pRealData++ = theCoords[i];
249 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
250 const std::string &theName,
251 const int theIntParam1 )
253 if (theType == CurveCreator_Operation::RenameSection ) {
254 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
255 int *pIntData = (int *)allocate(aSize);
256 *pIntData = theIntParam1;
258 if( !theName.empty() ){
259 strcpy( (char*)pIntData, theName.c_str() );
262 *((char*)pIntData) = 0;
270 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
271 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
275 if (theType == CurveCreator_Operation::InsertPoints ||
276 theType == CurveCreator_Operation::SetCoordinates ) {
278 const int aNbPoints = theParamList1.size();
280 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
281 theParamList1.begin();
282 const int aNbCoords = anIt->second.size();
285 sizeof(aNbPoints) + sizeof(aNbCoords) +
286 aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
287 int *pIntData = (int *)allocate(aSize);
289 *pIntData++ = aNbPoints;
290 *pIntData++ = aNbCoords;
291 int aSectionId, aPointId;
292 for ( ; anIt != theParamList1.end(); anIt++ ) {
293 aSectionId = anIt->first.first;
294 aPointId = anIt->first.second;
296 *pIntData++ = aSectionId;
297 *pIntData++ = aPointId;
298 *pIntData++ = aNbCoords;
300 const CurveCreator::Coordinates &aCoords = anIt->second;
301 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
302 for (int i = 0; i < aNbCoords; i++) {
303 *pRealData++ = aCoords[i];
305 pIntData = (int *)pRealData;
315 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
316 const CurveCreator_ICurve::SectionToPointList &theParamList1)
320 if (theType == CurveCreator_Operation::RemovePoints) {
321 const int aNbPoints = theParamList1.size();
323 CurveCreator_ICurve::SectionToPointList::const_iterator anIt =
324 theParamList1.begin();
328 aNbPoints * (2*sizeof(int));
329 int *pIntData = (int *)allocate(aSize);
331 *pIntData++ = aNbPoints;
332 int aSectionId, aPointId;
333 for ( ; anIt != theParamList1.end(); anIt++ ) {
334 aSectionId = anIt->first;
335 aPointId = anIt->second;
337 *pIntData++ = aSectionId;
338 *pIntData++ = aPointId;
348 //=======================================================================
351 //=======================================================================
352 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
354 if (theCurve != NULL) {
355 int *pInt = (int *)myPData;
358 case CurveCreator_Operation::AddPoints:
359 case CurveCreator_Operation::InsertPoints:
360 case CurveCreator_Operation::SetCoordinates:
362 int aSectionId, aPointId;
363 CurveCreator::SectionsMap aSectionsMap;
364 CurveCreator::PosPointsList aPoints;
365 CurveCreator::Coordinates aCoords;
367 int nbPoints = pInt[0];
368 int nbCoords = pInt[1];
369 int nbParams = 3+nbCoords*sizeof(double)/sizeof(int);
370 for (int i = 0; i < nbPoints*nbParams; i=i+nbParams) {
373 getCoords(&pInt[4+i], aCoords);
374 aSectionId = pInt[2+i];
375 aPointId = pInt[3+i];
376 if ( aSectionsMap.find( aSectionId ) != aSectionsMap.end() )
377 aPoints = aSectionsMap[aSectionId];
378 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( aPointId, aCoords );
379 aPoints.push_back( aPosPoint );
380 aPoints.sort(compId);
381 aSectionsMap[aSectionId] = aPoints;
384 case CurveCreator_Operation::AddPoints:
385 case CurveCreator_Operation::InsertPoints:
386 theCurve->addPointsInternal( aSectionsMap );
388 case CurveCreator_Operation::SetCoordinates:
389 theCurve->setPointInternal( aSectionsMap );
396 case CurveCreator_Operation::RemovePoints:
398 CurveCreator_ICurve::SectionToPointList aListOfSectionsToPoints;
399 int nbPoints = pInt[0];
400 for (int i = 1; i < nbPoints*2; i=i+2) {
401 aListOfSectionsToPoints.push_back(std::make_pair(pInt[i], pInt[i+1]));
403 theCurve->removePointsInternal(aListOfSectionsToPoints);
406 case CurveCreator_Operation::SetType:
408 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
410 theCurve->setSectionTypeInternal( pInt[1], aType );
413 case CurveCreator_Operation::Clear:
414 theCurve->clearInternal();
416 case CurveCreator_Operation::SetClosed:
417 theCurve->setClosedInternal(pInt[1], (pInt[0] != 0));
419 case CurveCreator_Operation::MoveSection:
420 theCurve->moveSectionInternal(pInt[0], pInt[1]);
422 case CurveCreator_Operation::Join:
425 std::list<int> aListOfSections;
426 int nbSections = pInt[0];
427 for (int i = 1; i < nbSections+1; i++) {
428 aListOfSections.push_back(pInt[i]);
430 theCurve->joinInternal(aListOfSections);
433 case CurveCreator_Operation::AddSection:
435 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
437 std::string aName = std::string((char*)&pInt[2]);
439 CurveCreator::Coordinates aCoords;
441 char* aPtr = ((char*)&pInt[2]);
442 aPtr += (aName.length()) + 1;
443 getCoords((int*)aPtr, aCoords);
444 Quantity_Color aLastColor = theCurve->getLastRemovedColor();
445 theCurve->popLastRemovedColor();
446 theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords, aLastColor);
449 case CurveCreator_Operation::RemoveSection:
450 theCurve->removeSectionInternal(pInt[0]);
452 case CurveCreator_Operation::RenameSection:
454 std::string aName = std::string((char*)&pInt[1]);
455 theCurve->setSectionNameInternal(pInt[0], aName);
458 case CurveCreator_Operation::SetColorSection:
460 Quantity_Color aColor = CurveCreator_Utils::colorConv(QColor(pInt[1], pInt[2], pInt[3]));
461 theCurve->setColorSectionInternal(pInt[0], aColor);
462 //theCurve->redisplayCurve(false);
471 //=======================================================================
472 // function: allocate
474 //=======================================================================
475 void *CurveCreator_Operation::allocate(const size_t theSize)
477 if (myPData != NULL) {
481 myPData = malloc(theSize);
486 //=======================================================================
489 //=======================================================================
490 void CurveCreator_Operation::clear()
492 myType = CurveCreator_Operation::Unknown;
494 if (myPData != NULL) {
500 //=======================================================================
501 // function: getCoords
503 //=======================================================================
504 void CurveCreator_Operation::getCoords
505 (int *thePInt, CurveCreator::Coordinates &theCoords) const
507 const int aNbPnts = *thePInt;
508 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
510 for (int i = 0; i < aNbPnts; i++) {
511 theCoords.push_back(pCoord[i]);