normals calculation fix
[voxconv.git] / vox_3dbmp.d
blob43e68e81ad1f1edaf5d643ff7cce21be6acdbfb1
1 module vox_3dbmp;
3 import iv.bclamp;
4 import iv.glbinds.utils;
5 import iv.cmdcongl;
6 import iv.strex;
7 import iv.vfs;
8 import iv.vfs.io;
9 import iv.vmath;
12 // ////////////////////////////////////////////////////////////////////////// //
14 this is just a voxel cube, where each voxel represents by one bit.
15 it is useful for fast "yes/no" queries and modifications, and is used
16 in "hollow fill" and t-junction fixer algorithms.
18 struct Vox3DBitmap {
19 uint xsize = 0, ysize = 0, zsize = 0;
20 uint xwdt = 0, xywdt = 0;
21 uint[] bmp; // bit per voxel
23 public:
24 void clear () {
25 delete bmp; bmp = null;
26 xsize = ysize = zsize = xwdt = xywdt = 0;
29 void setSize (uint xs, uint ys, uint zs) {
30 clear();
31 if (!xs || !ys || !zs) return;
32 xsize = xs;
33 ysize = ys;
34 zsize = zs;
35 xwdt = (xs+31)/32;
36 assert(xwdt<<5 >= xs);
37 xywdt = xwdt*ysize;
38 bmp = new uint[xywdt*zsize];
39 bmp[] = 0;
42 // returns old value
43 uint setPixel (int x, int y, int z) nothrow @trusted @nogc {
44 pragma(inline, true);
45 if (x < 0 || y < 0 || z < 0) return 1;
46 if (cast(uint)x >= xsize || cast(uint)y >= ysize || cast(uint)z >= zsize) return 1;
47 uint *bp = bmp.ptr+cast(uint)z*xywdt+cast(uint)y*xwdt+(cast(uint)x>>5);
48 const uint bmask = 1U<<(cast(uint)x&0x1f);
49 const uint res = (*bp)&bmask;
50 *bp |= bmask;
51 return res;
54 void resetPixel (int x, int y, int z) nothrow @trusted @nogc {
55 pragma(inline, true);
56 if (x < 0 || y < 0 || z < 0) return;
57 if (cast(uint)x >= xsize || cast(uint)y >= ysize || cast(uint)z >= zsize) return;
58 bmp.ptr[cast(uint)z*xywdt+cast(uint)y*xwdt+(cast(uint)x>>5)] &= ~(1U<<(cast(uint)x&0x1f));
61 uint getPixel (int x, int y, int z) const nothrow @trusted @nogc {
62 pragma(inline, true);
63 if (x < 0 || y < 0 || z < 0) return 1;
64 if (cast(uint)x >= xsize || cast(uint)y >= ysize || cast(uint)z >= zsize) return 1;
65 return (bmp.ptr[cast(uint)z*xywdt+cast(uint)y*xwdt+(cast(uint)x>>5)]&(1U<<(cast(uint)x&0x1f)));