Blender V4.3
math_geom_inline.c
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
9#ifndef __MATH_GEOM_INLINE_C__
10#define __MATH_GEOM_INLINE_C__
11
12#include "BLI_math_vector.h"
13
14#include <string.h>
15
16/* A few small defines. Keep'em local! */
17#define SMALL_NUMBER 1.e-8f
18
19/********************************** Polygons *********************************/
20
21MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
22{
23 return (v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0]);
24}
25
26MINLINE float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2])
27{
28 return 0.5f * ((v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0]));
29}
30
31MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
32{
33 return fabsf(area_tri_signed_v2(v1, v2, v3));
34}
35
36MINLINE float area_squared_tri_v2(const float v1[2], const float v2[2], const float v3[2])
37{
38 float area = area_tri_signed_v2(v1, v2, v3);
39 return area * area;
40}
41
42MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
43{
44 const float xn = fabsf(axis[0]);
45 const float yn = fabsf(axis[1]);
46 const float zn = fabsf(axis[2]);
47
48 if (zn >= xn && zn >= yn) {
49 *r_axis_a = 0;
50 *r_axis_b = 1;
51 }
52 else if (yn >= xn && yn >= zn) {
53 *r_axis_a = 0;
54 *r_axis_b = 2;
55 }
56 else {
57 *r_axis_a = 1;
58 *r_axis_b = 2;
59 }
60}
61
62MINLINE float axis_dominant_v3_max(int *r_axis_a, int *r_axis_b, const float axis[3])
63{
64 const float xn = fabsf(axis[0]);
65 const float yn = fabsf(axis[1]);
66 const float zn = fabsf(axis[2]);
67
68 if (zn >= xn && zn >= yn) {
69 *r_axis_a = 0;
70 *r_axis_b = 1;
71 return zn;
72 }
73 else if (yn >= xn && yn >= zn) {
74 *r_axis_a = 0;
75 *r_axis_b = 2;
76 return yn;
77 }
78 else {
79 *r_axis_a = 1;
80 *r_axis_b = 2;
81 return xn;
82 }
83}
84
85MINLINE int axis_dominant_v3_single(const float vec[3])
86{
87 const float x = fabsf(vec[0]);
88 const float y = fabsf(vec[1]);
89 const float z = fabsf(vec[2]);
90 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
91}
92
94{
95 const float x = fabsf(vec[0]);
96 const float y = fabsf(vec[1]);
97 const float z = fabsf(vec[2]);
98 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
99}
100
101MINLINE int max_axis_v3(const float vec[3])
102{
103 const float x = vec[0];
104 const float y = vec[1];
105 const float z = vec[2];
106 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
107}
108
109MINLINE int min_axis_v3(const float vec[3])
110{
111 const float x = vec[0];
112 const float y = vec[1];
113 const float z = vec[2];
114 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
115}
116
117MINLINE int poly_to_tri_count(const int poly_count, const int corner_count)
118{
119 BLI_assert(!poly_count || corner_count > poly_count * 2);
120 return corner_count - (poly_count * 2);
121}
122
123MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
124{
125 return dot_v3v3(co, plane) + plane[3];
126}
127
128MINLINE float shell_angle_to_dist(const float angle)
129{
130 return (UNLIKELY(angle < SMALL_NUMBER)) ? 1.0f : fabsf(1.0f / cosf(angle));
131}
132MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
133{
134 const float angle_cos = fabsf(dot_v3v3(a, b));
137 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
138}
139MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
140{
141 const float angle_cos = fabsf(dot_v2v2(a, b));
144 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
145}
146
147MINLINE float shell_v3v3_mid_normalized_to_dist(const float a[3], const float b[3])
148{
149 float angle_cos;
150 float ab[3];
153 add_v3_v3v3(ab, a, b);
154 angle_cos = (normalize_v3(ab) != 0.0f) ? fabsf(dot_v3v3(a, ab)) : 0.0f;
155 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
156}
157
158MINLINE float shell_v2v2_mid_normalized_to_dist(const float a[2], const float b[2])
159{
160 float angle_cos;
161 float ab[2];
164 add_v2_v2v2(ab, a, b);
165 angle_cos = (normalize_v2(ab) != 0.0f) ? fabsf(dot_v2v2(a, ab)) : 0.0f;
166 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
167}
168
169#undef SMALL_NUMBER
170
171#endif /* __MATH_GEOM_INLINE_C__ */
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_ASSERT_UNIT_V2(v)
#define BLI_ASSERT_UNIT_V3(v)
#define MINLINE
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v2(float n[2])
MINLINE float normalize_v3(float n[3])
#define UNLIKELY(x)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
local_group_size(16, 16) .push_constant(Type b
#define cosf(x)
#define fabsf(x)
MINLINE int poly_to_tri_count(const int poly_count, const int corner_count)
MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE float area_squared_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 cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE int axis_dominant_v3_ortho_single(const float vec[3])
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
#define SMALL_NUMBER
MINLINE float axis_dominant_v3_max(int *r_axis_a, int *r_axis_b, const float axis[3])
MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
MINLINE float shell_v3v3_mid_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 float shell_v2v2_mid_normalized_to_dist(const float a[2], const float b[2])
MINLINE int min_axis_v3(const float vec[3])
MINLINE float shell_angle_to_dist(const float angle)
MINLINE int max_axis_v3(const float vec[3])
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])