Blender V5.0
math_vec.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_hash.hh"
10#include "BLI_math_vector.hh"
12
13namespace blender::math {
14
15template<>
17 const float2 &v2,
18 const float2 &v3,
19 const float2 &v4)
20{
22 float div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
23 if (div == 0.0f) {
24 ans.lambda = 0.0f;
26 }
27 else {
28 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
29 float mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
30 if (ans.lambda >= 0.0f && ans.lambda <= 1.0f && mu >= 0.0f && mu <= 1.0f) {
31 if (ans.lambda == 0.0f || ans.lambda == 1.0f || mu == 0.0f || mu == 1.0f) {
33 }
34 else {
36 }
37 }
38 else {
40 }
41 }
42 return ans;
43}
44
45template<>
47 const double2 &v2,
48 const double2 &v3,
49 const double2 &v4)
50{
52 double div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
53 if (div == 0.0) {
54 ans.lambda = 0.0;
56 }
57 else {
58 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
59 double mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
60 if (ans.lambda >= 0.0 && ans.lambda <= 1.0 && mu >= 0.0 && mu <= 1.0) {
61 if (ans.lambda == 0.0 || ans.lambda == 1.0 || mu == 0.0 || mu == 1.0) {
63 }
64 else {
66 }
67 }
68 else {
70 }
71 }
72 return ans;
73}
74
75#ifdef WITH_GMP
76template<>
77isect_result<mpq2> isect_seg_seg(const mpq2 &v1, const mpq2 &v2, const mpq2 &v3, const mpq2 &v4)
78{
79 isect_result<mpq2> ans;
80 mpq_class div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
81 if (div == 0.0) {
82 ans.lambda = 0.0;
84 }
85 else {
86 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
87 /* Avoid dividing mu by div: it is expensive in multi-precision. */
88 mpq_class mudiv = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1]));
89 if (ans.lambda >= 0 && ans.lambda <= 1 &&
90 ((div > 0 && mudiv >= 0 && mudiv <= div) || (div < 0 && mudiv <= 0 && mudiv >= div)))
91 {
92 if (ans.lambda == 0 || ans.lambda == 1 || mudiv == 0 || mudiv == div) {
94 }
95 else {
97 }
98 }
99 else {
101 }
102 }
103 return ans;
104}
105#endif
106
107#ifdef WITH_GMP
108
109uint64_t hash_mpq_class(const mpq_class &value)
110{
111 /* TODO: better/faster implementation of this. */
112 return get_default_hash(float(value.get_d()));
113}
114
115#endif
116
117} // namespace blender::math
ATTR_WARN_UNUSED_RESULT const BMVert * v2
unsigned long long int uint64_t
isect_result< VecBase< T, Size > > isect_seg_seg(const VecBase< T, Size > &v1, const VecBase< T, Size > &v2, const VecBase< T, Size > &v3, const VecBase< T, Size > &v4)
VecBase< double, 2 > double2
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
VecBase< float, 2 > float2
enum blender::math::isect_result::@263246371363174113064124076102177335155253321343 kind