From 2d62784a5da798371eb82467f39782d114cde1e0 Mon Sep 17 00:00:00 2001
From: Raffaele Gaetano <raffaele.gaetano@cirad.fr>
Date: Sun, 21 Mar 2021 11:48:51 +0100
Subject: [PATCH] ENH: Reworking resume option

---
 app/otbLSGRM.cxx                 |  6 +++---
 include/lsgrmController.txx      |  7 ++-----
 include/lsgrmGraphOperations.txx | 23 ++++++++++++++++++++++-
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/app/otbLSGRM.cxx b/app/otbLSGRM.cxx
index c1601ae..af745e7 100644
--- a/app/otbLSGRM.cxx
+++ b/app/otbLSGRM.cxx
@@ -120,9 +120,9 @@ private:
 	AddParameter(ParameterType_Int, "resumetilecol", "Starting tile column in resume mode");
 	SetDefaultParameterInt("resumetilerow", 0);
 	SetDefaultParameterInt("resumetilecol", 0);
-	//MandatoryOff("resume");
-	//MandatoryOff("resume.tilex");
-	//MandatoryOff("resume.tiley");
+	MandatoryOff("resume");
+	MandatoryOff("resumetilerow");
+	MandatoryOff("resumetilecol");
   }
 
   void DoUpdateParameters()
diff --git a/include/lsgrmController.txx b/include/lsgrmController.txx
index aabbae2..e1c46ef 100644
--- a/include/lsgrmController.txx
+++ b/include/lsgrmController.txx
@@ -95,7 +95,6 @@ void Controller<TSegmenter>::RunSegmentation()
 	// temp. patch, maybe calculate real current memory after resuming graphs.
 	long long unsigned int accumulatedMemory = 2 * m_Memory;
 	
-    if (!m_Resuming) {
 	accumulatedMemory = RunFirstPartialSegmentation<TSegmenter>(
         m_InputImage,
         m_SpecificParameters,
@@ -107,10 +106,8 @@ void Controller<TSegmenter>::RunSegmentation()
         m_NbTilesY,
         m_TileWidth,
         m_TileHeight,
-        isFusion);
-	} else {
-		isFusion = true;
-	}
+        isFusion,
+        m_Resuming);
 
 #ifdef OTB_USE_MPI
     GatherUsefulVariables(accumulatedMemory, isFusion);
diff --git a/include/lsgrmGraphOperations.txx b/include/lsgrmGraphOperations.txx
index 9bbb6a7..15e8afc 100644
--- a/include/lsgrmGraphOperations.txx
+++ b/include/lsgrmGraphOperations.txx
@@ -2,11 +2,18 @@
 #define __LSGRM_GRAPH_OPERATIONS_TXX
 //#include "lsgrmGraphOperations.h"
 //#include <unistd.h>
+#include <fstream>
 #include <cstdio>
 
 namespace lsgrm
 {
 
+bool file_exists(const std::string& fileName)
+{
+    std::ifstream infile(fileName.c_str());
+    return infile.good();
+}
+
 template<class TSegmenter>
 typename TSegmenter::ImageType::Pointer ReadImageRegion(
     typename TSegmenter::ImageType * inputPtr,
@@ -640,7 +647,8 @@ long long unsigned int RunFirstPartialSegmentation(
     const unsigned int nbTilesY,
     const unsigned int tileWidth,
     const unsigned int tileHeight,
-    bool& isFusion)
+    bool& isFusion,
+    bool resume)
 {
 
   using ImageType = typename TSegmenter::ImageType;
@@ -664,6 +672,19 @@ long long unsigned int RunFirstPartialSegmentation(
         {
         // Reading images
         ProcessingTile currentTile = tiles[row*nbTilesX + col];
+        
+        if (resume && file_exists(currentTile.nodeFileName) && file_exists(currentTile.edgeFileName))
+		{
+			std::cout << "\tResuming graph..." << std::endl;
+			TSegmenter segmenter;
+			ReadGraph<TSegmenter>(segmenter.m_Graph, currentTile.nodeFileName, currentTile.edgeFileName);
+			// Retrieve the amount of memory to store this graph
+			std::cout << "\tGet graph memory..." << std::endl;
+			accumulatedMemory += segmenter.GetGraphMemory();
+			if(segmenter.GetComplete() == false)
+				isFusion = true;
+			continue;
+		}
 
         std::cout << "Processing tile " <<  (row*nbTilesX + col) << " / " << (nbTilesX*nbTilesY) <<
             " (" << col << ", " << row << ")" <<
-- 
GitLab