]> Creatis software - clitk.git/blob - tools/clitkMakeSphereImage.cxx
Add MousePositionUpdatedSignal
[clitk.git] / tools / clitkMakeSphereImage.cxx
1 #include "itkImage.h"
2 #include "itkSphereSpatialFunction.h"
3 #include <itkImageRegionIterator.h>
4 #include "itkImageFileWriter.h"
5
6 int main(int argc, char** argv) {
7   
8   const unsigned int dim = 3;
9   typedef char PixelType;
10   typedef itk::Image<PixelType, dim> ImageType;
11   typedef ImageType::IndexType IndexType;
12   typedef ImageType::PointType PointType;
13   typedef ImageType::SizeType SizeType;
14   typedef ImageType::RegionType RegionType;
15
16   IndexType index;
17   index.Fill(0);
18
19   SizeType size;
20   size[0] = atoi(argv[2]);
21   size[1] = atoi(argv[3]);
22   size[2] = atoi(argv[4]);
23   
24   PointType origin;
25   origin[0] = atoi(argv[5]);
26   origin[1] = atoi(argv[6]);
27   origin[2] = atoi(argv[7]);
28   
29   RegionType region;
30   region.SetIndex(index);
31   region.SetSize(size);
32
33   ImageType::Pointer image = ImageType::New();
34   image->SetRegions(region);
35   image->Allocate();
36   
37   typedef itk::SphereSpatialFunction<dim, PointType> ShpereFunctionType;
38   ShpereFunctionType::Pointer sphere = ShpereFunctionType::New();
39   
40   double radius = atof(argv[8])/2;
41   sphere->SetCenter(origin);
42   sphere->SetRadius(radius);
43   
44   PixelType max = itk::NumericTraits<PixelType>::max();
45   typedef itk::ImageRegionIterator<ImageType> ImageIteratorType;
46   ImageIteratorType it(image, region);
47   PointType point;
48   for (it.GoToBegin(); !it.IsAtEnd(); ++it) {
49     image->TransformIndexToPhysicalPoint(it.GetIndex(), point);
50     if (sphere->Evaluate(point)) {
51       PixelType value = static_cast<PixelType>(max*(point - origin).GetNorm()/radius); 
52       it.Set(value);
53     }
54     else
55       it.Set(0);
56   }
57   
58   typedef itk::ImageFileWriter<ImageType> ImageWriterType;
59   ImageWriterType::Pointer writer = ImageWriterType::New();
60   writer->SetInput(image);
61   writer->SetFileName(argv[1]);
62   writer->Update();
63 }