//=======================================================================
//function : Execute
//purpose  :
//=======================================================================
Standard_Integer GEOMImpl_HealingDriver::Execute(TFunction_Logbook& log) const
{
  if (Label().IsNull()) return 0;
  Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());

  if (aFunction.IsNull()) return 0;

  GEOMImpl_IHealing HI (aFunction);
  Standard_Integer aType = aFunction->GetType();
  Handle(GEOM_Function) anOriginalFunction = HI.GetOriginal();
  if (anOriginalFunction.IsNull()) return 0;
  TopoDS_Shape aShape, anOriginalShape = anOriginalFunction->GetValue();
  if (anOriginalShape.IsNull()) return 0;

  switch (aType)
  {
  case SHAPE_PROCESS:
    ShapeProcess(&HI, anOriginalShape, aShape);
    break;
  case SUPPRESS_FACES:
    SuppressFaces(&HI, anOriginalShape, aShape);
    break;
  case CLOSE_CONTOUR:
    CloseContour(&HI, anOriginalShape, aShape);
    break;
  case REMOVE_INT_WIRES:
    RemoveIntWires(&HI, anOriginalShape, aShape);
    break;
  case FILL_HOLES:
    RemoveHoles(&HI, anOriginalShape, aShape);
    break;
  case SEWING:
    Sew(&HI, anOriginalShape, aShape);
    break;
  case DIVIDE_EDGE:
    AddPointOnEdge(&HI, anOriginalShape, aShape);
    break;
  case CHANGE_ORIENTATION:
    ChangeOrientation(&HI, anOriginalShape, aShape);
    break;
  case LIMIT_TOLERANCE:
    LimitTolerance(&HI, anOriginalShape, aShape);
    break;
  default:
    return 0;
  }

  if (aShape.IsNull())
    raiseNotDoneExeption( ShHealOper_ErrorExecution );

  aFunction->SetValue(aShape);

  log.SetTouched(Label());
  return 1;
}
MRI *  
correct_largestCC_and_fill_holes(MRI *segmri, MRI *outmri)
{
  int i, x, y, z, width, height, depth = 0, val;
  width  = segmri->width ;
  height = segmri->height ;
  depth  = segmri->depth ;
  // char fname[STR_LEN];

  if (!outmri)
    printf("no outmri volume exists!\n") ;
    //outmri = MRIclone(segmri, NULL) ;
    //outmri = MRIalloc(width, height, depth, MRI_INT);
  
  int *segidlist;
  int nsegids = 0;
  segidlist = MRIsegIdListNot0(segmri, &nsegids, 0);

  MRI *currlabelvol = MRIalloc(width, height, depth, MRI_INT); 
  int currlabel = 0;

  for (i = 0; i < nsegids; i++)
    {
      currlabel = segidlist[i];
      
      if (currlabel > 0) 
	{
	  // label volume 
	  for (x = 0 ; x < width ; x++)
	    for (y = 0 ; y < height ; y++)
	      for (z = 0 ; z < depth ; z++)
		if (MRIgetVoxVal(segmri,x,y,z,0) == currlabel)
		  MRIsetVoxVal(currlabelvol,x,y,z,0,1);
		else
		  MRIsetVoxVal(currlabelvol,x,y,z,0,0);
	  
	  // choose largest connected components
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "before-largest-conn-component.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  GetLargestCC6(currlabelvol); // Note: how to fill the background???!! -- WM
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "largest-conn-component.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  // remove holes on that component
	  RemoveHoles(currlabelvol);
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "holes-removed.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  
	  //fill ouput volume with new labels
	  for (x = 0 ; x < width ; x++)
	    for (y = 0 ; y < height ; y++)
	      for (z = 0 ; z < depth ; z++)
		{
		  val = MRIgetVoxVal(currlabelvol,x,y,z,0);
		  if (val == 1)
		    MRIsetVoxVal(outmri,x,y,z,0,currlabel);
		}
	}
    }
  
  return(outmri) ;
}