/*
* 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 ImportRTStructure.
/*!
// \file    mlImportRTStructure.h
// \author  linal
// \date    2010-01-15
//
// import a RT Structure and create a corrsponding CSO group
*/
//----------------------------------------------------------------------------------


#ifndef __STUImportRTStructure_H
#define __STUImportRTStructure_H


// Local includes
#include "STUImportRTStructureSystem.h"
#include <CSOBase/CSOList.h>
#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_dicomtags.h> 
#include <dcmtree_exception.h> 
#include <dcmtree_message.h> 
#include <CSOTools/CSOGeneratePathPoints.h>


// ML includes
#include "mlOperatorIncludes.h"
#include "mlRTStructure.h"

ML_START_NAMESPACE



//! import a RT Structure and create a corrsponding CSO group
class STUIMPORTRTSTRUCTURE_EXPORT ImportRTStructure : public BaseOp
{
public:

  enum outputType {panel, console};
  //! Constructor.
  ImportRTStructure ();
  //! Destructor 
  ~ImportRTStructure();
  //! Get the "inDCMFile" field
  inline StringField        &getInDCMFileFld () const { return *_inDCMFileFld;}

  //! Get the "allTags" field
  inline StringField        &getAllTagsFld () const { return *_allTagsFld;}

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

  //! Handle field changes of the field field.
  virtual void handleNotification (Field *field);

  //! Update internal module state after load or clone and enable notification handling again.
  virtual void activateAttachments ();

  //! remove the countour data information from the Dcm Tree
  void removeContourData();

  //void checkTagValues();

private:
  // ----------------------------------------------------------
  //@{ \name Module field declarations
  // ----------------------------------------------------------
  
  //! output CSO list Field 
  BaseField   *_outListFld;

  //! output base object
  BaseField  *_dcmObjFld;
  RTStructure *_objToTransfer;
  DCMTree::TreePtr _treePtr;
  //! output CSO list
  CSOList	  *_outList;
  
  //! input DCM File name
  StringField *_inDCMFileFld;

  //! contour threshold 
  IntField *_thresholdFld;

  //! contour threshold 
  IntField *_dividendFld;
 
  //! contour threshold 
  IntField *_divisorFld;

  //! all tags 
  StringField *_allTagsFld;
  //! read Tags tab fields
  StringField *_dicmFileFld;
  StringField *_tagNameFld[9];
  
  StringField *_tagValueFld[9];
  NotifyField *_getValuesFld;
  BoolField   *_trimSpacesFld;
  BoolField   *_validFld;
  NotifyField *_printFld;

  //! name and value strings for all first level tags
  std::string _allTagValues;

  //! apply the change
  NotifyField *_applyFld;
  
  //DCMTree::TreePtr _dcmTreePtr;
  static const int tagFieldNum = 9;

  //! This function converts RT structure to CSOGroup
  bool RTStructure2CSO(std::string dcmfile);

  //! parse the DCM Tree, getCSO group Infomration
  void parseCSOGroup();

  //! parse the DCM Tree, get CSO coordinates, color and group they belongs to
  void parseCSO();

  //! fill up the DCMTag View tab with patient information, acquisition information and image information
  void fillUpDCMTagViewer();

  //! get the DCMTree ptr from DCM input File
  void getDCMTreeFromFile(std::string inDCMFile);

  //! get the tag value from a given tagPtr
  std::string getTagValue(DCMTree::Const_TagPtr tagPtr);

  //! Implements interface for the runtime type system of the ML.
  ML_BASEOP_CLASS_HEADER(ImportRTStructure)
};


ML_END_NAMESPACE

#endif // __STUImportRTStructure_H

