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_Curve.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_Curve.hxx"
25 #include "CurveCreator.hxx"
26 #include "CurveCreator_PosPoint.hxx"
27 #include "CurveCreator_Section.hxx"
28 #include "CurveCreator_Displayer.h"
29 #include "CurveCreator_Utils.h"
31 #include <AIS_Shape.hxx>
32 #include <AIS_InteractiveObject.hxx>
33 #include <Geom_CartesianPoint.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Wire.hxx>
42 //=======================================================================
43 // function: Constructor
45 //=======================================================================
46 CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimension )
48 myDimension (theDimension),
59 //=======================================================================
60 // function: Destructor
62 //=======================================================================
63 CurveCreator_Curve::~CurveCreator_Curve()
65 // Delete all allocated data.
69 //=======================================================================
70 // function: getDimension
72 //=======================================================================
73 CurveCreator::Dimension CurveCreator_Curve::getDimension() const
78 //=======================================================================
79 // function: getUniqSectionName
80 // purpose: return unique section name
81 //=======================================================================
82 std::string CurveCreator_Curve::getUniqSectionName() const
84 for( int i = 0 ; i < 1000000 ; i++ ){
86 sprintf( aBuffer, "Section_%d", i+1 );
87 std::string aName(aBuffer);
89 for( j = 0 ; j < mySections.size() ; j++ ){
90 if( mySections[j]->myName == aName )
93 if( j == mySections.size() )
99 //=======================================================================
100 // function: setDisplayer
101 // purpose: set curve changes Displayer
102 //=======================================================================
103 void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
105 myDisplayer = theDisplayer;
108 //=======================================================================
109 // function: getDisplayer
110 // purpose: get curve changes Displayer
111 //=======================================================================
112 CurveCreator_Displayer* CurveCreator_Curve::getDisplayer()
117 //=======================================================================
118 // function: removeDisplayer
119 // purpose: remove the attached Displayer
120 //=======================================================================
121 void CurveCreator_Curve::removeDisplayer()
126 //=======================================================================
129 //=======================================================================
130 bool CurveCreator_Curve::addEmptyDiff()
132 bool isEnabled = false;
134 if (myUndoDepth != 0) {
135 // Forget all Redos after the current one.
138 myListDiffs.erase(myCurrenPos, myListDiffs.end());
141 if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
142 // Increase the number of undos.
145 // If there are too many differences, remove the first one.
146 myListDiffs.pop_front();
149 // Add new difference.
150 myListDiffs.push_back(CurveCreator_Diff());
151 myCurrenPos = myListDiffs.end();
158 void CurveCreator_Curve::startOperation()
163 void CurveCreator_Curve::finishOperation()
168 //=======================================================================
169 // function: toICoord
171 //=======================================================================
172 int CurveCreator_Curve::toICoord(const int theIPnt) const
174 return theIPnt * myDimension;
177 //=======================================================================
178 // function: setUndoDepth
180 //=======================================================================
181 void CurveCreator_Curve::setUndoDepth(const int theDepth)
184 // Reset all undo/redo data.
188 myCurrenPos = myListDiffs.end();
190 } else if (theDepth == -1) {
191 // There is nothing to do as the depth become unlimited.
193 } else if (theDepth > 0) {
194 // The new "real" depth is set.
195 if (theDepth < myNbRedos) {
196 // The new depth is less then number of redos. Remove the latest redos.
197 int aShift = (myNbRedos - theDepth);
198 ListDiff::iterator aFromPos = myListDiffs.end();
204 myListDiffs.erase(aFromPos, myListDiffs.end());
205 myNbRedos = theDepth;
208 if (theDepth < myNbUndos + myNbRedos) {
209 // The new depth is less then the total number of differences.
210 // Remove the first undos.
211 int aShift = (myNbUndos + myNbRedos - theDepth);
212 ListDiff::iterator aToPos = myListDiffs.begin();
218 myListDiffs.erase(myListDiffs.begin(), aToPos);
219 myNbUndos = theDepth - myNbRedos;
222 myUndoDepth = theDepth;
226 //=======================================================================
227 // function: getUndoDepth
229 //=======================================================================
230 int CurveCreator_Curve::getUndoDepth() const
235 void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double& theX, double& theY, double& theZ ) const
237 CurveCreator::Coordinates aCoords = getPoint( theISection, theIPoint );
241 if( getDimension() == CurveCreator::Dim3d ){
246 void CurveCreator_Curve::redisplayCurve()
249 myDisplayer->eraseAll( false );
252 myDisplayer->display( getAISObject( true ), true );
256 //! For internal use only! Undo/Redo are not used here.
257 bool CurveCreator_Curve::moveSectionInternal(const int theISection,
258 const int theNewIndex)
261 if (theISection != theNewIndex) {
262 CurveCreator_Section *aSection = mySections.at(theISection);
265 CurveCreator::Sections::iterator anIter = mySections.begin() + theISection;
267 mySections.erase(anIter);
270 anIter = mySections.begin() + theNewIndex;
271 mySections.insert(anIter, aSection);
277 //=======================================================================
278 // function: moveSection
280 //=======================================================================
281 bool CurveCreator_Curve::moveSection(const int theISection,
282 const int theNewIndex)
285 // Set the difference.
287 if (addEmptyDiff()) {
288 myListDiffs.back().init(this, CurveCreator_Operation::MoveSection,
289 theISection, theNewIndex);
293 res = moveSectionInternal(theISection, theNewIndex);
298 /************ Implementation of INTERFACE methods ************/
300 /***********************************************/
301 /*** Undo/Redo methods ***/
302 /***********************************************/
304 //! Get number of available undo operations
305 int CurveCreator_Curve::getNbUndo() const
310 //! Undo previous operation
311 bool CurveCreator_Curve::undo()
318 myCurrenPos->applyUndo(this);
324 //! Get number of available redo operations
325 int CurveCreator_Curve::getNbRedo() const
330 //! Redo last previously "undone" operation
331 bool CurveCreator_Curve::redo()
335 myCurrenPos->applyRedo(this);
344 /***********************************************/
345 /*** Section methods ***/
346 /***********************************************/
347 //! For internal use only! Undo/Redo are not used here.
348 bool CurveCreator_Curve::clearInternal()
350 // erase curve from the viewer
352 myDisplayer->eraseAll( true );
355 // Delete all allocated data.
357 const int aNbSections = getNbSections();
359 for (; i < aNbSections; i++) {
360 delete mySections[i];
368 //=======================================================================
371 //=======================================================================
372 bool CurveCreator_Curve::clear()
376 // Set the difference.
377 if (addEmptyDiff()) {
378 myListDiffs.back().init(this, CurveCreator_Operation::Clear);
380 res = clearInternal();
385 //! For internal use only! Undo/Redo are not used here.
386 bool CurveCreator_Curve::joinInternal( const int theISectionTo,
387 const int theISectionFrom )
390 CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
391 CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
393 aSection1->myPoints.insert(aSection1->myPoints.end(),
394 aSection2->myPoints.begin(),
395 aSection2->myPoints.end());
397 res = removeSection(theISectionFrom);
402 //! Join range of sections to one section (join all sections if -1 is passed in theISectionFrom argument)
403 bool CurveCreator_Curve::join( const int theISectionTo,
404 const int theISectionFrom )
408 if ( theISectionTo != theISectionFrom ) {
411 myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
413 res = joinInternal( theISectionTo, theISectionFrom );
420 //! Get number of sections
421 int CurveCreator_Curve::getNbSections() const
423 return mySections.size();
426 //! For internal use only! Undo/Redo are not used here.
427 int CurveCreator_Curve::addSectionInternal
428 (const std::string& theName, const CurveCreator::SectionType theType,
429 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
431 CurveCreator_Section *aSection = new CurveCreator_Section;
433 std::string aName = theName;
435 aName = getUniqSectionName();
437 aSection->myName = aName;
438 aSection->myType = theType;
439 aSection->myIsClosed = theIsClosed;
440 aSection->myPoints = thePoints;
441 mySections.push_back(aSection);
443 return mySections.size()-1;
446 //=======================================================================
447 // function: addSection
448 // purpose: adds an empty section
449 //=======================================================================
450 int CurveCreator_Curve::addSection
451 (const std::string& theName, const CurveCreator::SectionType theType,
452 const bool theIsClosed)
454 int resISection = -1;
455 // Set the difference.
457 CurveCreator::Coordinates aCoords; //empty list
458 if (addEmptyDiff()) {
459 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
460 theName, aCoords, theType, theIsClosed);
463 resISection = addSectionInternal(theName, theType, theIsClosed, aCoords);
468 //=======================================================================
469 // function: addSection
470 // purpose: adds a section with the given points
471 //=======================================================================
472 int CurveCreator_Curve::addSection
473 (const std::string& theName, const CurveCreator::SectionType theType,
474 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
476 int resISection = -1;
477 // Set the difference.
479 if (addEmptyDiff()) {
480 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
481 theName, thePoints, theType, theIsClosed);
484 resISection = addSectionInternal(theName, theType, theIsClosed, thePoints);
490 //! For internal use only! Undo/Redo are not used here.
491 bool CurveCreator_Curve::removeSectionInternal( const int theISection )
493 if (theISection == -1) {
494 delete mySections.back();
495 mySections.pop_back();
497 CurveCreator::Sections::iterator anIterRm = mySections.begin() + theISection;
500 mySections.erase(anIterRm);
506 //! Removes the given sections.
507 bool CurveCreator_Curve::removeSection( const int theISection )
510 // Set the difference.
513 myListDiffs.back().init(this, CurveCreator_Operation::RemoveSection, theISection);
515 res = removeSectionInternal( theISection );
522 * Get number of points in specified section or (the total number of points
523 * in Curve if theISection is equal to -1).
525 int CurveCreator_Curve::getNbPoints( const int theISection ) const
529 if (theISection == -1) {
531 const int aNbSections = getNbSections();
533 for (; i < aNbSections; i++) {
534 aNbCoords += mySections[i]->myPoints.size();
537 if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
538 aNbCoords = mySections.at(theISection)->myPoints.size();
541 return aNbCoords/myDimension;
544 void CurveCreator_Curve::setSkipSorting( const bool theIsToSkip )
546 mySkipSorting = theIsToSkip;
549 bool CurveCreator_Curve::canPointsBeSorted()
554 //! Get "closed" flag of the specified section
555 bool CurveCreator_Curve::isClosed( const int theISection ) const
557 return mySections.at(theISection)->myIsClosed;
560 //! For internal use only! Undo/Redo are not used here.
561 bool CurveCreator_Curve::setClosedInternal( const int theISection,
562 const bool theIsClosed )
564 if (theISection == -1) {
565 int aSize = mySections.size();
568 for (i = 0; i < aSize; i++) {
569 mySections[i]->myIsClosed = theIsClosed;
573 mySections.at(theISection)->myIsClosed = theIsClosed;
580 * Set "closed" flag of the specified section (all sections if
581 * \a theISection is -1).
583 bool CurveCreator_Curve::setClosed( const int theISection,
584 const bool theIsClosed )
587 // Set the difference.
589 if (addEmptyDiff()) {
590 myListDiffs.back().init(this, CurveCreator_Operation::SetClosed,
591 theIsClosed, theISection);
593 res = setClosedInternal( theISection, theIsClosed );
598 //! Returns specified section name
599 std::string CurveCreator_Curve::getSectionName( const int theISection ) const
601 if( ( theISection >= 0 ) && ( theISection < mySections.size() ))
602 return mySections.at(theISection)->myName;
606 //! For internal use only! Undo/Redo are not used here.
607 bool CurveCreator_Curve::setSectionNameInternal( const int theISection,
608 const std::string& theName )
611 if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
612 mySections.at(theISection)->myName = theName;
618 /** Set name of the specified section */
619 bool CurveCreator_Curve::setSectionName( const int theISection,
620 const std::string& theName )
623 // Set the difference.
625 if (addEmptyDiff()) {
626 myListDiffs.back().init(this, CurveCreator_Operation::RenameSection,
627 theName, theISection);
629 res = setSectionNameInternal( theISection, theName );
634 //! Get type of the specified section
635 CurveCreator::SectionType CurveCreator_Curve::getSectionType
636 ( const int theISection ) const
638 return mySections.at(theISection)->myType;
641 //! For internal use only! Undo/Redo are not used here.
642 bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
643 const CurveCreator::SectionType theType )
645 if (theISection == -1) {
647 const int aNbSections = getNbSections();
649 for (; i < aNbSections; i++) {
650 mySections[i]->myType = theType;
654 if( mySections.at(theISection)->myType != theType ){
655 mySections.at(theISection)->myType = theType;
663 * Set type of the specified section (or all sections
664 * if \a theISection is -1).
666 bool CurveCreator_Curve::setSectionType( const int theISection,
667 const CurveCreator::SectionType theType )
671 // Set the difference.
672 if (addEmptyDiff()) {
673 myListDiffs.back().init(this, CurveCreator_Operation::SetType,
674 theType, theISection);
677 res = setSectionTypeInternal( theISection, theType );
684 /***********************************************/
685 /*** Point methods ***/
686 /***********************************************/
688 //! For internal use only! Undo/Redo are not used here.
689 bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
692 CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
693 CurveCreator_Section *aSection = 0;
694 for ( ; anIt != theSectionsMap.end(); anIt++ ) {
695 int anISection = anIt->first;
696 aSection = mySections.at(anISection);
698 CurveCreator::PosPointsList aSectionPoints = anIt->second;
699 CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
700 for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
701 int anIPnt = (*aPntIt)->myID;
702 CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
703 CurveCreator::Coordinates::iterator anIterPosition;
705 anIterPosition = aSection->myPoints.end();
707 anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
708 CurveCreator::Coordinates::const_iterator aFirstPosition =
710 aSection->myPoints.insert(anIterPosition,
711 aCoords.begin(), aCoords.end());
722 * Add one point to the specified section starting from the given theIPnt index
723 * (or at the end of points if \a theIPnt is -1).
725 bool CurveCreator_Curve::addPoints( const CurveCreator::Coordinates& theCoords,
726 const int theISection,
730 CurveCreator::Coordinates aCoords = theCoords;
731 // Set the difference.
733 if (addEmptyDiff()) {
734 CurveCreator_ICurve::SectionToPointCoordsList aList;
735 aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theCoords));
736 myListDiffs.back().init(this, CurveCreator_Operation::InsertPoints,
739 CurveCreator::SectionsMap aSectionsMap;
740 CurveCreator::PosPointsList aPoints;
741 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theCoords );
742 aPoints.push_back( aPosPoint );
743 aSectionsMap[theISection] = aPoints;
745 res = addPointsInternal( aSectionsMap );
751 //! For internal use only! Undo/Redo are not used here.
752 bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
756 CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
757 CurveCreator_Section *aSection = 0;
758 for ( ; anIt != theSectionsMap.end(); anIt++ ) {
759 int anISection = anIt->first;
760 aSection = mySections.at(anISection);
762 CurveCreator::PosPointsList aSectionPoints = anIt->second;
763 CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
764 for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
765 int anIPnt = (*aPntIt)->myID;
766 CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
767 for ( int i = 0; i < myDimension; i++)
768 aSection->myPoints.at(toICoord(anIPnt) + i) = aCoords[i];
779 //! Set coordinates of specified point
780 bool CurveCreator_Curve::setPoint( const int theISection,
782 const CurveCreator::Coordinates& theNewCoords )
785 // Set the difference.
787 if (addEmptyDiff()) {
788 CurveCreator_ICurve::SectionToPointCoordsList aList;
789 aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theNewCoords));
790 myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
793 CurveCreator::SectionsMap aSectionsMap;
794 CurveCreator::PosPointsList aPoints;
795 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theNewCoords );
796 aPoints.push_back( aPosPoint );
797 aSectionsMap[theISection] = aPoints;
799 int aSize1 = getNbPoints( theISection );
800 res = setPointInternal( aSectionsMap );
801 int aSize2 = getNbPoints( theISection );
808 //! Set coordinates of specified points from different sections
809 bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords)
812 // Set the difference.
814 if (addEmptyDiff()) {
815 myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
816 theSectionToPntCoords);
818 CurveCreator::SectionsMap aSectionsMap;
819 CurveCreator::PosPointsList aPosPoints;
820 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
821 theSectionToPntCoords.begin(), aLast = theSectionToPntCoords.end();
822 int aSectionId, aPointId;
823 for ( ; anIt != aLast; anIt++ ) {
825 aSectionId = anIt->first.first;
826 aPointId = anIt->first.second;
827 CurveCreator::Coordinates aNewCoords = anIt->second;
828 CurveCreator_PosPoint* aPosPoint =
829 new CurveCreator_PosPoint( aPointId, aNewCoords );
830 if( aSectionsMap.find(aSectionId) != aSectionsMap.end() )
831 aPosPoints = aSectionsMap[aSectionId];
832 aPosPoints.push_back( aPosPoint );
833 aSectionsMap[aSectionId] = aPosPoints;
836 res = setPointInternal( aSectionsMap );
842 //! For internal use only! Undo/Redo are not used here.
843 bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
846 std::map<int, std::list<int> > aConvPoints;
847 convert( thePoints, aConvPoints );
848 std::map<int, std::list<int> >::const_iterator anIt = aConvPoints.begin(),
849 aLast = aConvPoints.end();
850 for ( ; anIt != aLast; anIt++ ) {
851 int aSectionId = anIt->first;
852 aRes = removeSectionPoints(aSectionId, anIt->second);
860 //! Remove point with given id
861 bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
864 // Set the difference.
866 if (addEmptyDiff()) {
867 myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
868 theISection, theIPnt);
870 SectionToPointList aListOfSectionsToPoints;
871 aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
872 res = removePointsInternal( aListOfSectionsToPoints );
877 //! Remove several points from different sections with given ids
878 bool CurveCreator_Curve::removeSeveralPoints( const SectionToPointList &theSectionToPntIDs)
881 // Set the difference.
883 if (addEmptyDiff()) {
884 myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
887 res = removePointsInternal( theSectionToPntIDs );
892 //=======================================================================
893 // function: getCoordinates
895 //=======================================================================
896 CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
897 const int theIPnt) const
899 CurveCreator_Section *aSection = mySections.at(theISection);
900 CurveCreator::Coordinates::const_iterator
901 anIter = aSection->myPoints.begin() + toICoord(theIPnt);
902 CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
907 //=======================================================================
908 // function: getPoints
910 //=======================================================================
911 CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
913 CurveCreator::Coordinates aCoords;
914 if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
916 aCoords = mySections.at(theISection)->myPoints;
921 void CurveCreator_Curve::constructAISObject()
924 CurveCreator_Utils::constructShape( this, aShape );
926 myAISShape = new AIS_Shape( aShape );
929 Handle(AIS_InteractiveObject) CurveCreator_Curve::getAISObject( const bool theNeedToBuild ) const
931 if ( !myAISShape && theNeedToBuild ) {
932 CurveCreator_Curve* aCurve = (CurveCreator_Curve*)this;
933 aCurve->constructAISObject();
938 bool CurveCreator_Curve::removeSectionPoints( const int theSectionId,
939 const std::list<int>& thePointIds )
943 CurveCreator_Section *aSection = mySections.at( theSectionId );
947 std::list<int> aSectionPoints = thePointIds;
948 aSectionPoints.sort();
949 std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
950 for ( ; aPntIt != aSectionPoints.rend(); aPntIt++ ) {
951 int aPntIndx = *aPntIt;
952 CurveCreator::Coordinates::iterator aFirstPosition;
953 if ( aPntIndx == -1 )
954 aFirstPosition = aSection->myPoints.end() - getDimension();
956 aFirstPosition = aSection->myPoints.begin() + toICoord( aPntIndx );
957 aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
963 void CurveCreator_Curve::convert( const SectionToPointList& thePoints,
964 std::map< int, std::list<int> >& theConvPoints )
966 theConvPoints.clear();
968 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
969 std::list<int> aPoints;
970 int aSectionId, aPointId;
971 for ( ; anIt != aLast; anIt++ ) {
972 aSectionId = anIt->first;
973 aPointId = anIt->second;
975 if ( theConvPoints.find( aSectionId ) != theConvPoints.end() )
976 aPoints = theConvPoints[aSectionId];
977 aPoints.push_back( aPointId );
978 theConvPoints[aSectionId] = aPoints;