guillaume 1.0.0
Guillaume is a component-based UI framework library built around a primitive rendering system. It's designed for building user interfaces across XR platforms and traditional platforms with custom rendering backends and input handling. The framework is lightweight, flexible, and easy to integrate into existing projects.
Loading...
Searching...
No Matches
vector.hpp
1/*
2 Copyright (c) 2025 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <array>
26#include <cmath>
27#include <initializer_list>
28
39template <typename Type, size_t Dimension> class Vector {
40private:
41 std::array<Type, Dimension> _data;
42
43public:
47 Vector() : _data{} {}
48
58 Vector(std::initializer_list<Type> values) {
59 size_t i = 0;
60 for (const auto &value : values) {
61 if (i >= Dimension)
62 break;
63 _data[i++] = value;
64 }
65 }
66
73 Type &operator[](size_t index) { return _data[index]; }
74
81 const Type &operator[](size_t index) const { return _data[index]; }
82
90 Vector operator+(const Vector &other) const {
91 Vector result;
92 for (size_t i = 0; i < Dimension; ++i) {
93 result[i] = _data[i] + other[i];
94 }
95 return result;
96 }
97
104 Vector operator-(const Vector &other) const {
105 Vector result;
106 for (size_t i = 0; i < Dimension; ++i) {
107 result[i] = _data[i] - other[i];
108 }
109 return result;
110 }
111
118 Vector operator*(Type scalar) const {
119 Vector result;
120 for (size_t i = 0; i < Dimension; ++i) {
121 result[i] = _data[i] * scalar;
122 }
123 return result;
124 }
125
132 Type dot(const Vector &other) const {
133 Type result = Type{};
134 for (size_t i = 0; i < Dimension; ++i) {
135 result += _data[i] * other[i];
136 }
137 return result;
138 }
139
145 Type magnitude() const { return std::sqrt(dot(*this)); }
146
156 Type mag = magnitude();
157 return (mag != Type{}) ? (*this * (Type{1} / mag)) : *this;
158 }
159
165 constexpr size_t size() const { return Dimension; }
166};
A generic N-dimensional vector class template.
Definition vector.hpp:39
Type magnitude() const
Calculate the magnitude (length) of the vector.
Definition vector.hpp:145
Vector operator-(const Vector &other) const
Vector subtraction operator.
Definition vector.hpp:104
const Type & operator[](size_t index) const
Const element access operator.
Definition vector.hpp:81
Type dot(const Vector &other) const
Compute dot product with another vector.
Definition vector.hpp:132
Vector(std::initializer_list< Type > values)
Constructor from initializer list.
Definition vector.hpp:58
Vector operator*(Type scalar) const
Scalar multiplication operator.
Definition vector.hpp:118
Vector normalize() const
Create a normalized (unit) vector.
Definition vector.hpp:155
Vector()
Default constructor - initializes all components to zero.
Definition vector.hpp:47
Type & operator[](size_t index)
Non-const element access operator.
Definition vector.hpp:73
constexpr size_t size() const
Get the number of dimensions/components.
Definition vector.hpp:165
Vector operator+(const Vector &other) const
Vector addition operator.
Definition vector.hpp:90