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 ===========================================================================*/
18 #include <vtkPoints.h>
19 #include <vtkCellArray.h>
20 #include <vtkPointData.h>
21 #include <vtkImageData.h>
22 #include <vtkMetaImageReader.h>
23 #include <vtkMetaImageWriter.h>
24 #include <vtkPNGReader.h>
25 #include <vtkPolyData.h>
26 #include <vtkCellLocator.h>
33 #include "clitkGammaIndex_ggo.h"
37 #include <vvImageReader.h>
38 #include <vvImageWriter.h>
41 #include <itkImageRegionIterator.h>
42 typedef itk::Image<double> OutputImageType;
43 typedef itk::ImageRegionIterator<OutputImageType> OutputImageIterator;
45 vtkImageData* loadImage(const std::string& filename)
47 vvImageReader::Pointer reader = vvImageReader::New();
48 reader->SetInputFilename(filename);
50 vvImage::Pointer vvimage = reader->GetOutput();
51 if (!vvimage) { cerr << "can't load " << filename << endl; return NULL; }
53 vtkImageData *image = vtkImageData::New();
54 image->DeepCopy(vvimage->GetFirstVTKImageData());
58 void saveImage(OutputImageType* image,const std::string &filename) {
59 vvImage::Pointer vvimage = vvImage::New();
60 vvimage->AddItkImage(image);
62 vvImageWriter::Pointer writer = vvImageWriter::New();
63 writer->SetOutputFileName(filename.c_str());
64 writer->SetInput(vvimage);
69 void insertTriangles(vtkCellArray *cells, vtkPoints *points, const vtkIdType ids[4]) {
70 double p0[3]; points->GetPoint(ids[0],p0);
71 double p1[3]; points->GetPoint(ids[1],p1);
72 double p2[3]; points->GetPoint(ids[2],p2);
73 double p3[3]; points->GetPoint(ids[3],p3);
74 //cout << "----------------------------------" << endl;
75 //cout << "p0=[" << p0[0] << "," << p0[1] << "," << p0[2] << "]" << endl;
76 //cout << "p1=[" << p1[0] << "," << p1[1] << "," << p1[2] << "]" << endl;
77 //cout << "p2=[" << p2[0] << "," << p2[1] << "," << p2[2] << "]" << endl;
78 //cout << "p3=[" << p3[0] << "," << p3[1] << "," << p3[2] << "]" << endl;
80 double center[] = {0,0,0};
81 for (int kk=0; kk<3; kk++) {
89 vtkIdType center_id = points->InsertNextPoint(center);
90 //cout << "center=[" << center[0] << "," << center[1] << "," << center[2] << "]" << endl;
92 cells->InsertNextCell(3);
93 cells->InsertCellPoint(ids[0]);
94 cells->InsertCellPoint(ids[1]);
95 cells->InsertCellPoint(center_id);
97 cells->InsertNextCell(3);
98 cells->InsertCellPoint(ids[1]);
99 cells->InsertCellPoint(ids[3]);
100 cells->InsertCellPoint(center_id);
102 cells->InsertNextCell(3);
103 cells->InsertCellPoint(ids[3]);
104 cells->InsertCellPoint(ids[2]);
105 cells->InsertCellPoint(center_id);
107 cells->InsertNextCell(3);
108 cells->InsertCellPoint(ids[2]);
109 cells->InsertCellPoint(ids[0]);
110 cells->InsertCellPoint(center_id);
113 void insertLine(vtkCellArray *cells, vtkPoints *points, const vtkIdType ids[2]) {
114 cells->InsertNextCell(2);
115 cells->InsertCellPoint(ids[0]);
116 cells->InsertCellPoint(ids[1]);
119 double getMaximum(vtkImageData *image) {
123 vtkPointData* point_data = image->GetPointData();
125 vtkDataArray* scalars = point_data->GetScalars();
128 for (int kk=0; kk<image->GetNumberOfPoints(); kk++) {
129 double value = scalars->GetTuple1(kk);
137 if (maximum<value) maximum = value;
144 vtkPolyData *buildPlane(vtkImageData *image,double spatial_margin,double dose_margin) {
145 vtkPoints *points = vtkPoints::New();
146 for (int kk=0; kk<image->GetNumberOfPoints(); kk++) {
147 double *point = image->GetPoint(kk);
148 double value = image->GetPointData()->GetScalars()->GetTuple1(kk);
153 point[0] /= spatial_margin;
154 point[1] /= spatial_margin;
155 point[2] /= dose_margin;
158 vtkIdType point_id = points->InsertNextPoint(point);
159 assert(kk==point_id);
161 points->InsertNextPoint(point);
165 vtkCellArray *cells = vtkCellArray::New();
166 for (int kk=0; kk<image->GetNumberOfCells(); kk++) {
167 vtkCell *cell = image->GetCell(kk);
169 if (cell->GetNumberOfPoints()==4) {
170 insertTriangles(cells,points,cell->GetPointIds()->GetPointer(0));
174 if (cell->GetNumberOfPoints()==2) {
175 insertLine(cells,points,cell->GetPointIds()->GetPointer(0));
182 vtkPolyData *data = vtkPolyData::New();
183 data->SetPoints(points);
184 data->SetPolys(cells);
191 int main(int argc,char * argv[])
193 clitk::RegisterClitkFactories();
195 args_info_clitkGammaIndex args_info;
197 if (cmdline_parser_clitkGammaIndex(argc, argv, &args_info) != 0)
200 if (!args_info.absolute_dose_margin_given && !args_info.relative_dose_margin_given) {
201 std::cerr << "Specify either relative or absolute dose margin" << endl;
205 bool verbose = args_info.verbose_flag;
207 std::string reference_filename(args_info.reference_arg);
208 std::string target_filename(args_info.target_arg);
209 std::string gamma_filename(args_info.output_arg);
210 double space_margin = args_info.spatial_margin_arg;
211 double dose_rel_margin = args_info.relative_dose_margin_arg;
212 double dose_margin = args_info.absolute_dose_margin_arg;
213 bool use_dose_margin = args_info.absolute_dose_margin_given;
216 cout << "reference_filename=" << reference_filename << endl;
217 cout << "target_filename=" << target_filename << endl;
218 cout << "gamma_filename=" << gamma_filename << endl;
219 cout << "space_margin=" << space_margin << endl;
220 if (use_dose_margin) cout << "dose_margin=" << dose_margin << endl;
221 else cout << "dose_rel_margin=" << dose_rel_margin << endl;
225 vtkImageData* reference = loadImage(reference_filename);
228 // translate target with arguments values
229 // reference is translated instead of target so that the output space stay the same as target
231 double reference_origin[3];
232 reference->GetOrigin(reference_origin);
233 reference_origin[0] -= args_info.translation_x_arg;
234 reference_origin[1] -= args_info.translation_y_arg;
235 reference_origin[2] -= args_info.translation_z_arg;
236 reference->SetOrigin(reference_origin);
239 // intensity normalisation
240 if (!use_dose_margin) {
241 dose_margin = getMaximum(reference)*dose_rel_margin;
242 if (verbose) cout << "dose_margin=" << dose_margin << endl;
246 vtkPolyData *data = buildPlane(reference,space_margin,dose_margin);
249 vtkAbstractCellLocator *locator = vtkCellLocator::New();
250 locator->SetDataSet(data);
252 locator->CacheCellBoundsOn();
253 locator->AutomaticOn();
254 locator->BuildLocator();
257 vtkImageData* target = loadImage(target_filename);
262 OutputImageType::Pointer output = OutputImageType::New();
264 OutputImageType::SizeType::SizeValueType output_array_size[2];
265 output_array_size[0] = target->GetDimensions()[0];
266 output_array_size[1] = target->GetDimensions()[1];
267 OutputImageType::SizeType output_size;
268 output_size.SetSize(output_array_size);
269 output->SetRegions(OutputImageType::RegionType(output_size));
270 output->SetOrigin(target->GetOrigin());
271 output->SetSpacing(target->GetSpacing());
276 unsigned long kk = 0;
277 unsigned long over_one = 0;
278 OutputImageIterator iter(output,output->GetLargestPossibleRegion());
280 while (!iter.IsAtEnd()) {
281 double *point = target->GetPoint(kk);
282 double value = target->GetPointData()->GetScalars()->GetTuple1(kk);
287 point[0] /= space_margin;
288 point[1] /= space_margin;
289 point[2] /= dose_margin;
291 double closest_point[3] = {0,0,0};
292 vtkIdType cell_id = 0;
294 double squared_distance = 0;
296 locator->FindClosestPoint(point,closest_point,cell_id,foo,squared_distance);
297 double distance = sqrt(squared_distance);
300 if (distance>1) over_one++;
306 cout << "total=" << kk << endl;
307 cout << "over_one=" << over_one << endl;
308 cout << "ratio=" << static_cast<float>(over_one)/kk << endl;
314 saveImage(output,gamma_filename);