1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #include <QFileDialog>
25 #include "vvToolHistogram.h"
26 #include "vvProgressDialog.h"
27 #include "vvSlicerManager.h"
29 #include "vvToolInputSelectorWidget.h"
33 #include <vtkImageActor.h>
34 #include <vtkCamera.h>
35 #include <vtkImageClip.h>
36 #include <vtkRenderWindow.h>
37 #include <vtkChartXY.h>
39 #include <vtkRendererCollection.h>
40 #include <vtkRenderer.h>
43 #include <vtkCellArray.h>
44 #include <vtkCellData.h>
45 #include <vtkPolyDataMapper.h>
46 #include <vtkProperty.h>
48 #include <vtkInformation.h>
51 # include "vvOSXHelper.h"
54 //------------------------------------------------------------------------------
55 // Create the tool and automagically (I like this word) insert it in
56 // the main window menu.
57 ADD_TOOL(vvToolHistogram);
58 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 void vvToolHistogram::Initialize()
64 SetToolName("Histogram");
65 SetToolMenuName("Intensity Histogram");
66 SetToolIconFilename(":/common/icons/histogram.png");
67 SetToolTip("Display the histogram of the image.");
68 SetToolExperimental(false);
70 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
74 vvToolHistogram::vvToolHistogram(vvMainWindowBase * parent, Qt::WindowFlags f)
75 :vvToolWidgetBase(parent,f),
76 vvToolBase<vvToolHistogram>(parent),
80 Ui_vvToolHistogram::setupUi(mToolWidget);
82 // Connect signals & slots
83 connect(mSaveHistogramButton, SIGNAL(clicked()), this, SLOT(SaveAs()));
85 // Initialize some widget
86 HistogramWidget->hide();
88 mView = vtkSmartPointer<vtkContextView>::New();
89 vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
90 chart->SetAutoSize(false);
91 chart->SetRenderEmpty(true);
92 mView->GetScene()->AddItem(chart);
93 this->HistogramWidget->GetRenderWindow()->GetRenderers()->RemoveAllItems();
94 this->HistogramWidget->GetRenderWindow()->AddRenderer(mView->GetRenderer());
95 HistogramWidget->show();
98 disableGLHiDPI(HistogramWidget->winId());
102 mFilter = clitk::HistogramImageGenericFilter::New();
104 // Set how many inputs are needed for this tool
105 AddInputSelector("Select one image", mFilter);
107 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 vvToolHistogram::~vvToolHistogram()
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 void vvToolHistogram::computeHistogram()
120 if (!mCurrentSlicerManager) close();
121 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
122 GetArgsInfoFromGUI();
123 HistogramWidget->hide();
126 mFilter->SetInputVVImage(mCurrentImage);
127 mFilter->SetArgsInfo(mArgsInfo);
130 //Creation of the XY chart
131 vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
132 vtkSmartPointer<vtkFloatArray> arrX = vtkSmartPointer<vtkFloatArray>::New();
133 vtkSmartPointer<vtkFloatArray> arrY = vtkSmartPointer<vtkFloatArray>::New();
134 arrX = mFilter->GetArrayX();
135 arrY = mFilter->GetArrayY();
136 arrX->SetName("Intensity");
137 arrY->SetName("#Voxels");
139 table->AddColumn(arrX);
140 table->AddColumn(arrY);
142 mView->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
144 vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
145 chart->SetAutoSize(true);
146 mView->GetScene()->ClearItems();
147 mView->GetScene()->AddItem(chart);
148 vtkPlot *line = chart->AddPlot(vtkChart::LINE);
149 #if VTK_MAJOR_VERSION <= 5
150 line->SetInput(table, 0, 1);
152 line->SetInputData(table, 0, 1);
154 line->SetColor(0, 255, 0, 255);
156 chart->GetAxis(vtkAxis::LEFT)->SetTitle("#Voxels");
157 chart->GetAxis(vtkAxis::BOTTOM)->SetTitle("Intensity");
159 this->HistogramWidget->GetRenderWindow()->GetRenderers()->RemoveAllItems();
160 this->HistogramWidget->GetRenderWindow()->AddRenderer(mView->GetRenderer());
161 HistogramWidget->show();
163 QApplication::restoreOverrideCursor();
165 //------------------------------------------------------------------------------
168 //------------------------------------------------------------------------------
169 void vvToolHistogram::RemoveVTKObjects()
171 if (mCurrentSlicerManager)
173 connect(mCurrentSlicerManager, SIGNAL(callAddLandmark(float,float,float,float)), mCurrentSlicerManager, SLOT(AddLandmark(float,float,float,float)));
175 mCurrentSlicerManager->Render();
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 bool vvToolHistogram::close()
184 //RemoveVTKObjects();
186 return vvToolWidgetBase::close();
188 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 void vvToolHistogram::closeEvent(QCloseEvent *event)
197 //------------------------------------------------------------------------------
200 //------------------------------------------------------------------------------
201 void vvToolHistogram::reject()
203 // DD("vvToolHistogram::reject");
205 return vvToolWidgetBase::reject();
207 //------------------------------------------------------------------------------
210 //------------------------------------------------------------------------------
211 void vvToolHistogram::InputIsSelected(vvSlicerManager * m)
213 mCurrentSlicerManager = m;
215 mSaveHistogramButton->setEnabled(true);
216 mTextFileName = "Histogram.txt";
220 disconnect(mCurrentSlicerManager, SIGNAL(callAddLandmark(float,float,float,float)), mCurrentSlicerManager, SLOT(AddLandmark(float,float,float,float)));
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 void vvToolHistogram::GetArgsInfoFromGUI()
229 /* //KEEP THIS FOR READING GGO FROM FILE
231 std::string a = "toto";
232 char * const* argv = new char*;
234 struct cmdline_parser_params p;
235 p.check_required = 0;
236 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
239 cmdline_parser_clitkHistogramImage_init(&mArgsInfo); // Initialisation to default
241 mArgsInfo.verbose_flag = false;
243 // Required (even if not used)
244 mArgsInfo.input_given = 0;
245 mArgsInfo.output_given = 0;
247 mArgsInfo.input_arg = new char;
248 mArgsInfo.output_arg = new char;
251 //------------------------------------------------------------------------------
254 //------------------------------------------------------------------------------
255 void vvToolHistogram::apply()
259 //------------------------------------------------------------------------------
262 //------------------------------------------------------------------------------
263 void vvToolHistogram::SaveAs()
265 QStringList OutputListeFormat;
266 OutputListeFormat.clear();
267 OutputListeFormat.push_back(".txt");
269 QString Extensions = "AllFiles(*.*)";
270 for (int i = 0; i < OutputListeFormat.count(); i++) {
271 Extensions += ";;Text File ( *";
272 Extensions += OutputListeFormat[i];
275 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), mTextFileName.c_str(), Extensions);
276 if (!fileName.isEmpty()) {
277 std::string fileformat = itksys::SystemTools::GetFilenameLastExtension(fileName.toStdString());
278 QString fileQFormat = fileformat.c_str();
279 if (OutputListeFormat.contains(fileformat.c_str()) || fileQFormat.isEmpty()) {
280 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
281 std::string action = "Saving";
282 vvProgressDialog progress("Saving "+fileName.toStdString());
283 qApp->processEvents();
285 if (!mCurrentSlicerManager) {
291 mTextFileName = fileName.toStdString();
292 if (fileQFormat.isEmpty())
293 mTextFileName += ".txt";
294 ofstream fileOpen(mTextFileName.c_str(), std::ofstream::trunc);
297 cerr << "Error during saving" << endl;
298 QApplication::restoreOverrideCursor();
303 vtkSmartPointer<vtkFloatArray> arrX = vtkSmartPointer<vtkFloatArray>::New();
304 vtkSmartPointer<vtkFloatArray> arrY = vtkSmartPointer<vtkFloatArray>::New();
305 vtkSmartPointer<vtkFloatArray> coords = vtkSmartPointer<vtkFloatArray>::New();
306 arrX = mFilter->GetArrayX();
307 arrY = mFilter->GetArrayY();
309 fileOpen << "Value represents the number of voxels around the corresponding intensity (by default the windows size around intensity is log(range))" << endl;
310 fileOpen << "Intensity" << "\t" << "Value" << endl;
312 while (i<arrX->GetNumberOfTuples()) {
313 fileOpen << arrX->GetTuple(i)[0] << "\t" << arrY->GetTuple(i)[0] << endl;
318 QApplication::restoreOverrideCursor();
320 QString error = fileformat.c_str();
321 error += " format unknown !!!\n";
322 QMessageBox::information(this,tr("Saving Problem"),error);
327 //------------------------------------------------------------------------------