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 CurveCreator_Operation::Type theType,
122 const CurveCreator::Coordinates &theCoords,
123 const int theIntParam)
127 if (theType == CurveCreator_Operation::AddPoints) {
128 const int aNbCoords = theCoords.size();
130 2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
131 int *pIntData = (int *)allocate(aSize);
133 *pIntData++ = theIntParam;
134 *pIntData++ = aNbCoords;
136 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
139 for (; i < aNbCoords; i++) {
140 *pRealData++ = theCoords[i];
150 //=======================================================================
151 // function: Constructor
153 //=======================================================================
154 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
155 const std::string& theName,
156 const CurveCreator::Coordinates &theCoords,
157 const int theIntParam1,
158 const int theIntParam2)
161 if (theType == CurveCreator_Operation::AddSection ) {
162 const int aNbCoords = theCoords.size();
164 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
165 int *pIntData = (int *)allocate(aSize);
167 *pIntData++ = theIntParam1;
168 *pIntData++ = theIntParam2;
169 char* aStrPtr = (char*)pIntData;
170 if( !theName.empty() ){
171 strcpy( aStrPtr, theName.c_str() );
172 aStrPtr += theName.length();
178 pIntData = (int*)aStrPtr;
179 *pIntData++ = aNbCoords;
181 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
184 for (; i < aNbCoords; i++) {
185 *pRealData++ = theCoords[i];
195 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
196 const std::string &theName,
197 const int theIntParam1 )
199 if (theType == CurveCreator_Operation::RenameSection ) {
200 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
201 int *pIntData = (int *)allocate(aSize);
202 *pIntData = theIntParam1;
204 if( !theName.empty() ){
205 strcpy( (char*)pIntData, theName.c_str() );
208 *((char*)pIntData) = 0;
216 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
217 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
221 if (theType == CurveCreator_Operation::InsertPoints ||
222 theType == CurveCreator_Operation::SetCoordinates ) {
224 const int aNbPoints = theParamList1.size();
226 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
227 theParamList1.begin();
228 const int aNbCoords = anIt->second.size();
231 sizeof(aNbPoints) + sizeof(aNbCoords) +
232 aNbPoints * (3*sizeof(int) + aNbCoords*sizeof(CurveCreator::TypeCoord));
233 int *pIntData = (int *)allocate(aSize);
235 *pIntData++ = aNbPoints;
236 *pIntData++ = aNbCoords;
237 int aSectionId, aPointId;
238 for ( ; anIt != theParamList1.end(); anIt++ ) {
239 aSectionId = anIt->first.first;
240 aPointId = anIt->first.second;
242 *pIntData++ = aSectionId;
243 *pIntData++ = aPointId;
244 *pIntData++ = aNbCoords;
246 const CurveCreator::Coordinates &aCoords = anIt->second;
247 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
248 for (int i = 0; i < aNbCoords; i++) {
249 *pRealData++ = aCoords[i];
251 pIntData = (int *)pRealData;
261 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
262 const CurveCreator_ICurve::SectionToPointList &theParamList1)
266 if (theType == CurveCreator_Operation::RemovePoints) {
267 const int aNbPoints = theParamList1.size();
269 CurveCreator_ICurve::SectionToPointList::const_iterator anIt =
270 theParamList1.begin();
274 aNbPoints * (2*sizeof(int));
275 int *pIntData = (int *)allocate(aSize);
277 *pIntData++ = aNbPoints;
278 int aSectionId, aPointId;
279 for ( ; anIt != theParamList1.end(); anIt++ ) {
280 aSectionId = anIt->first;
281 aPointId = anIt->second;
283 *pIntData++ = aSectionId;
284 *pIntData++ = aPointId;
294 //=======================================================================
297 //=======================================================================
298 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
300 if (theCurve != NULL) {
301 int *pInt = (int *)myPData;
304 case CurveCreator_Operation::AddPoints:
305 case CurveCreator_Operation::InsertPoints:
306 case CurveCreator_Operation::SetCoordinates:
308 int aSectionId, aPointId;
309 CurveCreator::SectionsMap aSectionsMap;
310 CurveCreator::PosPointsList aPoints;
311 CurveCreator::Coordinates aCoords;
313 int nbPoints = pInt[0];
314 int nbCoords = pInt[1];
315 int nbParams = 3+nbCoords;
316 for (int i = 0; i < nbPoints*nbParams; i=i+nbParams) {
319 getCoords(&pInt[4+i], aCoords);
320 aSectionId = pInt[2+i];
321 aPointId = pInt[3+i];
322 if ( aSectionsMap.find( aSectionId ) != aSectionsMap.end() )
323 aPoints = aSectionsMap[aSectionId];
324 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( aPointId, aCoords );
325 aPoints.push_back( aPosPoint );
326 aPoints.sort(compId);
327 aSectionsMap[aSectionId] = aPoints;
330 case CurveCreator_Operation::AddPoints:
331 case CurveCreator_Operation::InsertPoints:
332 theCurve->addPointsInternal( aSectionsMap );
334 case CurveCreator_Operation::SetCoordinates:
335 theCurve->setPointInternal( aSectionsMap );
340 case CurveCreator_Operation::RemovePoints:
342 CurveCreator_ICurve::SectionToPointList aListOfSectionsToPoints;
343 int nbPoints = pInt[0];
344 for (int i = 1; i < nbPoints*2; i=i+2) {
345 aListOfSectionsToPoints.push_back(std::make_pair(pInt[i], pInt[i+1]));
347 theCurve->removePointsInternal(aListOfSectionsToPoints);
350 case CurveCreator_Operation::SetType:
352 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
354 theCurve->setSectionTypeInternal( pInt[1], aType );
357 case CurveCreator_Operation::Clear:
358 theCurve->clearInternal();
360 case CurveCreator_Operation::SetClosed:
361 theCurve->setClosedInternal(pInt[1], (pInt[0] != 0));
363 case CurveCreator_Operation::MoveSection:
364 theCurve->moveSectionInternal(pInt[0], pInt[1]);
366 case CurveCreator_Operation::Join:
367 if (myPData == NULL) {
368 theCurve->joinInternal();
370 theCurve->joinInternal(pInt[0], pInt[1]);
373 case CurveCreator_Operation::AddSection:
375 const CurveCreator::SectionType aType = (CurveCreator::SectionType) pInt[0];
377 std::string aName = std::string((char*)&pInt[2]);
379 CurveCreator::Coordinates aCoords;
381 char* aPtr = ((char*)&pInt[2]);
382 aPtr += (aName.length()) + 1;
383 getCoords((int*)aPtr, aCoords);
384 theCurve->addSectionInternal(aName, aType, (pInt[1] != 0), aCoords);
387 case CurveCreator_Operation::RemoveSection:
388 theCurve->removeSectionInternal(pInt[0]);
390 case CurveCreator_Operation::RenameSection:
392 std::string aName = std::string((char*)&pInt[1]);
393 theCurve->setSectionNameInternal(pInt[0], aName);
402 //=======================================================================
403 // function: allocate
405 //=======================================================================
406 void *CurveCreator_Operation::allocate(const size_t theSize)
408 if (myPData != NULL) {
412 myPData = malloc(theSize);
417 //=======================================================================
420 //=======================================================================
421 void CurveCreator_Operation::clear()
423 myType = CurveCreator_Operation::Unknown;
425 if (myPData != NULL) {
431 //=======================================================================
432 // function: getCoords
434 //=======================================================================
435 void CurveCreator_Operation::getCoords
436 (int *thePInt, CurveCreator::Coordinates &theCoords) const
438 const int aNbPnts = *thePInt;
439 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
441 for (int i = 0; i < aNbPnts; i++) {
442 theCoords.push_back(pCoord[i]);