jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
sstack.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef JAU_MATH_UTIL_SSTACK_HPP_
13#define JAU_MATH_UTIL_SSTACK_HPP_
14
15#include <cmath>
16#include <cstdarg>
17#include <cassert>
18#include <vector>
19#include <type_traits>
20#include <algorithm>
21
22#include <jau/cpp_lang_util.hpp>
23#include <jau/math/mat4f.hpp>
24
25namespace jau::math::util {
26
27 /** \addtogroup Math
28 *
29 * @{
30 */
31
32 /**
33 * A simple stack of compounds, each consisting of `element_size` * `T`
34 * @tparam T type of one element used in each compound
35 * @tparam element_size number of T elements making up one compound
36 */
37 template<typename Value_type, size_t Element_size,
38 std::enable_if_t<std::is_floating_point_v<Value_type> ||
39 std::is_integral_v<Value_type>,
40 bool> = true>
42 public:
44 constexpr static const size_t element_size = Element_size;
45
46 private:
47 int growSize;
48 std::vector<value_type> buffer;
49
50 public:
51 /**
52 * Start w/ zero size and growSize is 16, half GL-min size (32)
53 */
55 : growSize(16*element_size), buffer(0) {}
56
57 /**
58 * @param initialSize initial size
59 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
60 */
61 constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
62 : growSize(growSize_), buffer(initialSize) {}
63
64 constexpr_cxx20 size_t growIfNecessary(int length) noexcept {
65 const size_t p = buffer.size();
66 const size_t nsz = buffer.size() + length;
67 if( nsz > buffer.capacity() ) {
68 buffer.reserve(buffer.size() + std::max(length, growSize));
69 }
70 buffer.resize(nsz);
71 return p;
72 }
73
74 constexpr_cxx20 void push(const value_type* src) noexcept {
75 size_t p = growIfNecessary(element_size);
76 for(size_t i=0; i<element_size; ++i) {
77 buffer[p+i] = src[i];
78 }
79 }
80
81 constexpr_cxx20 void pop(value_type* dest) noexcept {
82 const size_t sz = buffer.size();
83 assert( sz >= element_size );
84 const size_t p = sz - element_size;
85 for(size_t i=0; i<element_size; ++i) {
86 dest[i] = buffer[p+i];
87 }
88 buffer.resize(p);
89 }
90 };
91
92 /**
93 * 4x4 float matrix stack based on single float elements
94 */
95 typedef SimpleStack<float, 16 /* Element_size */> Stack16f;
96
97 /**
98 * A Matrix stack of compounds, each consisting of 16 * `T`
99 * @tparam T type of one element used in each compound
100 */
101 template<typename Value_type,
102 std::enable_if_t<std::is_floating_point_v<Value_type> ||
103 std::is_integral_v<Value_type>,
104 bool> = true>
106 public:
109
110 private:
111 int growSize;
112 std::vector<matrix_t> buffer;
113
114 public:
115 /**
116 * Start w/ zero size and growSize is 16, half GL-min size (32)
117 */
119 : growSize(16), buffer(0) {}
120
121 /**
122 * @param initialSize initial size
123 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
124 */
125 constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
126 : growSize(growSize_), buffer(initialSize) {}
127
128 constexpr_cxx20 void growIfNecessary(int length) noexcept {
129 const size_t nsz = buffer.size() + length;
130 if( nsz > buffer.capacity() ) {
131 buffer.reserve(buffer.size() + std::max(length, growSize));
132 }
133 }
134
135 constexpr_cxx20 void push(const matrix_t& src) noexcept {
137 buffer.push_back(src);
138 }
139
140 constexpr_cxx20 void pop(matrix_t& dest) noexcept {
141 const size_t sz = buffer.size();
142 assert( sz >= 1 );
143 const size_t p = sz - 1;
144 dest.load( buffer[p] );
145 buffer.resize(p);
146 }
147 };
148
149 /**
150 * 4x4 float matrix stack
151 */
153
154 /**@}*/
155
156} // namespace jau::math
157
158#endif // JAU_MATH_UTIL_SSTACK_HPP_
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition mat4f.hpp:101
A Matrix stack of compounds, each consisting of 16 * T
Definition sstack.hpp:105
constexpr_cxx20 MatrixStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition sstack.hpp:118
constexpr_cxx20 void growIfNecessary(int length) noexcept
Definition sstack.hpp:128
constexpr_cxx20 void pop(matrix_t &dest) noexcept
Definition sstack.hpp:140
constexpr_cxx20 void push(const matrix_t &src) noexcept
Definition sstack.hpp:135
constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
Definition sstack.hpp:125
Matrix4< value_type > matrix_t
Definition sstack.hpp:108
A simple stack of compounds, each consisting of element_size * T
Definition sstack.hpp:41
constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
Definition sstack.hpp:61
static constexpr const size_t element_size
Definition sstack.hpp:44
constexpr_cxx20 size_t growIfNecessary(int length) noexcept
Definition sstack.hpp:64
constexpr_cxx20 void push(const value_type *src) noexcept
Definition sstack.hpp:74
constexpr_cxx20 SimpleStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition sstack.hpp:54
constexpr_cxx20 void pop(value_type *dest) noexcept
Definition sstack.hpp:81
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
SimpleStack< float, 16 > Stack16f
4x4 float matrix stack based on single float elements
Definition sstack.hpp:95
MatrixStack< float > Mat4fStack
4x4 float matrix stack
Definition sstack.hpp:152
uint8_t Value_type