From d99d09e51a413d4b093cb809726c97f6a0e6f0ab Mon Sep 17 00:00:00 2001 From: David Sarrut Date: Mon, 20 Feb 2012 16:30:24 +0100 Subject: [PATCH] First version that allow to open file (no button feedback yet) --- vv/vvToolROIManager.cxx | 199 ++++++++++++++++++++++++++++++++++++++++ vv/vvToolROIManager.h | 22 +++++ 2 files changed, 221 insertions(+) diff --git a/vv/vvToolROIManager.cxx b/vv/vvToolROIManager.cxx index 77a65d3..b681fc7 100644 --- a/vv/vvToolROIManager.cxx +++ b/vv/vvToolROIManager.cxx @@ -51,10 +51,30 @@ vvToolROIManager::vvToolROIManager(vvMainWindowBase * parent, Qt::WindowFlags f) // Insert the current QWidget into the tab layout (required) QWidget * mother = qFindChild(parent->GetTab(), "ROItab"); mother->layout()->addWidget(this); + mMainWindow = parent; // Build the UI Ui_vvToolROIManager::setupUi(this); setAttribute(Qt::WA_DeleteOnClose); + mTree->clear(); + mTree->header()->resizeSection(0, 30); + parent->GetTab()->setCurrentIndex(2); + + // Set default LUT + mDefaultLUTColor = vtkSmartPointer::New(); + DD(mDefaultLUTColor->GetNumberOfTableValues()); + for(int i=0; iGetNumberOfTableValues(); i++) { + double r = (rand()/(RAND_MAX+1.0)); + double v = (rand()/(RAND_MAX+1.0)); + double b = (rand()/(RAND_MAX+1.0)); + mDefaultLUTColor->SetTableValue(i, r, v, b); + // std::cout << "mDefaultLUTColor->SetTableValue(" << i << ", " << r << ", " << v << ", " << b << ");" << std::endl; + } +#include "vvDefaultLut.h" + + // Initialization + mNumberOfVisibleROI = 0; + mNumberOfVisibleContourROI = 0; // Select the current image as the target int i = parent->GetSlicerManagerCurrentIndex(); @@ -101,7 +121,18 @@ void vvToolROIManager::InputIsSelected(vvSlicerManager *m) connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close())); // Initialization + mSlicerManager = m; + mCurrentImage = mSlicerManager->GetImage(); + + // Refuse if 4D + if (mCurrentImage->GetNumberOfDimensions() != 3) { + QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet")); + close(); + return; + } + // Auto diusplay browser to select new contours + OpenBinaryImage(); } //------------------------------------------------------------------------------ @@ -120,3 +151,171 @@ void vvToolROIManager::SelectedImageHasChanged(vvSlicerManager * m) { else show(); } //------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +void vvToolROIManager::OpenBinaryImage() +{ + // Open images + QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)"; + Extensions += ";;All Files (*)"; + QStringList filename = + QFileDialog::getOpenFileNames(this,tr("Open binary image"), + mMainWindowBase->GetInputPathName(),Extensions); + if (filename.size() == 0) return; + + // For each selected file, open the image + for(int i=0; i filenames; + filenames.push_back(filename[i].toStdString()); + reader->SetInputFilenames(filenames); + reader->Update(vvImageReader::IMAGE); + QApplication::restoreOverrideCursor(); + + if (reader->GetLastError().size() != 0) { + std::cerr << "Error while reading " << filename[i].toStdString() << std::endl; + QString error = "Cannot open file \n"; + error += reader->GetLastError().c_str(); + QMessageBox::information(this,tr("Reading problem"),error); + return; + } + vvImage::Pointer binaryImage = reader->GetOutput(); + AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value(), + (!mBGModeCheckBox->isChecked())); + mOpenedBinaryImage.push_back(binaryImage); + } + + // Update the contours + UpdateAllContours(); +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +void vvToolROIManager::AddImage(vvImage * binaryImage, std::string filename, + double BG, bool modeBG) +{ + DD(modeBG); + + // Check Dimension + int dim = mCurrentImage->GetNumberOfDimensions(); + int bin_dim = binaryImage->GetNumberOfDimensions(); + if (dim < bin_dim) { + std::ostringstream os; + os << "Error. Loaded binary image is " << bin_dim + << "D while selected image is " << dim << "D" << std::endl; + QMessageBox::information(this,tr("Reading problem"),os.str().c_str()); + return; + } + + // Compute roi index + int n = mROIList.size(); + DD(n); + + // Compute the name of the new ROI + std::ostringstream oss; + oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename)); + std::string name = oss.str(); + DD(name); + + // Set color + std::vector color; + color.push_back(1); + color.push_back(0); + color.push_back(0); + + // Create ROI + clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New(); + roi->SetFromBinaryImage(binaryImage, n, name, color, filename); + + // Add a new roi to the list + mROIList.push_back(roi); + + // Set BG or FG mode + if (modeBG) + roi->SetBackgroundValueLabelImage(BG); + else + roi->SetForegroundValueLabelImage(BG); + + // Change color + DD("color"); + if (nGetNumberOfTableValues ()) { + double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ()); + roi->SetDisplayColor(color[0], color[1], color[2]); + } + + // Add a new roi actor + QSharedPointer actor = QSharedPointer(new vvROIActor); + actor->SetBGMode(modeBG); + actor->SetROI(roi); + actor->SetSlicerManager(mSlicerManager); + actor->Initialize(n+1); // depth is n+1 to start at 1 + mROIActorsList.push_back(actor); + + // CheckBox for "All" + if (actor->IsVisible()) mNumberOfVisibleROI++; + if (actor->IsContourVisible()) mNumberOfVisibleContourROI++; + + // Add ROI in tree + mTreeWidgetList.push_back(QSharedPointer(new QTreeWidgetItem(mTree))); + QTreeWidgetItem * w = mTreeWidgetList.back().data(); + w->setText(0, QString("%1").arg(roi->GetROINumber())); + w->setText(1, QString("%1").arg(roi->GetName().c_str())); + w->setText(3, QString("%1").arg(actor->GetDepth())); + QBrush brush(QColor(roi->GetDisplayColor()[0]*255, + roi->GetDisplayColor()[1]*255, + roi->GetDisplayColor()[2]*255)); + brush.setStyle(Qt::SolidPattern); + w->setBackground(2, brush); + mMapROIToTreeWidget[roi] = w; + mMapTreeWidgetToROI[w] = roi; + mTree->resizeColumnToContents(0); + mTree->resizeColumnToContents(1); + + // Update + UpdateAllROIStatus(); +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +void vvToolROIManager::UpdateAllContours() +{ + // Render loaded ROIs (the first is sufficient) + for(unsigned int i=0; iUpdate(); + } + for(int i=0; iGetNumberOfSlicers(); i++) { + mSlicerManager->GetSlicer(i)->Render(); + } +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +void vvToolROIManager::UpdateAllROIStatus() { + int nbVisible = 0; + int nb = mROIList.size(); + for(int i=0; iIsVisible()) { + nbVisible++; + } + } + DD(nbVisible); + + // change the states + disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int))); + disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool))); + if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked); + else { + if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked); + else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked); + } + connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool))); + connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int))); +} +//------------------------------------------------------------------------------ + diff --git a/vv/vvToolROIManager.h b/vv/vvToolROIManager.h index 2d91264..be3b987 100644 --- a/vv/vvToolROIManager.h +++ b/vv/vvToolROIManager.h @@ -43,13 +43,35 @@ class vvToolROIManager: static void Initialize(); virtual void InputIsSelected(vvSlicerManager *m); + void AddImage(vvImage * binaryImage, std::string filename, double BG, bool m_modeBG=true); + void UpdateAllContours(); + void UpdateAllROIStatus(); + public slots: void AnImageIsBeingClosed(vvSlicerManager *); void SelectedImageHasChanged(vvSlicerManager *); + void OpenBinaryImage(); protected: Ui::vvToolROIManager ui; vvSlicerManager * mSlicerManager; + vvImage * mCurrentImage; + vvMainWindowBase * mMainWindow; + + int mNumberOfVisibleROI; + int mNumberOfVisibleContourROI; + + vtkSmartPointer mDefaultLUTColor; + + std::vector mOpenedBinaryImage; + std::vector mROIList; + std::vector > mROIActorsList; + + // Data for the widget Tree + std::vector< QSharedPointer > mTreeWidgetList; + std::map mMapROIToTreeWidget; + std::map mMapTreeWidgetToROI; + }; // end class vvToolROIManager //------------------------------------------------------------------------------ -- 2.47.1