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_Section.hxx"
27 #include "CurveCreator_Displayer.h"
29 #include <AIS_Point.hxx>
30 #include <AIS_Line.hxx>
31 #include <AIS_Shape.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <Geom_CartesianPoint.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Wire.hxx>
40 #include <TColgp_HArray1OfPnt.hxx>
41 #include <GeomAPI_Interpolate.hxx>
45 //=======================================================================
46 // function: Constructor
48 //=======================================================================
49 CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimension )
51 myDimension (theDimension),
60 //=======================================================================
61 // function: Destructor
63 //=======================================================================
64 CurveCreator_Curve::~CurveCreator_Curve()
66 // Delete all allocated data.
70 //=======================================================================
71 // function: getDimension
73 //=======================================================================
74 CurveCreator::Dimension CurveCreator_Curve::getDimension() const
79 //=======================================================================
80 // function: getUniqSectionName
81 // purpose: return unique section name
82 //=======================================================================
83 std::string CurveCreator_Curve::getUniqSectionName() const
85 for( int i = 0 ; i < 1000000 ; i++ ){
87 sprintf( aBuffer, "Section_%d", i+1 );
88 std::string aName(aBuffer);
90 for( j = 0 ; j < mySections.size() ; j++ ){
91 if( mySections[j]->myName == aName )
94 if( j == mySections.size() )
100 //=======================================================================
101 // function: setDisplayer
102 // purpose: set curve changes Displayer
103 //=======================================================================
104 void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
106 myDisplayer = theDisplayer;
109 //=======================================================================
110 // function: removeDisplayer
111 // purpose: remove the attached Displayer
112 //=======================================================================
113 void CurveCreator_Curve::removeDisplayer()
118 //=======================================================================
121 //=======================================================================
122 bool CurveCreator_Curve::addEmptyDiff()
124 bool isEnabled = false;
126 if (myUndoDepth != 0) {
127 // Forget all Redos after the current one.
130 myListDiffs.erase(myCurrenPos, myListDiffs.end());
133 if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
134 // Increase the number of undos.
137 // If there are too many differences, remove the first one.
138 myListDiffs.pop_front();
141 // Add new difference.
142 myListDiffs.push_back(CurveCreator_Diff());
143 myCurrenPos = myListDiffs.end();
150 void CurveCreator_Curve::startOperation()
155 void CurveCreator_Curve::finishOperation()
160 //=======================================================================
161 // function: toICoord
163 //=======================================================================
164 int CurveCreator_Curve::toICoord(const int theIPnt) const
166 return theIPnt * myDimension;
169 std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructSection( int theISection ) const
171 std::vector<Handle_AIS_InteractiveObject> aSectionRepresentation;
173 CurveCreator::SectionType aSectType = getSectionType( theISection );
174 int aSectSize = getNbPoints( theISection );
175 bool aSectIsClosed = isClosed( theISection );
177 if( aSectType == CurveCreator::Polyline )
180 for( ; iPoint < ( aSectSize - 1 ) ; iPoint++ ){
181 Handle_AIS_Point anAISPnt = getAISPoint(theISection, iPoint);
182 aSectionRepresentation.push_back( anAISPnt );
183 Handle_AIS_Line aLine = getAISLine( theISection, iPoint, iPoint+1 );
184 aSectionRepresentation.push_back( aLine );
186 if( aSectSize != 0 ){
187 Handle_AIS_Point anAISPnt = getAISPoint(theISection, iPoint);
188 aSectionRepresentation.push_back( anAISPnt );
189 if( isClosed(theISection) && ( aSectSize > 1 ) ){
190 Handle_AIS_Line aLine = getAISLine( theISection, iPoint, 0 );
191 aSectionRepresentation.push_back( aLine );
195 else if( aSectType == CurveCreator::Spline )
197 std::vector<double> aPoints;
198 for( int iPoint = 0; iPoint < aSectSize; iPoint++ )
200 Handle_AIS_Point anAISPnt = getAISPoint( theISection, iPoint );
201 aSectionRepresentation.push_back( anAISPnt );
203 CurveCreator::Coordinates aCoords = getPoint( theISection, iPoint );
204 double aX = aCoords[0];
205 double aY = aCoords[1];
206 double aZ = aCoords[2];
207 aPoints.push_back( aX );
208 aPoints.push_back( aY );
213 Handle(Geom_BSplineCurve) aBSplineCurve;
214 // fill array for algorithm by the received coordinates
215 int aLen = aPoints.size() / 2;
216 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
217 std::vector<double>::const_iterator aListIter = aPoints.begin();
218 for (int ind = 1; ind <= aLen; ind++) {
219 gp_Pnt aPnt(gp::Origin());
220 aPnt.SetX(*aListIter);
222 aPnt.SetY(*aListIter);
225 aHCurvePoints->SetValue(ind, aPnt);
228 GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
231 aBSplineCurve = aGBC.Curve();
233 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
235 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
237 Handle(AIS_Shape) aShape = new AIS_Shape( aWire );
238 aSectionRepresentation.push_back( aShape );
241 return aSectionRepresentation;
244 //=======================================================================
245 // function: setUndoDepth
247 //=======================================================================
248 void CurveCreator_Curve::setUndoDepth(const int theDepth)
251 // Reset all undo/redo data.
255 myCurrenPos = myListDiffs.end();
257 } else if (theDepth == -1) {
258 // There is nothing to do as the depth become unlimited.
260 } else if (theDepth > 0) {
261 // The new "real" depth is set.
262 if (theDepth < myNbRedos) {
263 // The new depth is less then number of redos. Remove the latest redos.
264 int aShift = (myNbRedos - theDepth);
265 ListDiff::iterator aFromPos = myListDiffs.end();
271 myListDiffs.erase(aFromPos, myListDiffs.end());
272 myNbRedos = theDepth;
275 if (theDepth < myNbUndos + myNbRedos) {
276 // The new depth is less then the total number of differences.
277 // Remove the first undos.
278 int aShift = (myNbUndos + myNbRedos - theDepth);
279 ListDiff::iterator aToPos = myListDiffs.begin();
285 myListDiffs.erase(myListDiffs.begin(), aToPos);
286 myNbUndos = theDepth - myNbRedos;
289 myUndoDepth = theDepth;
293 //=======================================================================
294 // function: getUndoDepth
296 //=======================================================================
297 int CurveCreator_Curve::getUndoDepth() const
302 Handle_AIS_Point CurveCreator_Curve::getAISPoint( int theISection, int theIPoint ) const
305 getCoordinates( theISection, theIPoint, anX, anY, aZ );
306 gp_Pnt aPoint( anX, anY, aZ);
308 AIS_Point* aPnt = new AIS_Point( new Geom_CartesianPoint(aPoint));
312 Handle_AIS_Line CurveCreator_Curve::getAISLine( int theISection, int theIPoint1, int theIPoint2 ) const
315 getCoordinates( theISection, theIPoint1, anX, anY, aZ );
316 gp_Pnt aPoint1( anX, anY, aZ);
318 double anX2, anY2, aZ2;
319 getCoordinates( theISection, theIPoint2, anX2, anY2, aZ2 );
320 //MTN to avoid crash during line construction
321 if( ( anX == anX2 ) && ( anY == anY2 ) && (aZ == aZ2 ) ){
325 gp_Pnt aPoint2( anX2, anY2, aZ2 );
327 AIS_Line* aLine = new AIS_Line( new Geom_CartesianPoint(aPoint1), new Geom_CartesianPoint(aPoint2) );
331 void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double& theX, double& theY, double& theZ ) const
333 CurveCreator::Coordinates aCoords = getPoint( theISection, theIPoint );
337 if( getDimension() == CurveCreator::Dim3d ){
342 void CurveCreator_Curve::redisplayCurve()
345 myDisplayer->erase();
346 myDisplayer->display( constructWire() );
350 /************ Implementation of INTERFACE methods ************/
352 /***********************************************/
353 /*** Undo/Redo methods ***/
354 /***********************************************/
356 //! Get number of available undo operations
357 int CurveCreator_Curve::getNbUndo() const
362 //! Undo previous operation
363 bool CurveCreator_Curve::undo()
370 myCurrenPos->applyUndo(this);
376 //! Get number of available redo operations
377 int CurveCreator_Curve::getNbRedo() const
382 //! Redo last previously "undone" operation
383 bool CurveCreator_Curve::redo()
387 myCurrenPos->applyRedo(this);
396 /***********************************************/
397 /*** Section methods ***/
398 /***********************************************/
399 //! For internal use only! Undo/Redo are not used here.
400 bool CurveCreator_Curve::clearInternal()
402 // erase curve from the viewer
404 myDisplayer->erase();
405 // Delete all allocated data.
407 const int aNbSections = getNbSections();
409 for (; i < aNbSections; i++) {
410 delete mySections[i];
418 //=======================================================================
421 //=======================================================================
422 bool CurveCreator_Curve::clear()
426 // Set the difference.
427 if (addEmptyDiff()) {
428 myListDiffs.back().init(this, CurveCreator_Operation::Clear);
430 res = clearInternal();
435 //! For internal use only! Undo/Redo are not used here.
436 bool CurveCreator_Curve::joinInternal( const int theISectionTo,
437 const int theISectionFrom )
440 CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
441 CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
443 aSection1->myPoints.insert(aSection1->myPoints.end(),
444 aSection2->myPoints.begin(),
445 aSection2->myPoints.end());
447 res = removeSection(theISectionFrom);
452 //! Join range of sections to one section (join all sections if -1 is passed in one of arguments)
453 bool CurveCreator_Curve::join( const int theISectionTo,
454 const int theISectionFrom )
457 if (theISectionTo != theISectionFrom) {
460 myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
462 res = joinInternal( theISectionTo, theISectionFrom );
469 //! Get number of sections
470 int CurveCreator_Curve::getNbSections() const
472 return mySections.size();
475 //! For internal use only! Undo/Redo are not used here.
476 int CurveCreator_Curve::addSectionInternal
477 (const std::string& theName, const CurveCreator::SectionType theType,
478 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
480 CurveCreator_Section *aSection = new CurveCreator_Section;
482 std::string aName = theName;
484 aName = getUniqSectionName();
486 aSection->myName = aName;
487 aSection->myType = theType;
488 aSection->myIsClosed = theIsClosed;
489 aSection->myPoints = thePoints;
490 mySections.push_back(aSection);
492 return mySections.size()-1;
495 //=======================================================================
496 // function: addSection
497 // purpose: adds an empty section
498 //=======================================================================
499 int CurveCreator_Curve::addSection
500 (const std::string& theName, const CurveCreator::SectionType theType,
501 const bool theIsClosed)
503 int resISection = -1;
504 // Set the difference.
506 CurveCreator::Coordinates aCoords; //empty list
507 if (addEmptyDiff()) {
508 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
509 theName, aCoords, theType, theIsClosed);
512 resISection = addSectionInternal(theName, theType, theIsClosed, aCoords);
517 //=======================================================================
518 // function: addSection
519 // purpose: adds a section with the given points
520 //=======================================================================
521 int CurveCreator_Curve::addSection
522 (const std::string& theName, const CurveCreator::SectionType theType,
523 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
525 int resISection = -1;
526 // Set the difference.
528 if (addEmptyDiff()) {
529 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
530 theName, thePoints, theType, theIsClosed);
533 resISection = addSectionInternal(theName, theType, theIsClosed, thePoints);
539 //! For internal use only! Undo/Redo are not used here.
540 bool CurveCreator_Curve::removeSectionInternal( const int theISection )
542 if (theISection == -1) {
543 delete mySections.back();
544 mySections.pop_back();
546 CurveCreator::Sections::iterator anIterRm = mySections.begin() + theISection;
549 mySections.erase(anIterRm);
555 //! Removes the given sections.
556 bool CurveCreator_Curve::removeSection( const int theISection )
559 // Set the difference.
562 myListDiffs.back().init(this, CurveCreator_Operation::RemoveSection, theISection);
564 res = removeSectionInternal( theISection );
571 * Get number of points in specified section or (the total number of points
572 * in Curve if theISection is equal to -1).
574 int CurveCreator_Curve::getNbPoints( const int theISection ) const
578 if (theISection == -1) {
580 const int aNbSections = getNbSections();
582 for (; i < aNbSections; i++) {
583 aNbCoords += mySections[i]->myPoints.size();
586 aNbCoords = mySections.at(theISection)->myPoints.size();
589 return aNbCoords/myDimension;
592 //! Get "closed" flag of the specified section
593 bool CurveCreator_Curve::isClosed( const int theISection ) const
595 return mySections.at(theISection)->myIsClosed;
598 //! For internal use only! Undo/Redo are not used here.
599 bool CurveCreator_Curve::setClosedInternal( const int theISection,
600 const bool theIsClosed )
602 if (theISection == -1) {
603 int aSize = mySections.size();
606 for (i = 0; i < aSize; i++) {
607 mySections[i]->myIsClosed = theIsClosed;
611 mySections.at(theISection)->myIsClosed = theIsClosed;
618 * Set "closed" flag of the specified section (all sections if
619 * \a theISection is -1).
621 bool CurveCreator_Curve::setClosed( const int theISection,
622 const bool theIsClosed )
625 // Set the difference.
627 if (addEmptyDiff()) {
628 myListDiffs.back().init(this, CurveCreator_Operation::SetClosed,
629 theIsClosed, theISection);
631 res = setClosedInternal( theISection, theIsClosed );
636 //! Returns specified section name
637 std::string CurveCreator_Curve::getSectionName( const int theISection ) const
639 if( ( theISection >= 0 ) && ( theISection < mySections.size() ))
640 return mySections.at(theISection)->myName;
644 //! For internal use only! Undo/Redo are not used here.
645 bool CurveCreator_Curve::setSectionNameInternal( const int theISection,
646 const std::string& theName )
649 if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
650 mySections.at(theISection)->myName = theName;
656 /** Set name of the specified section */
657 bool CurveCreator_Curve::setSectionName( const int theISection,
658 const std::string& theName )
661 // Set the difference.
663 if (addEmptyDiff()) {
664 myListDiffs.back().init(this, CurveCreator_Operation::RenameSection,
665 theName, theISection);
667 res = setSectionNameInternal( theISection, theName );
672 //! Get type of the specified section
673 CurveCreator::SectionType CurveCreator_Curve::getSectionType
674 ( const int theISection ) const
676 return mySections.at(theISection)->myType;
679 //! For internal use only! Undo/Redo are not used here.
680 bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
681 const CurveCreator::SectionType theType )
683 if (theISection == -1) {
685 const int aNbSections = getNbSections();
687 for (; i < aNbSections; i++) {
688 mySections[i]->myType = theType;
692 if( mySections.at(theISection)->myType != theType ){
693 mySections.at(theISection)->myType = theType;
701 * Set type of the specified section (or all sections
702 * if \a theISection is -1).
704 bool CurveCreator_Curve::setSectionType( const int theISection,
705 const CurveCreator::SectionType theType )
709 // Set the difference.
710 if (addEmptyDiff()) {
711 myListDiffs.back().init(this, CurveCreator_Operation::SetType,
712 theType, theISection);
715 res = setSectionTypeInternal( theISection, theType );
722 /***********************************************/
723 /*** Point methods ***/
724 /***********************************************/
726 //! For internal use only! Undo/Redo are not used here.
727 bool CurveCreator_Curve::addPointsInternal( const CurveCreator::Coordinates& theCoords,
728 const std::vector<int> &theISections,
729 const std::vector<int> &theIPnts )
732 if( (theCoords.size()/getDimension()) == theISections.size() == theIPnts.size() ) {
733 for( int ind = 0; ind < theISections.size(); ind++ ) {
734 int anISection = theISections.at(ind);
735 CurveCreator_Section *aSection =
736 (anISection == -1 ? mySections.back() : mySections.at(anISection));
739 int anIPnt = theIPnts.at(ind);
740 CurveCreator::Coordinates::iterator anIterPosition;
742 anIterPosition = aSection->myPoints.end();
744 anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
745 CurveCreator::Coordinates::const_iterator aFirstPosition =
746 theCoords.begin() + toICoord(ind);
747 aSection->myPoints.insert(anIterPosition,
749 aFirstPosition + getDimension());
760 * Add one point to the specified section starting from the given theIPnt index
761 * (or at the end of points if \a theIPnt is -1).
763 bool CurveCreator_Curve::addPoints( const CurveCreator::Coordinates& theCoords,
764 const int theISection,
768 CurveCreator::Coordinates aCoords = theCoords;
769 // Set the difference.
771 if (addEmptyDiff()) {
772 myListDiffs.back().init(this, CurveCreator_Operation::InsertPoints,
773 theCoords, theISection, theIPnt);
775 std::vector<int> anISections, anIPnts;
776 anISections.push_back( theISection );
777 anIPnts.push_back( theIPnt );
778 res = addPointsInternal( theCoords, anISections, anIPnts );
783 //! For internal use only! Undo/Redo are not used here.
784 bool CurveCreator_Curve::setPointInternal( const int theISection,
786 const CurveCreator::Coordinates& theNewCoords )
790 if (theNewCoords.size() == myDimension) {
791 CurveCreator_Section *aSection = mySections.at(theISection);
793 for (i = 0; i < myDimension; i++) {
794 aSection->myPoints.at(toICoord(theIPnt) + i) = theNewCoords[i];
803 //! Set coordinates of specified point
804 bool CurveCreator_Curve::setPoint( const int theISection,
806 const CurveCreator::Coordinates& theNewCoords )
809 // Set the difference.
811 if (addEmptyDiff()) {
812 myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
813 theNewCoords, theISection, theIPnt);
815 res = setPointInternal( theISection, theIPnt, theNewCoords );
821 //! For internal use only! Undo/Redo are not used here.
822 bool CurveCreator_Curve::removePointsInternal( const std::vector<int> &theISections,
823 const std::vector<int> &theIPnts )
826 if( theISections.size() == theIPnts.size() ) {
827 for( int ind = 0; ind < theISections.size(); ind++ ) {
828 int anISection = theISections.at(ind);
829 CurveCreator_Section *aSection = mySections.at(anISection);
831 int anIPnt = theIPnts.at(ind);
832 CurveCreator::Coordinates::iterator aFirstPosition;
834 aFirstPosition = aSection->myPoints.end();
836 aFirstPosition = aSection->myPoints.begin() + toICoord(anIPnt);
837 aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
847 //! Remove point with given id
848 bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
851 // Set the difference.
853 if (addEmptyDiff()) {
854 myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
855 theISection, theIPnt);
857 std::vector<int> anISections, anIPnts;
858 anISections.push_back( theISection );
859 anIPnts.push_back( theIPnt );
860 res = removePointsInternal( anISections, anIPnts );
865 //=======================================================================
866 // function: getCoordinates
868 //=======================================================================
869 CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
870 const int theIPnt) const
872 CurveCreator_Section *aSection = mySections.at(theISection);
873 CurveCreator::Coordinates::const_iterator
874 anIter = aSection->myPoints.begin() + toICoord(theIPnt);
875 CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
880 //=======================================================================
881 // function: getPoints
883 //=======================================================================
884 CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
886 return mySections.at(theISection)->myPoints;
890 /***********************************************/
891 /*** Presentation methods ***/
892 /***********************************************/
893 std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructWire() const
895 std::vector<Handle_AIS_InteractiveObject> aCurveRepresentation;
896 std::vector<Handle_AIS_InteractiveObject> aSectionObjects;
897 for( int iSection = 0 ; iSection < getNbSections() ; iSection++ ){
898 aSectionObjects = constructSection( iSection );
899 for( int iObject = 0 ; iObject < aSectionObjects.size() ; iObject++ ){
900 aCurveRepresentation.push_back( aSectionObjects.at(iObject) );
903 return aCurveRepresentation;