+ } else
+ QMessageBox::information(this,tr("Problem reading VF !"),"File doesn't exist!");
+
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SetVFProperty(int subsampling, int scale, int log, int width, double r, double g, double b)
+{
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ if (mSlicerManagers[index]->GetSlicer(0)->GetVF()) {
+ for (int i = 0; i < 4; i++) {
+ mSlicerManagers[index]->GetSlicer(i)->SetVFSubSampling(subsampling);
+ mSlicerManagers[index]->GetSlicer(i)->SetVFScale(scale);
+ mSlicerManagers[index]->GetSlicer(i)->SetVFWidth(width);
+ mSlicerManagers[index]->GetSlicer(i)->SetVFColor(r,g,b);
+ if (log > 0)
+ mSlicerManagers[index]->GetSlicer(i)->SetVFLog(1);
+ else
+ mSlicerManagers[index]->GetSlicer(i)->SetVFLog(0);
+ }
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SetOverlayProperty(int color, int linked, double window, double level)
+{
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ if (mSlicerManagers[index]->GetSlicer(0)->GetOverlay()) {
+ mSlicerManagers[index]->SetOverlayColor(color);
+ mSlicerManagers[index]->SetColorMap(0);
+ mSlicerManagers[index]->SetLinkOverlayWindowLevel(linked);
+ mSlicerManagers[index]->SetOverlayColorWindow(window);
+ mSlicerManagers[index]->SetOverlayColorLevel(level);
+ mSlicerManagers[index]->Render();
+ }
+}
+//------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SetFusionProperty(int opacity, int thresOpacity, int colormap,double window, double level, bool showLegend)
+{
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ if (mSlicerManagers[index]->GetSlicer(0)->GetFusion()) {
+ mSlicerManagers[index]->SetFusionColorMap(colormap);
+ mSlicerManagers[index]->SetFusionOpacity(opacity);
+ mSlicerManagers[index]->SetFusionThresholdOpacity(thresOpacity);
+ mSlicerManagers[index]->SetFusionWindow(window);
+ mSlicerManagers[index]->SetFusionLevel(level);
+ mSlicerManagers[index]->SetFusionShowLegend(showLegend);
+ mSlicerManagers[index]->SetColorMap(0);
+ mSlicerManagers[index]->Render();
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SelectFusionSequence()
+{
+ //get the index of the slicer manager of the main sequence (CT)
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ //check if one overlay image is already associated
+ for (int child = 0; child < DataTree->topLevelItem(index)->childCount(); child++)
+ if ( (DataTree->topLevelItem(index)->child(child)->data(1,Qt::UserRole).toString() == "fusion") ||
+ (DataTree->topLevelItem(index)->child(child)->data(1,Qt::UserRole).toString() == "fusionSequence") ) {
+ QString error = "Cannot add more than one compared image\n";
+ error += "Please remove first ";
+ error += DataTree->topLevelItem(index)->child(child)->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
+ QMessageBox::information(this,tr("Problem adding compared image !"),error);
+ return;
+ }
+
+ QString Extensions = EXTENSIONS;
+ Extensions += ";;All Files (*)";
+ QStringList files = QFileDialog::getOpenFileNames(this,tr("Load Overlay image sequence"),mInputPathName,Extensions);
+ if (files.isEmpty())
+ return;
+
+ std::vector<std::string> vecFileNames;
+ for (int i = 0; i < files.size(); i++) {
+ vecFileNames.push_back(files[i].toStdString());
+ }
+
+ //associate the secondary sequence (US) to the main one
+ AddFusionSequence(index,vecFileNames,vvImageReader::MERGEDWITHTIME);
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SelectFusionSequenceTemporalSignal() {
+
+ //make sure the index is right?
+ //in the end, I should attach the temporal data to the right sequence!
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ //in case the other sequence got selected, make sure we select the primary sequence
+ if ( (!mSlicerManagers[index]->GetSlicer(0)->GetFusion()) && mSlicerManagers[index]->GetFusionSequenceIndexOfLinkedManager()>=0 ) {
+ index = mSlicerManagers[index]->GetFusionSequenceIndexOfLinkedManager();
+ }
+
+ //open a dialog box to find a file
+ QString Extensions = EXTENSIONS;
+ Extensions += ";;All Files (*)";
+ QString fileName = QFileDialog::getOpenFileName(this,tr("Load respiratory signal for fused sequence"),mInputPathName,Extensions);
+ if (fileName.isNull())
+ return;
+
+ //read it as a vector of values
+ std::vector<double> signal;
+ //...TODO, look for itk functions that can do that... vnl in the worst case.
+ signal.push_back(1);signal.push_back(2);
+
+ //TODO: instead: if the loaded signal is longer, just crop it...
+ //this allows loading only the first few frames when testing.
+ //->maybe raise a message that this behavior may be unsafe...
+
+ //if compatible with the fused image sequence (number of images = number of entries), enable the temporalSync
+ if ( signal.size() >= mSlicerManagers[index]->GetFusionSequenceNbFrames()) {
+ //for convenience, associate this sequence to both the current slicer manager, and to the linked one
+ mSlicerManagers[index]->SetFusionSequenceTemporalSignal(signal);
+ mSlicerManagers[ mSlicerManagers[index]->GetFusionSequenceIndexOfLinkedManager() ]->SetFusionSequenceTemporalSignal(signal);
+ overlayPanel->enableFusionSequenceTemporalSync();
+ QMessageBox::information(this,tr("Adding signal"),"would add the signal from file: "+ fileName);
+ }
+ else {//else, send a message to signal the failure...
+ QString error = "The provided signal doesn't have the same duration as the sequence\n";
+ error += "Ignoring file: " + fileName;
+ QMessageBox::information(this,tr("Problem adding signal!"),error);
+ return;
+ }
+
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+//when this function is called index is the slicer manager index corresponding to the main sequence (CT)
+//the files behind fileNames points to the data for the secondary sequence
+void vvMainWindow::AddFusionSequence(int index, std::vector<std::string> fileNames, vvImageReader::LoadedImageType type)
+{
+ QString file(fileNames[0].c_str());
+ if (QFile::exists(file))
+ {
+ mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
+ itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
+ file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
+ reader->SetFileName(fileNames[0].c_str());
+ reader->ReadImageInformation();
+ std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
+ int dimension = reader->GetNumberOfDimensions();
+ QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+ vvProgressDialog progress("Opening " + file.toStdString());
+ qApp->processEvents();
+
+ std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
+
+ if (mSlicerManagers[index]->SetFusionSequence(fileNames,dimension, component,type)) {
+ //create an item in the tree with good settings
+ QTreeWidgetItem *item = new QTreeWidgetItem();
+ item->setData(0,Qt::UserRole,file.toStdString().c_str());
+ item->setData(1,Qt::UserRole,tr("fusionSequence"));
+
+ QFileInfo fileinfo(file); //Do not show the path
+ item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
+ item->setToolTip(COLUMN_IMAGE_NAME, mSlicerManagers[index]->GetListOfAbsoluteFilePathInOneString("fusionSequence").c_str());
+ qApp->processEvents();
+ for (int j = 1; j <= 4; j++) {
+ item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
+ }
+
+ //Create the buttons for reload and close
+ qApp->processEvents();
+ QTreePushButton* cButton = new QTreePushButton;
+ cButton->setItem(item);
+ cButton->setColumn(COLUMN_CLOSE_IMAGE);
+ cButton->setToolTip(tr("close image"));
+ cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
+ connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+ this,SLOT(CloseImage(QTreeWidgetItem*, int)));
+
+ QTreePushButton* rButton = new QTreePushButton;
+ rButton->setItem(item);
+ rButton->setColumn(COLUMN_RELOAD_IMAGE);
+ rButton->setToolTip(tr("reload image"));
+ rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
+ connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+ this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
+
+ DataTree->topLevelItem(index)->setExpanded(1);
+ DataTree->topLevelItem(index)->addChild(item);
+ DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
+ DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
+
+ //store the original transform matrix
+ int indexParent = GetSlicerIndexFromItem( DataTree->topLevelItem(index) );
+ mSlicerManagers[indexParent]->SetFusionSequenceMainTransformMatrix( mSlicerManagers[indexParent]->GetSlicer(0)->GetImage()->GetTransform()[0]->GetMatrix() );
+
+ //set the id of the image
+ QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
+ item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
+ UpdateTree();
+ qApp->processEvents();
+
+ //ImageInfoChanged();
+
+ QApplication::restoreOverrideCursor();
+ // Update the display to update, e.g., the sliders
+ for(int i=0; i<4; i++)
+ DisplaySliders(index, i);
+
+
+ //This loads the secondary sequence (US) as an independent sequence
+ LoadImages(fileNames, type);
+ //reset the transforms to identiy
+ for (unsigned i=0 ; i<mSlicerManagers.back()->GetImage()->GetTransform().size() ; i++) {
+ mSlicerManagers.back()->GetImage()->GetTransform()[i]->Identity();
+ mSlicerManagers.back()->GetImage()->GetTransform()[i]->Update();
+ }
+
+ //automatically link both images...
+ AddLink(mSlicerManagers[indexParent]->GetId().c_str(), mSlicerManagers.back()->GetId().c_str(), false);
+
+ //tie the main and secondary sequences by raising flags and informing each another of their respective SlicerManager indices
+ mSlicerManagers[indexParent]->SetFusionSequenceIndexOfLinkedManager(mSlicerManagers.size()-1);
+ mSlicerManagers[indexParent]->SetFusionSequenceInvolvmentCode(0); //main sequence
+ mSlicerManagers.back()->SetFusionSequenceIndexOfLinkedManager(indexParent);
+ mSlicerManagers.back()->SetFusionSequenceInvolvmentCode(1); //secondary sequence
+ for (unsigned i=0 ; i<4 ; i++) {
+ mSlicerManagers.back()->GetSlicer(i)->SetFusionSequenceCode(1); //flag the slicers of the secondary sequence
+ }
+
+ } else {
+ QApplication::restoreOverrideCursor();
+ QString error = "Cannot import the new image.\n";
+ error += mSlicerManagers[index]->GetLastError().c_str();
+ QMessageBox::information(this,tr("Problem reading image !"),error);
+ }
+ //WindowLevelChanged();
+ ImageInfoChanged(); //this internally calls WindowLevelChanged...
+ }
+ else {
+ QMessageBox::information(this,tr("Problem reading fusion sequence !"),"File doesn't exist!");
+ return;
+ }
+
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+//fusionSequenceFrameIndex and fusionSequenceNbFrames are relative to the secondary sequence (US)
+void vvMainWindow::SetFusionSequenceProperty(int fusionSequenceFrameIndex, bool spatialSyncFlag, unsigned int fusionSequenceNbFrames, bool temporalSyncFlag)
+{
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+
+ if (!mSlicerManagers[index]->IsInvolvedInFusionSequence()) return;
+
+ //check if the focus moved to the linked sequence, and in this case, select the master sequence instead
+ if (!mSlicerManagers[index]->IsMainSequenceOfFusionSequence()) {
+ index = mSlicerManagers[index]->GetFusionSequenceIndexOfLinkedManager();
+ }
+ int secondaryIndex = mSlicerManagers[index]->GetFusionSequenceIndexOfLinkedManager();
+ if (secondaryIndex==-1) return; //this should never happen
+ if ( (!mSlicerManagers[index]->IsMainSequenceOfFusionSequence()) ||
+ (!mSlicerManagers[secondaryIndex]->IsSecondarySequenceOfFusionSequence()) )
+ {return;} //this should never happen, raise an exception?
+
+ if (mSlicerManagers[index]->GetSlicer(0)->GetFusion()) {
+
+ //First, if the spatialSync button is unchecked, then reposition the parent sequence (CT) in its original coordinate frame
+ if ( (!spatialSyncFlag) && (mSlicerManagers[index]->GetFusionSequenceSpatialSyncFlag()) ) {
+ for ( unsigned i=0 ; i<mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform().size() ; i++ ) {
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->SetMatrix( mSlicerManagers[index]->GetFusionSequenceMainTransformMatrix() );
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->Update();
+ }
+
+ for (int i=0; i<mSlicerManagers[index]->GetNumberOfSlicers(); i++) {
+ mSlicerManagers[index]->GetSlicer(i)->ForceUpdateDisplayExtent();
+ mSlicerManagers[index]->GetSlicer(i)->Render();
+ }
+ }
+
+ //then, update the property values in the slicer manager
+ mSlicerManagers[index]->SetFusionSequenceLength(fusionSequenceNbFrames);
+ mSlicerManagers[index]->SetFusionSequenceSpatialSyncFlag(spatialSyncFlag);
+ mSlicerManagers[index]->SetFusionSequenceTemporalSyncFlag(temporalSyncFlag);
+ mSlicerManagers[index]->SetFusionSequenceFrameIndex(fusionSequenceFrameIndex);
+
+ //select the right frame of the US sequence
+ mSlicerManagers[index]->SetFusionSequenceTSlice(fusionSequenceFrameIndex); //here, I should only update the overlayed sequence
+ mSlicerManagers[secondaryIndex]->SetTSlice(fusionSequenceFrameIndex, false); //this should update the secondary sequence (individual version)
+
+ //update the horizontal sliders of the main window
+ overlayPanel->updateFusionSequenceSliderValueFromWindow(fusionSequenceFrameIndex, false);
+
+ if (spatialSyncFlag) { //reslice the CT
+
+ if (temporalSyncFlag) { //do the temporal synchronisation
+ //TODO: add the temporal synchronisation stuff
+ //if the button is checked, get the phase of the requested US frame from the available signal
+ //and select the corresponding one in the CT. (check the one just before, and the one just after, and select the closest)
+
+ //TODO: do it also the other way around, when modifying the time index related to CT, select a close frame
+ //this should not be done here directly, but the code should be inspired from the one here
+ //->find a good US frame such that when calling this function with this US frame, it produces the expected result
+
+
+ //TODO: select the right CT image to display
+ int mainSequenceFrameIndex=0;
+ //estimate the TSlice to set to the CT
+
+ //and set it!
+ mSlicerManagers[index]->SetTSlice(mainSequenceFrameIndex, false);
+ }
+
+
+ //Set the transform matrix of the parent sequence (typically CT / 4DCT)
+ vtkSmartPointer<vtkMatrix4x4> tmpMat = vtkSmartPointer<vtkMatrix4x4>::New();
+ vtkMatrix4x4::Invert( mSlicerManagers[index]->GetFusionSequenceInitialTransformMatrixAtFrame(fusionSequenceFrameIndex), tmpMat );
+ for ( unsigned i=0 ; i<mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform().size() ; i++ ) {
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->SetMatrix( mSlicerManagers[index]->GetFusionSequenceMainTransformMatrix() );
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->PreMultiply();
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->Concatenate( tmpMat );
+ mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetTransform()[i]->Update();
+ }
+
+ for (int i=0; i<mSlicerManagers[index]->GetNumberOfSlicers(); i++) {
+ mSlicerManagers[index]->GetSlicer(i)->ForceUpdateDisplayExtent();
+ mSlicerManagers[index]->GetSlicer(i)->Render();
+ }
+
+ }
+
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvMainWindow::SaveAs()
+{
+ if (DataTree->selectedItems()[0]->data(1,Qt::UserRole).toString() == "vector") {
+ QMessageBox::warning(this,tr("Unsupported type"),tr("Sorry, saving a vector field is unsupported for the moment"));
+ return;
+ }
+
+ int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
+ int dimension = mSlicerManagers[index]->GetDimension();
+ QStringList OutputListeFormat;
+ OutputListeFormat.clear();
+ if (dimension == 1) {
+ OutputListeFormat.push_back(".mhd");
+ OutputListeFormat.push_back(".mha");
+ }
+ if (dimension == 2) {
+ OutputListeFormat.push_back(".bmp");
+ OutputListeFormat.push_back(".png");
+ OutputListeFormat.push_back(".jpeg");
+ OutputListeFormat.push_back(".tif");
+ OutputListeFormat.push_back(".mhd");
+ OutputListeFormat.push_back(".mha");
+ OutputListeFormat.push_back(".hdr");
+ OutputListeFormat.push_back(".vox");
+ } else if (dimension == 3) {
+ OutputListeFormat.push_back(".mhd");
+ OutputListeFormat.push_back(".mha");
+ OutputListeFormat.push_back(".nii");
+ OutputListeFormat.push_back(".nrrd");
+ OutputListeFormat.push_back(".nhdr");
+ OutputListeFormat.push_back(".hdr");
+ OutputListeFormat.push_back(".vox");
+ } else if (dimension == 4) {
+ OutputListeFormat.push_back(".mhd");
+ OutputListeFormat.push_back(".mha");
+ OutputListeFormat.push_back(".nii");
+ OutputListeFormat.push_back(".nrrd");
+ OutputListeFormat.push_back(".nhdr");
+ }
+ QString Extensions = "AllFiles(*.*)";
+ for (int i = 0; i < OutputListeFormat.count(); i++) {
+ Extensions += ";;Images ( *";
+ Extensions += OutputListeFormat[i];
+ Extensions += ")";
+ }
+ QString fileName = QFileDialog::getSaveFileName(this,
+ tr("Save As"),
+ mSlicerManagers[index]->GetFileName().c_str(),
+ Extensions);
+ if (!fileName.isEmpty()) {
+ std::string fileformat = itksys::SystemTools::GetFilenameLastExtension(fileName.toStdString());
+ if (OutputListeFormat.contains(
+ fileformat.c_str())) {
+ QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+ std::string action = "Saving";
+ vvProgressDialog progress("Saving "+fileName.toStdString());
+ qApp->processEvents();
+ vvImageWriter::Pointer writer = vvImageWriter::New();
+ writer->SetOutputFileName(fileName.toStdString());
+ writer->SetInput(mSlicerManagers[index]->GetImage());
+
+ // Check on transform and prompt user
+ writer->SetSaveTransform(false);
+ bool bId = true;
+ for(int i=0; i<4; i++)
+ for(int j=0; j<4; j++) {
+ // TODO SR and BP: check on the list of transforms and not the first only
+ double elt = mSlicerManagers[index]->GetImage()->GetTransform()[0]->GetMatrix()->GetElement(i,j);
+ if(i==j && elt!=1.)
+ bId = false;
+ if(i!=j && elt!=0.)
+ bId = false;
+ }
+ if( !bId ) {
+ QString warning = "The image has an associated linear transform. Do you want to save it along?";
+ QMessageBox msgBox(QMessageBox::Warning, tr("Save transform"), warning, 0, this);
+ msgBox.addButton(tr("Yes"), QMessageBox::AcceptRole);
+ msgBox.addButton(tr("No"), QMessageBox::RejectRole);
+ if (msgBox.exec() == QMessageBox::AcceptRole)
+ writer->SetSaveTransform(true);
+ }
+
+ writer->Update();
+ QApplication::restoreOverrideCursor();
+ if (writer->GetLastError().size()) {
+ QString error = "Saving did not succeed\n";
+ error += writer->GetLastError().c_str();
+ QMessageBox::information(this,tr("Saving Problem"),error);
+ SaveAs();
+ }
+ } else {
+ QString error = fileformat.c_str();
+ if (error.isEmpty())
+ error += "no file format specified !";
+ else
+ error += " format unknown !!!\n";
+ QMessageBox::information(this,tr("Saving Problem"),error);
+ SaveAs();
+ }
+ }