3d/datafield.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2015 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef __MIA_3DDATAFIELD_HH
22 #define __MIA_3DDATAFIELD_HH 1
23 
24 #include <cstdio>
25 #include <vector>
26 #include <cmath>
27 #include <cassert>
28 
29 #include <mia/3d/vector.hh>
30 #include <mia/3d/defines3d.hh>
31 #include <mia/3d/iterator.hh>
32 #include <mia/2d/datafield.hh>
33 #include <mia/core/msgstream.hh>
34 #include <mia/core/parameter.hh>
35 #include <mia/core/typedescr.hh>
36 #include <miaconfig.h>
37 
39 
40 
41 #define DECLARE_EXTERN_ITERATORS(TYPE) \
42  extern template class EXPORT_3D range3d_iterator<std::vector<TYPE>::iterator>; \
43  extern template class EXPORT_3D range3d_iterator<std::vector<TYPE>::const_iterator>; \
44  extern template class EXPORT_3D range3d_iterator_with_boundary_flag<std::vector<TYPE>::iterator>; \
45  extern template class EXPORT_3D range3d_iterator_with_boundary_flag<std::vector<TYPE>::const_iterator>; \
46  extern template class EXPORT_3D range2d_iterator<std::vector<TYPE>::iterator>; \
47  extern template class EXPORT_3D range2d_iterator<std::vector<TYPE>::const_iterator>;
48 
49 
52 DECLARE_EXTERN_ITERATORS(unsigned int);
55 DECLARE_EXTERN_ITERATORS(unsigned short);
56 DECLARE_EXTERN_ITERATORS(unsigned char );
57 DECLARE_EXTERN_ITERATORS(signed char);
59 
60 #ifdef LONG_64BIT
61 DECLARE_EXTERN_ITERATORS(signed long);
62 DECLARE_EXTERN_ITERATORS(unsigned long);
63 #endif
64 
67 
68 
73 template <class T>
75 
76  typedef ::std::vector<T> data_array;
77 
78  typedef std::shared_ptr<data_array> ref_data_type;
79 
81  C3DBounds m_size;
82 
84  size_t m_xy;
85 
87  ref_data_type m_data;
88 
90  static const T Zero;
91 
92  static const size_t m_elements;
93 
94 public:
95 
96 
99  void make_single_ref();
100 
105  bool holds_unique_data()const {
106  return m_data.unique();
107  }
108 
109 
111 
113  typedef typename std::vector<T>::iterator iterator;
114  typedef typename std::vector<T>::const_iterator const_iterator;
115  typedef typename std::vector<T>::const_reference const_reference;
116  typedef typename std::vector<T>::reference reference;
117  typedef typename std::vector<T>::const_pointer const_pointer;
118  typedef typename std::vector<T>::pointer pointer;
119  typedef typename std::vector<T>::value_type value_type;
120  typedef typename std::vector<T>::size_type size_type;
121  typedef typename std::vector<T>::difference_type difference_type;
122  typedef typename atomic_data<T>::type atomic_type;
123  typedef range3d_iterator<iterator> range_iterator;
124  typedef range3d_iterator<const_iterator> const_range_iterator;
125 
126  typedef range3d_iterator_with_boundary_flag<iterator> range_iterator_with_boundary_flag;
127  typedef range3d_iterator_with_boundary_flag<const_iterator> const_range_iterator_with_boundary_flag;
128 
129  typedef C3DBounds dimsize_type;
131 
141  class EXPORT_3D Range {
142  friend class T3DDatafield<T>;
143  friend class ConstRange;
144  public:
145 
147 
148  iterator begin();
149 
150  iterator end();
151 
152  private:
153  Range(const C3DBounds& start, const C3DBounds& end, T3DDatafield<T>& field);
154 
155  iterator m_begin;
156  iterator m_end;
157  };
158 
160  public:
161  friend class T3DDatafield<T>;
162 
164 
165  iterator begin() const;
166 
167  iterator end() const;
168 
169  private:
170  ConstRange(const C3DBounds& start, const C3DBounds& end, const T3DDatafield<T>& field);
171 
172  ConstRange(const Range& range);
173 
174  iterator m_begin;
175  iterator m_end;
176  };
177 
178 
179  T3DDatafield();
180 
182  T3DDatafield(const C3DBounds& _Size);
183 
188  T3DDatafield(const C3DBounds& size, const T *data);
189 
190 
195  T3DDatafield(const C3DBounds& size, const data_array& data);
196 
197 
199  T3DDatafield(const T3DDatafield<T>& org);
200 
202  virtual ~T3DDatafield();
203 
208  template <typename Out>
210 
212  template <typename Out>
213  T3DVector<Out> get_gradient(size_t x, size_t y, size_t z) const;
214 
216  template <typename Out>
217  T3DVector<Out> get_gradient(int index) const;
218 
220  value_type get_interpol_val_at(const T3DVector<float >& p) const;
221 
222  /* some rough interpolation using barycentric coordinates, needs less addition and
223  multiplications then tri-linear interp. but is usally of low quality
224  \remark this function may vanish
225  value_type get_barycent_interpol_val_at(const T3DVector<float >& p) const;
226  */
227 
229  value_type get_trilin_interpol_val_at(const T3DVector<float >& p) const;
230 
233  value_type get_block_avrg(const C3DBounds& Start, const C3DBounds& BlockSize) const;
234 
239  T3DDatafield& operator = (const T3DDatafield& org);
240 
242  const C3DBounds& get_size() const
243  {
244  return m_size;
245  }
246 
248  void clear();
249 
251  size_type size()const
252  {
253  return m_data->size();
254  }
255 
257  void swap(T3DDatafield& other);
258 
260  value_type get_avg();
261 
264  value_type strip_avg();
265 
266 
268  value_type operator()(const T3DVector<float >& pos)const;
269 
271  const_reference operator()(size_t x, size_t y, size_t z) const
272  {
273  // Look if we are inside, and give reference, else give the zero
274  if (x < m_size.x && y < m_size.y && z < m_size.z) {
275  const std::vector<T>& data = *m_data;
276  return data[x+ m_size.x * (y + m_size.y * z)];
277  }
278  return Zero;
279  }
280 
281 
283  const_reference operator()(const C3DBounds& l)const
284  {
285  return (*this)(l.x,l.y,l.z);
286  }
287 
289  reference operator()(size_t x, size_t y, size_t z)
290  {
291  // Look if we are inside, and give reference, else throw exception
292  // since write access is wanted
293  assert(x < m_size.x && y < m_size.y && z < m_size.z);
294  return (*m_data)[x + m_size.x *(y + m_size.y * z)];
295  }
296 
297 
298 
300  reference operator()(const C3DBounds& l)
301  {
302  return (*this)(l.x,l.y,l.z);
303  }
304 
306  void get_data_line_x(int y, int z, std::vector<T>& buffer)const;
307 
309  void get_data_line_y(int x, int z, std::vector<T>& buffer)const;
310 
312  void get_data_line_z(int x, int y, std::vector<T>& buffer)const;
313 
315  void put_data_line_x(int y, int z, const std::vector<T> &buffer);
316 
318  void put_data_line_y(int x, int z, const std::vector<T> &buffer);
319 
321  void put_data_line_z(int x, int y, const std::vector<T> &buffer);
322 
324  template <class TMask>
325  void mask(const TMask& m);
326 
340  void read_xslice_flat(size_t x, std::vector<atomic_type>& buffer) const;
341 
354  void read_yslice_flat(size_t y, std::vector<atomic_type>& buffer) const;
355 
368  void read_zslice_flat(size_t z, std::vector<atomic_type>& buffer) const;
369 
374  void write_zslice_flat(size_t z, const std::vector<atomic_type>& buffer);
375 
376 
381  void write_yslice_flat(size_t y, const std::vector<atomic_type>& buffer);
382 
387  void write_xslice_flat(size_t x, const std::vector<atomic_type>& buffer);
388 
394  T2DDatafield<T> get_data_plane_xy(size_t z)const;
395 
401  T2DDatafield<T> get_data_plane_yz(size_t x)const;
402 
408  T2DDatafield<T> get_data_plane_xz(size_t y)const;
409 
415  void put_data_plane_xy(size_t z, const T2DDatafield<T>& p);
416 
422  void put_data_plane_yz(size_t x, const T2DDatafield<T>& p);
423 
429  void put_data_plane_xz(size_t y, const T2DDatafield<T>& p);
430 
432  const_iterator begin()const
433  {
434  return m_data->begin();
435  }
436 
440  const_iterator begin_at(size_t x, size_t y, size_t z)const
441  {
442  return m_data->begin() + (z * m_size.y + y) * m_size.x + x;
443  }
444 
445 
449  const_iterator end()const
450  {
451  return m_data->end();
452  }
453 
457  iterator begin()
458  {
459  make_single_ref();
460  return m_data->begin();
461  }
462 
463  Range get_range(const C3DBounds& start, const C3DBounds& end);
464 
465  ConstRange get_range(const C3DBounds& start, const C3DBounds& end) const;
466 
469  range_iterator begin_range(const C3DBounds& begin, const C3DBounds& end);
470 
472  range_iterator end_range(const C3DBounds& begin, const C3DBounds& end);
473 
474 
477  const_range_iterator begin_range(const C3DBounds& begin, const C3DBounds& end)const;
478 
480  const_range_iterator end_range(const C3DBounds& begin, const C3DBounds& end)const;
481 
482 
484  range_iterator_with_boundary_flag begin_range_with_boundary_flags(const C3DBounds& begin, const C3DBounds& end);
485 
487  range_iterator_with_boundary_flag end_range_with_boundary_flags(const C3DBounds& begin, const C3DBounds& end);
488 
489 
491  const_range_iterator_with_boundary_flag begin_range_with_boundary_flags(const C3DBounds& begin, const C3DBounds& end)const;
492 
494  const_range_iterator_with_boundary_flag end_range_with_boundary_flags(const C3DBounds& begin, const C3DBounds& end)const;
495 
496 
505  iterator begin_at(size_t x, size_t y, size_t z)
506  {
507  make_single_ref();
508  return m_data->begin() + (z * m_size.y + y) * m_size.x + x;
509  }
510 
514  iterator end()
515  {
516  make_single_ref();
517  return m_data->end();
518  }
519 
521  const_reference operator[](int i)const
522  {
523  return (*m_data)[i];
524  }
525 
529  reference operator[](int i)
530  {
531  assert(m_data.unique());
532  return (*m_data)[i];
533  }
534 
535 
537  size_t get_plane_size_xy()const
538  {
539  return m_xy;
540  };
541 
542 private:
543 };
544 
547 
550 
553 
554 
557 
560 
563 
566 
569 
572 
574 
576 DECLARE_TYPE_DESCR(C3DBounds);
577 DECLARE_TYPE_DESCR(C3DFVector);
579 
580 // some implementations
581 
582 template <class T>
583 template <typename Out>
584 T3DVector<Out> T3DDatafield<T>::get_gradient(size_t x, size_t y, size_t z) const
585 {
586  const std::vector<T>& data = *m_data;
587  const int sizex = m_size.x;
588  // Look if we are inside the used space
589  if (x - 1 < m_size.x - 2 && y - 1 < m_size.y - 2 && z - 1 < m_size.z - 2) {
590 
591  // Lookup all neccessary Values
592  const T *H = &data[x + m_size.x * (y + m_size.y * z)];
593 
594  return T3DVector<Out> (Out((H[1] - H[-1]) * 0.5),
595  Out((H[sizex] - H[-sizex]) * 0.5),
596  Out((H[m_xy] - H[-m_xy]) * 0.5));
597  }
598 
599  return T3DVector<Out>();
600 }
601 
602 
603 template <class T>
604 template <typename Out>
606 {
607  const int sizex = m_size.x;
608  // Lookup all neccessary Values
609  const T *H = &(*m_data)[hardcode];
610 
611  return T3DVector<Out> (Out((H[1] - H[-1]) * 0.5),
612  Out((H[sizex] - H[-sizex]) * 0.5),
613  Out((H[m_xy] - H[-m_xy]) * 0.5));
614 }
615 
616 
620 template <>
621 template <typename Out>
623 {
624 
625  // Lookup all neccessary Values
626  return T3DVector<Out> (Out(((*m_data)[hardcode + 1] - (*m_data)[hardcode -1]) * 0.5),
627  Out(((*m_data)[hardcode + m_size.x] - (*m_data)[hardcode -m_size.x]) * 0.5),
628  Out(((*m_data)[hardcode + m_xy] - (*m_data)[hardcode -m_xy]) * 0.5));
629 }
630 
631 template <class T>
632 template <typename Out>
634 {
635  // This will become really funny
636  const int sizex = m_size.x;
637  const std::vector<T>& data = *m_data;
638  // Calculate the int coordinates near the POI
639  // and the distances
640  size_t x = size_t (p.x);
641  float dx = p.x - x;
642  float xm = 1 - dx;
643  size_t y = size_t (p.y);
644  float dy = p.y - y;
645  float ym = 1 - dy;
646  size_t z = size_t (p.z);
647  float dz = p.z - z;
648  float zm = 1 - dz;
649 
650  // Look if we are inside the used space
651  if (x-1 < m_size.x-3 && y -1 < m_size.y-3 && z - 1 < m_size.z-3 ) {
652  // Lookup all neccessary Values
653  const T *H000 = &data[x + sizex * y + m_xy * z];
654 
655  const T* H_100 = &H000[-m_xy];
656  const T* H_101 = &H_100[1];
657  const T* H_110 = &H_100[sizex];
658  const T* H_111 = &H_110[1];
659 
660  const T* H0_10 = &H000[-sizex];
661  const T* H0_11 = &H0_10[1];
662 
663  const T* H00_1 = &H000[-1];
664  const T* H001 = &H000[ 1];
665  const T* H002 = &H000[ 2];
666 
667 
668  const T* H010 = &H000[sizex];
669  const T* H011 = &H010[ 1];
670  const T* H012 = &H010[ 2];
671  const T* H01_1 = &H010[-1];
672 
673  const T* H020 = &H010[sizex];
674  const T* H021 = &H020[ 1];
675 
676  const T* H100 = &H000[m_xy];
677 
678  const T* H1_10 = &H100[sizex];
679  const T* H1_11 = &H1_10[1];
680 
681  const T* H10_1 = &H100[-1];
682  const T* H101 = &H100[ 1];
683  const T* H102 = &H100[ 2];
684 
685  const T* H110 = &H100[sizex];
686  const T* H111 = &H110[ 1];
687  const T* H112 = &H110[ 2];
688  const T* H11_1 = &H110[-1];
689 
690  const T* H120 = &H110[sizex];
691  const T* H121 = &H120[ 1];
692 
693  const T* H200 = &H100[m_xy];
694  const T* H201 = &H200[1];
695  const T* H210 = &H200[sizex];
696  const T* H211 = &H210[1];
697 
698  // use trilinear interpolation to calc the gradient
699  return T3DVector<Out> (
700  Out((zm * (ym * (dx * (*H002 - *H000) + xm * (*H001 - *H00_1))+
701  dy * (dx * (*H012 - *H010) + xm * (*H011 - *H01_1)))+
702  dz * (ym * (dx * (*H102 - *H100) + xm * (*H101 - *H10_1))+
703  dy * (dx * (*H112 - *H110) + xm * (*H111 - *H11_1)))) * 0.5),
704 
705  Out((zm * (ym * (xm * (*H010 - *H0_10) + dx * (*H011 - *H0_11))+
706  dy * (xm * (*H020 - *H000) + dx * (*H021 - *H001)))+
707  dz * (ym * (xm * (*H110 - *H1_10) + dx * (*H111 - *H1_11))+
708  dy * (xm * (*H120 - *H100) + dx * (*H121 - *H101)))) * 0.5),
709  Out((zm * (ym * (xm * (*H100 - *H_100) + dx * (*H101 - *H_101))+
710  dy * (xm * (*H110 - *H_110) + dx * (*H111 - *H_111)))+
711  dz * (ym * (xm * (*H200 - *H000) + dx * (*H201 - *H001))+
712  dy * (xm * (*H210 - *H010) + dx * (*H211 - *H011)))) * 0.5));
713  }
714  return T3DVector<Out>();
715 
716 }
717 
718 #ifdef __GNUC__
719 #pragma GCC diagnostic push
720 #ifndef __clang__
721 #pragma GCC diagnostic ignored "-Wattributes"
722 #endif
723 #endif
724 
725 #define DECLARE_EXTERN(TYPE) \
726  extern template class EXPORT_3D T3DDatafield<TYPE>;
727 
728 
729 DECLARE_EXTERN(double);
730 DECLARE_EXTERN(float);
731 DECLARE_EXTERN(unsigned int);
732 DECLARE_EXTERN(int);
733 DECLARE_EXTERN(short);
734 DECLARE_EXTERN(unsigned short);
735 DECLARE_EXTERN(unsigned char );
736 DECLARE_EXTERN(signed char);
737 
738 #ifdef LONG_64BIT
739 DECLARE_EXTERN(signed long);
740 DECLARE_EXTERN(unsigned long);
741 #endif
742 
745 
746 #undef DECLARE_EXTERN
747 
748 #ifdef __GNUC__
749 #pragma GCC diagnostic pop
750 #endif
751 
753 
754 #endif
T2DDatafield< T > get_data_plane_yz(size_t x) const
T3DDatafield< T >::range_iterator iterator
void put_data_plane_xy(size_t z, const T2DDatafield< T > &p)
range_iterator begin_range(const C3DBounds &begin, const C3DBounds &end)
T3DVector< Out > get_gradient(const T3DVector< float > &p) const
T2DDatafield< T > get_data_plane_xz(size_t y) const
a 3D iterator that knows its position in the 3D grid ans supports iterating over sub-ranges ...
Definition: 3d/iterator.hh:189
void swap(T3DDatafield &other)
swap the data ofthis 3DDatafield with another one
Generic string vs. attribute translator singleton.
Definition: attributes.hh:507
void write_yslice_flat(size_t y, const std::vector< atomic_type > &buffer)
void make_single_ref()
void put_data_line_y(int x, int z, const std::vector< T > &buffer)
range_iterator_with_boundary_flag begin_range_with_boundary_flags(const C3DBounds &begin, const C3DBounds &end)
T z
vector element
Definition: 3d/vector.hh:55
a 3D iterator that knows its position in the 3D grid, has a flag indicating whether it is on a bounda...
Definition: 3d/iterator.hh:43
value_type get_trilin_interpol_val_at(const T3DVector< float > &p) const
A templated class of a 3D data field.
Definition: 3d/datafield.hh:74
iterator begin_at(size_t x, size_t y, size_t z)
const_iterator begin_at(size_t x, size_t y, size_t z) const
void read_zslice_flat(size_t z, std::vector< atomic_type > &buffer) const
T3DDatafield< unsigned char > C3DUBDatafield
a data field of float values
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:43
size_type size() const
T3DDatafield< bool > C3DBitDatafield
a data field of float values
#define EXPORT_3D
Definition: defines3d.hh:44
reference operator[](int i)
CTParameter< C3DBounds > C3DBoundsParameter
3D size parameter type
void read_yslice_flat(size_t y, std::vector< atomic_type > &buffer) const
const_reference operator()(const C3DBounds &l) const
value_type strip_avg()
TTranslator< C3DFVector > C3DFVectorTranslator
Range get_range(const C3DBounds &start, const C3DBounds &end)
T3DDatafield< unsigned int > C3DUIDatafield
a data field of 32 bit unsigned int values
void get_data_line_z(int x, int y, std::vector< T > &buffer) const
Generic type of a complex paramter.
Definition: parameter.hh:163
const_reference operator[](int i) const
T3DDatafield< long > C3DLDatafield
a data field of 32 bit signed int values
EXPORT_2D C2DFVectorfield get_gradient(const C2DImage &image)
T y
vector element
Definition: 3d/vector.hh:53
void write_xslice_flat(size_t x, const std::vector< atomic_type > &buffer)
T3DDatafield< T >::const_range_iterator iterator
CTParameter< C3DFVector > C3DFVectorParameter
3D vector parameter type
const C3DBounds & get_size() const
#define DECLARE_EXTERN(TYPE)
size_t get_plane_size_xy() const
const_reference operator()(size_t x, size_t y, size_t z) const
A class to hold data on a regular 2D grid.
Definition: 2d/datafield.hh:53
T2DDatafield< T > get_data_plane_xy(size_t z) const
void put_data_line_z(int x, int y, const std::vector< T > &buffer)
void write_zslice_flat(size_t z, const std::vector< atomic_type > &buffer)
const_iterator end() const
#define DECLARE_EXTERN_ITERATORS(TYPE)
Definition: 3d/datafield.hh:41
T3DDatafield & operator=(const T3DDatafield &org)
void read_xslice_flat(size_t x, std::vector< atomic_type > &buffer) const
void put_data_plane_xz(size_t y, const T2DDatafield< T > &p)
range_iterator_with_boundary_flag end_range_with_boundary_flags(const C3DBounds &begin, const C3DBounds &end)
void mask(const TMask &m)
a shortcut data type
iterator begin()
T3DDatafield< int > C3DIDatafield
a data field of 32 bit signed int values
virtual ~T3DDatafield()
make sure the destructor is virtual
value_type get_avg()
range_iterator end_range(const C3DBounds &begin, const C3DBounds &end)
value_type get_interpol_val_at(const T3DVector< float > &p) const
void get_data_line_x(int y, int z, std::vector< T > &buffer) const
reference operator()(size_t x, size_t y, size_t z)
value_type operator()(const T3DVector< float > &pos) const
iterator end()
void put_data_plane_yz(size_t x, const T2DDatafield< T > &p)
void get_data_line_y(int x, int z, std::vector< T > &buffer) const
value_type get_block_avrg(const C3DBounds &Start, const C3DBounds &BlockSize) const
reference operator()(const C3DBounds &l)
const_iterator begin() const
T3DDatafield< unsigned long > C3DULDatafield
a data field of 32 bit unsigned int values
void put_data_line_x(int y, int z, const std::vector< T > &buffer)
T3DDatafield< float > C3DFDatafield
a data field of float values
T x
vector element
Definition: 3d/vector.hh:51
bool holds_unique_data() const
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:46