Salome HOME
OCC functionality moving out from the widget
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index f27cfb2ef807efd86750ec8da738cab674fd5be5..99845e43dac2aa3ae38942d1adf1f86bcc0f5a54 100644 (file)
@@ -23,6 +23,7 @@
 #include "CurveCreator_Curve.hxx"
 
 #include "CurveCreator.hxx"
+#include "CurveCreator_PosPoint.hxx"
 #include "CurveCreator_Section.hxx"
 #include "CurveCreator_Displayer.h"
 
@@ -42,6 +43,8 @@
 
 #include <stdio.h>
 
+//#define AIS_CURVE_DISPLAY
+
 //=======================================================================
 // function: Constructor
 // purpose:
@@ -55,6 +58,9 @@ CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimensi
   myUndoDepth (-1),
   myOpLevel(0)
 {
+#ifdef AIS_CURVE_DISPLAY
+  myAISCurve = new CurveCreator_AISCurve( this );
+#endif
 }
 
 //=======================================================================
@@ -104,6 +110,19 @@ std::string CurveCreator_Curve::getUniqSectionName() const
 void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
 {
   myDisplayer = theDisplayer;
+  
+#ifdef AIS_CURVE_DISPLAY
+  myDisplayer->displayAIS( myAISCurve, false );
+#endif
+}
+
+//=======================================================================
+// function: getDisplayer
+// purpose: get curve changes Displayer
+//=======================================================================
+CurveCreator_Displayer* CurveCreator_Curve::getDisplayer()
+{
+  return myDisplayer;
 }
 
 //=======================================================================
@@ -166,36 +185,83 @@ int CurveCreator_Curve::toICoord(const int theIPnt) const
   return theIPnt * myDimension;
 }
 
+#include <TopoDS_Compound.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
+#include <BRepBuilderAPI_MakeEdge.hxx>
+//#define USE_COMPOUND
 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 );
+  int aPointSize = getNbPoints( theISection );
   bool aSectIsClosed = isClosed( theISection );
