However, how about encapsulating everything into a class that might use 1D array to store all data and simply provides an interface that mimics 3D? |
this is nice for 2d; for 3d and up its almost required if you want to keep your sanity. You don't have to, but it makes so many things much easier (single linear loop copy / read -write to file etc, and many other operations can be trivialized).
C memory is in simple block format.
1d: c1 c2 c3
2d: r1c1 r1c2 r1c3 r2c1 r2c2 …
3d: z1r1c1 z1r1c2... z1rncn z2r1c1 and so on.
where this breaks down is if you need to grow the thing mid-processing. Its hard to add more rows here: you have to make a new one and copy over with adjusted dimension conversion. triple vector can grow cleanly, but it is a big mess to do most anything else. Got to decide if growing is what you need most, or if fast processing is what you need most.
watch for page fault problems if the structure gets large. you want to iterate across it sequentially, not, for example, hopping around in the z dimension above, that could cause a page fault every single access if it were very large!