Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkPlaneCutFilter.h"
00019
00020 #include <mitkImageAccessByItk.h>
00021 #include <mitkLine.h>
00022
00023 #define roundf(x) ((x - floor(x)) > 0.5f ? ceil(x) : floor(x))
00024
00025 void mitk::PlaneCutFilter::GenerateData()
00026 {
00027 if (!this->m_Plane)
00028 {
00029 return;
00030 }
00031
00032 InputImageType *input = const_cast<InputImageType*>(this->GetInput());
00033
00034 if (!input)
00035 {
00036 return;
00037 }
00038
00039
00040 OutputImageType *output = this->GetOutput();
00041
00042 output->Initialize(input);
00043 output->SetImportVolume(input->GetData());
00044
00045
00046 AccessByItk_2(output, _computeIntersection, this->m_Plane, input->GetGeometry());
00047 }
00048
00049 mitk::PlaneCutFilter::PlaneCutFilter()
00050 : m_BackgroundLevel(0.0f), m_Plane(0), m_FillMode(FILL)
00051 {
00052 }
00053
00054 mitk::PlaneCutFilter::~PlaneCutFilter()
00055 {
00056 }
00057
00058 template <typename TPixel, unsigned int VImageDimension>
00059 void mitk::PlaneCutFilter::_computeIntersection(itk::Image<TPixel, VImageDimension> *image, const PlaneGeometry *plane, const Geometry3D *geometry)
00060 {
00061 typedef itk::Image<TPixel, VImageDimension> ImageType;
00062
00063 const typename ImageType::RegionType &image_region = image->GetLargestPossibleRegion();
00064 const typename ImageType::SizeValueType
00065 max_x = image_region.GetSize(0ul),
00066 max_y = image_region.GetSize(1ul),
00067 max_z = image_region.GetSize(2ul),
00068 img_size = max_x * max_y;
00069 TPixel *data = image->GetBufferPointer();
00070 Point3D p1, p2;
00071
00072
00073 TPixel casted_background_level = static_cast<TPixel>(this->m_BackgroundLevel);
00074
00075 p1[0] = 0;
00076 p2[0] = max_x - 1ul;
00077
00078 if (FILL == this->m_FillMode)
00079 {
00080 for (unsigned long z = 0ul; z < max_z; ++z)
00081 {
00082 p1[2] = z;
00083 p2[2] = z;
00084
00085 for (unsigned long y = 0ul; y < max_y; ++y)
00086 {
00087 p1[1] = y;
00088 p2[1] = y;
00089
00090 Point3D p1_t, p2_t;
00091
00092 geometry->IndexToWorld(p1, p1_t);
00093 geometry->IndexToWorld(p2, p2_t);
00094
00095 if (plane->IsAbove(p1_t))
00096 {
00097 if (plane->IsAbove(p2_t))
00098 {
00099 if (0.0f == this->m_BackgroundLevel)
00100 {
00101 memset(&data[(y * max_x) + (z * img_size)], 0, max_x * sizeof(TPixel));
00102 }
00103 else
00104 {
00105 TPixel *subdata = &data[(y * max_x) + (z * img_size)];
00106
00107 for (unsigned long x = 0; x < max_x; ++x)
00108 {
00109 subdata[x] = casted_background_level;
00110 }
00111 }
00112 }
00113 else
00114 {
00115 Point3D intersection;
00116 Line3D line;
00117
00118 line.SetPoints(p1_t, p2_t);
00119 plane->IntersectionPoint(line, intersection);
00120 geometry->WorldToIndex(intersection, intersection);
00121
00122 if (0.0f == this->m_BackgroundLevel)
00123 {
00124 memset(&data[(y * max_x) + (z * img_size)], 0, (static_cast<unsigned long>(roundf(intersection[0])) + 1u) * sizeof(TPixel));
00125 }
00126 else
00127 {
00128
00129 TPixel *subdata = &data[(y * max_x) + (z * img_size)];
00130 const unsigned long x_size = static_cast<unsigned long>(roundf(intersection[0])) + 1u;
00131
00132 for (unsigned long x = 0; x < x_size; ++x)
00133 {
00134 subdata[x] = casted_background_level;
00135 }
00136 }
00137 }
00138 }
00139 else if (plane->IsAbove(p2_t))
00140 {
00141 Point3D intersection;
00142 Line3D line;
00143
00144 line.SetPoints(p1_t, p2_t);
00145 plane->IntersectionPoint(line, intersection);
00146 geometry->WorldToIndex(intersection, intersection);
00147
00148 if (0.0f == this->m_BackgroundLevel)
00149 {
00150 unsigned long x = static_cast<unsigned long>(roundf(intersection[0]));
00151
00152 memset(&data[x + (y * max_x) + (z * img_size)], 0, (max_x - x) * sizeof(TPixel));
00153 }
00154 else
00155 {
00156 unsigned long x = static_cast<unsigned long>(roundf(intersection[0]));
00157 TPixel *subdata = &data[x + (y * max_x) + (z * img_size)];
00158 const unsigned long x_size = max_x - x;
00159
00160 for (x = 0; x < x_size; ++x)
00161 {
00162 subdata[x] = casted_background_level;
00163 }
00164 }
00165 }
00166 }
00167 }
00168 }
00169 else
00170 {
00171 for (unsigned long z = 0ul; z < max_z; ++z)
00172 {
00173 p1[2] = z;
00174 p2[2] = z;
00175
00176 for (unsigned long y = 0ul; y < max_y; ++y)
00177 {
00178 p1[1] = y;
00179 p2[1] = y;
00180
00181 Point3D p1_t, p2_t;
00182
00183 geometry->IndexToWorld(p1, p1_t);
00184 geometry->IndexToWorld(p2, p2_t);
00185
00186 if (!plane->IsAbove(p1_t))
00187 {
00188 if (!plane->IsAbove(p2_t))
00189 {
00190 if (0.0f == this->m_BackgroundLevel)
00191 {
00192 memset(&data[(y * max_x) + (z * img_size)], 0, max_x * sizeof(TPixel));
00193 }
00194 else
00195 {
00196 TPixel *subdata = &data[(y * max_x) + (z * img_size)];
00197
00198 for (unsigned long x = 0; x < max_x; ++x)
00199 {
00200 subdata[x] = casted_background_level;
00201 }
00202 }
00203 }
00204 else
00205 {
00206 Point3D intersection;
00207 Line3D line;
00208
00209 line.SetPoints(p1_t, p2_t);
00210 plane->IntersectionPoint(line, intersection);
00211 geometry->WorldToIndex(intersection, intersection);
00212
00213 if (0.0f == this->m_BackgroundLevel)
00214 {
00215 memset(&data[(y * max_x) + (z * img_size)], 0, (static_cast<unsigned long>(roundf(intersection[0])) + 1u) * sizeof(TPixel));
00216 }
00217 else
00218 {
00219
00220 TPixel *subdata = &data[(y * max_x) + (z * img_size)];
00221 const unsigned long x_size = static_cast<unsigned long>(roundf(intersection[0])) + 1u;
00222
00223 for (unsigned long x = 0; x < x_size; ++x)
00224 {
00225 subdata[x] = casted_background_level;
00226 }
00227 }
00228 }
00229 }
00230 else if (!plane->IsAbove(p2_t))
00231 {
00232 Point3D intersection;
00233 Line3D line;
00234
00235 line.SetPoints(p1_t, p2_t);
00236 plane->IntersectionPoint(line, intersection);
00237 geometry->WorldToIndex(intersection, intersection);
00238
00239 if (0.0f == this->m_BackgroundLevel)
00240 {
00241 unsigned long x = static_cast<unsigned long>(roundf(intersection[0]));
00242
00243 memset(&data[x + (y * max_x) + (z * img_size)], 0, (max_x - x) * sizeof(TPixel));
00244 }
00245 else
00246 {
00247 unsigned long x = static_cast<unsigned long>(roundf(intersection[0]));
00248 TPixel *subdata = &data[x + (y * max_x) + (z * img_size)];
00249 const unsigned long x_size = max_x - x;
00250
00251 for (x = 0; x < x_size; ++x)
00252 {
00253 subdata[x] = casted_background_level;
00254 }
00255 }
00256 }
00257 }
00258 }
00259 }
00260 }