summaryrefslogtreecommitdiff
path: root/src/cuboid.h
blob: c4ff8033514405f783e617b129cb161a13b256e9 (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
#pragma once

#include <array>

class Cuboid {
private:
	const std::array<std::size_t,3> _size;
	const std::size_t _volume;

public:
	Cuboid(std::size_t nX, std::size_t nY, std::size_t nZ):
		_size{nX, nY, nZ},
		_volume(nX*nY*nZ) { }

	std::size_t volume() const {
		return _volume;
	}

	std::size_t index(unsigned iX, unsigned iY, unsigned iZ) const {
		return iX*_size[1]*_size[2] + iY*_size[2] + iZ;
	}

	int operator[](unsigned i) const {
		return _size[i];
	}

	template <typename F>
	void traverse(F f) {
		for (int iX = 0; iX < _size[0]; ++iX) {
			for (int iY = 0; iY < _size[1]; ++iY) {
				for (int iZ = 0; iZ < _size[2]; ++iZ) {
					f(iX, iY, iZ, index(iX,iY,iZ));
				}
			}
		}
	}
};