summaryrefslogtreecommitdiff
path: root/c++/ico.hpp
blob: 257f12de22d4da18eb8197cae16ca72a6649e324 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once

#include <array>
#include <cassert>
#include <cstdint>

namespace kel {
namespace impl {
struct ico_surface_helper {
	/**
	 * On each triangle we assume a ccw order and we start on the top-left part of the
	 * triangle. The orientation of the triangle is determined by the horizontal line
	 * (on the 2D foldout of the icosahedron) which is the down direction.  
	 */
	static constexpr std::array<std::array<uint8_t,3u>, 20u> neighbour_map {{
		{1,4,5},//0
		{2,0,6},//1
		{3,1,7},//2
		{4,2,8},//3
		{0,3,9},//4
		{14,10,0},//5
		{10,11,1},//6
		{11,12,2},//7
		{12,13,3},//8
		{13,14,4},//9
		{6,5,15},//10
		{7,6,16},//11
		{8,7,17},//12
		{9,8,18},//13
		{5,9,19},//14
		{19,16,10},//15
		{15,17,11},//16
		{16,18,12},//17
		{17,19,13},//18
		{18,15,14} //19
	}};
};
}

template<uint64_t SubD>
class ico_addr {
private:
	std::array<uint8_t, SubD> vals_;
public:
	uint8_t& at(uint64_t i){
		return vals_[i];
	}

	const uint8_t& at(uint64_t i) const {
		return vals_[i];
	}

	constexpr uint64_t size() const {
		return SubD;
	}

	bool is_equal(const ico_addr<SubD>& rhs) const {
		bool eq = true;

		for(uint64_t i = 0u; i < size(); ++i){
			eq &= (at(i) == rhs.at(i));
		}

		return eq;
	}

	bool has_zero() const {
		uint64_t z_ctr = 0u;
		for(uint64_t i = 1u; i < size(); ++i){
			z_ctr += (at(i) == 0u ? 1u : 0u);
		}
		return (z_ctr > 0u);
	}

	std::array<uint64_t,4u> count_groups() const {
		std::array<uint64_t,4u> grps{0,0,0,0};

		for(uint64_t i = 1u; i < size(); ++i){
			auto& addr_i = vals_[i];
			assert(addr_i < 4);

			++grps[addr_i];
		}

		return grps;
	}

	uint64_t count_non_zero_groups() const {
		auto ctr = count_groups();

		uint64_t grp_ctr = 0u;
		for(uint64_t i = 1u; i < ctr.size(); ++i){
			grp_ctr += (ctr[i] > 0u ? 1u : 0u);
		}
		return grp_ctr;
	}

	bool has_all_non_zero_groups() const {
		auto ctr = count_non_zero_groups();
		return (ctr == 3u);
	}

	ico_addr<SubD> go_to(uint8_t dir_i){
		ico_addr<SubD> cpy_addr;
		cpy_addr.vals_ = vals_;

		if(at(size() - 1u) == 0u){
			cpy_addr.at(size() - 1u) = dir_i + 1u;
		}
		if(at(size() - 1u) == (dir_i + 1u)){
			cpy_addr.at(size() - 1u) = 0u;
		}

		return cpy_addr;
	}

	bool borders_root() const {
		return not (has_all_non_zero_groups() or has_zero());
	}
};

template<typename T, uint64_t D>
class ico_triangle {
private:
	std::array<ico_triangle<T,D-1u>,4u> subdivs_;
public:

	ico_triangle<T,D-1u>& at(uint8_t i){
		return subdivs_[i];
	}

	template<uint64_t AddrOrigLen>
	ico_triangle<T,0u> at(const ico_addr<AddrOrigLen>& addr, uint64_t next_i){
		auto& addr_i = addr[next_i];

		auto& sub_triangle = subdivs_[addr_i];
		if constexpr ( D == 1u){
			return sub_triangle;
		}
		return sub_triangle.at(addr,next_i+1u);
	}
};

template<typename T>
class ico_triangle<T,0u> final {
private:
	T val_;
public:
};

namespace impl {
}

template<typename T, uint64_t SubD>
class icosahedron final {
private:
	std::array<ico_triangle<T,SubD>,20u> surfaces_;
public:

	template<uint64_t AddrD>
	ico_triangle<T,SubD+1u-AddrD> at(const ico_addr<AddrD>& addr){
		static_assert(AddrD <= (SubD+1u), "Addr length to large.");
		static_assert(AddrD > 0u, "Addr length to small.");

		auto& root_addr = addr[0u];
		auto& root_triangle = surfaces_[root_addr];
		return root_triangle.at(addr, 1u);
	}
};
}