Salome HOME
Bug 158: Crash in profile edition
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index 824338b29055919aa2f048b9efc3a97e2ef0724e..0c6c45be4b9bb85032d42816ef96cd575d0aabac 100644 (file)
 #include "CurveCreator_PosPoint.hxx"
 #include "CurveCreator_Section.hxx"
 #include "CurveCreator_Displayer.h"
+#include "CurveCreator_Utils.h"
 
-#include <AIS_Point.hxx>
-#include <AIS_Line.hxx>
 #include <AIS_Shape.hxx>
-#include <BRepBuilderAPI_MakeEdge.hxx>
-#include <BRepBuilderAPI_MakeWire.hxx>
+#include <AIS_InteractiveObject.hxx>
 #include <Geom_CartesianPoint.hxx>
 #include <gp_Pnt.hxx>
 #include <gp_Lin.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopoDS_Wire.hxx>
-#include <TColgp_HArray1OfPnt.hxx>
-#include <GeomAPI_Interpolate.hxx>
 
 #include <stdio.h>
 
@@ -51,6 +47,7 @@ CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimensi
 : myIsLocked  (false),
   myDimension (theDimension),
   myDisplayer (NULL),
+  myAISShape  (NULL),
   myNbUndos   (0),
   myNbRedos   (0),
   myUndoDepth (-1),
@@ -176,81 +173,6 @@ int CurveCreator_Curve::toICoord(const int theIPnt) const
   return theIPnt * myDimension;
 }
 
-std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructSection( int theISection ) const
-{
-  std::vector<Handle_AIS_InteractiveObject> aSectionRepresentation;
-
-  CurveCreator::SectionType aSectType = getSectionType( theISection );
-  int aSectSize = getNbPoints( theISection );
-  bool aSectIsClosed = isClosed( theISection );
-
-  if( aSectType == CurveCreator::Polyline )
-  {
-    int iPoint = 0; 
-    for( ; iPoint < ( aSectSize - 1 ) ; iPoint++ ){
-      Handle_AIS_Point anAISPnt = getAISPoint(theISection, iPoint);
-      aSectionRepresentation.push_back( anAISPnt );
-      Handle_AIS_Line aLine = getAISLine( theISection, iPoint, iPoint+1 );
-      aSectionRepresentation.push_back( aLine );
-    }
-    if( aSectSize != 0 ){
-      Handle_AIS_Point anAISPnt = getAISPoint(theISection, iPoint); 
-      aSectionRepresentation.push_back( anAISPnt );
-      if( isClosed(theISection) && ( aSectSize > 1 ) ){
-        Handle_AIS_Line aLine = getAISLine( theISection, iPoint, 0 );
-        aSectionRepresentation.push_back( aLine );
-      }
-    }
-  }
-  else if( aSectType == CurveCreator::Spline )
-  {
-    std::vector<double> aPoints;
-    for( int iPoint = 0; iPoint < aSectSize; iPoint++ )
-    {
-      Handle_AIS_Point anAISPnt = getAISPoint( theISection, iPoint );
-      aSectionRepresentation.push_back( anAISPnt );
-
-      CurveCreator::Coordinates aCoords = getPoint( theISection, iPoint );
-      double aX = aCoords[0];
-      double aY = aCoords[1];
-      double aZ = aCoords[2];
-      aPoints.push_back( aX );
-      aPoints.push_back( aY );
-    }
-
-    if( aSectSize > 1 )
-    {
-      Handle(Geom_BSplineCurve) aBSplineCurve;
-      // fill array for algorithm by the received coordinates
-      int aLen = aPoints.size() / 2;
-      Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
-      std::vector<double>::const_iterator aListIter = aPoints.begin();
-      for (int ind = 1; ind <= aLen; ind++) {
-        gp_Pnt aPnt(gp::Origin());
-        aPnt.SetX(*aListIter);
-        aListIter++;
-        aPnt.SetY(*aListIter);
-        aListIter++;
-        aPnt.SetZ(0);
-        aHCurvePoints->SetValue(ind, aPnt);
-      }
-      // compute BSpline
-      GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
-      aGBC.Perform();
-      if (aGBC.IsDone()) {
-        aBSplineCurve = aGBC.Curve();
-      }
-      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
-
-      TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
-
-      Handle(AIS_Shape) aShape = new AIS_Shape( aWire );
-      aSectionRepresentation.push_back( aShape );
-    }
-  }
-  return aSectionRepresentation;
-}
-
 //=======================================================================
 // function: setUndoDepth
 // purpose:
