jaulib v1.4.1
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 <algorithm>
16#include <cassert>
17#include <cmath>
18#include <cstdarg>
19#include <vector>
20
21#include <jau/cpp_lang_util.hpp>
22#include <jau/math/mat4f.hpp>
23#include <jau/type_concepts.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<jau::req::arithmetic Value_type, size_t Element_size>
39 public:
41 constexpr static const size_t element_size = Element_size;
42
43 private:
44 int growSize;
45 std::vector<value_type> buffer;
46
47 public:
48 /**
49 * Start w/ zero size and growSize is 16, half GL-min size (32)
50 */
52 : growSize(16*element_size), buffer(0) {}
53
54 /**
55 * @param initialSize initial size
56 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
57 */
58 constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
59 : growSize(growSize_), buffer(initialSize) {}
60
61 constexpr_cxx20 size_t growIfNecessary(int length) noexcept {
62 const size_t p = buffer.size();
63 const size_t nsz = buffer.size() + length;
64 if( nsz > buffer.capacity() ) {
65 buffer.reserve(buffer.size() + std::max(length, growSize));
66 }
67 buffer.resize(nsz);
68 return p;
69 }
70
71 constexpr_cxx20 void push(const value_type* src) noexcept {
72 size_t p = growIfNecessary(element_size);
73 for(size_t i=0; i<element_size; ++i) {
74 buffer[p+i] = src[i];
75 }
76 }
77
78 constexpr_cxx20 void pop(value_type* dest) noexcept {
79 const size_t sz = buffer.size();
80 assert( sz >= element_size );
81 const size_t p = sz - element_size;
82 for(size_t i=0; i<element_size; ++i) {
83 dest[i] = buffer[p+i];
84 }
85 buffer.resize(p);
86 }
87 };
88
89 /**
90 * 4x4 float matrix stack based on single float elements
91 */
92 typedef SimpleStack<float, 16 /* Element_size */> Stack16f;
93
94 /**
95 * A Matrix stack of compounds, each consisting of 16 * `T`
96 * @tparam T type of one element used in each compound
97 */
98 template <jau::req::packed_floating_point Value_type>
100 public:
103
104 private:
105 int growSize;
106 std::vector<matrix_t> buffer;
107
108 public:
109 /**
110 * Start w/ zero size and growSize is 16, half GL-min size (32)
111 */
113 : growSize(16), buffer(0) {}
114
115 /**
116 * @param initialSize initial size
117 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
118 */
119 constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
120 : growSize(growSize_), buffer(initialSize) {}
121
122 constexpr_cxx20 void growIfNecessary(int length) noexcept {
123 const size_t nsz = buffer.size() + length;
124 if( nsz > buffer.capacity() ) {
125 buffer.reserve(buffer.size() + std::max(length, growSize));
126 }
127 }
128
129 constexpr_cxx20 void push(const matrix_t& src) noexcept {
131 buffer.push_back(src);
132 }
133
134 constexpr_cxx20 void pop(matrix_t& dest) noexcept {
135 const size_t sz = buffer.size();
136 assert( sz >= 1 );
137 const size_t p = sz - 1;
138 dest.load( buffer[p] );
139 buffer.resize(p);
140 }
141 };
142
143 /**
144 * 4x4 float matrix stack
145 */
147
148 /**@}*/
149
150} // namespace jau::math
151
152#endif // JAU_MATH_UTIL_SSTACK_HPP_
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition mat4f.hpp:100
A Matrix stack of compounds, each consisting of 16 * T
Definition sstack.hpp:99
constexpr_cxx20 void growIfNecessary(int length) noexcept
Definition sstack.hpp:122
constexpr_cxx20 void push(const matrix_t &src) noexcept
Definition sstack.hpp:129
Matrix4< value_type > matrix_t
Definition sstack.hpp:102
constexpr_cxx20 MatrixStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition sstack.hpp:112
constexpr_cxx20 void pop(matrix_t &dest) noexcept
Definition sstack.hpp:134
constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
Definition sstack.hpp:119
A simple stack of compounds, each consisting of element_size * T
Definition sstack.hpp:38
constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
Definition sstack.hpp:58
constexpr_cxx20 size_t growIfNecessary(int length) noexcept
Definition sstack.hpp:61
constexpr_cxx20 SimpleStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition sstack.hpp:51
constexpr_cxx20 void pop(value_type *dest) noexcept
Definition sstack.hpp:78
constexpr_cxx20 void push(const value_type *src) noexcept
Definition sstack.hpp:71
static constexpr const size_t element_size
Definition sstack.hpp:41
#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:92
MatrixStack< float > Mat4fStack
4x4 float matrix stack
Definition sstack.hpp:146
uint8_t Value_type