Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_math_vec_mpq_types.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7dad2921dda1f9831b69e4235cd20f19d13a511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

/** \file
 * \ingroup bli
 */

#include "BLI_math_vec_types.hh"

#ifdef WITH_GMP

#  include "BLI_math_mpq.hh"

namespace blender {

using mpq2 = vec_base<mpq_class, 2>;
using mpq3 = vec_base<mpq_class, 3>;

namespace math {

uint64_t hash_mpq_class(const mpq_class &value);

template<> inline uint64_t vector_hash(const mpq2 &vec)
{
  return hash_mpq_class(vec.x) ^ (hash_mpq_class(vec.y) * 33);
}

template<> inline uint64_t vector_hash(const mpq3 &vec)
{
  return hash_mpq_class(vec.x) ^ (hash_mpq_class(vec.y) * 33) ^ (hash_mpq_class(vec.z) * 33 * 37);
}

/**
 * Cannot do this exactly in rational arithmetic!
 * Approximate by going in and out of doubles.
 */
template<> inline mpq_class length(const mpq2 &a)
{
  return mpq_class(sqrt(length_squared(a).get_d()));
}

/**
 * Cannot do this exactly in rational arithmetic!
 * Approximate by going in and out of doubles.
 */
template<> inline mpq_class length(const mpq3 &a)
{
  return mpq_class(sqrt(length_squared(a).get_d()));
}

/**
 * The buffer avoids allocating a temporary variable.
 */
inline mpq_class distance_squared_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer)
{
  buffer = a;
  buffer -= b;
  return dot(buffer, buffer);
}

/**
 * The buffer avoids allocating a temporary variable.
 */
inline mpq_class dot_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer)
{
  buffer = a;
  buffer *= b;
  buffer.x += buffer.y;
  buffer.x += buffer.z;
  return buffer.x;
}

}  // namespace math

}  // namespace blender

#endif /* WITH_GMP */