jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
geom3f.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#ifndef JAU_MATH_GEOM_GEOM3F_HPP_
12#define JAU_MATH_GEOM_GEOM3F_HPP_
13
14#include <jau/math/vec3f.hpp>
16
17namespace jau::math::geom {
18
19 /** \addtogroup Math
20 *
21 * @{
22 */
23
24 struct LineSeg3f {
28
29 /**
30 * Scale this line segment with given scale factor
31 * @param s scale factor
32 * @return this instance
33 */
34 constexpr LineSeg3f& operator*=(const float s ) noexcept {
35 p0 *= s;
36 p1 *= s;
37 return *this;
38 }
39
40 std::string toString() const noexcept { return "L[" + p0.toString() + ", " + p1.toString() + "]"; }
41
42 /**
43 * Compute intersection between two lines segments
44 * @param result storage for the intersection coordinates if the lines intersect, otherwise unchanged
45 * @param a vertex 1 of first line
46 * @param b vertex 2 of first line
47 * @param c vertex 1 of second line
48 * @param d vertex 2 of second line
49 * @return true if the line segments intersect, otherwise false
50 */
51 bool intersects(Point3f& result, const LineSeg3f & o) {
52 const float determinant = ( p0.x - p1.x ) * ( o.p0.y - o.p1.y) - ( p0.y - p1.y ) * ( o.p0.x - o.p1.x );
53 if( 0 == determinant ) {
54 return false;
55 }
56 const float alpha = p0.x * p1.y - p0.y * p1.x;
57 const float beta = o.p0.x*o.p1.y-o.p0.y*o.p1.y;
58 const float xi = ((o.p0.x-o.p1.x)*alpha-(p0.x-p1.x)*beta)/determinant;
59
60 const float gamma0 = (xi - p0.x) / (p1.x - p0.x);
61 const float gamma1 = (xi - o.p0.x) / (o.p1.x - o.p0.x);
62 if(gamma0 <= 0 || gamma0 >= 1 || gamma1 <= 0 || gamma1 >= 1) {
63 return false;
64 }
65 const float yi = ((o.p0.y-o.p1.y)*alpha-(p0.y-p1.y)*beta)/determinant;
66 result.x = xi;
67 result.y = yi;
68 return true;
69 }
70
71 /**
72 * Compute intersection between two lines segments
73 * @param a vertex 1 of first line
74 * @param b vertex 2 of first line
75 * @param c vertex 1 of second line
76 * @param d vertex 2 of second line
77 * @return true if the line segments intersect, otherwise false
78 */
79 bool intersects(const LineSeg3f & o) {
80 const float determinant = ( p0.x - p1.x ) * ( o.p0.y - o.p1.y) - ( p0.y - p1.y ) * ( o.p0.x - o.p1.x );
81 if( 0 == determinant ) {
82 return false;
83 }
84 const float alpha = p0.x * p1.y - p0.y * p1.x;
85 const float beta = o.p0.x*o.p1.y-o.p0.y*o.p1.y;
86 const float xi = ((o.p0.x-o.p1.x)*alpha-(p0.x-p1.x)*beta)/determinant;
87
88 const float gamma0 = (xi - p0.x) / (p1.x - p0.x);
89 const float gamma1 = (xi - o.p0.x) / (o.p1.x - o.p0.x);
90 if(gamma0 <= 0 || gamma0 >= 1 || gamma1 <= 0 || gamma1 >= 1) {
91 return false;
92 }
93 return true;
94 }
95
96 bool intersects(const AABBox3f& box) const noexcept {
97 // separating axis theorem.
98 const Vec3f d = (p1 - p0) * 0.5f; // half lineseg direction
99 const Vec3f e = (box.tr - box.bl) * 0.5f;
100 const Vec3f aabb_center = (box.bl + box.tr) * 0.5f;
101 const Vec3f lseg_center = p0 + d;
102 const Vec3f c = lseg_center - aabb_center;
103 Vec3f ad(std::abs(d.x), std::abs(d.y), std::abs(d.z));
104 if (std::abs(c.x) > e.x + ad.x)
105 return false;
106 if (std::abs(c.y) > e.y + ad.y)
107 return false;
108 if (std::abs(c.z) > e.z + ad.z)
109 return false;
110 if (std::abs(d.y * c.z - d.z * c.y) > e.y * ad.z + e.z * ad.y + std::numeric_limits<float>::epsilon()) {
111 return false;
112 }
113 if (std::abs(d.z * c.x - d.x * c.z) > e.z * ad.x + e.x * ad.z + std::numeric_limits<float>::epsilon()) {
114 return false;
115 }
116 if (std::abs(d.x * c.y - d.y * c.x) > e.x * ad.y + e.y * ad.x + std::numeric_limits<float>::epsilon()) {
117 return false;
118 }
119 return true;
120 }
121
122 };
123
124 /**@}*/
125
126} // namespace jau::math::geom
127
128#endif /* JAU_MATH_GEOM_GEOM3F_HPP_ */
value_type x
Definition vec3f.hpp:69
value_type y
Definition vec3f.hpp:70
value_type z
Definition vec3f.hpp:71
Axis Aligned Bounding Box.
Definition aabbox3f.hpp:43
Vector3F< float > Vec3f
Definition vec3f.hpp:433
Point3F< float > Point3f
Definition vec3f.hpp:448
bool intersects(const AABBox3f &box) const noexcept
Definition geom3f.hpp:96
constexpr LineSeg3f & operator*=(const float s) noexcept
Scale this line segment with given scale factor.
Definition geom3f.hpp:34
bool intersects(Point3f &result, const LineSeg3f &o)
Compute intersection between two lines segments.
Definition geom3f.hpp:51
std::string toString() const noexcept
Definition geom3f.hpp:40
bool intersects(const LineSeg3f &o)
Compute intersection between two lines segments.
Definition geom3f.hpp:79