OGLplus  (0.59.0) a C++ wrapper for rendering APIs

primitives.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_MATH_PRIMITIVES_HPP
9 #define EAGINE_MATH_PRIMITIVES_HPP
10 
11 #include "tvec.hpp"
12 #include <array>
13 
14 namespace eagine::math {
15 //------------------------------------------------------------------------------
18 template <typename T, int N, bool V>
19 class basic_line {
20 public:
22  constexpr basic_line() noexcept = default;
23 
25  constexpr basic_line(tvec<T, N, V> orig, tvec<T, N, V> dir) noexcept
26  : _origin{orig}
27  , _direction{dir} {}
28 
30  constexpr auto origin() const noexcept -> vector<T, N, V> {
31  return _origin;
32  }
33 
35  constexpr auto direction() const noexcept -> vector<T, N, V> {
36  return _direction;
37  }
38 
40  constexpr auto point_at(T t) const noexcept -> vector<T, N, V> {
41  return _origin + _direction * t;
42  }
43 
44 private:
45  vector<T, N, V> _origin{};
46  vector<T, N, V> _direction{};
47 };
48 
51 template <typename T, bool V>
53 //------------------------------------------------------------------------------
56 template <typename T, int N, bool V>
58 public:
60  constexpr basic_triangle() noexcept = default;
61 
63  constexpr basic_triangle(
66  tvec<T, N, V> c) noexcept
67  : _vertices{{a, b, c}} {}
68 
71  constexpr auto vertex(span_size_t index) const noexcept -> vector<T, N, V> {
72  return _vertices[index];
73  }
74 
76  constexpr auto a() const noexcept -> vector<T, N, V> {
77  return _vertices[0];
78  }
79 
81  constexpr auto b() const noexcept -> vector<T, N, V> {
82  return _vertices[1];
83  }
84 
86  constexpr auto c() const noexcept -> vector<T, N, V> {
87  return _vertices[2];
88  }
89 
91  constexpr auto ab() const noexcept -> vector<T, N, V> {
92  return b() - a();
93  }
94 
96  constexpr auto ac() const noexcept -> vector<T, N, V> {
97  return c() - a();
98  }
99 
101  constexpr auto center() const noexcept -> vector<T, N, V> {
102  return (a() + b() + c()) / T(3);
103  }
104 
106  constexpr auto normal(bool cw) const noexcept -> vector<T, N, V> {
107  return cw ? cross(ac(), ab()) : cross(ab(), ac());
108  }
109 
110 private:
111  std::array<vector<T, N, V>, 3> _vertices{};
112 };
113 
116 template <typename T, bool V>
118 //------------------------------------------------------------------------------
121 template <typename T, int N, bool V>
123 public:
125  constexpr basic_sphere() noexcept = default;
126 
128  constexpr basic_sphere(tvec<T, N, V> cntr, T rad) noexcept
129  : _params{vector<T, N + 1, V>::from(cntr, rad)} {}
130 
132  constexpr auto center() const noexcept -> vector<T, N, V> {
133  return vector<T, N, V>::from(_params);
134  }
135 
137  constexpr auto radius() const noexcept -> T {
138  return _params[N];
139  }
140 
141 private:
142  vector<T, N + 1, V> _params{};
143 };
144 
147 template <typename T, bool V>
149 //------------------------------------------------------------------------------
150 } // namespace eagine::math
151 
152 #endif // EAGINE_MATH_PRIMITIVES_HPP
constexpr auto direction() const noexcept -> vector< T, N, V >
Returns the line direction.
Definition: primitives.hpp:35
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
constexpr auto c() const noexcept -> vector< T, N, V >
Returns the third point of the triangle.
Definition: primitives.hpp:86
Basic template for spheres in N-dimensional space.
Definition: primitives.hpp:122
static constexpr auto from(const vector< P, M, W > &v, T d=T(0)) noexcept
Creates vector instance from vector of another dimension.
Definition: vector.hpp:111
constexpr basic_triangle() noexcept=default
Default constructor.
constexpr basic_sphere(tvec< T, N, V > cntr, T rad) noexcept
Construction from center point and radius value.
Definition: primitives.hpp:128
Basic template for lines in N-dimensional space.
Definition: primitives.hpp:19
constexpr auto center() const noexcept -> vector< T, N, V >
Returns the center of the triangle.
Definition: primitives.hpp:101
constexpr basic_line(tvec< T, N, V > orig, tvec< T, N, V > dir) noexcept
Construction from origin point and direction vector.
Definition: primitives.hpp:25
Basic template for triangles in N-dimensional space.
Definition: primitives.hpp:57
Generic template for N-dimensional vectors.
Definition: fwd.hpp:22
constexpr auto vertex(span_size_t index) const noexcept -> vector< T, N, V >
Returns the point at the specified index.
Definition: primitives.hpp:71
Basic N-dimensional vector implementation template.
Definition: fwd.hpp:19
constexpr auto point_at(T t) const noexcept -> vector< T, N, V >
Returns a point on the line at the specified parameter t.
Definition: primitives.hpp:40
constexpr auto center() const noexcept -> vector< T, N, V >
Returns the sphere center.
Definition: primitives.hpp:132
constexpr basic_sphere() noexcept=default
Default constructor.
constexpr auto radius() const noexcept -> T
Returns the sphere radius.
Definition: primitives.hpp:137
Math-related code is placed in this namespace.
Definition: eagine.hpp:48
constexpr basic_triangle(tvec< T, N, V > a, tvec< T, N, V > b, tvec< T, N, V > c) noexcept
Construction from three points making up the triangle.
Definition: primitives.hpp:63
constexpr auto ac() const noexcept -> vector< T, N, V >
Returns the ac edge vector of the triangle.
Definition: primitives.hpp:96
constexpr auto origin() const noexcept -> vector< T, N, V >
Returns the line origin.
Definition: primitives.hpp:30
constexpr auto b() const noexcept -> vector< T, N, V >
Returns the second point of the triangle.
Definition: primitives.hpp:81
constexpr basic_line() noexcept=default
Default constructor.
constexpr auto ab() const noexcept -> vector< T, N, V >
Returns the ab edge vector of the triangle.
Definition: primitives.hpp:91
static auto cross(const vector< T, 3, V > &a, const vector< T, 3, V > &b) noexcept
3D vector cross product.
Definition: vector.hpp:326
constexpr auto normal(bool cw) const noexcept -> vector< T, N, V >
Returns the normal vector of the triangle, in specified direction.
Definition: primitives.hpp:106
constexpr auto a() const noexcept -> vector< T, N, V >
Returns the first point of the triangle.
Definition: primitives.hpp:76

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).