Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
geom3f.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef JAU_GEOM3F_HPP_
25#define JAU_GEOM3F_HPP_
26
27#include <jau/math/vec3f.hpp>
29
30namespace jau::math::geom {
31
32 /** \addtogroup Math
33 *
34 * @{
35 */
36
37 struct LineSeg3f {
41
42 /**
43 * Scale this line segment with given scale factor
44 * @param s scale factor
45 * @return this instance
46 */
47 constexpr LineSeg3f& operator*=(const float s ) noexcept {
48 p0 *= s;
49 p1 *= s;
50 return *this;
51 }
52
53 std::string toString() const noexcept { return "L[" + p0.toString() + ", " + p1.toString() + "]"; }
54
55 /**
56 * Compute intersection between two lines segments
57 * @param result storage for the intersection coordinates if the lines intersect, otherwise unchanged
58 * @param a vertex 1 of first line
59 * @param b vertex 2 of first line
60 * @param c vertex 1 of second line
61 * @param d vertex 2 of second line
62 * @return true if the line segments intersect, otherwise false
63 */
64 bool intersects(Point3f& result, const LineSeg3f & o) {
65 const float determinant = ( p0.x - p1.x ) * ( o.p0.y - o.p1.y) - ( p0.y - p1.y ) * ( o.p0.x - o.p1.x );
66 if( 0 == determinant ) {
67 return false;
68 }
69 const float alpha = p0.x * p1.y - p0.y * p1.x;
70 const float beta = o.p0.x*o.p1.y-o.p0.y*o.p1.y;
71 const float xi = ((o.p0.x-o.p1.x)*alpha-(p0.x-p1.x)*beta)/determinant;
72
73 const float gamma0 = (xi - p0.x) / (p1.x - p0.x);
74 const float gamma1 = (xi - o.p0.x) / (o.p1.x - o.p0.x);
75 if(gamma0 <= 0 || gamma0 >= 1 || gamma1 <= 0 || gamma1 >= 1) {
76 return false;
77 }
78 const float yi = ((o.p0.y-o.p1.y)*alpha-(p0.y-p1.y)*beta)/determinant;
79 result.x = xi;
80 result.y = yi;
81 return true;
82 }
83
84 /**
85 * Compute intersection between two lines segments
86 * @param a vertex 1 of first line
87 * @param b vertex 2 of first line
88 * @param c vertex 1 of second line
89 * @param d vertex 2 of second line
90 * @return true if the line segments intersect, otherwise false
91 */
92 bool intersects(const LineSeg3f & o) {
93 const float determinant = ( p0.x - p1.x ) * ( o.p0.y - o.p1.y) - ( p0.y - p1.y ) * ( o.p0.x - o.p1.x );
94 if( 0 == determinant ) {
95 return false;
96 }
97 const float alpha = p0.x * p1.y - p0.y * p1.x;
98 const float beta = o.p0.x*o.p1.y-o.p0.y*o.p1.y;
99 const float xi = ((o.p0.x-o.p1.x)*alpha-(p0.x-p1.x)*beta)/determinant;
100
101 const float gamma0 = (xi - p0.x) / (p1.x - p0.x);
102 const float gamma1 = (xi - o.p0.x) / (o.p1.x - o.p0.x);
103 if(gamma0 <= 0 || gamma0 >= 1 || gamma1 <= 0 || gamma1 >= 1) {
104 return false;
105 }
106 return true;
107 }
108
109 bool intersects(const AABBox3f& box) const noexcept {
110 // separating axis theorem.
111 const Vec3f d = (p1 - p0) * 0.5f; // half lineseg direction
112 const Vec3f e = (box.tr - box.bl) * 0.5f;
113 const Vec3f aabb_center = (box.bl + box.tr) * 0.5f;
114 const Vec3f lseg_center = p0 + d;
115 const Vec3f c = lseg_center - aabb_center;
116 Vec3f ad(std::abs(d.x), std::abs(d.y), std::abs(d.z));
117 if (std::abs(c.x) > e.x + ad.x)
118 return false;
119 if (std::abs(c.y) > e.y + ad.y)
120 return false;
121 if (std::abs(c.z) > e.z + ad.z)
122 return false;
123 if (std::abs(d.y * c.z - d.z * c.y) > e.y * ad.z + e.z * ad.y + std::numeric_limits<float>::epsilon()) {
124 return false;
125 }
126 if (std::abs(d.z * c.x - d.x * c.z) > e.z * ad.x + e.x * ad.z + std::numeric_limits<float>::epsilon()) {
127 return false;
128 }
129 if (std::abs(d.x * c.y - d.y * c.x) > e.x * ad.y + e.y * ad.x + std::numeric_limits<float>::epsilon()) {
130 return false;
131 }
132 return true;
133 }
134
135 };
136
137 /**@}*/
138
139} // namespace jau::math::geom
140
141#endif /* JAU_GEOM3F_HPP_ */
value_type x
Definition: vec3f.hpp:72
value_type y
Definition: vec3f.hpp:73
std::string toString() const noexcept
Definition: vec3f.hpp:214
value_type z
Definition: vec3f.hpp:74
Axis Aligned Bounding Box.
Definition: aabbox3f.hpp:49
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
Definition: base_math.hpp:155
bool intersects(const AABBox3f &box) const noexcept
Definition: geom3f.hpp:109
constexpr LineSeg3f & operator*=(const float s) noexcept
Scale this line segment with given scale factor.
Definition: geom3f.hpp:47
bool intersects(Point3f &result, const LineSeg3f &o)
Compute intersection between two lines segments.
Definition: geom3f.hpp:64
std::string toString() const noexcept
Definition: geom3f.hpp:53
bool intersects(const LineSeg3f &o)
Compute intersection between two lines segments.
Definition: geom3f.hpp:92