cgv
Loading...
Searching...
No Matches
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
11namespace cgv {
12 namespace data {
13
43
46{
47 CII_DEFAULT, // default of used api such as opengl
48 CII_SNORM, // map signed integers to [-1,1]
49 CII_INTEGER // keep integer values even if api would map them to [0,1]
50};
51
55class CGV_API component_format : public packing_info
56{
57protected:
63 std::string component_string;
65 std::vector<unsigned short> component_positions;
67 void extract_components();
69 static std::string last_error;
70public:
72 explicit component_format(const std::string& description);
113 bool set_component_format(const std::string& description);
115 void set_components(const std::string& _components);
117 static const std::string& get_last_error();
121 component_format(cgv::type::info::TypeId _component_type = cgv::type::info::TypeId::TI_UNDEF, const std::string& _component_name_list = "",
122 unsigned align = 1, unsigned d0 = 0, unsigned d1 = 0, unsigned d2 = 0, unsigned d3 = 0);
125 unsigned align = 1, unsigned d0 = 0, unsigned d1 = 0, unsigned d2 = 0, unsigned d3 = 0);
127 void set_integer_interpretation(ComponentIntegerInterpretation cii);
129 ComponentIntegerInterpretation get_integer_interpretation() const;
131 friend FRIEND_MEMBER_API std::ostream& operator << (std::ostream& os, const component_format& cf);
133 template <typename T>
134 T get(int ci, const void* ptr) const {
135 if (is_packing()) {
136 if (get_component_type() <= cgv::type::info::TI_INT64) {
137 int i = packing_info::get_signed(ci, ptr);
138 return cgv::type::info::type_access<T>::get(&i, get_component_type());
139 }
140 else {
141 unsigned int i = packing_info::get_unsigned(ci, ptr);
142 return cgv::type::info::type_access<T>::get(&i, get_component_type());
143 }
144 }
145 else
146 return cgv::type::info::type_access<T>::get(static_cast<const unsigned char*>(ptr)+ci*align(get_type_size(get_component_type()),get_component_alignment()), get_component_type());
147 }
149 template <typename T>
150 bool set(int ci, void* ptr, const T& v) const {
151 if (is_packing()) {
152 if (get_component_type() <= cgv::type::info::TI_INT64) {
153 int i = 0;
154 return cgv::type::info::type_access<T>::set(&i, get_component_type(), v) && packing_info::set_signed(ci, ptr, i);
155 }
156 else {
157 unsigned int i = 0;
158 return cgv::type::info::type_access<T>::set(&i, get_component_type(), v) && packing_info::set_unsigned(ci, ptr, i);
159 }
160 }
161 else
162 return cgv::type::info::type_access<T>::set(static_cast<unsigned char*>(ptr)+ci*align(get_type_size(get_component_type()),get_component_alignment()), get_component_type(), v);
163 }
165 bool empty() const;
167 void clear();
169 const packing_info& get_packing_info() const;
171 void set_packing_info(const packing_info& pi);
173 unsigned int get_nr_components() const;
175 unsigned int get_component_index(const std::string& name) const;
177 std::string get_component_name(unsigned int i) const;
179 ComponentFormat get_standard_component_format() const;
181 void set_component_names(const std::string& _component_name_list);
183 void set_component_format(ComponentFormat _cf);
185 cgv::type::info::TypeId get_component_type() const;
187 void set_component_type(cgv::type::info::TypeId _type_id);
189 unsigned int get_entry_size() const;
191 bool operator == (const component_format& cf) const;
193 bool operator != (const component_format& cf) const;
194};
195
198extern CGV_API std::ostream& operator << (std::ostream& os, const component_format& cf);
199
201extern CGV_API bool fmt1_compares_better(const component_format& fmt,
202 const component_format& fmt1,
203 const component_format& fmt2);
204
207extern CGV_API unsigned int find_best_match(
208 const component_format& fmt,
209 const char** format_descriptions,
210 const component_format* fmt0 = 0,
211 bool (*fmt1_better_match)(
212 const component_format& fmt,
213 const component_format& fmt1,
214 const component_format& fmt2) = fmt1_compares_better);
215
216 }
217}
218
219#include <cgv/config/lib_end.h>
the component format inherits the information of a packing_info and adds information on the component...
std::string component_string
store all component names in one string separated with 0-chars
T get(int ci, const void *ptr) const
constant access to the i-th component stored at the given location
cgv::type::info::TypeId component_type
store the type id of the component type
bool set(int ci, void *ptr, const T &v) const
write access to the i-th component, return whether write was successful
static std::string last_error
store the last error that appeared during parsing of a description
std::vector< unsigned short > component_positions
store the position of each component in the component string
ComponentIntegerInterpretation component_interpretation
interpretation of integer typed components
the packing_info class stores information about packed integers structures.
ComponentFormat
define standard formats, which should be used to avoid wrong assignment of component names
@ CF_IA
color format with luminance and alpha components: L and A
@ CF_LAST
texture coordinats format with components ts, tt, tr and tq
@ CF_B
green channel of color format
@ CF_P2D
stencil component
@ CF_T3D
texture coordinats format with components ts and tt
@ CF_BGR
color format with components R, G, B and A
@ CF_RGBA
color format with components R, G and B
@ CF_R
undefinded format with no component
@ CF_I
color format with luminance component L
@ CF_A
blue channel of color format
@ CF_N3D
normal format with components nx and ny
@ CF_L
alpha format
@ CF_S
depth component
@ CF_RGB
color format with two components R and G
@ CF_T2D
normal format with components nx, ny and nz
@ CF_BGRA
color format with components B, G and R
@ CF_LA
color format with intensity component I
@ CF_RG
color format with intensity and alpha components: I and A
@ CF_T4D
texture coordinats format with components ts, tt and tr
@ CF_G
red channel of color format
@ CF_D
color format with components B, G, R and A
@ CF_N2D
point format with components px, py, pz and pw
@ CF_P4D
point format with components px, py and pz
@ CF_P3D
point format with components px and py
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...
unsigned find_best_match(const component_format &fmt, const char **format_descriptions, const component_format *fmt0, bool(*fmt1_better_match)(const component_format &fmt, const component_format &fmt1, const component_format &fmt2))
find the best matching format in a list of formats described by strings and return index of best matc...
ComponentIntegerInterpretation
define different interpretations of integer components
bool fmt1_compares_better(const component_format &fmt, const component_format &fmt1, const component_format &fmt2)
default function to check whether fmt1 is a better match to fmt than fmt2
TypeId
ids for the different types and type constructs
Definition type_id.h:12
@ TI_INT64
signed integer stored in 32 bits
Definition type_id.h:22
the cgv namespace
Definition print.h:11
static T get(const void *ptr, TypeId tid)
return stored value converted into type T
Definition type_access.h:16
static bool set(void *ptr, TypeId tid, const T &v)
convert value from type T and store it
Definition type_access.h:37