cgv
Loading...
Searching...
No Matches
element_traits.h
1
2#pragma once
3
4#include <cgv/math/vec.h>
5#include <cgv/math/mat.h>
6#include <cgv/math/fvec.h>
7#include <cgv/math/fmat.h>
8#include <cgv/math/quaternion.h>
9#include <cgv/media/color.h>
10#include <cgv/type/info/type_id.h>
11#include "context.h"
12
13namespace cgv {
14 namespace render {
15
16 template <typename T>
18 {
19 static type_descriptor get_type_descriptor(const T&) { return type_descriptor(cgv::type::info::type_id <T>::get_id()); }
20 static const void* get_address(const T& value) { return &value; }
21 static void* get_address(T& value) { return &value; }
22 };
23 template <typename T, cgv::type::uint32_type N>
25 {
26 static type_descriptor get_type_descriptor(const cgv::math::fvec<T, N>&) { return type_descriptor(cgv::type::info::type_id<T>::get_id(), N); }
27 static const void* get_address(const cgv::math::fvec<T, N>& element) { return &element; }
28 static void* get_address(cgv::math::fvec<T, N>& element) { return &element; }
29 };
30 template <typename T>
32 {
34 static const void* get_address(const cgv::math::quaternion<T>& element) { return &element; }
35 static void* get_address(cgv::math::quaternion<T>& element) { return &element; }
36 };
37 template <typename T, cgv::media::ColorModel cm, cgv::media::AlphaModel am>
39 {
41 static const void* get_address(const cgv::media::color<T, cm, am>& element) { return &element; }
42 static void* get_address(cgv::media::color<T, cm, am>& element) { return &element; }
43 };
44 template <typename T>
46 {
47 static type_descriptor get_type_descriptor(const cgv::math::vec<T>& vec) { return type_descriptor(cgv::type::info::type_id<T>::get_id(), vec.size()); }
48 static const void* get_address(const cgv::math::vec<T>& element) { return &element(0); }
49 static void* get_address(cgv::math::vec<T>& element) { return &element(0); }
50 };
51 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
53 {
54 static type_descriptor get_type_descriptor(const cgv::math::fmat<T, N, M>&) { return type_descriptor(cgv::type::info::type_id<T>::get_id(), N, M, true); }
55 static const void* get_address(const cgv::math::fmat<T, N, M>& element) { return &element(0,0); }
56 static void* get_address(cgv::math::fmat<T, N, M>& element) { return &element(0,0); }
57 };
58 template <typename T>
60 {
61 static type_descriptor get_type_descriptor(const cgv::math::mat<T>& mat) { return type_descriptor(cgv::type::info::type_id<T>::get_id(), mat.nrows(), mat.ncols(), true); }
62 static const void* get_address(const cgv::math::mat<T>& element) { return &element(0, 0); }
63 static void* get_address(cgv::math::mat<T>& element) { return &element(0, 0); }
64 };
65
66 template <typename T>
67 type_descriptor get_element_type(const T& element)
68 {
70 }
71
72 template <typename T>
74 {
75 /*
76 If your compilation fails here, this is intentional!!! It means a function template has a deduced type which
77 is not std::vector<T> or cgv::math::vec<T>. Only these two types are, what this framework considers an
78 array-like type.
79
80 To exemplify this problem, assume that vertex_buffer::resize(ctx, buf_size) is called with an unfortunately
81 choosen type for the "buf_size" parameter and uses array_descriptor_traits in its implementation. Function
82 overloads are, among others, resize(ctx, size_t) for an empty initialization and resize(ctx, const T&) for
83 copying the data.
84
85 If the buf_size parameter has the type int, the overload resolution chooses the second variant which would
86 be wrong --- we want to specify a buffer size for allocation. The overload resolution however determines the
87 second templated overload more fitting because the first would need an implicit type conversion whereas the
88 second does not. Therefore the implementation of resize(ctx, const T&) tries to treat "buf_size" like an
89 array.
90 */
91 static_assert(sizeof(T) == 0, "This type is not an array type or was not detected as an array type");
92 };
93
94 template <typename T>
95 struct array_descriptor_traits < std::vector<T> >
96 {
100 static const T* get_address(const std::vector<T>& vec) { return &vec.front(); }
102 static T* get_address(std::vector<T>& vec) { return &vec.front(); }
104 static size_t get_nr_elements(const std::vector<T>& vec) { return vec.size(); }
106 static size_t get_size(const std::vector<T>& vec) { return vec.size() * sizeof(T); }
107 };
108
109 template <typename T>
111 {
115 static const T* get_address(const cgv::math::vec<T>& vec) { return &vec(0); }
117 static T* get_address(cgv::math::vec<T>& vec) { return &vec(0); }
119 static size_t get_nr_elements(const cgv::math::vec<T>& vec) { return vec.size(); }
121 static size_t get_size(const cgv::math::vec<T>& vec) { return vec.size() * sizeof(T); }
122 };
123
124 template <typename T>
125 type_descriptor get_array_type(const T& arr)
126 {
127 return typename array_descriptor_traits<T>::get_type_descriptor(arr);
128 }
129 }
130}
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
matrix of fixed size dimensions
Definition fmat.h:23
A vector with zero based index.
Definition fvec.h:26
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
unsigned ncols() const
number of columns
Definition mat.h:546
unsigned nrows() const
number of rows
Definition mat.h:540
implements a quaternion.
Definition quaternion.h:21
A column vector class.
Definition vec.h:28
unsigned size() const
number of elements
Definition vec.h:59
represent a color with components of given type and color and alpha model as specified.
Definition color.h:574
the cgv namespace
Definition print.h:11
static const T * get_address(const cgv::math::vec< T > &vec)
return const start address in array
static T * get_address(cgv::math::vec< T > &vec)
return start address in array
static size_t get_nr_elements(const cgv::math::vec< T > &vec)
return number elements in array
static size_t get_size(const cgv::math::vec< T > &vec)
return size of array in bytes
static type_descriptor get_type_descriptor(const cgv::math::vec< T > &vec)
return type descriptor for array
static size_t get_size(const std::vector< T > &vec)
return size of array in bytes
static const T * get_address(const std::vector< T > &vec)
return const start address in array
static size_t get_nr_elements(const std::vector< T > &vec)
return number elements in array
static type_descriptor get_type_descriptor(const std::vector< T > &vec)
return type descriptor for array
static T * get_address(std::vector< T > &vec)
return start address in array
compact type description of data that can be sent to the context; convertible to int
Definition context.h:47
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86