/* 
Description: apply a mask on a tensor and/or eigendata

Author:      Raimundo Sierra
             www.rsierra.com

Copyright:   Copyright (c) 2000 Raimundo Sierra. All rights reserved.
LICENSE:     This program is free software; you can redistribute it and/or modify
             it under the terms of the GNU General Public License as published by
             the Free Software Foundation; either version 2 of the License, or
             (at your option) any later version.

             This program is distributed in the hope that it will be useful,
             but WITHOUT ANY WARRANTY; without even the implied warranty of
             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
             GNU General Public License for more details.

             You should have received a copy of the GNU General Public License
             along with this program; if not, write to the Free Software
             Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


Institution: Surgical Planning Laboratory
             Department of Radiology
             Brigham and Women's Hospital
             Harvard Medical School
             MA 02115
             USA

Date:        11.2000 - 3.2001

Modifications:
<Date>   <Init>    <Version>    <Description>

 */

#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

//#include "rsfunctions.h"
#include "tensorfield.h"
#include "eigenfield.h"
#include "display.h"
#include "background.h"

extern "C"{
#include "memfuns.h"
#include "fileops.h"
  
} 


void mask(float *segmented, const background &back, const int selection, const int label, const int size, const int slice)
{
  for (int i=slice*size; i<(slice+1)*size; i++){
    if(!back.isZero(i, selection)){
      segmented[i] = label;
    }
  }
}

int main(int argc, char *argv[]) 
{
  if(argc!=2){
    cout << "Usage:\n"
      "./mask xml-file\n\n";
    exit(0);
  }
  cout << "\n---------------------------------------------- start\n\n";

  tensorfield t_field(&argv[1][0]);
  eigenfield  e_field(&argv[1][0]);
  background  back(   &argv[1][0]);
  display display(t_field);
  
  int offset[3] = {0,0,0};
  int x, y, z, x0, y0, z0, selection;
  int smooth;
  int smootheigen=1;
  
  float thresh, lastthresh, tol, method;
  int i,j; 
  int similarity, coloring;
  int loop=1;
  int loop2=0;
  int remask=0;
  int pre;

  char * path = new char[2048];

  t_field.getDimension(x, y, z);
  int window[3] = {x, y, z};

  float * segmented = new float[x*y*z];
  for (int i=0; i<x*y*z; i++){
    segmented[i] = 0;
  }

  while(loop2==0){
    for(int l=0; l<z; l++){
      offset[2]=l;
      while(loop==1){
	cout << "Threshold for T2W image (e.g. 0.8, -1=continue):";   
	cin >> thresh;
	if (thresh==-1) break;
	i = 4;
	lastthresh = display.backgd(back, offset, window, i, thresh);   
      } 
      back.mask(lastthresh, i, l); 
      mask(segmented, back, 4, 64, x*y, l); 
      keepLargestGroup(segmented, x, y, l);
    }
    
    display.greyarray(segmented, x, y, z);
    
    cout << "Continue (1=yes, 0=no):  \t\t\t";
    cin >>  loop2;
  }
   
  // this is singleslice!
  t_field.getPath(path);
  strcat(path, ".mask.001");
  saveFloatAsMRI(path, segmented, x*y*z);
  
  cout << "Mask all like the segmented (1=yes, 0=no):\t";
  cin >> remask;
  if(remask==1){
    back.remask(segmented);
    t_field.mask(back);
    e_field.mask(back);
    t_field.getPath(path);
    strcat(path, ".masked");
    //cout << path<< endl;
    t_field.save(path);
    e_field.save(path);
    back.save(path);
  }

}
