cgv
Loading...
Searching...
No Matches
data_format.cxx
1#include "data_format.h"
2#include <iostream>
3#include <cgv/utils/tokenizer.h>
4#include <cgv/utils/scan.h>
5
6using namespace cgv::utils;
7using namespace cgv::type::info;
8
9namespace cgv {
10 namespace data {
14data_format::data_format(const std::string& description)
15{
16 set_data_format(description);
17}
18bool data_format::set_data_format(const std::string& description)
19{
20 if (description.empty()) {
21 dimensions.clear();
23 return true;
24 }
25 last_error = "";
26 size_t p = description.find_first_of(']');
27 if (p == std::string::npos) {
28 last_error = "no <]> found in description";
29 return false;
30 }
31 if (!component_format::set_component_format(description.substr(0,p+1)))
32 return false;
33 std::string sub_descr = description.substr(p+1);
34 std::vector<token> toks;
35 tokenizer(sub_descr).set_sep("():|,").bite_all(toks);
36 if (toks.size() < 3) {
37 last_error = "incomplete format description";
38 return false;
39 }
40 if (to_string(toks.back()) != ")") {
41 last_error = "format description not terminated by <)>";
42 return false;
43 }
44 unsigned i=0;
45 unsigned di = 0;
46 int last_a = 1;
47 if (to_string(toks[i]) == "|") {
48 if (!is_integer(to_string(toks[1]), last_a)) {
49 last_error = "expected integer after <|>";
50 return false;
51 }
52 i += 2;
53 }
54 if (to_string(toks[i]) != "(") {
55 last_error = "expected <(> to define dimensions";
56 return false;
57 }
58 if (++i == toks.size()) {
59 last_error = "incomplete format description";
60 return false;
61 }
62 dimensions.clear();
63 do {
64 int n, ld = di, a = 1;
65 if (!is_integer(to_string(toks[i]),n)) {
66 last_error = "expected integer after <(> or <,>";
67 return false;
68 }
69 ++i;
70 if (i+1 < toks.size()) {
71 if (to_string(toks[i]) == ":") {
72 if (!is_integer(to_string(toks[i+1]),ld)) {
73 last_error = "expected integer after <:>";
74 return false;
75 }
76 i += 2;
77 }
78 }
79 if (i+1 < toks.size()) {
80 if (to_string(toks[i]) == "|") {
81 if (!is_integer(to_string(toks[i+1]),a)) {
82 last_error = "expected integer after <:>";
83 return false;
84 }
85 i += 2;
86 }
87 }
88 dimensions.push_back(dimension_info(n,last_a,ld));
89 last_a = a;
90 if (to_string(toks[i]) == ")")
91 return true;
92 if (to_string(toks[i]) != ",") {
93 last_error = "expected <,> after definition of dimension";
94 return false;
95 }
96 ++i;
97 ++di;
98 } while (true);
99 return true;
100}
101data_format::data_format(size_t _width, TypeId _ct, const std::string& _cnl,
102 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
103 : component_format(_ct,_cnl,a,d0,d1,d2,d3)
104{
105 dimensions.push_back(dimension_info(_width));
106}
108 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
109 : component_format(_ct,_cf,a,d0,d1,d2,d3)
110{
111 dimensions.push_back(dimension_info(_width));
112}
113data_format::data_format(size_t _width, size_t _height, TypeId _ct, const std::string& _cnl,
114 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
115 : component_format(_ct,_cnl,a,d0,d1,d2,d3)
116{
117 dimensions.push_back(dimension_info(_width));
118 dimensions.push_back(dimension_info(_height,1,1));
119}
120data_format::data_format(size_t _width, size_t _height, TypeId _ct, ComponentFormat _cf,
121 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
122 : component_format(_ct,_cf,a,d0,d1,d2,d3)
123{
124 dimensions.push_back(dimension_info(_width));
125 dimensions.push_back(dimension_info(_height,1,1));
126}
127
128data_format::data_format(size_t _width, size_t _height, size_t _depth, TypeId _ct, const std::string& _cnl,
129 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
130 : component_format(_ct,_cnl,a,d0,d1,d2,d3)
131{
132 dimensions.push_back(dimension_info(_width));
133 dimensions.push_back(dimension_info(_height,1,1));
134 dimensions.push_back(dimension_info(_depth,1,2));
135}
136data_format::data_format(size_t _width, size_t _height, size_t _depth, TypeId _ct, ComponentFormat _cf,
137 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
138 : component_format(_ct,_cf,a,d0,d1,d2,d3)
139{
140 dimensions.push_back(dimension_info(_width));
141 dimensions.push_back(dimension_info(_height,1,1));
142 dimensions.push_back(dimension_info(_depth,1,2));
143}
144data_format::data_format(size_t _width, size_t _height, size_t _depth, size_t _count, TypeId _ct, const std::string& _cnl,
145 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
146 : component_format(_ct, _cnl, a, d0, d1, d2, d3)
147{
148 dimensions.push_back(dimension_info(_width));
149 dimensions.push_back(dimension_info(_height,1,1));
150 dimensions.push_back(dimension_info(_depth,1,2));
151 dimensions.push_back(dimension_info(_count,1,3));
152}
153data_format::data_format(size_t _width, size_t _height, size_t _depth, size_t _count, TypeId _ct, ComponentFormat _cf,
154 unsigned a, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
155 : component_format(_ct,_cf,a,d0,d1,d2,d3)
156{
157 dimensions.push_back(dimension_info(_width));
158 dimensions.push_back(dimension_info(_height,1,1));
159 dimensions.push_back(dimension_info(_depth,1,2));
160 dimensions.push_back(dimension_info(_count,1,3));
161}
162void data_format::set_dimensions(size_t _d0, size_t _d1, size_t _d2, size_t _d3)
163{
164 set_width(_d0);
165 if (_d1 != -1)
166 set_height(_d1);
167 if (_d2 != -1)
168 set_depth(_d2);
169 if (_d3 != -1)
171}
173{
174 return (unsigned) dimensions.size();
175}
177{
178 dimensions.resize(_d);
179}
180size_t data_format::get_resolution(unsigned i) const
181{
182 if (i >= get_nr_dimensions())
183 return 0;
184 return dimensions[i].resolution;
185}
186void data_format::set_resolution(unsigned i, size_t resolution)
187{
188 if (i >= get_nr_dimensions())
189 dimensions.resize(i+1);
190 dimensions[i].resolution = resolution;
191}
192unsigned data_format::get_layout_dimension(unsigned i) const
193{
194 if (i >= get_nr_dimensions())
195 return i;
196 return dimensions[i].layout_dimension;
197}
198void data_format::set_layout_dimension(unsigned i, unsigned layout_dim)
199{
200 if (i >= get_nr_dimensions())
201 dimensions.resize(i+1);
202 dimensions[i].layout_dimension = layout_dim;
203}
205{
206 size_t size = 1;
207 for (unsigned int i=0; i<get_nr_dimensions(); ++i)
208 size *= get_resolution(i);
209 return size;
210}
212{
214}
216{
217 return get_resolution(0);
218}
220{
221 return get_resolution(1);
222}
224{
225 return get_resolution(2);
226}
228{
229 if (get_nr_dimensions() == 0)
230 return 1;
232}
234{
235 if (get_nr_dimensions() == 0)
236 return 1;
237 return dimensions[0].alignment;
238}
239unsigned data_format::get_alignment(unsigned i) const
240{
241 if (i+1 >= get_nr_dimensions())
242 return 1;
243 return dimensions[i+1].alignment;
244}
245void data_format::set_width (size_t _width)
246{
247 set_resolution(0, _width);
248}
249void data_format::set_height(size_t _height)
250{
251 set_resolution(1, _height);
252}
253void data_format::set_depth (size_t _depth)
254{
255 set_resolution(2, _depth);
256}
257void data_format::set_nr_time_steps(size_t _nr_time_steps)
258{
259 if (get_nr_dimensions() == 0)
260 return;
261 set_resolution(get_nr_dimensions()-1, _nr_time_steps);
262}
264{
265 if (get_nr_dimensions() == 0)
266 dimensions.resize(1);
267 dimensions[0].alignment = _a;
268}
269void data_format::set_alignment(unsigned i, unsigned _a)
270{
271 if (i >= get_nr_dimensions())
272 dimensions.resize(i+1);
273 if (i+1 >= get_nr_dimensions())
274 return;
275 dimensions[i+1].alignment = _a;
276}
278{
279 return *static_cast<const component_format*>(this);
280}
282{
283 *static_cast<component_format*>(this) = cf;
284}
286{
288 return false;
289 for (unsigned int i=0; i<get_nr_dimensions(); ++i) {
290 if (get_resolution(i) != df.get_resolution(i))
291 return false;
292 if (get_alignment(i) != df.get_alignment(i))
293 return false;
294 }
296}
298{
299 return !(*this == df);
300}
301std::ostream& operator << (std::ostream& os, const data_format& df)
302{
303 os << df.get_component_format();
304 if (df.get_nr_dimensions() > 0 && df.get_entry_alignment() != 1)
305 os << '|' << df.get_entry_alignment();
306 os << "(";
307 for (unsigned int i=0; i<df.get_nr_dimensions(); ++i) {
308 if (i > 0)
309 os << ',';
310 os << df.get_resolution(i);
311 if (df.get_layout_dimension(i) != i)
312 os << ':' << df.get_layout_dimension(i);
313 if (df.get_alignment(i) != 1)
314 os << '|' << df.get_alignment(i);
315 }
316 return os << ')';
317}
318 }
319}
the component format inherits the information of a packing_info and adds information on the component...
bool set_component_format(const std::string &description)
set component format from description string, which has the following syntax.
void clear()
clear the component format
static std::string last_error
store the last error that appeared during parsing of a description
unsigned int get_entry_size() const
return the size of one entry of components in bytes
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
unsigned get_nr_dimensions() const
return the number of dimensions of the data set
void set_layout_dimension(unsigned dim, unsigned layout_dim)
set the layout dimension of a given dimension, add dimensions if necessary
size_t get_depth() const
return the resolution in the third dimension, or 1 if not defined
void set_component_format(const component_format &cf)
set component_format by simply assigning to a converted this pointer
void set_nr_time_steps(size_t _nr_time_steps)
set the resolution in the last dimension, add dimensions if necessary
void set_resolution(unsigned i, size_t resolution)
set the resolution in the i-th dimension, add dimensions if necessary
bool operator!=(const data_format &df) const
comparison between component formats
void set_height(size_t _height)
set the resolution in the second dimension, add dimensions if necessary
std::vector< dimension_info > dimensions
store for each dimension resolution and alignment in a dimension_info struct
Definition data_format.h:28
unsigned get_alignment(unsigned i) const
return the alignment of a given dimension, where the alignment of the last dimension is always 1 and ...
void set_width(size_t _width)
set the resolution in the first dimension, add dimensions if necessary
size_t get_nr_time_steps() const
return the resolution in the highest dimension, or 1 if not defined
void set_entry_alignment(unsigned _a)
set the alignment of entries
unsigned get_layout_dimension(unsigned dim) const
return the layout dimension of a given dimension
size_t get_width() const
return the resolution in the first dimension, or 1 if not defined
unsigned get_entry_alignment() const
return the alignment of entries
void set_depth(size_t _depth)
set the resolution in the third dimension, add dimensions if necessary
size_t get_height() const
return the resolution in the second dimension, or 1 if not defined
bool operator==(const data_format &df) const
comparison between component formats
void set_nr_dimensions(unsigned _d)
set the number of dimensions of the data set
void set_alignment(unsigned i, unsigned _a)
set the alignment of a given dimension, add dimensions if necessary.
size_t get_nr_bytes() const
return the total number of bytes necessary to store the data
const component_format & get_component_format() const
return the component_format info by simple conversion of the this pointer
data_format()
construct an undefined data format
void set_dimensions(size_t _d0, size_t _d1=-1, size_t _d2=-1, size_t _d3=-1)
set the dimensions to the given values
size_t get_nr_entries() const
return the total number of data entries
bool set_data_format(const std::string &description)
Set data format from description string, which adds information to the description string of the comp...
size_t get_resolution(unsigned i) const
return the resolution in the i-th dimension, or 0 if not defined
the tokenizer allows to split text into tokens in a convenient way.
Definition tokenizer.h:68
tokenizer & set_sep(const std::string &sep, bool merge)
set the list of separators and specify whether succeeding separators are merged into single tokens
Definition tokenizer.cxx:59
ComponentFormat
define standard formats, which should be used to avoid wrong assignment of component names
std::ostream & operator<<(std::ostream &os, const component_format &cf)
stream out operator writes the component format in the syntax of description strings as defined in th...
namespace for templates that provide type information
Definition type_access.h:9
TypeId
ids for the different types and type constructs
Definition type_id.h:12
namespace that holds tools that dont fit any other namespace
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
Definition scan.cxx:367
the cgv namespace
Definition print.h:11
Helper functions to process strings.