- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/AngioLocalizeNoAuto/MevisLabNoAutoReg/STUMevisApp/Angiolocalize/Modules/Macros/STULocalization/Localization.py

    00001 """
    00002 * Copyright (c) 2009 Stanford University, School of Medicine All rights reserved.
    00003 *
    00004 * STANFORD ACADEMIC SOFTWARE LICENSE AGREEMENT
    00005 * FOR AngioLocalize SOURCE CODE:
    00006 *
    00007 * 1.  This is a legal agreement between you, RECIPIENT, and STANFORD UNIVERSITY.
    00008 *       By accepting, receiving, and using AngioLocalize.including any accompanying information, materials or
    00009 *       manuals (Program), you are agreeing to be bound by the terms of this Agreement.
    00010 *       If you do not agree to the terms of this Agreement, promptly return the Program to the STANFORD.
    00011
    00012 * 2.  STANFORD grants to RECIPIENT a royalty-free, nonexclusive, and nontransferable license to use the Program
    00013 *       and accompanying source code furnished hereunder, upon the terms and conditions set out below.
    00014 *       documentation and/or other materials provided with the distribution.
    00015 * 3.  RECIPIENT acknowledges that the Program is a research tool still in the development stage and that
    00016 *       it is being supplied as is, without any accompanying services, support or improvements from STANFORD.
    00017 * 4.  RECIPIENT agrees to use the Program solely for internal non-commercial purposes.
    00018 * 5.  RECIPIENT may create derivatives and further transfer the Program under the following terms and conditions:
    00019 *        A) Recipient agrees to not commercially exploit the Program and agrees all uses will be for non-commercial purposes; and,
    00020 *        B) Recipient may not mark any derivatives with the title of Program and may not to use iPad mark for any derivatives; and,
    00021 *        C) Recipient agrees to mark and acknowledge all derivatives as follows: "This is a derivative work based on AngioLocalize:
    00022 *           Frameless AVM localization with 2D Digital Subtraction Angiography (DSA) and 3D Rotational Angiography (DRA) Images developed
    00023 *           by Dimitre Hristov, Lina Liu.
    00024 *        D) Recipient agrees to make all improvements, enhancements and derivatives available under
    00025 *           similar terms and conditions to the research community.
    00026 * 6.   RECIPIENT warrants that RECIPIENT will not remove or export any part of the software or Program from the United
    00027 *       States except in full compliance with all United States and other applicable laws and regulations.
    00028 * 7.  RECIPIENT will use the Program in compliance with all applicable laws, policies and regulations including, but not
    00029 *       limited to, any approvals, informed consent and patient confidentiality principles.
    00030 * 8.   RECIPIENT will indemnify, hold harmless, and defend Stanford against any claim of any kind arising out of or
    00031 *       related to the exercise of any rights granted under this Agreement or the breach of this Agreement by RECIPIENT.
    00032 * 9.  Title and copyright to the Program and any associated documentation shall at all times remain with STANFORD,
    00033 *       and RECIPIENT agrees to preserve same.
    00034 """
    00035 #----------------------------------------------------------------------------------
    00036 #! Macro module Localization
    00037 #/*!
    00038 # \file    Localization.py
    00039 # \author  dimitre
    00040 # \date    2009-02-17
    00041 #
    00042 #
    00043 #*/
    00044 #----------------------------------------------------------------------------------
    00045
    00046
    00047 from mevis import *
    00048 from PythonQt import *
    00049 from linear import *
    00050
    00051
    00052 ## Global variables
    00053 wind = None;
    00054 proj_matrix1=matrix();
    00055 postProjAffine1=matrix();
    00056 proj_matrix2=matrix();
    00057 postProjAffine2=matrix();
    00058
    00059 ## send a query to the Database and return the result set
    00060 def sendQuery(query):
    00061   ctx.field("DBConn.Statement").setValue(query);
    00062   dbModule = ctx.module("DBConn");
    00063   result = dbModule.call("sendQuery");
    00064   if (result == -1):
    00065     MLAB.logError("The following query to database failed. \n" + query + "\n.");
    00066   dbModule.call("close");
    00067   return result;
    00068
    00069 ## this function is used to init projection Matrix1 and affineMatrix1
    00070 def initProjection1():
    00071   global proj_matrix1;
    00072   global postProjAffine1;
    00073
    00074   proj_matrix1=matrix.fromSeq(proj_matrix1,ctx.field("projectionMatrix1").value);
    00075   postProjAffine1=postProjAffine1.fromSeq(ctx.field("AffinePostProjMatrix1").value);
    00076
    00077 ## This function is used to init projectMatrix2 and affineMatrix2 parameter
    00078 def initProjection2():
    00079   global proj_matrix2;
    00080   global postProjAffine2;
    00081
    00082   proj_matrix2=matrix.fromSeq(proj_matrix2,ctx.field("projectionMatrix2").value);
    00083   postProjAffine2=postProjAffine2.fromSeq(ctx.field("AffinePostProjMatrix2").value);
    00084
    00085 ## poject the cursor from 3D to 2D image
    00086 def projectCursor():
    00087   global proj_matrix1;
    00088   global postProjAffine1;
    00089   cursor=vec3(ctx.field("TargetView3D.worldPosition").value);
    00090   projcursor=proj_matrix1.multMatrixVec(cursor);
    00091   z=projcursor.__getitem__(2);
    00092   projcursor=projcursor.__div__(z);
    00093   projcursor=postProjAffine1.multMatrixVec(projcursor);
    00094   ctx.field("SoView2DPosition1.worldPosition").setValue(projcursor[0],projcursor[1],projcursor[2]);
    00095   global proj_matrix2;
    00096   global postProjAffine2;
    00097   cursor=vec3(ctx.field("TargetView3D.worldPosition").value);
    00098   projcursor=proj_matrix2.multMatrixVec(cursor);
    00099   z=projcursor.__getitem__(2);
    00100   projcursor=projcursor.__div__(z);
    00101   projcursor=postProjAffine2.multMatrixVec(projcursor);
    00102   ctx.field("SoView2DPosition2.worldPosition").setValue(projcursor[0],projcursor[1],projcursor[2]);
    00103
    00104 ## display the CSO
    00105 def structureDisplay():
    00106 # Toggles Structure (CSO) drawing
    00107   selection=ctx.field("StructureToggleSelection.lastSelectedId").value;
    00108   if selection == 'CSO_on':
    00109     ctx.field("StructureEditToggle.i01showItem").value=False;
    00110     ctx.field("StructureEditToggle.i02showItem").value=True;
    00111     ctx.field("CSO3DEditor.editingOn").value = False;
    00112     ctx.field("CSO3DEditor.editingOn").value = False;
    00113   elif selection == 'CSO_off':
    00114     ctx.field("StructureEditToggle.i02showItem").value=False;
    00115     ctx.field("StructureEditToggle.i01showItem").value=True;
    00116     ctx.field("CSO3DEditor.editingOn").value = True ;
    00117     ctx.field("CSO3DEditor.editingOn").value = True ;
    00118
    00119 def update3DOverlay():
    00120 # Update the overlay since there is a new registration matrix.
    00121 # The matrix should be changed after the corresponding image is loaded
    00122 # A proper message should be logged
    00123   print "update 3D Overaly"
    00124
    00125 ## display the CSOManager panel
    00126 def showCSOManager():
    00127   global wind;
    00128   if (ctx.field("showCSOM").value == True):
    00129     wind = ctx.module("StructureManager").showWindow();
    00130   else:
    00131     if (wind != None):
    00132        wind.close();
    00133
    00134 ## save CSO to a file
    00135 def saveCSOToFile():
    00136   ctx.connectField("StructureSave.inCSOList", "StructureManager.outCSOList");
    00137   targetImage = ctx.parent().field("ImgLoad3DTarget.filename").value;
    00138   dirIndex = targetImage.rfind("\\");
    00139   fileName = targetImage[dirIndex + 1: len(targetImage)-4] + ".cso";
    00140   CSOName = getCSODir(ctx.parent().field("PatientID").value, fileName);
    00141   ctx.field("StructureSave.fileName").setValue(CSOName);
    00142   ctx.field("StructureSave.apply").touch();
    00143   MLAB.log("Structure :" + fileName + " has been saved.");
    00144   return CSOName;
    00145
    00146 ## save CSO to file and database
    00147 def saveCSO():
    00148   CSOName = saveCSOToFile();
    00149   CSOName = CSOName.replace("\\","/");
    00150   newID = saveCSOToDB(CSOName);
    00151   saveApprovalStatus(0, newID);
    00152   ctx.disconnectField("StructureSave.inCSOList");
    00153   ctx.field("updateCSOList").touch();
    00154
    00155 ## check if CSO Structure exists or not
    00156 def ApprovalSExist(objID):
    00157    ID = 0;
    00158    dbM = ctx.module("DBConn");
    00159    query = "select userID from approvals where objID = " + str(objID) + " and objType = 1;";
    00160    result = sendQuery(query);
    00161    if (result == "()"):
    00162      return ID;
    00163    for item in result:
    00164      ID = item["userid"];
    00165    return ID;
    00166
    00167 ## save the approval status of a CSO
    00168 def saveApprovalStatus(approvalS, objID):
    00169   if(ApprovalSExist(objID) == 0):
    00170     query = "INSERT INTO approvals(userID, objID, patientID, approvalStatus, objType) values(1, " + str(objID) + ","+ getPatientRecordID(ctx.parent().field("PatientID").value) + "," + str(approvalS) + ", 1);";
    00171   else:
    00172     query = "Update approvals set approvalStatus = " + str(approvalS) + " where objID = " + str(objID) + " and objType = 1; ";
    00173   result = sendQuery(query);
    00174
    00175 ## check if a CSO Exist or not
    00176 def CSOExist(refImage):
    00177   num = 0;
    00178   query = "select CSOId from CSOlist where srcImage = " + str(refImage) + ";";
    00179   result = sendQuery(query);
    00180   if (result == "()"):
    00181     return num;
    00182   for item in result:
    00183     num = item["csoid"];
    00184   return num;
    00185
    00186 ## save CSO to the database.
    00187 def saveCSOToDB(CSOName):
    00188   PatientRecID = getPatientRecordID(ctx.parent().field("PatientID").value);
    00189   refImage = ctx.parent().field("TargetImgID").value;
    00190   CSOExistVar = False;
    00191   if (CSOExist(refImage) == 0):
    00192     query = "INSERT INTO CSOLIST (filename, patientID, srcImage) VALUES('" + CSOName + "'," + str(PatientRecID) + "," + refImage + ");";
    00193   else:
    00194     CSOExistVar = True;
    00195     query = "Update CSOList set filename = '" + CSOName + "' WHERE srcImage = " + refImage + ";";
    00196   result = sendQuery(query);
    00197   if (result == -1):
    00198     return;
    00199   query = "select CSOId as newID from CSOList where srcImage = " + refImage + ";";
    00200   result = sendQuery(query);
    00201   if (result == "()"):
    00202     return;
    00203   MLAB.log("Structure " + CSOName + " has been saved.");
    00204   return result[0]["newid"];
    00205
    00206 ## get the patient Record ID with given MRO
    00207 def getPatientRecordID(MRO):
    00208   query = "SELECT PatientID from Patients where MRO = '" + MRO + "';";
    00209   result = sendQuery(query);
    00210   patientID = 0;
    00211   if (result == "()"):
    00212     return str(patientID);
    00213   for item in result:
    00214     patientID = item["patientid"];
    00215   return str(patientID);
    00216
    00217 ## save and approve the CSO
    00218 def saveAndApproval():
    00219   CSOName = saveCSOToFile();
    00220   CSOName = CSOName.replace("\\","/");
    00221   newID = saveCSOToDB(CSOName);
    00222   saveApprovalStatus(1, newID);
    00223   ctx.disconnectField("StructureSave.inCSOList");
    00224   closePanel();
    00225
    00226 ## close the panel
    00227 def closePanel():
    00228   ctx.field("StructureManager.removeAllCSOsAndGroups").touch();
    00229   ctx.field("updateCSOList").touch();
    00230   ctx.field("closeP").touch();
    00231
    00232 ## trigger the showSelection field
    00233 def selection():
    00234   ctx.field("showSelection").touch();
    00235
    00236 ## hide or show the 3D target
    00237 def hide3DTarget():
    00238   hideTgt = ctx.field("hideTarget").value;
    00239   if (hideTgt):
    00240     ctx.field("showTarget").setValue(False);
    00241   else:
    00242     ctx.field("showTarget").setValue(True);
    00243   ctx.field("showTarget").touch();
    00244
    00245 ## hide or show 3D source
    00246 def hide3DSource():
    00247   hideSrc = ctx.field("hideSource").value;
    00248   if(hideSrc):
    00249     ctx.field("showSource").setValue(False);
    00250   else:
    00251     ctx.field("showSource").setValue(True);
    00252   ctx.field("showSource").touch();
    00253
    00254 ## combine the source and target
    00255 def combineSrcTgt():
    00256   combine = ctx.field("combineSrcTgt").value;
    00257   if(combine):
    00258     ctx.field("showSource").setValue(False);
    00259     ctx.field("showSource").touch();
    00260     ctx.connectField("TargetView3D.inInvPreLUT", "SourceOverlay3D.outScene");
    00261     ctx.field("showTarget").setValue(True);
    00262     ctx.field("showTarget").touch();
    00263     ctx.field("showTargetSrc").setValue(True);
    00264   else:
    00265     ctx.disconnectField("TargetView3D.inInvPreLUT");
    00266     ctx.field("showTargetSrc").setValue(False);
    00267     hide3DSource();
    00268     hide3DTarget();
    00269
    00270 ## toggle the drawing on Image2D1
    00271 def toggleDrawingonImage2D1():
    00272   CSOName = saveCSOToFile();
    00273   newID = saveCSOToDB(CSOName);
    00274   saveApprovalStatus(0, newID);
    00275   ctx.disconnectField("StructureSave.inCSOList");
    00276   ctx.field("StructureLoad.fileName").setValue(CSOName);
    00277   ctx.connectField("StructureManager.inCSOList", "StructureLoad.outCSOList");
    00278   ctx.field("StructureLoad.apply").touch();
    00279   toggle = ctx.field("image1Check").value;
    00280   if(toggle):
    00281     ctx.field("SoView2DCSOEditor1.drawingOn").setValue(False);
    00282   else:
    00283     ctx.field("SoView2DCSOEditor1.drawingOn").setValue(True);
    00284
    00285 ## toggle the drawing on Image2D2
    00286 def toggleDrawingonImage2D2():
    00287   CSOName = saveCSOToFile();
    00288   newID = saveCSOToDB(CSOName);
    00289   saveApprovalStatus(0, newID);
    00290   ctx.disconnectField("StructureSave.inCSOList");
    00291   ctx.field("StructureLoad.fileName").setValue(CSOName);
    00292   ctx.connectField("StructureManager.inCSOList", "StructureLoad.outCSOList");
    00293   ctx.field("StructureLoad.apply").touch();
    00294
    00295   toggle = ctx.field("image2Check").value;
    00296   if(toggle):
    00297     ctx.field("SoView2DCSOEditor2.drawingOn").setValue(False);
    00298   else:
    00299     ctx.field("SoView2DCSOEditor2.drawingOn").setValue(True);
    00300
    00301 ## show help for the module
    00302 def help():
    00303   ctx.showHtmlHelp();
    00304
    00305 #//# MeVis signature v1
    00306 #//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
    00307 #//# owner: MeVis
    00308 #//# date: 2009-06-10T04:12:25
    00309 #//# hash: vHSfj+Qt8xrIkDVP4W0Rep0YH6a3+MySa0+Cs+AoqEK/IbB+f5szwmyVTENOjzz/JEgm2t31FtHmUUMFcQg5VQ==
    00310 #//# MeVis end
    00311

All Classes Namespaces Files Functions Variables Enumerator Friends