Blender V5.3
BLI_math_geom.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
10
11#include <optional>
12
13#include "BLI_math_geom_c.hh"
15#include "BLI_math_vector.hh"
16#include "BLI_span.hh"
17
18namespace blender::math {
19
20/* -------------------------------------------------------------------- */
23
31template<typename T>
32[[nodiscard]] inline VecBase<T, 3> normal_quad(const VecBase<T, 3> &v1,
33 const VecBase<T, 3> &v2,
34 const VecBase<T, 3> &v3,
35 const VecBase<T, 3> &v4)
36{
37 return normalize(cross(v1 - v3, v2 - v4));
38}
39
43template<typename T>
44[[nodiscard]] inline T area_tri(const VecBase<T, 3> &v1,
45 const VecBase<T, 3> &v2,
46 const VecBase<T, 3> &v3)
47{
48 return T(0.5) * length(cross(v1 - v2, v2 - v3));
49}
50
55template<typename T>
56[[nodiscard]] inline T cotangent_tri_weight(const VecBase<T, 3> &v1,
57 const VecBase<T, 3> &v2,
58 const VecBase<T, 3> &v3)
59{
60 const VecBase<T, 3> a = v2 - v1;
61 const VecBase<T, 3> b = v3 - v1;
62 const T c = length(cross(a, b));
63 return (c > std::numeric_limits<T>::epsilon()) ? (dot(a, b) / c) : T(0);
64}
65
67
68/* -------------------------------------------------------------------- */
76
80template<typename T>
82 const VecBase<T, 3> &no)
83{
84 return VecBase<T, 4>(no.x, no.y, no.z, -dot(no, co));
85}
86
93template<typename T>
94[[nodiscard]] inline T plane_point_side(const VecBase<T, 4> &plane, const VecBase<T, 3> &co)
95{
96 return co.x * plane.x + co.y * plane.y + co.z * plane.z + plane.w;
97}
98
100
101/* -------------------------------------------------------------------- */
104
109template<typename T, int Size>
111 const VecBase<T, Size> &l1,
112 const VecBase<T, Size> &l2)
113{
114 const VecBase<T, Size> d = l2 - l1;
115 const T len_sq = length_squared(d);
116 if (len_sq == T(0)) {
117 return l1;
118 }
119 const T lambda = dot(p - l1, d) / len_sq;
120 if (lambda <= T(0)) {
121 return l1;
122 }
123 if (lambda >= T(1)) {
124 return l2;
125 }
126 return l1 + lambda * d;
127}
128
133template<typename T, int Size>
134[[nodiscard]] inline T dist_squared_to_line_segment(const VecBase<T, Size> &p,
135 const VecBase<T, Size> &l1,
136 const VecBase<T, Size> &l2)
137{
138 return length_squared(p - closest_to_line_segment(p, l1, l2));
139}
140
146template<typename T>
148 const VecBase<T, 3> &pt)
149{
150 const VecBase<T, 3> n(plane.x, plane.y, plane.z);
151 return pt - (dot(n, pt) + plane.w) * n;
152}
153
160template<typename T>
161[[nodiscard]] inline VecBase<T, 3> closest_to_plane(const VecBase<T, 4> &plane,
162 const VecBase<T, 3> &pt)
163{
164 const VecBase<T, 3> n(plane.x, plane.y, plane.z);
165 const T len_sq = length_squared(n);
166 const T side = plane_point_side(plane, pt);
167 return pt - (side / len_sq) * n;
168}
169
175template<typename T>
176[[nodiscard]] inline T dist_squared_to_plane(const VecBase<T, 3> &p, const VecBase<T, 4> &plane)
177{
178 const VecBase<T, 3> n(plane.x, plane.y, plane.z);
179 const T len_sq = length_squared(n);
180 const T side = plane_point_side(plane, p);
181 const T fac = side / len_sq;
182 return len_sq * (fac * fac);
183}
184
186
187/* -------------------------------------------------------------------- */
190
198template<typename T, int Size>
199[[nodiscard]] inline T line_point_factor(const VecBase<T, Size> &p,
200 const VecBase<T, Size> &l1,
201 const VecBase<T, Size> &l2)
202{
203 const VecBase<T, Size> d = l2 - l1;
204 const T len_sq = length_squared(d);
205 if (len_sq == T(0)) {
206 return T(0);
207 }
208 return dot(p - l1, d) / len_sq;
209}
210
212
213/* -------------------------------------------------------------------- */
216
224[[nodiscard]] inline float3x3 axis_dominant_to_m3(const float3 &normal)
225{
226 float tmp[3][3];
227 axis_dominant_v3_to_m3(tmp, &normal.x);
228 return float3x3(float3(tmp[0]), float3(tmp[1]), float3(tmp[2]));
229}
230
232
233/* -------------------------------------------------------------------- */
236
249[[nodiscard]] inline int isect_line_line(const float3 &v1,
250 const float3 &v2,
251 const float3 &v3,
252 const float3 &v4,
253 float3 &r_i1,
254 float3 &r_i2)
255{
256 return isect_line_line_v3(v1, v2, v3, v4, &r_i1.x, &r_i2.x);
257}
258
266[[nodiscard]] inline std::optional<float3> isect_line_plane(const float3 &l1,
267 const float3 &l2,
268 const float3 &plane_co,
269 const float3 &plane_no)
270{
271 float3 r_isect_co;
272 if (isect_line_plane_v3(&r_isect_co.x, l1, l2, plane_co, plane_no)) {
273 return r_isect_co;
274 }
275 return std::nullopt;
276}
277
286template<typename T>
287[[nodiscard]] inline int isect_point_tri(const VecBase<T, 2> &pt,
288 const VecBase<T, 2> &v1,
289 const VecBase<T, 2> &v2,
290 const VecBase<T, 2> &v3)
291{
292 const T s1 = cross(v2 - v1, pt - v1);
293 const T s2 = cross(v3 - v2, pt - v2);
294 const T s3 = cross(v1 - v3, pt - v3);
295 if (s1 >= T(0) && s2 >= T(0) && s3 >= T(0)) {
296 return 1;
297 }
298 if (s1 <= T(0) && s2 <= T(0) && s3 <= T(0)) {
299 return -1;
300 }
301 return 0;
302}
303
312template<typename T>
313[[nodiscard]] inline int isect_point_quad(const VecBase<T, 2> &p,
314 const VecBase<T, 2> &v1,
315 const VecBase<T, 2> &v2,
316 const VecBase<T, 2> &v3,
317 const VecBase<T, 2> &v4)
318{
319 const T s1 = cross(v2 - v1, p - v1);
320 const T s2 = cross(v3 - v2, p - v2);
321 const T s3 = cross(v4 - v3, p - v3);
322 const T s4 = cross(v1 - v4, p - v4);
323 if (s1 >= T(0) && s2 >= T(0) && s3 >= T(0) && s4 >= T(0)) {
324 return 1;
325 }
326 if (s1 <= T(0) && s2 <= T(0) && s3 <= T(0) && s4 <= T(0)) {
327 return -1;
328 }
329 return 0;
330}
331
339[[nodiscard]] inline bool isect_point_poly(const float2 &pt, Span<float2> verts)
340{
341 return isect_point_poly_v2(
342 pt, reinterpret_cast<const float (*)[2]>(verts.data()), uint(verts.size()));
343}
344
346
347/* -------------------------------------------------------------------- */
350
355[[nodiscard]] inline float3 barycentric_weights(const float2 &v1,
356 const float2 &v2,
357 const float2 &v3,
358 const float2 &co)
359{
360 float3 w;
361 barycentric_weights_v2(v1, v2, v3, co, &w.x);
362 return w;
363}
364
366
367/* -------------------------------------------------------------------- */
370
382[[nodiscard]] constexpr int poly_to_tri_count(const int poly_count, const int corner_count)
383{
384 return corner_count - (poly_count * 2);
385}
386
390[[nodiscard]] inline float area_poly(Span<float2> verts)
391{
392 return area_poly_v2(reinterpret_cast<const float (*)[2]>(verts.data()), uint(verts.size()));
393}
394
403inline void interp_weights_poly(MutableSpan<float> r_weights, Span<float2> verts, const float2 &co)
404{
405 BLI_assert(r_weights.size() == verts.size());
407 r_weights.data(),
408 /* Cast needed because the C API takes a non-const pointer
409 * to a 2D float array; the values are read-only here. */
410 const_cast<float (*)[2]>(reinterpret_cast<const float (*)[2]>(verts.data())),
411 int(verts.size()),
412 co);
413}
414
416
417/* -------------------------------------------------------------------- */
420
437template<typename T>
438[[nodiscard]] inline VecBase<T, 3> interp_bilinear_quad(const VecBase<T, 3> &v0,
439 const VecBase<T, 3> &v1,
440 const VecBase<T, 3> &v2,
441 const VecBase<T, 3> &v3,
442 const T u,
443 const T v)
444{
445 return v0 * ((T(1) - u) * (T(1) - v)) + v1 * (u * (T(1) - v)) + v2 * (u * v) +
446 v3 * ((T(1) - u) * v);
447}
448
450
451/* -------------------------------------------------------------------- */
454
461[[nodiscard]] inline float2 map_to_sphere(const float3 &co)
462{
464 blender::map_to_sphere(&result.x, &result.y, co.x, co.y, co.z);
465 return result;
466}
467
469
470} // namespace blender::math
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
#define T
int isect_point_tri(const VecBase< T, 2 > &pt, const VecBase< T, 2 > &v1, const VecBase< T, 2 > &v2, const VecBase< T, 2 > &v3)
int isect_point_quad(const VecBase< T, 2 > &p, const VecBase< T, 2 > &v1, const VecBase< T, 2 > &v2, const VecBase< T, 2 > &v3, const VecBase< T, 2 > &v4)
T length_squared(const VecBase< T, Size > &a)
T cotangent_tri_weight(const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3)
float3x3 axis_dominant_to_m3(const float3 &normal)
T dist_squared_to_line_segment(const VecBase< T, Size > &p, const VecBase< T, Size > &l1, const VecBase< T, Size > &l2)
VecBase< T, Size > closest_to_line_segment(const VecBase< T, Size > &p, const VecBase< T, Size > &l1, const VecBase< T, Size > &l2)
VecBase< T, 3 > interp_bilinear_quad(const VecBase< T, 3 > &v0, const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3, const T u, const T v)
T dist_squared_to_plane(const VecBase< T, 3 > &p, const VecBase< T, 4 > &plane)
float2 map_to_sphere(const float3 &co)
T plane_point_side(const VecBase< T, 4 > &plane, const VecBase< T, 3 > &co)
std::optional< float3 > isect_line_plane(const float3 &l1, const float3 &l2, const float3 &plane_co, const float3 &plane_no)
T length(const VecBase< T, Size > &a)
constexpr int poly_to_tri_count(const int poly_count, const int corner_count)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T area_tri(const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3)
VecBase< T, 3 > closest_to_plane_normalized(const VecBase< T, 4 > &plane, const VecBase< T, 3 > &pt)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > normal_quad(const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3, const VecBase< T, 3 > &v4)
float3 barycentric_weights(const float2 &v1, const float2 &v2, const float2 &v3, const float2 &co)
float area_poly(Span< float2 > verts)
int isect_line_line(const float3 &v1, const float3 &v2, const float3 &v3, const float3 &v4, float3 &r_i1, float3 &r_i2)
VecBase< T, 4 > plane_from_point_normal(const VecBase< T, 3 > &co, const VecBase< T, 3 > &no)
T line_point_factor(const VecBase< T, Size > &p, const VecBase< T, Size > &l1, const VecBase< T, Size > &l2)
VecBase< T, 3 > closest_to_plane(const VecBase< T, 4 > &plane, const VecBase< T, 3 > &pt)
void interp_weights_poly(MutableSpan< float > r_weights, Span< float2 > verts, const float2 &co)
bool isect_point_poly(const float2 &pt, Span< float2 > verts)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
float area_poly_v2(const float verts[][2], unsigned int nr)
Definition math_geom.cc:186
VecBase< float, 2 > float2
bool isect_point_poly_v2(const float pt[2], const float verts[][2], unsigned int nr)
MatBase< float, 3, 3 > float3x3
void interp_weights_poly_v2(float w[], float v[][2], int n, const float co[2])
bool isect_line_plane_v3(float r_isect_co[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3]) ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const BMVert * v2
VecBase< float, 3 > float3
void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])