Home /Modules/impl/slvoxelgrid_basic.sleela

slvoxelgrid_basic.sleela

impl/examples/slvoxelgrid_basic.sleela · 49 line(s) · sleela

#sleela 2.1
import spatial;
// SLVoxelGrid checks: flat indexing, bounds, occupancy scan, occupied walk, decode.
class SLVoxelGridBasic {
  void main() {
    SLVoxelGrid g = new SLVoxelGrid();
    g.allocate(4, 3, 2, 0);       // 4*3*2 = 24 cells, empty sentinel 0
    print(g.size());              // 24
    print(g.getWidth());          // 4
    print(g.inBounds(3, 2, 1));   // true
    print(g.inBounds(4, 0, 0));   // false

    print(g.indexOf(0, 0, 0));    // 0
    print(g.indexOf(1, 0, 0));    // 1 (x fastest)
    print(g.indexOf(0, 1, 0));    // 4 (y stride = width)
    print(g.indexOf(0, 0, 1));    // 12 (z stride = width*height)

    print(g.get(2, 1, 1));        // 0: empty
    print(g.set(2, 1, 1, 7));     // true
    print(g.get(2, 1, 1));        // 7
    print(g.set(9, 9, 9, 1));     // false: out of range
    print(g.isEmpty(2, 1, 1));    // false
    print(g.isEmpty(0, 0, 0));    // true

    g.set(0, 0, 0, 5);
    g.set(3, 2, 1, 9);
    print(g.occupancy());         // 3

    // Walk only the occupied cells and decode their coordinates.
    int at = g.nextOccupied(0);
    print(at);                    // 0 (cell 0,0,0)
    print(g.xOf(at));             // 0
    print(g.yOf(at));             // 0
    print(g.zOf(at));             // 0
    at = g.nextOccupied(at + 1);
    print(at);                    // 18 (cell 2,1,1 -> 2 + 1*4 + 1*12)
    at = g.nextOccupied(at + 1);
    print(at);                    // 23 (cell 3,2,1 -> 3 + 2*4 + 1*12)
    print(g.xOf(at));             // 3
    print(g.yOf(at));             // 2
    print(g.zOf(at));             // 1
    at = g.nextOccupied(at + 1);
    print(at);                    // -1: no more

    g.clear();
    print(g.occupancy());         // 0
  }
}