-
   if( aSectType == CurveCreator::Polyline )
   {
+#ifdef USE_COMPOUND
+    BRep_Builder aBuilder;
+    TopoDS_Compound aComp;
+    aBuilder.MakeCompound(aComp);
+
+    int iPoint = 0;
+    gp_Pnt aPrevPoint, aPoint;
+    if ( aPointSize == 1 ) {
+      getPoint( theISection, iPoint, aPrevPoint );
+      TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+    }
+    else if ( aPointSize > 1 ) {
+      TopoDS_Edge aPointEdge;
+      TopoDS_Vertex aVertex;
+      getPoint( theISection, iPoint, aPrevPoint );
+      aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+      iPoint++;
+      for( ; iPoint < aPointSize; iPoint++ ) {
+        getPoint( theISection, iPoint, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+        aBuilder.Add( aComp, aPointEdge );
+        aPrevPoint = aPoint;
+      }
+      if( isClosed( theISection ) && ( aPointSize > 2 ) ) {
+        getPoint( theISection, 0, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+        aBuilder.Add( aComp, aPointEdge );
+      }
+      AIS_Shape* aShape = new AIS_Shape( aComp );
+      //aShape->SetSelectionMode( AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
+      aSectionRepresentation.push_back( aShape );
+    }
+#else
     int iPoint = 0; 
-    for( ; iPoint < ( aSectSize - 1 ) ; iPoint++ ){
+    for( ; iPoint < ( aPointSize - 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 ){
+    if( aPointSize != 0 ){
       Handle_AIS_Point anAISPnt = getAISPoint(theISection, iPoint); 
       aSectionRepresentation.push_back( anAISPnt );
-      if( isClosed(theISection) && ( aSectSize > 1 ) ){
+      if( isClosed(theISection) && ( aPointSize > 1 ) ){
         Handle_AIS_Line aLine = getAISLine( theISection, iPoint, 0 );
         aSectionRepresentation.push_back( aLine );
       }
     }
+#endif
   }
   else if( aSectType == CurveCreator::Spline )
   {
+#ifdef USE_COMPOUND
+
+#else
     std::vector<double> aPoints;
-    for( int iPoint = 0; iPoint < aSectSize; iPoint++ )
+    for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
     {
       Handle_AIS_Point anAISPnt = getAISPoint( theISection, iPoint );
       aSectionRepresentation.push_back( anAISPnt );
@@ -208,7 +274,7 @@ std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructSection(
       aPoints.push_back( aY );
     }
 
-    if( aSectSize > 1 )
+    if( aPointSize > 1 )
     {
       Handle(Geom_BSplineCurve) aBSplineCurve;
       // fill array for algorithm by the received coordinates
@@ -237,6 +303,7 @@ std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructSection(
       Handle(AIS_Shape) aShape = new AIS_Shape( aWire );
       aSectionRepresentation.push_back( aShape );
     }
+#endif
   }
   return aSectionRepresentation;
 }
@@ -342,8 +409,26 @@ void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double&
 void CurveCreator_Curve::redisplayCurve()
 {
   if( myDisplayer ) {
-    myDisplayer->erase();
-    myDisplayer->display( constructWire() );
+#ifdef AIS_CURVE_DISPLAY
+    myDisplayer->redisplayAIS(myAISCurve, false);
+#else
+    myDisplayer->erase( false );
+//#ifndef USE_COMPOUND
+    myDisplayer->display( constructWire(), true );
+//#else
+    /*std::vector<Handle_AIS_InteractiveObject> anAISObjects = constructWire();
+    int aSelMode = AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX );
+    Handle(AIS_InteractiveContext) aContext = myDisplayer->getAISContext();
+    for( int i = 0 ; i < anAISObjects.size() ; i++ ){
+      Handle_AIS_InteractiveObject anAISObject = anAISObjects[i];
+      //anAISObject->SetSelectionMode( aSelMode );
+      aContext->Display( anAISObject, Standard_False );
+      //aContext->Display( anAISObject, 0, aSelMode, Standard_False, Standard_True );
+    }
+      aContext->UpdateCurrentViewer();
+    */
+//#endif
+#endif
   }
 }
 
@@ -389,6 +474,25 @@ 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   ************/
 
 /***********************************************/
@@ -443,7 +547,7 @@ bool CurveCreator_Curve::clearInternal()
 {
   // erase curve from the viewer
   if( myDisplayer )
-    myDisplayer->erase();
+    myDisplayer->erase( true );
   // Delete all allocated data.
   int i = 0;
   const int aNbSections = getNbSections();
@@ -491,12 +595,13 @@ 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 (theISectionTo != theISectionFrom) {
+  if ( theISectionTo != theISectionFrom ) {
     startOperation();
     if (addEmptyDiff())
       myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
@@ -625,7 +730,8 @@ int CurveCreator_Curve::getNbPoints( const int theISection ) const
       aNbCoords += mySections[i]->myPoints.size();
     }
   } else {
-    aNbCoords = mySections.at(theISection)->myPoints.size();
+    if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
+      aNbCoords = mySections.at(theISection)->myPoints.size();
   }
 
   return aNbCoords/myDimension;
@@ -766,35 +872,35 @@ bool CurveCreator_Curve::setSectionType( const int theISection,
 /***********************************************/
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::addPointsInternal( const CurveCreator::Coordinates& theCoords,
-                                            const std::vector<int> &theISections,
-                                            const std::vector<int> &theIPnts  )
+bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
 {
   bool res = false;
-  if( (theCoords.size()/getDimension()) == theISections.size() == theIPnts.size() ) {
-    for( int ind = 0; ind < theISections.size(); ind++ ) {
-      int anISection = theISections.at(ind);
-      CurveCreator_Section *aSection =
-        (anISection == -1 ? mySections.back() : mySections.at(anISection));
-
-      if( aSection ) {
-        int anIPnt = theIPnts.at(ind);
+  CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
+  CurveCreator_Section *aSection = 0;
+  for ( ; anIt != theSectionsMap.end(); anIt++ ) {
+    int anISection = anIt->first;
+    aSection = mySections.at(anISection);
+    if( aSection ) {
+      CurveCreator::PosPointsList aSectionPoints = anIt->second;
+      CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
+      for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
+        int anIPnt = (*aPntIt)->myID;
+        CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
         CurveCreator::Coordinates::iterator anIterPosition;
         if(anIPnt == -1)
           anIterPosition = aSection->myPoints.end();
         else
           anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
         CurveCreator::Coordinates::const_iterator aFirstPosition = 
-          theCoords.begin() + toICoord(ind);        
+          aCoords.begin();
         aSection->myPoints.insert(anIterPosition,
-                                  aFirstPosition, 
-                                  aFirstPosition + getDimension());
-        res = true;
+                                  aCoords.begin(), aCoords.end());
       }
+      res = true;
     }
-    if(res)
-      redisplayCurve();
   }
+  if(res)
+    redisplayCurve();
   return res;
 }
 
@@ -811,34 +917,48 @@ bool CurveCreator_Curve::addPoints( const CurveCreator::Coordinates& theCoords,
   // Set the difference.
   startOperation();
   if (addEmptyDiff()) {
+    CurveCreator_ICurve::SectionToPointCoordsList aList;
+    aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theCoords));
     myListDiffs.back().init(this, CurveCreator_Operation::InsertPoints,
-                            theCoords, theISection, theIPnt);
+                            aList);
   }
-  std::vector<int> anISections, anIPnts;
-  anISections.push_back( theISection );
-  anIPnts.push_back( theIPnt );
-  res = addPointsInternal( theCoords, anISections, anIPnts );
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPoints;
+  CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theCoords );
+  aPoints.push_back( aPosPoint );
+  aSectionsMap[theISection] = aPoints;
+
+  res = addPointsInternal( aSectionsMap );
+
   finishOperation();
   return res;
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::setPointInternal( const int theISection,
-                                           const int theIPnt,
-                                           const CurveCreator::Coordinates& theNewCoords )
+bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
 {
   bool res = false;
   // Update the curve.
-  if (theNewCoords.size() == myDimension) {
-    CurveCreator_Section *aSection = mySections.at(theISection);
-    int i;
-    for (i = 0; i < myDimension; i++) {
-      aSection->myPoints.at(toICoord(theIPnt) + i) = theNewCoords[i];
+  CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
+  CurveCreator_Section *aSection = 0;
+  for ( ; anIt != theSectionsMap.end(); anIt++ ) {
+    int anISection = anIt->first;
+    aSection = mySections.at(anISection);
+    if( aSection ) { 
+      CurveCreator::PosPointsList aSectionPoints = anIt->second;
+      CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
+      for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
+        int anIPnt = (*aPntIt)->myID;
+        CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
+        for ( int i = 0; i < myDimension; i++)
+          aSection->myPoints.at(toICoord(anIPnt) + i) = aCoords[i];
+      }
+      res = true;
     }
-    redisplayCurve();
-    
-    res = true;
   }
+  if(res)
+    redisplayCurve();
+  
   return res;
 }
 
@@ -851,38 +971,90 @@ bool CurveCreator_Curve::setPoint( const int theISection,
   // Set the difference.
   startOperation();
   if (addEmptyDiff()) {
+    CurveCreator_ICurve::SectionToPointCoordsList aList;
+    aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theNewCoords));
     myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
-                            theNewCoords, theISection, theIPnt);
+                            aList);
   }
-  res = setPointInternal( theISection, theIPnt, theNewCoords );
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPoints;
+  CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theNewCoords );
+  aPoints.push_back( aPosPoint );
+  aSectionsMap[theISection] = aPoints;
+
+  int aSize1 = getNbPoints( theISection );
+  res = setPointInternal( aSectionsMap );
+  int aSize2 = getNbPoints( theISection );
+
+  finishOperation();
+
+  return res; 
+}
+
+//! Set coordinates of specified points from different sections
+bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords)
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
+                            theSectionToPntCoords);
+  }
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPosPoints;
+  CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = 
+    theSectionToPntCoords.begin(), aLast = theSectionToPntCoords.end();
+  int aSectionId, aPointId;
+  for ( ; anIt != aLast; anIt++ ) {
+    aPosPoints.clear();
+    aSectionId = anIt->first.first;
+    aPointId = anIt->first.second;
+    CurveCreator::Coordinates aNewCoords = anIt->second;
+    CurveCreator_PosPoint* aPosPoint = 
+      new CurveCreator_PosPoint( aPointId, aNewCoords );
+    if( aSectionsMap.find(aSectionId) != aSectionsMap.end() )
+      aPosPoints = aSectionsMap[aSectionId];
+    aPosPoints.push_back( aPosPoint );
+    aSectionsMap[aSectionId] = aPosPoints;
+    
+  }
+  res = setPointInternal( aSectionsMap );
   finishOperation();
 
   return res; 
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::removePointsInternal( const std::vector<int> &theISections, 
-                                               const std::vector<int> &theIPnts )
+bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
 {
   bool res = false;
-  if( theISections.size() == theIPnts.size() ) {
-    for( int ind = 0; ind < theISections.size(); ind++ ) {
-      int anISection = theISections.at(ind);
-      CurveCreator_Section *aSection = mySections.at(anISection);
-      if( aSection ) {
-        int anIPnt = theIPnts.at(ind);
+  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(anIPnt == -1)
-          aFirstPosition = aSection->myPoints.end();
+        if(aPntIndx == -1)
+          aFirstPosition = aSection->myPoints.end() - getDimension();
         else
-          aFirstPosition = aSection->myPoints.begin() + toICoord(anIPnt);
+          aFirstPosition = aSection->myPoints.begin() + toICoord(aPntIndx);
         aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
         res = true;
       }
     }
-    if(res)
-      redisplayCurve();
   }
+  if(res)
+    redisplayCurve();
   return res;
 }
 
