Blender V4.3
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
9#include "BLI_hash.hh"
10#include "BLI_math_vector.hh"
12#include "BLI_span.hh"
13#include "BLI_utildefines.h"
14
15namespace blender::math {
16
17template<>
19 const float2 &v2,
20 const float2 &v3,
21 const float2 &v4)
22{
24 float div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
25 if (div == 0.0f) {
26 ans.lambda = 0.0f;
28 }
29 else {
30 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
31 float mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
32 if (ans.lambda >= 0.0f && ans.lambda <= 1.0f && mu >= 0.0f && mu <= 1.0f) {
33 if (ans.lambda == 0.0f || ans.lambda == 1.0f || mu == 0.0f || mu == 1.0f) {
35 }
36 else {
38 }
39 }
40 else {
42 }
43 }
44 return ans;
45}
46
47template<>
49 const double2 &v2,
50 const double2 &v3,
51 const double2 &v4)
52{
54 double div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
55 if (div == 0.0) {
56 ans.lambda = 0.0;
58 }
59 else {
60 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
61 double mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
62 if (ans.lambda >= 0.0 && ans.lambda <= 1.0 && mu >= 0.0 && mu <= 1.0) {
63 if (ans.lambda == 0.0 || ans.lambda == 1.0 || mu == 0.0 || mu == 1.0) {
65 }
66 else {
68 }
69 }
70 else {
72 }
73 }
74 return ans;
75}
76
77#ifdef WITH_GMP
78template<>
79isect_result<mpq2> isect_seg_seg(const mpq2 &v1, const mpq2 &v2, const mpq2 &v3, const mpq2 &v4)
80{
81 isect_result<mpq2> ans;
82 mpq_class div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
83 if (div == 0.0) {
84 ans.lambda = 0.0;
86 }
87 else {
88 ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
89 /* Avoid dividing mu by div: it is expensive in multi-precision. */
90 mpq_class mudiv = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1]));
91 if (ans.lambda >= 0 && ans.lambda <= 1 &&
92 ((div > 0 && mudiv >= 0 && mudiv <= div) || (div < 0 && mudiv <= 0 && mudiv >= div)))
93 {
94 if (ans.lambda == 0 || ans.lambda == 1 || mudiv == 0 || mudiv == div) {
96 }
97 else {
99 }
100 }
101 else {
103 }
104 }
105 return ans;
106}
107#endif
108
109#ifdef WITH_GMP
110
111uint64_t hash_mpq_class(const mpq_class &value)
112{
113 /* TODO: better/faster implementation of this. */
114 return get_default_hash(float(value.get_d()));
115}
116
117#endif
118
119} // namespace blender::math
ATTR_WARN_UNUSED_RESULT const BMVert * v2
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)
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
unsigned __int64 uint64_t
Definition stdint.h:90
enum blender::math::isect_result::@111 kind