Antkeeper  0.0.1
sdf.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_SDF_HPP
21 #define ANTKEEPER_GEOM_SDF_HPP
22 
23 #include <engine/math/vector.hpp>
24 #include <algorithm>
25 
26 namespace geom {
27 
29 namespace sdf {
30 
31 inline math::fvec3 translate(const math::fvec3& sample, const math::fvec3& offset)
32 {
33  return sample - offset;
34 }
35 
36 inline float sphere(const math::fvec3& p, float r)
37 {
38  return math::length(p) - r;
39 }
40 
41 inline float cylinder(const math::fvec3& p, float r, float h)
42 {
43  float dx = std::abs(math::length(math::swizzle<0, 2>(p))) - r;
44  float dy = std::abs(p[1]) - h;
45  return std::min<float>(std::max<float>(dx, dy), 0.0f) + math::length(math::fvec2{std::max<float>(dx, 0.0f), std::max<float>(dy, 0.0f)});
46 }
47 
48 inline float op_union(float a, float b)
49 {
50  return std::min<float>(a, b);
51 }
52 
53 inline float op_difference(float a, float b)
54 {
55  return std::max<float>(-a, b);
56 }
57 
58 inline float op_intersection(float a, float b)
59 {
60  return std::max<float>(a, b);
61 }
62 
63 inline float op_round(float d, float r)
64 {
65  return d - r;
66 }
67 
68 } // namespace sdf
69 } // namespace geom
70 
71 #endif // ANTKEEPER_GEOM_SDF_HPP
72 
float op_round(float d, float r)
Definition: sdf.hpp:63
math::fvec3 translate(const math::fvec3 &sample, const math::fvec3 &offset)
Definition: sdf.hpp:31
float sphere(const math::fvec3 &p, float r)
Definition: sdf.hpp:36
float cylinder(const math::fvec3 &p, float r, float h)
Definition: sdf.hpp:41
float op_difference(float a, float b)
Definition: sdf.hpp:53
float op_intersection(float a, float b)
Definition: sdf.hpp:58
float op_union(float a, float b)
Definition: sdf.hpp:48
Geometric algorithms.
@ a
Vertex A region.
@ b
Vertex B region.
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
constexpr vector< T, N > abs(const vector< T, N > &x)
Returns the absolute values of each element.
Definition: vector.hpp:985
T offset(T longitude)
Calculates the UTC offset at a given longitude.
Definition: utc.hpp:38
n-dimensional vector.
Definition: vector.hpp:44