@@ -896,15 +1068,29 @@ bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
     myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
                             theISection, theIPnt);
   }
-  std::vector<int> anISections, anIPnts;
-  anISections.push_back( theISection );
-  anIPnts.push_back( theIPnt );
-  res = removePointsInternal( anISections, anIPnts );
+  SectionToPointList aListOfSectionsToPoints;
+  aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
+  res = removePointsInternal( aListOfSectionsToPoints );
   finishOperation();
   return res;
 }
 
-//=======================================================================
+//! Remove several points from different sections with given ids
+bool CurveCreator_Curve::removeSeveralPoints( const SectionToPointList &theSectionToPntIDs)
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
+                            theSectionToPntIDs);
+  }
+  res = removePointsInternal( theSectionToPntIDs );
+  finishOperation();
+  return res;
+}
+
+  //=======================================================================
 // function: getCoordinates
 // purpose:
 //=======================================================================
@@ -925,7 +1111,12 @@ CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
 //=======================================================================
 CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
 {
-  return mySections.at(theISection)->myPoints;
+  CurveCreator::Coordinates aCoords;
+  if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
+  {
+    aCoords = mySections.at(theISection)->myPoints;
+  }
+  return aCoords;
 }
 
 
@@ -944,3 +1135,11 @@ std::vector<Handle_AIS_InteractiveObject> CurveCreator_Curve::constructWire() co
   }
   return aCurveRepresentation;
 }
+
+void CurveCreator_Curve::getPoint( const int theISection, const int theIPoint,
+                                   gp_Pnt& thePoint ) const
+{
+  double anX, anY, aZ;
+  getCoordinates( theISection, theIPoint, anX, anY, aZ );
+  thePoint = gp_Pnt( anX, anY, aZ);
+}