cgv
Loading...
Searching...
No Matches
data_view.h
1#pragma once
2
3#include <cgv/type/info/type_ptr.h>
4#include <cgv/type/func/transfer_const.h>
5#include <cgv/type/func/drop_pointer.h>
6#include <cgv/data/data_format.h>
7
8#include "lib_begin.h"
9
10namespace cgv {
11 namespace data {
12
15class CGV_API data_view_base
16{
17protected:
18 const data_format* format;
21 unsigned int dim;
22 size_t step_sizes[4];
24 data_view_base(const data_format* _format, unsigned int _dim, const size_t* _step_sizes);
25public:
29 data_view_base(const data_format* _format = 0);
31 virtual ~data_view_base();
33 void manage_format(bool enable = true);
35 const data_format* get_format() const;
37 void set_format(const data_format* _format);
39 unsigned int get_dim() const;
41 size_t get_step_size(unsigned int dim) const;
42};
43
46template <class D, typename P = unsigned char*>
47class CGV_API data_view_impl : public data_view_base
48{
49protected:
53 data_view_impl(const data_format* _format, P _data_ptr, unsigned _dim, const size_t* _step_sizes);
54public:
56 data_view_impl(const data_format* _format = 0, typename cgv::type::func::transfer_const<P,void*>::type _data_ptr = 0);
58 bool empty() const;
60 template <typename S>
61 typename cgv::type::func::transfer_const<P,S*>::type get_ptr() const {
62 return (typename cgv::type::func::transfer_const<P,S*>::type)(data_ptr);
63 }
65 template <typename S>
66 typename cgv::type::func::transfer_const<P, S*>::type get_ptr(size_t i) const {
67 return (typename cgv::type::func::transfer_const<P, S*>::type)(data_ptr + i*step_sizes[0]);
68 }
70 template <typename S>
71 typename cgv::type::func::transfer_const<P, S*>::type get_ptr(size_t i, size_t j) const {
72 return (typename cgv::type::func::transfer_const<P, S*>::type)(data_ptr + i*step_sizes[0] + j*step_sizes[1]);
73 }
75 template <typename S>
76 typename cgv::type::func::transfer_const<P, S*>::type get_ptr(size_t i, size_t j, size_t k) const {
77 return (typename cgv::type::func::transfer_const<P, S*>::type)(data_ptr + i*step_sizes[0] + j*step_sizes[1] + k*step_sizes[2]);
78 }
80 template <typename S>
81 typename cgv::type::func::transfer_const<P, S*>::type get_ptr(size_t i, size_t j, size_t k, size_t l) const {
82 return (typename cgv::type::func::transfer_const<P, S*>::type)(data_ptr + i*step_sizes[0] + j*step_sizes[1] + k*step_sizes[2] + l*step_sizes[3]);
83 }
85 template <typename S>
86 S get(unsigned ci) const {
87 return format->get<S>(ci, data_ptr);
88 }
90 template <typename S> S get(unsigned ci, size_t i) const {
91 return format->get<S>(ci, get_ptr<typename cgv::type::func::drop_pointer<P>::type>(i));
92 }
94 template <typename S> S get(unsigned ci, size_t i, size_t j) const {
95 return format->get<S>(ci, get_ptr<typename cgv::type::func::drop_pointer<P>::type>(i, j));
96 }
98 template <typename S> S get(unsigned ci, size_t i, size_t j, size_t k) const {
99 return format->get<S>(ci, get_ptr<typename cgv::type::func::drop_pointer<P>::type>(i, j, k));
100 }
102 template <typename S> S get(unsigned ci, size_t i, size_t j, size_t k, size_t l) const {
103 return format->get<S>(ci, get_ptr<typename cgv::type::func::drop_pointer<P>::type>(i, j, k, l));
104 }
106 D operator () (size_t i) const;
108 D operator () (size_t i, size_t j) const;
110 D operator () (size_t i, size_t j, size_t k) const;
112 D operator () (size_t i, size_t j, size_t k, size_t l) const;
113
119 D permute(const std::string& permutation) const;
121 D transpose() const { return permute("ji"); }
122
124 template <typename S>
125 typename cgv::type::func::transfer_const<P,S*>::type
126 step_i(S* ptr, std::ptrdiff_t n=1) const { return static_cast<typename cgv::type::func::transfer_const<P,S*>::type>(static_cast<P>(ptr)+n*step_sizes[0]); }
128 template <typename S>
129 typename cgv::type::func::transfer_const<P,S*>::type
130 step_j(S* ptr, std::ptrdiff_t n=1) const { return static_cast<typename cgv::type::func::transfer_const<P,S*>::type>(static_cast<P>(ptr)+n*step_sizes[1]); }
132 template <typename S>
133 typename cgv::type::func::transfer_const<P,S*>::type
134 step_k(S* ptr, std::ptrdiff_t n=1) const { return static_cast<typename cgv::type::func::transfer_const<P,S*>::type>(static_cast<P>(ptr)+n*step_sizes[2]); }
136 template <typename S>
137 typename cgv::type::func::transfer_const<P,S*>::type
138 step_l(S* ptr, std::ptrdiff_t n=1) const { return static_cast<typename cgv::type::func::transfer_const<P,S*>::type>(static_cast<P>(ptr)+n*step_sizes[3]); }
139};
140
141class CGV_API const_data_view;
142
152class CGV_API data_view : public data_view_impl<data_view, unsigned char*>
153{
154protected:
156 friend class data_view_impl<data_view, unsigned char*>;
157 friend class const_data_view;
161 data_view(const data_format* _format, unsigned char* _data_ptr,
162 unsigned int _dim, const size_t* _step_sizes);
163public:
165 data_view();
167 ~data_view();
172 data_view(const data_format* _format);
175 data_view(const data_format* _format, void* _data_ptr);
180 data_view(const data_format* _format, unsigned char* _data_ptr, bool manage_ptr);
184 data_view(const data_view& other) : data_view(other.format) {
185 const cgv::type::uint8_type* src_ptr = other.get_ptr<cgv::type::uint8_type>();
186 cgv::type::uint8_type* dst_ptr = get_ptr<cgv::type::uint8_type>();
187 memcpy(dst_ptr, src_ptr, other.format->get_nr_bytes());
188 }
191 data_view& operator = (const data_view& dv);
194 return data_view(*this);
195 }
199 void set_ptr(unsigned char* ptr, bool manage_ptr);
202 void set_ptr(void* ptr);
204 template <typename T>
205 bool set(int ci, const T& v) {
206 return format->set(ci, data_ptr, v);
207 }
209 void reflect_horizontally();
211 static bool compose(data_view& composed_dv, const std::vector<data_view>& dvs);
213 static bool combine_components(data_view& dv, const std::vector<data_view>::iterator first, const std::vector<data_view>::iterator last);
214};
215
220class CGV_API const_data_view : public data_view_impl<const_data_view, const unsigned char*>
221{
222protected:
224 friend class data_view_impl<const_data_view, const unsigned char*>;
226 const_data_view(const data_format* _format, const unsigned char* _data_ptr,
227 unsigned _dim, const size_t* _step_sizes);
228public:
232 const_data_view(const data_view& dv);
235 const_data_view(const data_format* _format, const void* _data_ptr);
237 const_data_view& operator = (const const_data_view& dv);
239 void set_ptr(const void* ptr);
240};
241
242 }
243}
244
245#include <cgv/config/lib_end.h>
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
Definition data_view.h:221
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
size_t get_nr_bytes() const
return the total number of bytes necessary to store the data
base class of both implementations of the data view managing the component format,...
Definition data_view.h:16
data_view_base(const data_format *_format, unsigned int _dim, const size_t *_step_sizes)
constructor used to construct sub views onto the data view
bool owns_format
whether to own the data format
Definition data_view.h:20
template class implementing the part of the view that depends on whether the pointer is const or not ...
Definition data_view.h:48
S get(unsigned ci) const
constant access to the ci-th component
Definition data_view.h:86
P data_ptr
data pointer of type unsigned char or const unsigned char
Definition data_view.h:51
cgv::type::func::transfer_const< P, S * >::type get_ptr(size_t i) const
return a pointer to type S for i-th data entry
Definition data_view.h:66
S get(unsigned ci, size_t i, size_t j, size_t k, size_t l) const
constant access to the ci-th component of (i,j,k,l)-th data entry
Definition data_view.h:102
S get(unsigned ci, size_t i, size_t j, size_t k) const
constant access to the ci-th component of (i,j,k)-th data entry
Definition data_view.h:98
cgv::type::func::transfer_const< P, S * >::type get_ptr(size_t i, size_t j) const
return a pointer to type S for (i,j)-th data entry
Definition data_view.h:71
cgv::type::func::transfer_const< P, S * >::type step_j(S *ptr, std::ptrdiff_t n=1) const
return a pointer that points to the n-th next location if index j is increase by n
Definition data_view.h:130
D transpose() const
transpose is equivalent to permute("ji")
Definition data_view.h:121
S get(unsigned ci, size_t i, size_t j) const
constant access to the ci-th component of (i,j)-th data entry
Definition data_view.h:94
cgv::type::func::transfer_const< P, S * >::type get_ptr() const
return a data pointer to type S
Definition data_view.h:61
cgv::type::func::transfer_const< P, S * >::type get_ptr(size_t i, size_t j, size_t k, size_t l) const
return a pointer to type S for (i,j,k,l)-th data entry
Definition data_view.h:81
cgv::type::func::transfer_const< P, S * >::type step_i(S *ptr, std::ptrdiff_t n=1) const
return a pointer that points to the n-th next location if index i is increase by n
Definition data_view.h:126
cgv::type::func::transfer_const< P, S * >::type step_k(S *ptr, std::ptrdiff_t n=1) const
return a pointer that points to the n-th next location if index k is increase by n
Definition data_view.h:134
cgv::type::func::transfer_const< P, S * >::type get_ptr(size_t i, size_t j, size_t k) const
return a pointer to type S for (i,j,k)-th data entry
Definition data_view.h:76
S get(unsigned ci, size_t i) const
constant access to the ci-th component of i-th data entry
Definition data_view.h:90
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
bool owns_ptr
a flag telling whether the data ptr is owned by the view
Definition data_view.h:159
data_view copy()
return a copy of this data_view and tha data via the copy constructor
Definition data_view.h:193
data_view(const data_format *_format, unsigned char *_data_ptr, unsigned int _dim, const size_t *_step_sizes)
use base class for construction and don't manage data pointer
data_view(const data_view &other)
construct a data view as a copy of the given data view.
Definition data_view.h:184
bool set(int ci, const T &v)
write access to the i-th component, return whether write was successful
Definition data_view.h:205
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
the cgv namespace
Definition print.h:11