00001
00022 #ifndef DGVBLITZARRAYCASTERVTK_H
00023 #define DGVBLITZARRAYCASTERVTK_H
00024
00025 #include <vtkTransform.h>
00026 #include <vtkImageChangeInformation.h>
00027 #include <vtkImageReslice.h>
00028
00029 #include "vtkImageData.h"
00030
00031 #include "DGVAliases.h"
00032
00041 template<typename type>
00042 class DGV_VTK_TEMPLATEDLL DGVBlitzArrayCasterVTK
00043 {
00044 public:
00045 DGVBlitzArrayCasterVTK();
00046 virtual ~DGVBlitzArrayCasterVTK();
00047
00054 vtkImageData* arrayToVTKImageData(Array<type,2> &data);
00055 vtkImageData* arrayToVTKImageData_UnsignedChar(Array<type,2> &data);
00056 vtkImageData* arrayToVTKImageData(Array<type,3> &data);
00057 vtkImageData* arrayToVTKImageData_UnsignedChar(Array<type,3> &data);
00058 vtkImageData* arrayToVTKImageData(Array<complex<type>,3> &data);
00059
00065 bool vtkImageDataToArray(vtkImageData *data, Array<type,2> &newArray);
00066 bool vtkImageDataToArray(vtkImageData *data, int slice, Array<type,2> &newArray);
00067 bool vtkImageDataToArray(vtkImageData *data, Array<type,3> &newArray);
00068
00074 void convertCoordinatesToArray(vtkImageData *&data);
00075
00081 inline void alignmentForImages(bool forImages)
00082 { imageAlignment = forImages; }
00083
00084 inline type getMin()
00085 { return min; }
00086 inline type getMax()
00087 { return max; }
00088 inline type getMean()
00089 { return mean; }
00090
00091 protected:
00092 bool imageAlignment;
00093 type min;
00094 type max;
00095 type mean;
00096 };
00097
00098 template<typename type>
00099 DGVBlitzArrayCasterVTK<type>::DGVBlitzArrayCasterVTK()
00100 {
00101 imageAlignment = false;
00102 }
00103
00104 template<typename type>
00105 DGVBlitzArrayCasterVTK<type>::~DGVBlitzArrayCasterVTK()
00106 {
00107
00108 }
00109
00110 template<typename type>
00111 vtkImageData* DGVBlitzArrayCasterVTK<type>::arrayToVTKImageData(Array<type,2> &data)
00112 {
00113 vtkImageData *imageData = vtkImageData::New();
00114 type tmp;
00115 numeric_limits< type > typeLimits;
00116
00117 min = typeLimits.max();
00118 max = typeLimits.min();
00119 mean = 0;
00120
00121 imageData->SetScalarTypeToDouble();
00122 imageData->SetNumberOfScalarComponents(1);
00123
00124 if(imageAlignment)
00125 {
00126 int dimensions[3] = {data.cols(),data.rows(),0}, extents[6] = {0,data.cols()-1,0,data.rows()-1,0,0};
00127
00128 imageData->SetDimensions(dimensions);
00129 imageData->SetExtent(extents);
00130
00131 for(int j = 0; j < data.rows(); j ++)
00132 for(int k = 0; k < data.cols(); k ++)
00133 {
00134 tmp = static_cast<type>(data(j,k));
00135 if(tmp < min)
00136 min = tmp;
00137 else if(tmp > max)
00138 max = tmp;
00139 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,0,0,tmp);
00140 mean += tmp;
00141 }
00142 }
00143 else
00144 {
00145 int dimensions[3] = {data.rows(),data.cols(),0}, extents[6] = {0,data.rows()-1,0,data.cols()-1,0,0};
00146
00147 imageData->SetDimensions(dimensions);
00148 imageData->SetExtent(extents);
00149
00150 for(int j = 0; j < data.rows(); j ++)
00151 for(int k = 0; k < data.cols(); k ++)
00152 {
00153 tmp = static_cast<type>(data(j,k));
00154 if(tmp < min)
00155 min = tmp;
00156 else if(tmp > max)
00157 max = tmp;
00158 imageData->SetScalarComponentFromDouble(j,k,0,0,tmp);
00159 mean += tmp;
00160 }
00161 }
00162
00163 mean /= data.rows()*data.cols();
00164 imageData->Update();
00165
00166 return imageData;
00167 }
00168
00169 template<typename type>
00170 vtkImageData* DGVBlitzArrayCasterVTK<type>::arrayToVTKImageData_UnsignedChar(Array<type,2> &data)
00171 {
00172 vtkImageData *imageData = vtkImageData::New();
00173 type tmp;
00174 numeric_limits< type > typeLimits;
00175
00176 min = typeLimits.max();
00177 max = typeLimits.min();
00178 mean = 0;
00179
00180 imageData->SetScalarTypeToUnsignedChar();
00181 imageData->SetNumberOfScalarComponents(1);
00182
00183 if(imageAlignment)
00184 {
00185 int dimensions[3] = {data.cols(),data.rows(),0}, extents[6] = {0,data.cols()-1,0,data.rows()-1,0,0};
00186
00187 imageData->SetDimensions(dimensions);
00188 imageData->SetExtent(extents);
00189
00190 for(int j = 0; j < data.rows(); j ++)
00191 for(int k = 0; k < data.cols(); k ++)
00192 {
00193 tmp = static_cast<type>(data(j,k));
00194 if(tmp < min)
00195 min = tmp;
00196 else if(tmp > max)
00197 max = tmp;
00198 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,0,0,tmp);
00199 mean += tmp;
00200 }
00201 }
00202 else
00203 {
00204 int dimensions[3] = {data.rows(),data.cols(),0}, extents[6] = {0,data.rows()-1,0,data.cols()-1,0,0};
00205
00206 imageData->SetDimensions(dimensions);
00207 imageData->SetExtent(extents);
00208
00209 for(int j = 0; j < data.rows(); j ++)
00210 for(int k = 0; k < data.cols(); k ++)
00211 {
00212 tmp = static_cast<type>(data(j,k));
00213 if(tmp < min)
00214 min = tmp;
00215 else if(tmp > max)
00216 max = tmp;
00217 imageData->SetScalarComponentFromDouble(j,k,0,0,tmp);
00218 mean += tmp;
00219 }
00220 }
00221 mean /= data.rows()*data.cols();
00222 imageData->Update();
00223
00224 return imageData;
00225 }
00226
00227 template<typename type>
00228 vtkImageData* DGVBlitzArrayCasterVTK<type>::arrayToVTKImageData(Array<type,3> &data)
00229 {
00230 vtkImageData *imageData = vtkImageData::New();
00231 type tmp;
00232 numeric_limits< type > typeLimits;
00233
00234 min = typeLimits.max();
00235 max = typeLimits.min();
00236 mean = 0;
00237
00238 imageData->SetScalarTypeToDouble();
00239 imageData->SetNumberOfScalarComponents(1);
00240
00241 if(imageAlignment)
00242 {
00243 int dimensions[3] = {data.cols(),data.rows(),data.depth()}, extents[6] = {0,data.cols()-1,0,data.rows()-1,0,data.depth()-1};
00244
00245 imageData->SetDimensions(dimensions);
00246 imageData->SetExtent(extents);
00247
00248 for(int j = 0; j < data.rows(); j ++)
00249 for(int k = 0; k < data.cols(); k ++)
00250 for(int l = 0; l < data.depth(); l ++)
00251 {
00252 tmp = static_cast<type>(data(j,k,l));
00253 if(tmp < min)
00254 min = tmp;
00255 else if(tmp > max)
00256 max = tmp;
00257 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,l,0,tmp);
00258 mean += tmp;
00259 }
00260 }
00261 else
00262 {
00263 int dimensions[3] = {data.rows(),data.cols(),data.depth()}, extents[6] = {0,data.rows()-1,0,data.cols()-1,0,data.depth()-1};
00264
00265 imageData->SetDimensions(dimensions);
00266 imageData->SetExtent(extents);
00267
00268 for(int j = 0; j < data.rows(); j ++)
00269 for(int k = 0; k < data.cols(); k ++)
00270 for(int l = 0; l < data.depth(); l ++)
00271 {
00272 tmp = static_cast<type>(data(j,k,l));
00273 if(tmp < min)
00274 min = tmp;
00275 else if(tmp > max)
00276 max = tmp;
00277 imageData->SetScalarComponentFromDouble(j,k,l,0,tmp);
00278 mean += tmp;
00279 }
00280 }
00281 mean /= data.rows()*data.cols()*data.depth();
00282 imageData->Update();
00283
00284 return imageData;
00285 }
00286
00287 template<typename type>
00288 vtkImageData* DGVBlitzArrayCasterVTK<type>::arrayToVTKImageData_UnsignedChar(Array<type,3> &data)
00289 {
00290 vtkImageData *imageData = vtkImageData::New();
00291 type tmp;
00292 numeric_limits< type > typeLimits;
00293
00294 min = typeLimits.max();
00295 max = typeLimits.min();
00296 mean = 0;
00297
00298 imageData->SetScalarTypeToUnsignedChar();
00299 imageData->SetNumberOfScalarComponents(1);
00300
00301 if(imageAlignment)
00302 {
00303 int dimensions[3] = {data.cols(),data.rows(),data.depth()}, extents[6] = {0,data.cols()-1,0,data.rows()-1,0,data.depth()-1};
00304
00305 imageData->SetDimensions(dimensions);
00306 imageData->SetExtent(extents);
00307
00308 for(int j = 0; j < data.rows(); j ++)
00309 for(int k = 0; k < data.cols(); k ++)
00310 for(int l = 0; l < data.depth(); l ++)
00311 {
00312 tmp = static_cast<type>(data(j,k,l));
00313 if(tmp < min)
00314 min = tmp;
00315 else if(tmp > max)
00316 max = tmp;
00317 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,l,0,tmp);
00318 mean += tmp;
00319 }
00320 }
00321 else
00322 {
00323 int dimensions[3] = {data.rows(),data.cols(),data.depth()}, extents[6] = {0,data.rows()-1,0,data.cols()-1,0,data.depth()-1};
00324
00325 imageData->SetDimensions(dimensions);
00326 imageData->SetExtent(extents);
00327
00328 for(int j = 0; j < data.rows(); j ++)
00329 for(int k = 0; k < data.cols(); k ++)
00330 for(int l = 0; l < data.depth(); l ++)
00331 {
00332 tmp = static_cast<type>(data(j,k,l));
00333 if(tmp < min)
00334 min = tmp;
00335 else if(tmp > max)
00336 max = tmp;
00337 imageData->SetScalarComponentFromDouble(j,k,l,0,tmp);
00338 mean += tmp;
00339 }
00340 }
00341 mean /= data.rows()*data.cols()*data.depth();
00342 imageData->Update();
00343
00344 return imageData;
00345 }
00346
00347 template<typename type>
00348 vtkImageData* DGVBlitzArrayCasterVTK<type>::arrayToVTKImageData(Array<complex<type>,3> &data)
00349 {
00350 vtkImageData *imageData = vtkImageData::New();
00351 type tmp1, tmp2, tmp3;
00352 numeric_limits< type > typeLimits;
00353
00354 min = typeLimits.max();
00355 max = typeLimits.min();
00356 mean = 0;
00357
00358 imageData->SetScalarTypeToDouble();
00359 imageData->SetNumberOfScalarComponents(3);
00360
00361 if(imageAlignment)
00362 {
00363 int dimensions[3] = {data.cols(),data.rows(),data.depth()}, extents[6] = {0,data.cols()-1,0,data.rows()-1,0,data.depth()-1};
00364
00365 imageData->SetDimensions(dimensions);
00366 imageData->SetExtent(extents);
00367
00368 for(int j = 0; j < data.rows(); j ++)
00369 for(int k = 0; k < data.cols(); k ++)
00370 for(int l = 0; l < data.depth(); l ++)
00371 {
00372 tmp1 = static_cast<type>( abs(data(j,k,l)) );
00373 tmp2 = static_cast<type>( data(j,k,l).real() );
00374 tmp3 = static_cast<type>( data(j,k,l).imag() );
00375 if(tmp1 < min)
00376 min = tmp1;
00377 else if(tmp1 > max)
00378 max = tmp1;
00379 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,l,0,tmp1);
00380 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,l,1,tmp2);
00381 imageData->SetScalarComponentFromDouble(k,data.rows()-1-j,l,2,tmp3);
00382 mean += tmp1;
00383 }
00384 }
00385 else
00386 {
00387 int dimensions[3] = {data.rows(),data.cols(),data.depth()}, extents[6] = {0,data.rows()-1,0,data.cols()-1,0,data.depth()-1};
00388
00389 imageData->SetDimensions(dimensions);
00390 imageData->SetExtent(extents);
00391
00392 for(int j = 0; j < data.rows(); j ++)
00393 for(int k = 0; k < data.cols(); k ++)
00394 for(int l = 0; l < data.depth(); l ++)
00395 {
00396 tmp1 = static_cast<type>( abs(data(j,k,l)) );
00397 tmp2 = static_cast<type>( data(j,k,l).real() );
00398 tmp3 = static_cast<type>( data(j,k,l).imag() );
00399 if(tmp1 < min)
00400 min = tmp1;
00401 else if(tmp1 > max)
00402 max = tmp1;
00403 imageData->SetScalarComponentFromDouble(j,k,l,0,tmp1);
00404 imageData->SetScalarComponentFromDouble(j,k,l,1,tmp2);
00405 imageData->SetScalarComponentFromDouble(j,k,l,2,tmp3);
00406 mean += tmp1;
00407 }
00408 }
00409 mean /= data.rows()*data.cols()*data.depth();
00410 imageData->Update();
00411
00412 return imageData;
00413 }
00414
00415 template<typename type>
00416 bool DGVBlitzArrayCasterVTK<type>::vtkImageDataToArray(vtkImageData *data, Array<type,2> &newArray)
00417 {
00418 int bounds[6];
00419 type tmp;
00420 numeric_limits< type > typeLimits;
00421
00422 min = typeLimits.max();
00423 max = typeLimits.min();
00424 mean = 0;
00425
00426 data->GetExtent(bounds);
00427
00428 int boundX = bounds[1]+1, boundY = bounds[3]+1, boundZ = bounds[5]+1;
00429
00430 if(boundX < 0 || boundY < 0 || boundZ < 0)
00431 return false;
00432
00433 int rows = boundX-bounds[0], cols = boundY-bounds[2];
00434
00435 if(imageAlignment)
00436 newArray.resizeAndPreserve(cols,rows);
00437 else
00438 newArray.resizeAndPreserve(rows,cols);
00439
00440 for(int j = bounds[0]; j < rows; j ++)
00441 for(int k = bounds[2]; k < cols; k ++)
00442 {
00443 tmp = static_cast<type>(data->GetScalarComponentAsDouble(j,k,0,0));
00444
00445 if(tmp < min)
00446 min = tmp;
00447 else if(tmp > max)
00448 max = tmp;
00449
00450 if(imageAlignment)
00451 newArray(cols-1-k,j) = tmp;
00452 else
00453 newArray(j,k) = tmp;
00454
00455 mean += tmp;
00456 }
00457
00458 mean /= newArray.rows()*newArray.cols();
00459 return true;
00460 }
00461
00462 template<typename type>
00463 bool DGVBlitzArrayCasterVTK<type>::vtkImageDataToArray(vtkImageData *data, int slice, Array<type,2> &newArray)
00464 {
00465 int bounds[6];
00466 type tmp;
00467 numeric_limits< type > typeLimits;
00468
00469 min = typeLimits.max();
00470 max = typeLimits.min();
00471 mean = 0;
00472
00473 data->GetExtent(bounds);
00474
00475 int boundX = bounds[1]+1, boundY = bounds[3]+1, boundZ = bounds[5]+1;
00476 int rows = boundX-bounds[0], cols = boundY-bounds[2], depth = boundZ-bounds[4];
00477
00478 if(boundX < 0 || boundY < 0 || boundZ < 0)
00479 return false;
00480 else if(slice > depth || slice < 0)
00481 return false;
00482
00483 if(imageAlignment)
00484 newArray.resizeAndPreserve(cols,rows);
00485 else
00486 newArray.resizeAndPreserve(rows,cols);
00487
00488 for(int j = bounds[0]; j < rows; j ++)
00489 for(int k = bounds[2]; k < cols; k ++)
00490 {
00491 tmp = static_cast<type>(data->GetScalarComponentAsDouble(j,k,slice,0));
00492
00493 if(tmp < min)
00494 min = tmp;
00495 else if(tmp > max)
00496 max = tmp;
00497
00498 if(imageAlignment)
00499 newArray(cols-1-k,j) = tmp;
00500 else
00501 newArray(j,k) = tmp;
00502
00503 mean += tmp;
00504 }
00505
00506 mean /= newArray.rows()*newArray.cols();
00507 return true;
00508 }
00509
00510 template<typename type>
00511 bool DGVBlitzArrayCasterVTK<type>::vtkImageDataToArray(vtkImageData *data, Array<type,3> &newArray)
00512 {
00513 int bounds[6];
00514 type tmp;
00515 numeric_limits< type > typeLimits;
00516
00517 min = typeLimits.max();
00518 max = typeLimits.min();
00519 mean = 0;
00520
00521 data->GetExtent(bounds);
00522
00523 int boundX = bounds[1]+1, boundY = bounds[3]+1, boundZ = bounds[5]+1;
00524
00525 if(boundX < 0 || boundY < 0 || boundZ < 0)
00526 return false;
00527
00528 int rows = boundX-bounds[0], cols = boundY-bounds[2], depth = boundZ-bounds[4];
00529
00530 if(imageAlignment)
00531 newArray.resizeAndPreserve(cols,rows,depth);
00532 else
00533 newArray.resizeAndPreserve(rows,cols,depth);
00534
00535 for(int j = bounds[0]; j < rows; j ++)
00536 for(int k = bounds[2]; k < cols; k ++)
00537 for(int l = bounds[4]; l < depth; l ++)
00538 {
00539 tmp = static_cast<type>(data->GetScalarComponentAsDouble(j,k,l,0));
00540
00541 if(tmp < min)
00542 min = tmp;
00543 else if(tmp > max)
00544 max = tmp;
00545
00546 if(imageAlignment)
00547 newArray(cols-1-k,j,l) = tmp;
00548 else
00549 newArray(j,k,l) = tmp;
00550
00551 mean += tmp;
00552 }
00553
00554 mean /= newArray.rows()*newArray.cols()*newArray.depth();
00555 return true;
00556 }
00557
00558 template<typename type>
00559 void DGVBlitzArrayCasterVTK<type>::convertCoordinatesToArray(vtkImageData *&data)
00560 {
00561 int bounds[6];
00562 vtkTransform *transform = vtkTransform::New();
00563 vtkImageReslice *reslice = vtkImageReslice::New();
00564 vtkImageChangeInformation *info = vtkImageChangeInformation::New();
00565
00566
00567 transform->RotateZ(90);
00568 data->GetExtent(bounds);
00569 info->SetInput(data);
00570 info->CenterImageOn();
00571
00572 double center_x = (bounds[1])/2.0, center_y = (bounds[3])/2.0;
00573 reslice->SetInputConnection(info->GetOutputPort());
00574 reslice->SetInformationInput(data);
00575 reslice->SetResliceTransform(transform);
00576 reslice->SetOutputDimensionality(2);
00577 reslice->SetOutputOrigin(-center_x,-center_y,0);
00578 reslice->InterpolateOff();
00579 reslice->Update();
00580
00581 data->Delete();
00582 data = reslice->GetOutput();
00583 transform->Delete();
00584 info->Delete();
00585
00586 data->Update();
00587 }
00588
00589 #endif // DGVBLITZARRAYCASTERVTK_H