Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
volume_gpu.h
1 #pragma once
2 
3 #include "volume.h"
4 #include "volume_allocator_gpu.h"
5 #include <thrust/device_ptr.h>
6 
7 template<typename, int> class ndarray_ref;
8 
9 namespace iu {
10 
11 template<typename PixelType, class Allocator>
15 class VolumeGpu : public Volume
16 {
17 public:
19  typedef PixelType pixel_type;
20 
23  Volume(),
24  data_(0), pitch_(0), ext_data_pointer_(false)
25  {
26  }
27 
29  virtual ~VolumeGpu()
30  {
31  if(!ext_data_pointer_)
32  {
33  // do not delete externally handeled data pointers.
34  Allocator::free(data_);
35  data_ = 0;
36  }
37  pitch_ = 0;
38  }
39 
45  VolumeGpu(unsigned int _width, unsigned int _height, unsigned int _depth) :
46  Volume(_width, _height, _depth), data_(0), pitch_(0),
47  ext_data_pointer_(false)
48  {
49  data_ = Allocator::alloc(this->size(), &pitch_);
50  }
51 
56  Volume(size), data_(0), pitch_(0),
57  ext_data_pointer_(false)
58  {
59  data_ = Allocator::alloc(this->size(), &pitch_);
60  }
61 
70  VolumeGpu(PixelType* _data, unsigned int _width, unsigned int _height, unsigned int _depth,
71  size_t _pitch, bool ext_data_pointer = false) :
72  Volume(_width, _height, _depth), data_(0), pitch_(0),
73  ext_data_pointer_(ext_data_pointer)
74  {
75  if(ext_data_pointer_)
76  {
77  // This uses the external data pointer as internal data pointer.
78  data_ = _data;
79  pitch_ = _pitch;
80  }
81  else
82  {
83  // allocates an internal data pointer and copies the external data onto it.
84  if(_data == 0)
85  return;
86 
87  data_ = Allocator::alloc(this->size(), &pitch_);
88  Allocator::copy(_data, _pitch, data_, pitch_, this->size());
89  }
90  }
91 
93  size_t bytes() const
94  {
95  return depth()*height()*pitch_;
96  }
97 
99  size_t pitch() const
100  {
101  return pitch_;
102  }
103 
105  size_t slice_pitch() const
106  {
107  return height()*pitch_;
108  }
109 
111  size_t stride() const
112  {
113  return pitch_/sizeof(PixelType);
114  }
115 
117  size_t slice_stride() const
118  {
119  return height()*pitch_/sizeof(PixelType);
120  }
121 
123  virtual unsigned int bitDepth() const
124  {
125  return 8*sizeof(PixelType);
126  }
127 
129  virtual bool onDevice() const
130  {
131  return true;
132  }
133 
134 
141  PixelType* data(int ox = 0, int oy = 0, int oz = 0)
142  {
143  return &data_[oz*slice_stride() + oy*stride() + ox];
144  }
145 
152  const PixelType* data(int ox = 0, int oy = 0, int oz = 0) const
153  {
154  return reinterpret_cast<const PixelType*>(
155  &data_[oz*slice_stride() + oy*stride() + ox]);
156  }
157 
164  {
166  &data_[oz*slice_stride()], width(), height(), pitch_, true);
167  }
168 
172  thrust::device_ptr<PixelType> begin(void)
173  {
174  return thrust::device_ptr<PixelType>(data());
175  }
176 
180  thrust::device_ptr<PixelType> end(void)
181  {
182  return thrust::device_ptr<PixelType>(data()+slice_stride()*depth());
183  }
184 
217  struct KernelData
218  {
220  PixelType* data_;
222  int width_;
224  int height_;
226  int depth_;
228  int stride_;
229 
236  __device__ PixelType& operator()(int x, int y, int z)
237  {
238  return data_[z*height_*stride_ + y*stride_ + x];
239  }
240 
243  : data_(const_cast<PixelType*>(vol.data())), width_(vol.width()), height_(vol.height()),
244  depth_(vol.depth()), stride_(vol.stride())
245  { }
246  };
247 
250 
253 
254 protected:
255 
256 private:
258  PixelType* data_;
260  size_t pitch_;
262  bool ext_data_pointer_;
263 
264 private:
266  VolumeGpu(const VolumeGpu&);
268  VolumeGpu& operator=(const VolumeGpu&);
269 };
270 
271 } // namespace iu
272 
273 
const PixelType * data(int ox=0, int oy=0, int oz=0) const
Definition: volume_gpu.h:152
ImageGpu< PixelType, iuprivate::ImageAllocatorGpu< PixelType > > getSlice(int oz)
Definition: volume_gpu.h:163
PixelType pixel_type
Definition: volume_gpu.h:19
VolumeGpu()
Definition: volume_gpu.h:22
size_t slice_stride() const
Definition: volume_gpu.h:117
size_t bytes() const
Definition: volume_gpu.h:93
thrust::device_ptr< PixelType > end(void)
Definition: volume_gpu.h:180
unsigned int width() const
Definition: volume.h:81
VolumeGpu(unsigned int _width, unsigned int _height, unsigned int _depth)
Definition: volume_gpu.h:45
IUCORE_DLLAPI void copy(const LinearHostMemory_8u_C1 *src, LinearHostMemory_8u_C1 *dst)
int stride_
Definition: volume_gpu.h:228
VolumeGpu(const iu::Size< 3 > &size)
Definition: volume_gpu.h:55
size_t pitch() const
Definition: volume_gpu.h:99
virtual ~VolumeGpu()
Definition: volume_gpu.h:29
PixelType * data_
Definition: volume_gpu.h:220
Template specialization for 3-d unsigned int vectors (size vectors).
Definition: vector.h:605
unsigned int height() const
Definition: volume.h:89
Definition: image_cpu.h:7
unsigned int depth() const
Definition: volume.h:97
PixelType * data(int ox=0, int oy=0, int oz=0)
Definition: volume_gpu.h:141
__host__ KernelData(const VolumeGpu< PixelType, Allocator > &vol)
Definition: volume_gpu.h:242
int width_
Definition: volume_gpu.h:222
int height_
Definition: volume_gpu.h:224
thrust::device_ptr< PixelType > begin(void)
Definition: volume_gpu.h:172
Base class for 3D volumes (pitched memory).
Definition: volume.h:29
VolumeGpu(PixelType *_data, unsigned int _width, unsigned int _height, unsigned int _depth, size_t _pitch, bool ext_data_pointer=false)
Definition: volume_gpu.h:70
virtual bool onDevice() const
Definition: volume_gpu.h:129
Struct pointer KernelData that can be used in CUDA kernels.
Definition: volume_gpu.h:217
size_t stride() const
Definition: volume_gpu.h:111
__device__ PixelType & operator()(int x, int y, int z)
Definition: volume_gpu.h:236
Device 2D image class (pitched memory).
Definition: image_gpu.h:34
virtual unsigned int bitDepth() const
Definition: volume_gpu.h:123
ndarray_ref< PixelType, 3 > ref() const
iu::Size< 3 > size() const
Definition: volume.h:73
size_t slice_pitch() const
Definition: volume_gpu.h:105
int depth_
Definition: volume_gpu.h:226
Device 3D volume class (pitched memory).
Definition: volume_gpu.h:15