binvox-rw-py icon indicating copy to clipboard operation
binvox-rw-py copied to clipboard

Bug in the write function when state change after 255 identical states

Open Lswbanban opened this issue 4 years ago • 0 comments

I guess the write function is bugged if you have exactly 255 zero to write, and then the next state the 256th character is a 1 (or vice versa, i.e. if you switch state after exactly 255 count).

The fix is easy: in the else of the "if c==state", add another test before dumping, the test should be: "if ctr > 0:", otherwise you will write a state with zero count, and actually may shift the whole data.

The whole code becomes:

for c in voxels_flat:
        if c==state:
            ctr += 1
            # if ctr hits max, dump
            if ctr==255:
                fp.write(chr(state))
                fp.write(chr(ctr))
                ctr = 0
        else:
            # if switch state, dump
            if ctr > 0:
                fp.write(chr(state))
                fp.write(chr(ctr))
            state = c
            ctr = 1

Lswbanban avatar Mar 10 '20 04:03 Lswbanban