/*
* Copyright (c) 2009 Stanford University, School of Medicine All rights reserved. 
* 
* STANFORD ACADEMIC SOFTWARE LICENSE AGREEMENT 
* FOR AngioLocalize SOURCE CODE: 
* 
* 1.  This is a legal agreement between you, RECIPIENT, and STANFORD UNIVERSITY.  
*       By accepting, receiving, and using AngioLocalize.including any accompanying information, materials or 
*       manuals (Program), you are agreeing to be bound by the terms of this Agreement.  
*       If you do not agree to the terms of this Agreement, promptly return the Program to the STANFORD.

* 2.  STANFORD grants to RECIPIENT a royalty-free, nonexclusive, and nontransferable license to use the Program 
*       and accompanying source code furnished hereunder, upon the terms and conditions set out below. 
*       documentation and/or other materials provided with the distribution. 
* 3.  RECIPIENT acknowledges that the Program is a research tool still in the development stage and that 
*       it is being supplied as is, without any accompanying services, support or improvements from STANFORD.
* 4.  RECIPIENT agrees to use the Program solely for internal non-commercial purposes. 
* 5.  RECIPIENT may create derivatives and further transfer the Program under the following terms and conditions: 
*        A) Recipient agrees to not commercially exploit the Program and agrees all uses will be for non-commercial purposes; and,
*        B) Recipient may not mark any derivatives with the title of Program and may not to use iPad mark for any derivatives; and,
*        C) Recipient agrees to mark and acknowledge all derivatives as follows: "This is a derivative work based on AngioLocalize:
*           Frameless AVM localization with 2D Digital Subtraction Angiography (DSA) and 3D Rotational Angiography (DRA) Images developed
*           by Dimitre Hristov, Lina Liu.
*        D) Recipient agrees to make all improvements, enhancements and derivatives available under 
*           similar terms and conditions to the research community. 
* 6.   RECIPIENT warrants that RECIPIENT will not remove or export any part of the software or Program from the United 
*       States except in full compliance with all United States and other applicable laws and regulations.
* 7.  RECIPIENT will use the Program in compliance with all applicable laws, policies and regulations including, but not 
*       limited to, any approvals, informed consent and patient confidentiality principles.
* 8.   RECIPIENT will indemnify, hold harmless, and defend Stanford against any claim of any kind arising out of or
*       related to the exercise of any rights granted under this Agreement or the breach of this Agreement by RECIPIENT.
* 9.  Title and copyright to the Program and any associated documentation shall at all times remain with STANFORD, 
*       and RECIPIENT agrees to preserve same.
*/ 
//----------------------------------------------------------------------------------
//! The ML module class CSODCMHdrToRTStructure.
/*!
// \file    mlCSODCMHdrToRTStructure.h
// \author  linal
// \date    2010-08-18
//
// CSO and DCM header convert to a RT Structure dcm file
*/
//----------------------------------------------------------------------------------


#ifndef __mlCSODCMHdrToRTStructure_H
#define __mlCSODCMHdrToRTStructure_H


// Local includes
#include <DCMTree_UIDGenerator.h>
#include "STUCSODCMHdrToRTStructureSystem.h"
#include <CSOBase/CSOList.h>

// ML includes
#include "mlOperatorIncludes.h"
#include "mlRTStructure.h"
#include <iostream>
#include <fstream>

#include <DCMTree_Lib.h>
#include "DCMTree_IOParameter.h"
#include "DCMTree_Reader.h"
#include "DCMTree_Dict.h"
#include "DCMTree_Tag.h"
#include "DCMTree_Value.h"
#include "DCMTree_Unicode.h"
#include "DCMTree_Writer.h"
#include <dcmtree_dicomtags.h> 
#include <dcmtree_exception.h> 
#include <dcmtree_message.h> 
#include <CSOBase/CSOList.h>
#include "mlBase.h"

ML_START_NAMESPACE


//! CSO and DCM header convert to a RT Structure dcm file
class STUCSODCMHDRTORTSTRUCTURE_EXPORT CSODCMHdrToRTStructure : public BaseOp
{
public:

  //! Constructor.
  CSODCMHdrToRTStructure ();

	~CSODCMHdrToRTStructure (){}
    //! get the exportDestination field
  inline StringField        &getExportDestinationFld () const { return *_exportDestinationFld;}

  //! Get the value of "Apply" field
  inline NotifyField        &getExportFld () const { return *_exportFld;}

  //! Handle field changes of the field field.
  virtual void handleNotification (Field *field);
  
   //! Configures (in)validation handling of inputs which are not connected or up to date.
  virtual INPUT_HANDLE handleInput(int inIndex, INPUT_STATE state) const;
  
private:

  BaseField  *_inListFld;

  CSOList	 *_inputList;
  
  NotifyField *_exportFld;

  StringField *_exportDestinationFld;

  BaseField   *_dcmObjFld;

  CSOList	     *_inputCSOs;
  //RTStructure	 *_inputRTStructure;
  DCMTree::TreePtr  _inputTree;
  
  DCMTree::TreePtr  outputTree;
  DCMTree::TagPtr   tagPtr;
  std::string       _sopInstanceUID;

  void copyCSOGroups(DCMTree::TagPtr groupTagPtr, unsigned int num);
  void combineCSOs();
  bool writeToDCMFile(DCMTree::TreePtr tree, std::string dest);
  void insertCSOList();
  void insertCSOGroup();
  void insertObservation();
  bool checkCSOList();
  //! Implements interface for the runtime type system of the ML.
  ML_BASEOP_CLASS_HEADER(CSODCMHdrToRTStructure)
};


ML_END_NAMESPACE

#endif // __mlCSODCMHdrToRTStructure_H

