utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
field_of_view.hpp
1/*
2 Copyright (c) 2026 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 <cmath>
26#include <numbers>
27#include <stdexcept>
28#include <string>
29#include <type_traits>
30#include <ostream>
31
32namespace utility::graphic
33{
34
39 template<typename Type>
40 concept CanBeFieldOfViewComponent = std::is_floating_point_v<Type>;
41
49 template<CanBeFieldOfViewComponent Type> class FieldOfView
50 {
51 private:
52 Type _up;
53 Type _down;
54 Type _left;
55 Type _right;
56
57 public:
62 : FieldOfView(Type { 0 }, Type { 0 }, Type { 0 }, Type { 0 })
63 {
64 }
65
75 FieldOfView(Type up, Type down, Type left, Type right)
76 : _up(up)
77 , _down(down)
78 , _left(left)
79 , _right(right)
80 {
81 }
82
87 FieldOfView(const FieldOfView &other) = default;
88
93 FieldOfView(FieldOfView &&other) noexcept = default;
94
100 FieldOfView &operator=(const FieldOfView &other) = default;
101
107 FieldOfView &operator=(FieldOfView &&other) noexcept = default;
108
112 ~FieldOfView(void) = default;
113
119 FieldOfView &setUp(const Type up)
120 {
121 _up = up;
122 return *this;
123 }
124
129 Type getUp(void) const noexcept
130 {
131 return _up;
132 }
133
139 FieldOfView &setDown(const Type down)
140 {
141 _down = down;
142 return *this;
143 }
144
149 Type getDown(void) const noexcept
150 {
151 return _down;
152 }
153
159 FieldOfView &setLeft(const Type left)
160 {
161 _left = left;
162 return *this;
163 }
164
169 Type getLeft(void) const noexcept
170 {
171 return _left;
172 }
173
179 FieldOfView &setRight(const Type right)
180 {
181 _right = right;
182 return *this;
183 }
184
189 Type getRight(void) const noexcept
190 {
191 return _right;
192 }
193
200 bool isSymmetric(Type epsilon = Type { 1e-6 }) const noexcept
201 {
202 return std::abs(_up - _down) <= epsilon
203 && std::abs(_left - _right) <= epsilon;
204 }
205 };
206
210 using FieldOfViewF = FieldOfView<float>;
211
215 using FieldOfViewD = FieldOfView<double>;
216
223 std::ostream &operator<<(std::ostream &stream, const FieldOfViewF &fov);
224
231 std::ostream &operator<<(std::ostream &stream, const FieldOfViewD &fov);
232
233} // namespace utility::graphic
Field of view representation in radians for up, down, left, and right directions.
FieldOfView & setRight(const Type right)
Set right field-of-view in radians.
FieldOfView & setDown(const Type down)
Set down field-of-view in radians.
FieldOfView & setLeft(const Type left)
Set left field-of-view in radians.
FieldOfView(void)
Default constructor initializing all FOV angles to 0 radians.
FieldOfView(Type up, Type down, Type left, Type right)
Constructor to initialize field of view angles in radians.
Type getLeft(void) const noexcept
Get left field-of-view in radians.
Type getUp(void) const noexcept
Get up field-of-view in radians.
FieldOfView(const FieldOfView &other)=default
Copy constructor.
FieldOfView & operator=(const FieldOfView &other)=default
Copy assignment operator.
Type getDown(void) const noexcept
Get down field-of-view in radians.
~FieldOfView(void)=default
Default destructor for FieldOfView.
FieldOfView(FieldOfView &&other) noexcept=default
Move constructor.
Type getRight(void) const noexcept
Get right field-of-view in radians.
FieldOfView & operator=(FieldOfView &&other) noexcept=default
Move assignment operator.
bool isSymmetric(Type epsilon=Type { 1e-6 }) const noexcept
Check if this FOV is symmetric around vertical and horizontal axes.
FieldOfView & setUp(const Type up)
Set up field-of-view in radians.
Concept to constrain field-of-view component type.