1 #ifndef BLANK_GEOMETRY_LOCATION_HPP_
2 #define BLANK_GEOMETRY_LOCATION_HPP_
12 using Coarse = glm::ivec3;
13 using CoarseScalar = int;
14 using Fine = glm::tvec3<T>;
16 using Self = Location<T>;
23 /// scale of coarse vs fine coordinates
24 static constexpr CoarseScalar scale = 16;
25 /// scale with same type as fine
26 static constexpr FineScalar fscale = scale;
27 /// scale in three dimensions
28 static Coarse Extent() noexcept { return Coarse(scale); }
29 /// extent with same type as fine
30 static Fine FExtent() noexcept { return Fine(fscale); }
33 Location() noexcept : chunk(CoarseScalar(0)), block(FineScalar(0)) { }
34 Location(const Coarse &c, const Fine &b) noexcept : chunk(c), block(b) { }
36 /// from absolute position
38 explicit Location(const Fine &b) noexcept : chunk(CoarseScalar(0)), block(b) { }
40 /// make sure fine coordinates are within [0,scale)
41 /// use this if block isn't too far out of range
42 Self &Correct() noexcept;
43 /// use this if block is way out of range
44 Self &Sanitize() noexcept;
46 /// resolve absolute position
47 Fine Absolute() const noexcept {
48 return Fine(chunk * Extent()) + block;
50 /// get location relative to given coarse coordinates
51 Self Relative(const Coarse &reference) const noexcept {
52 return Self(chunk - reference, block);
54 /// get difference between this and given location
55 /// (= this - given, points from given to this)
56 /// returned location is not sanitized
57 Self Difference(const Location &other) const noexcept {
58 return Self(chunk - other.chunk, block - other.block);
63 template<class T> constexpr typename Location<T>::CoarseScalar Location<T>::scale;
64 template<class T> constexpr typename Location<T>::FineScalar Location<T>::fscale;
67 inline Location<T> &Location<T>::Correct() noexcept {
68 while (block.x >= fscale) {
76 while (block.y >= fscale) {
84 while (block.z >= fscale) {
96 inline Location<T> &Location<T>::Sanitize() noexcept {
97 Coarse diff = Coarse(block) / Extent();
99 block -= diff * Extent();
100 // may leave negative coordinates in block
104 using ExactLocation = Location<float>;
105 using RoughLocation = Location<int>;