"""
* 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.
""" 
#----------------------------------------------------------------------------------
#! Macro module ImgRegManual
#/*!
# \file    ImgRegManual.py
# \author  Lina Liu and Dimitre Hristov
# \date    2009-01-23
#
# 
#*/
#----------------------------------------------------------------------------------

from mevis import *
## Global Variable
transWin = None;

## initialize the module
def init():
  ctx.field("reformat.resetMatrix").touch();
#  ctx.field("reformat.translation").connectFrom(ctx.field("AngioTransformEdit.translation"));
#  ctx.field("AngioTransformEdit.translation").connectFrom(ctx.field("reformat.translation"));
#  ctx.field("reformat.rotationAngles").connectFrom(ctx.field("AngioTransformEdit.rotation"));
#  ctx.field("AngioTransformEdit.rotation").connectFrom(ctx.field("reformat.rotationAngles"));
#  ctx.addModule("AutoRegistration");
#  ctx.field("AutoRegistration.input0").connectFrom(ctx.field("ROISelectSource.outImage"));
#  ctx.field("AutoRegistration.input1").connectFrom(ctx.field("ROISelectTarget.outImage"));
    
## get the Final Matrix which will be saved to database
def getFinalMatrix():
  MLAB.log("InitialAlignMent:" + ctx.field("InitialAlignment.matrix").stringValue());
  ctx.field("MatrixArithmetic.matrixA").setStringValue(ctx.field("InitialAlignment.matrix").stringValue());
  ctx.field("MatrixArithmetic.matrixB").setStringValue(ctx.field("reformat.matrix").stringValue());
  ctx.field("MatrixArithmetic.operation").setStringValue("Multiply");
  return ctx.field("MatrixArithmetic.outputMatrixC").stringValue();

## show/close AngioTransformEdit module window
def showAngioTranformEdit():
  global transWin;
  if (ctx.field("interactive").boolValue() == True):
#    transWin = ctx.module("AngioTransformEdit").showWindow();
    ctx.field("reformat.resetMatrix").touch();
  #else:
  #  if (transWin != None):
  #   transWin.close();
  #   transWin = None;
## shows a panel for selection of ROIs for the automatic registration
def turnOnROI():
  if (ctx.field("roiOn").boolValue() == True):
    ctx.field("ROICombineSelect.ReferenceImage").connectFrom(ctx.field("ReferenceIm.output0"));
    ctx.field("ROICombineSelect.InputImage").connectFrom(ctx.field("reformat.output0"));
    ctx.addModule("AutoRegistration");
    ctx.field("AutoRegistration.input0").connectFrom(ctx.field("ROICombineSelect.outRefROI"));
    ctx.field("AutoRegistration.input1").connectFrom(ctx.field("ROICombineSelect.outModelROI"));
  else:
    ctx.disconnectField("ROICombineSelect.ReferenceImage");
    ctx.disconnectField("ROICombineSelect.InputImage");
    ctx.module("AutoRegistration").remove();
#    ctx.field("boxOn").value=False ;
    
## performs autoregistration
def autoRegister():
# to prevent unnecessary double clicks ideally we should disable the control
# start the registration
#  ctx.field("AutoRegistration.Accuracy").setStringValue(ctx.field("accuracy").stringValue());
  ctx.field("AutoRegistration.start").touch();
  
# create a new matrix that captures all current transformations
  MLAB.log("InitialAlignMent:" + ctx.field("InitialAlignment.matrix").stringValue());
  ctx.field("MatrixArithmetic.matrixA").setStringValue(ctx.field("InitialAlignment.matrix").stringValue());
  ctx.field("MatrixArithmetic.matrixB").setStringValue(ctx.field("reformat.matrix").stringValue());
  ctx.field("MatrixArithmetic.operation").setStringValue("Multiply");
  
# generates the total transformation before the automatic registration
  ctx.field("MatrixArithmetic.matrixA").setStringValue(ctx.field("MatrixArithmetic.outputMatrixC").stringValue());
  ctx.field("MatrixArithmetic.matrixB").setStringValue(ctx.field("AutoRegistration.Matrix").stringValue());
  ctx.field("MatrixArithmetic.operation").setStringValue("Multiply");
  ctx.field("roiOn").value=False;
  
#sets the resulting transformation as an initial one for subsequent manual adjustments
  ctx.field("initialAlignMatrix").setStringValue(ctx.field("MatrixArithmetic.outputMatrixC").stringValue());
  ctx.field("initialAlignMatrix").touch();
  ctx.field("MemCache.clear").touch();
  ctx.field("MemCache.update").touch();
  ctx.field("reformat.resetMatrix").touch();
  ctx.field("MemCache.update").touch();
  
## set the Matrix in reformat field
def setMatrix():
  ctx.field("reformat.matrix").setStringValue(ctx.parent().field("regMatrix").stringValue());
  ctx.field("reformat.matrix").touch();
  
## trigger the callback of parent module when user clicks save  
def saveOnRegManual():
  ctx.field("saveReg").touch();
  
## trigger the callback of parent module when user clicks approve and save  
def approveOnRegManual():
  ctx.field("saveApprove").touch();

## close the window when user clicks cancel button
def cancelonRegManual():
  ctx.closeWindow();
  ctx.field("cancel").touch();

## show Help for ImgRegManual module
def helponRegManual():
  ctx.showHtmlHelp();
  
## close the Window  
def closeTransWin():
   global transWin;
   if (transWin != None):
      transWin.close();

#//# MeVis signature v1
#//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
#//# owner: MeVis
#//# date: 2009-06-10T04:12:25
#//# hash: vHSfj+Qt8xrIkDVP4W0Rep0YH6a3+MySa0+Cs+AoqEK/IbB+f5szwmyVTENOjzz/JEgm2t31FtHmUUMFcQg5VQ==
#//# MeVis end