@@ -309,35 +231,6 @@ int CurveCreator_Curve::getUndoDepth() const
   return myUndoDepth;
 }
 
-Handle_AIS_Point CurveCreator_Curve::getAISPoint( int theISection, int theIPoint ) const
-{
-  double anX, anY, aZ;
-  getCoordinates( theISection, theIPoint, anX, anY, aZ );
-  gp_Pnt aPoint( anX, anY, aZ);
-
-  AIS_Point* aPnt = new AIS_Point( new Geom_CartesianPoint(aPoint));
-  return aPnt;
-}
-
-Handle_AIS_Line CurveCreator_Curve::getAISLine( int theISection, int theIPoint1, int theIPoint2 ) const
-{
-  double anX, anY, aZ;
-  getCoordinates( theISection, theIPoint1, anX, anY, aZ );
-  gp_Pnt aPoint1( anX, anY, aZ);
-
-  double anX2, anY2, aZ2;
-  getCoordinates( theISection, theIPoint2, anX2, anY2, aZ2 );
-//MTN to avoid crash during line construction
-  if( ( anX == anX2 ) && ( anY == anY2 ) && (aZ == aZ2 ) ){
-    aZ2 += 1e-7;
-  }
-
-  gp_Pnt aPoint2( anX2, anY2, aZ2 );
-
-  AIS_Line* aLine = new AIS_Line( new Geom_CartesianPoint(aPoint1), new Geom_CartesianPoint(aPoint2) );
-  return aLine;
-}
-
 void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double& theX, double& theY, double& theZ ) const
 {
   CurveCreator::Coordinates aCoords = getPoint( theISection, theIPoint );
@@ -352,8 +245,10 @@ void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double&
 void CurveCreator_Curve::redisplayCurve()
 {
   if( myDisplayer ) {
-    myDisplayer->erase();
-    myDisplayer->display( constructWire() );
+    myDisplayer->eraseAll( false );
+    myAISShape = NULL;
+
+    myDisplayer->display( getAISObject( true ), true );
   }
 }
 
@@ -399,25 +294,6 @@ bool CurveCreator_Curve::moveSection(const int theISection,
   return res;
 }
 
-void CurveCreator_Curve::convert( const SectionToPointList& thePoints,
-                                  std::map< int, std::list<int> >& theConvPoints )
-{
-  theConvPoints.clear();
-
-  SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
-  std::list<int> aPoints;
-  int aSectionId, aPointId;
-  for ( ; anIt != aLast; anIt++ ) {
-    aSectionId = anIt->first;
-    aPointId = anIt->second;
-    aPoints.clear();
-    if ( theConvPoints.find( aSectionId ) != theConvPoints.end() )
-      aPoints = theConvPoints[aSectionId];
-    aPoints.push_back( aPointId );
-    theConvPoints[aSectionId] = aPoints;
-  }
-}
-
 /************   Implementation of INTERFACE methods   ************/
 
 /***********************************************/
@@ -471,8 +347,10 @@ bool CurveCreator_Curve::redo()
 bool CurveCreator_Curve::clearInternal()
 {
   // erase curve from the viewer
-  if( myDisplayer )
-    myDisplayer->erase();
+  if( myDisplayer ) {
+    myDisplayer->eraseAll( true );
+    myAISShape = NULL;
+  }
   // Delete all allocated data.
   int i = 0;
   const int aNbSections = getNbSections();
@@ -520,20 +398,18 @@ bool CurveCreator_Curve::joinInternal( const int theISectionTo,
   return res;
 }
 
-//! Join range of sections to one section (join all sections if -1 is passed in one of arguments)
+//! Join range of sections to one section (join all sections if -1 is passed in theISectionFrom argument)
 bool CurveCreator_Curve::join( const int theISectionTo, 
                                const int theISectionFrom )
 {
+  //TODO
   bool res = false;
-  if (mySections.size() >= 2 && 
-    ( theISectionTo != theISectionFrom || (theISectionTo == -1 && theISectionFrom == -1) )) {
+  if ( theISectionTo != theISectionFrom ) {
     startOperation();
-    int aISectionTo = getNbSections()-1;
-    int aISectionFrom = 0;
     if (addEmptyDiff())
-      myListDiffs.back().init(this, CurveCreator_Operation::Join, aISectionTo, aISectionFrom);
+      myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
 
-    res = joinInternal( aISectionTo, aISectionFrom );
+    res = joinInternal( theISectionTo, theISectionFrom );
 
     finishOperation();
   }
@@ -955,34 +831,19 @@ bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSe
 //! For internal use only! Undo/Redo are not used here.
 bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
 {
-  bool res = false;
+  bool aRes = false;
   std::map<int, std::list<int> > aConvPoints;
   convert( thePoints, aConvPoints );
   std::map<int, std::list<int> >::const_iterator anIt = aConvPoints.begin(),
                                           aLast = aConvPoints.end();
-  CurveCreator_Section *aSection = 0;
   for ( ; anIt != aLast; anIt++ ) {
     int aSectionId = anIt->first;
-    aSection = mySections.at(aSectionId);
-    if( aSection ) {
-      std::list<int> aSectionPoints = anIt->second;
-      aSectionPoints.sort();
-      std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
-      for( ; aPntIt != aSectionPoints.rend(); aPntIt++ ){
-        int aPntIndx = *aPntIt;
-        CurveCreator::Coordinates::iterator aFirstPosition;
-        if(aPntIndx == -1)
-          aFirstPosition = aSection->myPoints.end() - getDimension();
-        else
-          aFirstPosition = aSection->myPoints.begin() + toICoord(aPntIndx);
-        aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
-        res = true;
-      }
-    }
+    aRes = removeSectionPoints(aSectionId, anIt->second);
   }
-  if(res)
+  if( aRes)
     redisplayCurve();
-  return res;
+
+  return aRes;
 }
 
 //! Remove point with given id
@@ -1046,19 +907,63 @@ CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection )
   return aCoords;
 }
 
+void CurveCreator_Curve::constructAISObject()
+{
+  TopoDS_Shape aShape;
+  CurveCreator_Utils::constructShape( this, aShape );
 
-/***********************************************/
-/***       Presentation methods              ***/
-/***********************************************/
-std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructWire() const
+  myAISShape = new AIS_Shape( aShape );
+}
+
+Handle(AIS_InteractiveObject) CurveCreator_Curve::getAISObject( const bool theNeedToBuild ) const
 {
-  std::vector<Handle_AIS_InteractiveObject> aCurveRepresentation;
-  std::vector<Handle_AIS_InteractiveObject> aSectionObjects;
-  for( int iSection = 0 ; iSection < getNbSections() ; iSection++ ){
-    aSectionObjects = constructSection( iSection );
-    for( int iObject = 0 ; iObject < aSectionObjects.size() ; iObject++ ){
-      aCurveRepresentation.push_back( aSectionObjects.at(iObject) );
-    }
+  if ( !myAISShape && theNeedToBuild ) {
+    CurveCreator_Curve* aCurve = (CurveCreator_Curve*)this;
+    aCurve->constructAISObject();
+  }
+  return myAISShape;
+}
+
+bool CurveCreator_Curve::removeSectionPoints( const int theSectionId,
+                                              const std::list<int>& thePointIds )
+{
+  bool aRes = false;
+
+  CurveCreator_Section *aSection = mySections.at( theSectionId );
+  if ( !aSection )
+    return aRes;
+
+  std::list<int> aSectionPoints = thePointIds;
+  aSectionPoints.sort();
+  std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
+  for ( ; aPntIt != aSectionPoints.rend(); aPntIt++ ) {
+    int aPntIndx = *aPntIt;
+    CurveCreator::Coordinates::iterator aFirstPosition;
+    if ( aPntIndx == -1 )
+      aFirstPosition = aSection->myPoints.end() - getDimension();
+    else
+      aFirstPosition = aSection->myPoints.begin() + toICoord( aPntIndx );
+    aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
+    aRes = true;
+  }
+  return aRes;
+}
+
+void CurveCreator_Curve::convert( const SectionToPointList& thePoints,
+                                  std::map< int, std::list<int> >& theConvPoints )
+{
+  theConvPoints.clear();
+
+  SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
+  std::list<int> aPoints;
+  int aSectionId, aPointId;
+  for ( ; anIt != aLast; anIt++ ) {
+    aSectionId = anIt->first;
+    aPointId = anIt->second;
+    aPoints.clear();
+    if ( theConvPoints.find( aSectionId ) != theConvPoints.end() )
+      aPoints = theConvPoints[aSectionId];
+    aPoints.push_back( aPointId );
+    theConvPoints[aSectionId] = aPoints;
   }
-  return aCurveRepresentation;
 }