Blender V5.3
GEO_xpbd_constraint_math.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_math_vector.hh"
8
9#include <optional>
10
11namespace blender::xpbd {
12
14 /* Intersection point. */
16 /* Position of the intersection relative to segment points. */
18};
19
29inline std::optional<PlaneIntersection> intersect_plane(const float3 &pos0,
30 const float3 &pos1,
31 const float3 &origin,
32 const float3 &edge,
33 const float3 &normal)
34{
37
38 const float3 tangent = math::cross(edge, normal);
39 BLI_assert(math::is_unit(tangent));
40
41 const float len = math::dot(tangent, pos0 - pos1);
42 if (math::abs(len) < 1e-9) {
43 /* Segment is parallel to the plane or too short. */
44 return std::nullopt;
45 }
46
47 const float dist0 = math::dot(tangent, pos0 - origin);
48 const float lambda = dist0 / len;
49 if (lambda < 0.0f || lambda > 1.0f) {
50 return std::nullopt;
51 }
52
53 const float3 position = math::interpolate(pos0, pos1, lambda);
54 return PlaneIntersection{position, lambda};
55}
56
58 /* Position relative to segment points. */
60 /* Distance of the closest point along the ray, unclamped. */
62};
63
73 const float3 &pos1,
74 const float3 &ray_pos,
75 const float3 &ray_dir,
76 const bool clamp)
77{
78 BLI_assert(math::is_unit(ray_dir));
79
80 const float3 segment = pos1 - pos0;
81 const float3 dist0 = pos0 - ray_pos;
82 const float a = math::dot(segment, dist0);
83 const float b = math::dot(ray_dir, dist0);
84 const float c = math::dot(segment, ray_dir);
85
86 const float len_segment_sq = math::length_squared(segment);
87 if (len_segment_sq <= 1e-9) [[unlikely]] {
88 return SegmentClosestToRay{0.0f, b};
89 }
90
91 float segment_lambda;
92 if (clamp) {
93 segment_lambda = math::clamp(math::safe_divide(c * b - a, len_segment_sq - c * c), 0.0f, 1.0f);
94 }
95 else {
96 segment_lambda = math::safe_divide(c * b - a, len_segment_sq - c * c);
97 }
98
99 const float ray_lambda = c * segment_lambda + b;
100
101 return SegmentClosestToRay{segment_lambda, ray_lambda};
102}
103
104} // namespace blender::xpbd
#define position
constexpr T clamp(T, U, U) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
T length_squared(const VecBase< T, Size > &a)
T clamp(const T &a, const T &min, const T &max)
T safe_divide(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T interpolate(const T &a, const T &b, const FactorT &t)
bool is_unit(const VecBase< T, Size > &a, const T epsilon=T(10) *std::numeric_limits< T >::epsilon())
T abs(const T &a)
std::optional< PlaneIntersection > intersect_plane(const float3 &pos0, const float3 &pos1, const float3 &origin, const float3 &edge, const float3 &normal)
SegmentClosestToRay closest_on_segment_to_ray(const float3 &pos0, const float3 &pos1, const float3 &ray_pos, const float3 &ray_dir, const bool clamp)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e