cgv
component_format.h
1 #pragma once
2 
3 #include <cgv/type/info/type_id.h>
4 #include <cgv/type/info/type_access.h>
5 #include "packing_info.h"
6 #include <string>
7 #include <vector>
8 
9 #include "lib_begin.h"
10 
11 using namespace cgv::type::info;
12 
13 namespace cgv {
14  namespace data {
15 
19  CF_UNDEF,
20  CF_R,
21  CF_G,
22  CF_B,
23  CF_A,
24  CF_L,
25  CF_I,
33  CF_D,
34  CF_S,
43  CF_LAST
44 };
45 
48 {
49  CII_DEFAULT, // default of used api such as opengl
50  CII_SNORM, // map signed integers to [-1,1]
51  CII_INTEGER // keep integer values even if api would map them to [0,1]
52 };
53 
57 class CGV_API component_format : public packing_info
58 {
59 protected:
65  std::string component_string;
67  std::vector<unsigned short> component_positions;
69  void extract_components();
71  static std::string last_error;
72 public:
74  explicit component_format(const std::string& description);
115  bool set_component_format(const std::string& description);
117  void set_components(const std::string& _components);
119  static const std::string& get_last_error();
123  component_format(TypeId _component_type = TI_UNDEF,
124  const std::string& _component_name_list = "",
125  unsigned int align = 1,
126  unsigned int d0 = 0, unsigned int d1 = 0,
127  unsigned int d2 = 0, unsigned int d3 = 0);
129  component_format(TypeId _component_type,
130  ComponentFormat cf,
131  unsigned int align = 1,
132  unsigned int d0 = 0, unsigned int d1 = 0,
133  unsigned int d2 = 0, unsigned int d3 = 0);
135  void set_integer_interpretation(ComponentIntegerInterpretation cii);
137  ComponentIntegerInterpretation get_integer_interpretation() const;
139  friend FRIEND_MEMBER_API std::ostream& operator << (std::ostream& os, const component_format& cf);
141  template <typename T>
142  T get(int ci, const void* ptr) const {
143  if (is_packing()) {
144  if (get_component_type() <= TI_INT64) {
145  int i = packing_info::get_signed(ci, ptr);
146  return type_access<T>::get(&i, get_component_type());
147  }
148  else {
149  unsigned int i = packing_info::get_unsigned(ci, ptr);
150  return type_access<T>::get(&i, get_component_type());
151  }
152  }
153  else
154  return type_access<T>::get(static_cast<const unsigned char*>(ptr)+ci*align(get_type_size(get_component_type()),get_component_alignment()), get_component_type());
155  }
157  template <typename T>
158  bool set(int ci, void* ptr, const T& v) const {
159  if (is_packing()) {
160  if (get_component_type() <= TI_INT64) {
161  int i = 0;
162  return type_access<T>::set(&i, get_component_type(), v) && packing_info::set_signed(ci, ptr, i);
163  }
164  else {
165  unsigned int i = 0;
166  return type_access<T>::set(&i, get_component_type(), v) && packing_info::set_unsigned(ci, ptr, i);
167  }
168  }
169  else
170  return type_access<T>::set(static_cast<unsigned char*>(ptr)+ci*align(get_type_size(get_component_type()),get_component_alignment()), get_component_type(), v);
171  }
173  bool empty() const;
175  void clear();
177  const packing_info& get_packing_info() const;
179  void set_packing_info(const packing_info& pi);
181  unsigned int get_nr_components() const;
183  unsigned int get_component_index(const std::string& name) const;
185  std::string get_component_name(unsigned int i) const;
187  ComponentFormat get_standard_component_format() const;
189  void set_component_names(const std::string& _component_name_list);
191  void set_component_format(ComponentFormat _cf);
193  TypeId get_component_type() const;
195  void set_component_type(TypeId _type_id);
197  unsigned int get_entry_size() const;
199  bool operator == (const component_format& cf) const;
201  bool operator != (const component_format& cf) const;
202 };
203 
206 extern CGV_API std::ostream& operator << (std::ostream& os, const component_format& cf);
207 
209 extern CGV_API bool fmt1_compares_better(const component_format& fmt,
210  const component_format& fmt1,
211  const component_format& fmt2);
212 
215 extern CGV_API unsigned int find_best_match(
216  const component_format& fmt,
217  const char** format_descriptions,
218  const component_format* fmt0 = 0,
219  bool (*fmt1_better_match)(
220  const component_format& fmt,
221  const component_format& fmt1,
222  const component_format& fmt2) = fmt1_compares_better);
223 
224  }
225 }
226 
227 #include <cgv/config/lib_end.h>
cgv::data::component_format::component_positions
std::vector< unsigned short > component_positions
store the position of each component in the component string
Definition: component_format.h:67
cgv::data::CF_P4D
@ CF_P4D
point format with components px, py and pz
Definition: component_format.h:37
cgv::data::component_format
Definition: component_format.h:58
cgv::data::CF_P2D
@ CF_P2D
stencil component
Definition: component_format.h:35
cgv::data::CF_RGB
@ CF_RGB
color format with two components R and G
Definition: component_format.h:29
cgv::data::CF_BGRA
@ CF_BGRA
color format with components B, G and R
Definition: component_format.h:32
cgv::type::info::TypeId
TypeId
ids for the different types and type constructs
Definition: type_id.h:12
cgv::data::component_format::component_interpretation
ComponentIntegerInterpretation component_interpretation
interpretation of integer typed components
Definition: component_format.h:63
cgv::data::CF_N3D
@ CF_N3D
normal format with components nx and ny
Definition: component_format.h:39
cgv::data::component_format::set
bool set(int ci, void *ptr, const T &v) const
write access to the i-th component, return whether write was successful
Definition: component_format.h:158
cgv::data::CF_P3D
@ CF_P3D
point format with components px and py
Definition: component_format.h:36
cgv::data::CF_S
@ CF_S
depth component
Definition: component_format.h:34
cgv::data::CF_I
@ CF_I
color format with luminance component L
Definition: component_format.h:25
cgv::data::CF_B
@ CF_B
green channel of color format
Definition: component_format.h:22
cgv::data::CF_RG
@ CF_RG
color format with intensity and alpha components: I and A
Definition: component_format.h:28
cgv::data::CF_N2D
@ CF_N2D
point format with components px, py, pz and pw
Definition: component_format.h:38
cgv::data::CF_A
@ CF_A
blue channel of color format
Definition: component_format.h:23
cgv::data::CF_T2D
@ CF_T2D
normal format with components nx, ny and nz
Definition: component_format.h:40
cgv::data::CF_T3D
@ CF_T3D
texture coordinats format with components ts and tt
Definition: component_format.h:41
cgv::data::CF_L
@ CF_L
alpha format
Definition: component_format.h:24
cgv::type::info::type_access::set
static bool set(void *ptr, TypeId tid, const T &v)
convert value from type T and store it
Definition: type_access.h:37
cgv::type::info::get_type_size
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
Definition: type_id.cxx:18
cgv::type::info::TI_INT64
@ TI_INT64
signed integer stored in 32 bits
Definition: type_id.h:22
cgv::data::CF_R
@ CF_R
undefinded format with no component
Definition: component_format.h:20
cgv::data::ComponentIntegerInterpretation
ComponentIntegerInterpretation
Definition: component_format.h:48
cgv::data::CF_G
@ CF_G
red channel of color format
Definition: component_format.h:21
cgv::data::component_format::component_string
std::string component_string
store all component names in one string separated with 0-chars
Definition: component_format.h:65
cgv::data::CF_LA
@ CF_LA
color format with intensity component I
Definition: component_format.h:26
cgv::data::CF_LAST
@ CF_LAST
texture coordinats format with components ts, tt, tr and tq
Definition: component_format.h:43
cgv::type::info
namespace for templates that provide type information
Definition: type_access.h:9
cgv::data::CF_D
@ CF_D
color format with components B, G, R and A
Definition: component_format.h:33
cgv::data::component_format::component_type
TypeId component_type
store the type id of the component type
Definition: component_format.h:61
cgv::data::CF_T4D
@ CF_T4D
texture coordinats format with components ts, tt and tr
Definition: component_format.h:42
cgv::data::packing_info
Definition: packing_info.h:23
cgv::data::component_format::last_error
static std::string last_error
store the last error that appeared during parsing of a description
Definition: component_format.h:71
cgv::data::ComponentFormat
ComponentFormat
Definition: component_format.h:18
cgv::data::component_format::get
T get(int ci, const void *ptr) const
constant access to the i-th component stored at the given location
Definition: component_format.h:142
cgv::data::CF_IA
@ CF_IA
color format with luminance and alpha components: L and A
Definition: component_format.h:27
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::type::info::type_access::get
static T get(const void *ptr, TypeId tid)
return stored value converted into type T
Definition: type_access.h:16
cgv::data::CF_RGBA
@ CF_RGBA
color format with components R, G and B
Definition: component_format.h:30
cgv::data::CF_BGR
@ CF_BGR
color format with components R, G, B and A
Definition: component_format.h:31