Blender V5.0
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#ifndef __MATH_GEOM_INLINE_C__
10#define __MATH_GEOM_INLINE_C__
11
12#include "BLI_math_vector.h"
13
14#include <cstring>
15
16/* A few small defines. Keep them 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 if (yn >= xn && yn >= zn) {
74 *r_axis_a = 0;
75 *r_axis_b = 2;
76 return yn;
77 }
78 *r_axis_a = 1;
79 *r_axis_b = 2;
80 return xn;
81}
82
83MINLINE int axis_dominant_v3_single(const float vec[3])
84{
85 const float x = fabsf(vec[0]);
86 const float y = fabsf(vec[1]);
87 const float z = fabsf(vec[2]);
88 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
89}
90
92{
93 const float x = fabsf(vec[0]);
94 const float y = fabsf(vec[1]);
95 const float z = fabsf(vec[2]);
96 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
97}
98
99MINLINE int max_axis_v3(const float vec[3])
100{
101 const float x = vec[0];
102 const float y = vec[1];
103 const float z = vec[2];
104 return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
105}
106
107MINLINE int min_axis_v3(const float vec[3])
108{
109 const float x = vec[0];
110 const float y = vec[1];
111 const float z = vec[2];
112 return ((x < y) ? ((x < z) ? 0 : 2) : ((y < z) ? 1 : 2));
113}
114
115MINLINE int poly_to_tri_count(const int poly_count, const int corner_count)
116{
117 BLI_assert(!poly_count || corner_count > poly_count * 2);
118 return corner_count - (poly_count * 2);
119}
120
121MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
122{
123 return dot_v3v3(co, plane) + plane[3];
124}
125
127{
128 return (UNLIKELY(angle < SMALL_NUMBER)) ? 1.0f : fabsf(1.0f / cosf(angle));
129}
130MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
131{
134 const float angle_cos = fabsf(dot_v3v3(a, b));
135 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
136}
137MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
138{
141 const float angle_cos = fabsf(dot_v2v2(a, b));
142 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
143}
144
145MINLINE float shell_v3v3_mid_normalized_to_dist(const float a[3], const float b[3])
146{
149 float angle_cos;
150 float ab[3];
151 add_v3_v3v3(ab, a, b);
152 angle_cos = (normalize_v3(ab) != 0.0f) ? fabsf(dot_v3v3(a, ab)) : 0.0f;
153 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
154}
155
156MINLINE float shell_v2v2_mid_normalized_to_dist(const float a[2], const float b[2])
157{
160 float angle_cos;
161 float ab[2];
162 add_v2_v2v2(ab, a, b);
163 angle_cos = (normalize_v2(ab) != 0.0f) ? fabsf(dot_v2v2(a, ab)) : 0.0f;
164 return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
165}
166
167#undef SMALL_NUMBER
168
169#endif /* __MATH_GEOM_INLINE_C__ */
#define BLI_assert(a)
Definition BLI_assert.h:46
#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)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
ATTR_WARN_UNUSED_RESULT const BMVert * v2
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
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])
#define fabsf
#define cosf