Merge branch 'main' into dev
This commit is contained in:
+17
-7
@@ -118,6 +118,11 @@ namespace util {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
/// @return integer dot product of two vectors
|
||||
inline int dot(const glm::ivec3& a, const glm::ivec3& b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
/// @brief Find nearest point on segment to given
|
||||
/// @param a segment point A
|
||||
/// @param b segment point B
|
||||
@@ -144,12 +149,17 @@ namespace util {
|
||||
inline glm::ivec3 closest_point_on_segment(
|
||||
const glm::ivec3& a, const glm::ivec3& b, const glm::ivec3& point
|
||||
) {
|
||||
auto vec = b - a;
|
||||
float da = distance2(point, a);
|
||||
float db = distance2(point, b);
|
||||
float len = length2(vec);
|
||||
float t = (((da - db) / len) * 0.5f + 0.5f);
|
||||
t = std::min(1.0f, std::max(0.0f, t));
|
||||
return a + glm::ivec3(glm::vec3(vec) * t);
|
||||
glm::ivec3 vec = b - a;
|
||||
int len2 = length2(vec);
|
||||
|
||||
if (len2 == 0) return a;
|
||||
|
||||
glm::ivec3 ap = point - a;
|
||||
int dot_product = dot(ap, vec);
|
||||
|
||||
float t = static_cast<float>(dot_product) / static_cast<float>(len2);
|
||||
t = glm::clamp(t, 0.0f, 1.0f);
|
||||
|
||||
return a + glm::ivec3(glm::round(glm::vec3(vec) * t));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user