Blender V5.3
math_geom_inline.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_math_vector_c.hh"
12
13#include <cstring>
14
15namespace blender {
16
17/* A few small defines. Keep them local! */
18#define SMALL_NUMBER 1.e-8f
19
20/* -------------------------------------------------------------------- */
23
24MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
25{
26 return (v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0]);
27}
28
29MINLINE float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2])
30{
31 return 0.5f * ((v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0]));
32}
33
34MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
35{
36 return fabsf(area_tri_signed_v2(v1, v2, v3));
37}
38
39MINLINE float area_squared_tri_v2(const float v1[2], const float v2[2], const float v3[2])
40{
41 float area = area_tri_signed_v2(v1, v2, v3);
42 return area * area;
43}
44
45MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
46{
47 const float xn = fabsf(axis[0]);
48 const float yn = fabsf(axis[1]);
49 const float zn = fabsf(axis[2]);
50
51 if (zn >= xn && zn >= yn) {
52 *r_axis_a = 0;
53 *r_axis_b = 1;
54 }
55 else if (yn >= xn && yn >= zn) {
56 *r_axis_a = 0;
57 *r_axis_b = 2;
58 }
59 else {
60 *r_axis_a = 1;
61 *r_axis_b = 2;
62 }
63}
64
65MINLINE float axis_dominant_v3_max(int *r_axis_a, int *r_axis_b, const float axis[3])
66{
67 const float xn = fabsf(axis[0]);
68 const float yn = fabsf(axis[1]);
69 const float zn = fabsf(axis[2]);
70
71 if (zn >= xn && zn >= yn) {
72 *r_axis_a = 0;
73 *r_axis_b = 1;
74 return zn;
75 }
76 if (yn >= xn && yn >= zn) {
77 *r_axis_a = 0;
78 *r_axis_b = 2;
79 return yn;
80 }
81 *r_axis_a = 1;
82 *r_axis_b = 2;
83 return xn;
84}
85
86MINLINE int axis_dominant_v3_single(const float vec[3])
87{
88 const float x = fabsf(vec[0]);
89 const float y = fabsf(vec[1]);
90 const float z = fabsf(vec[2]);
91 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
92}
93
95{
96 const float x = fabsf(vec[0]);
97 const float y = fabsf(vec[1]);
98 const float z = fabsf(vec[2]);
99 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
100}
101
102MINLINE int max_axis_v3(const float vec[3])
103{
104 const float x = vec[0];
105 const float y = vec[1];
106 const float z = vec[2];
107 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
108}
109
110MINLINE int min_axis_v3(const float vec[3])
111{
112 const float x = vec[0];
113 const float y = vec[1];
114 const float z = vec[2];
115 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
116}
117
118MINLINE int poly_to_tri_count(const int poly_count, const int corner_count)
119{
120 BLI_assert(!poly_count || corner_count > poly_count * 2);
121 return corner_count - (poly_count * 2);
122}
123
124MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
125{
126 return dot_v3v3(co, plane) + plane[3];
127}
128
130{
131 return (UNLIKELY(angle < SMALL_NUMBER)) ? 1.0f : fabsf(1.0f / cosf(angle));
132}
133MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
134{
137 const float angle_cos = fabsf(dot_v3v3(a, b));
138 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
139}
140MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
141{
144 const float angle_cos = fabsf(dot_v2v2(a, b));
145 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
146}
147
148MINLINE float shell_v3v3_mid_normalized_to_dist(const float a[3], const float b[3])
149{
152 float angle_cos;
153 float ab[3];
154 add_v3_v3v3(ab, a, b);
155 angle_cos = (normalize_v3(ab) != 0.0f) ? fabsf(dot_v3v3(a, ab)) : 0.0f;
156 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
157}
158
159MINLINE float shell_v2v2_mid_normalized_to_dist(const float a[2], const float b[2])
160{
163 float angle_cos;
164 float ab[2];
165 add_v2_v2v2(ab, a, b);
166 angle_cos = (normalize_v2(ab) != 0.0f) ? fabsf(dot_v2v2(a, ab)) : 0.0f;
167 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
168}
169
170#undef SMALL_NUMBER
171
173
174} // namespace blender
#define BLI_ASSERT_UNIT_V2(v)
#define BLI_ASSERT_UNIT_V3(v)
#define MINLINE
#define UNLIKELY(x)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define SMALL_NUMBER
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
MINLINE float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v2(float n[2])
MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE int axis_dominant_v3_single(const float vec[3])
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE int axis_dominant_v3_ortho_single(const float vec[3])
MINLINE float normalize_v3(float n[3])
MINLINE int min_axis_v3(const float vec[3])
MINLINE float shell_v3v3_mid_normalized_to_dist(const float a[3], const float b[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE float axis_dominant_v3_max(int *r_axis_a, int *r_axis_b, const float axis[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
MINLINE int max_axis_v3(const float vec[3])
MINLINE float area_squared_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float shell_v2v2_mid_normalized_to_dist(const float a[2], const float b[2])
MINLINE float shell_angle_to_dist(float angle)
#define fabsf
#define cosf