/*
* 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 PerspectiveTransformation.
/*!
// \file    mlPerspectiveTransformation.h
// \author  Lina L.
// \date    2009-12-09
//
// 
*/
//----------------------------------------------------------------------------------


#ifndef __mlPerspectiveTransformation_H
#define __mlPerspectiveTransformation_H


// Local includes
#include "STUPerspectiveTransformationSystem.h"

// ML includes
#include "mlOperatorIncludes.h"
#include <CSOBase/CSOList.h>

ML_START_NAMESPACE


//! 
class STUPERSPECTIVETRANSFORMATION_EXPORT PerspectiveTransformation : public BaseOp
{
public:

  //! Constructor.
  PerspectiveTransformation ();

  //! Destructor.
  virtual ~PerspectiveTransformation () { if (_inputList) delete _inputList; }

  //@{ Access to module fields. See corresponding field comments for more information.
   // inline StringField        &getInputCSOStringFld () const { return *_inputCSOStringFld;}
   // inline BoolField          &getCopySelectedCSOsFld () const { return *_copySelectedCSOsFld;}
	//inline StringField		  $getCopiedCSOIds() const {return *_copiedCSOIdsFld;}
	inline MatrixField        &getMatrixFld () const { return *_matrixFld;}
	inline NotifyField        &getApplyFld () const { return *_applyFld;}

	inline BoolField          &getListenToFinishingNotificationsFld () const 
	                          { return *_listenToFinishingNotificationsFld;}
    inline BoolField          &getListenToSelectionNotificationsFld () const 
	                          { return *_listenToSelectionNotificationsFld;}

	inline BoolField          &getUseCreatorFld () const { return *_useCreatorIdFld;}
	inline IntField           &getCreatorIdFld () const { return *_creatorIdFld;}
	inline StringField        &getTargetGroupFld () const { return *_targetGroupFld;}

	inline BoolField          &getAutoApplyFld () const { return *_autoApplyFld;}

	inline EnumField          &getUpdateModeFld () const { return *_updateModeFld;}
	//inline BoolField          &get_workDirectlyOnInputListFld () const 
	//						  { return *_workDirectlyOnInputListFld;}
	
  //@}

  //! 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 ();

  
protected:

//----------------------------------------------------------------------------------
//! This method use perspective transformation matrix to do the tranformation on inputCSOList
//! It copies the inputList, then for each point on the path of the copied list,
//! 1 was added to the world coordinate of each point and do multiplication with the input(4*4) matrix.
//! The result will be devided by the last component to get the vect of 3 component representing 
//! x, y and z coordinates, and then divided by the 3rd component to get the vector of 2,
//! which represents the x and y coordinates for each point. The result is put to outListFld.
//----------------------------------------------------------------------------------
  virtual void transform (const mat4& matrix);


private:
	
   //! Field making marker list object available as module parameter.
  BaseField  *_inListFld;

  BaseField  *_outListFld;

  CSOList    *_inputList;
  CSOList    *_outputList;

  //! pointer to inputList and outputList or their copies,used in tranformation 
  CSOList	 *_outList;
  CSOList    *_inList;

  
  // ----------------------------------------------------------
  //@{ \name Module field declarations
  // ----------------------------------------------------------

  //! CSO ids
  //StringField *_inputCSOStringFld;

  //! indicate whether to transform on copied or original CSOs
  //BoolField   *_copySelectedCSOsFld;

  //! indicate the label "Ids:"
  //StringField *_copiedCSOIdsFld;

  //! indicate the matrix to perform perspective project on
  MatrixField *_matrixFld;

   //! apply the change
  NotifyField *_applyFld;

  //! Listen to Notifications
  //! Shall the module update on every event?
  BoolField *_listenToRepaintNotificationsFld;
  BoolField *_listenToFinishingNotificationsFld;
  BoolField *_listenToSelectionNotificationsFld;

  //! creation
  BoolField   *_useCreatorIdFld;
  IntField	  *_creatorIdFld;
  StringField *_targetGroupFld;

  BoolField   *_autoApplyFld;
  
  //! update the change
  EnumField *_updateModeFld;

  //! update the change
  //BoolField *_workDirectlyOnInputListFld;

  //! Locking variable...
  bool _isInNotificationCB;

   //! Can be notified from anywhere in the network.
  static void _csoListNotificationCB(void* userData, int notificationFlag);

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


ML_END_NAMESPACE

#endif // __mlPerspectiveTransformation_H

