dimatura / binvox-rw-py Goto Github PK
View Code? Open in Web Editor NEWPython module to read and write .binvox files.
License: GNU General Public License v3.0
Python module to read and write .binvox files.
License: GNU General Public License v3.0
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
Hi.
I still have this two failures when executing the binvox file. Anybody has an idea or knows the solution for that ?
I would appreciate it a lot. Thanks !
**********************************************************************
File ".../binvox.py", line 29, in __main__
Failed example:
m1.scale
Expected:
41.133000000000003
Got:
41.133
**********************************************************************
File ".../binvox.py", line 60, in __main__
Failed example:
np.all(ms.data[:, np.lexsort(ms.data)] == data_ds[:, np.lexsort(data_ds)])
Expected:
True
Got:
False
**********************************************************************
1 items had failures:
2 of 18 in __main__
***Test Failed*** 2 failures.
What are these x,y,z coordinates locations, outputting by binvox_rw.read_as_coord_array(f)
function. Are they mid point of any voxel or any vertices of a particular voxel. I want to find all the 8 vertices of a voxel. So please let me know the conversion formulas.
Hi, I'm new and I apologize for my lack of experience. I have a problem how to convert .off file to .binvox format
Directory: C:....\ModelNet10\bathtub\test
-a---- 14.08.2014 07:50 73975 bathtub_0107.off
-a---- 14.08.2014 07:50 14938 bathtub_0108.off
-a---- 14.08.2014 07:51 428850 bathtub_0109.off
-a---- 14.08.2014 07:51 338935 bathtub_0110.off
-a---- 14.08.2014 07:51 470698 bathtub_0111.off
-a---- 14.08.2014 07:50 117702 bathtub_0112.off
-a---- 14.08.2014 07:50 48393 bathtub_0113.off
-a---- 14.08.2014 07:51 143730 bathtub_0114.off
-a---- 14.08.2014 07:51 191575 bathtub_0115.off
-a---- 14.08.2014 07:51 15594 bathtub_0116.off
-a---- 14.08.2014 07:51 311254 bathtub_0117.off
-a---- 14.08.2014 07:50 286763 bathtub_0118.off
-a---- 14.08.2014 07:50 304718 bathtub_0119.off
-a---- 14.08.2014 07:50 42378 bathtub_0120.off
-a---- 14.08.2014 07:51 118111 bathtub_0121.off
-a---- 14.08.2014 07:50 411882 bathtub_0122.off
-a---- 14.08.2014 07:51 56186 bathtub_0123.off
-a---- 14.08.2014 07:50 157223 bathtub_0124.off
-a---- 14.08.2014 07:50 383230 bathtub_0125.off
-a---- 14.08.2014 07:50 9986 bathtub_0126.off
-a---- 14.08.2014 07:50 36448 bathtub_0127.off
-a---- 14.08.2014 07:51 216594 bathtub_0128.off
-a---- 14.08.2014 07:51 30617 bathtub_0129.off
-a---- 14.08.2014 07:51 112736 bathtub_0130.off
-a---- 14.08.2014 07:51 56037 bathtub_0131.off
-a---- 14.08.2014 07:50 33398 bathtub_0132.off
-a---- 14.08.2014 07:50 154185 bathtub_0133.off
-a---- 14.08.2014 07:50 54843 bathtub_0134.off
-a---- 14.08.2014 07:50 605524 bathtub_0135.off
-a---- 14.08.2014 07:50 155996 bathtub_0136.off
-a---- 14.08.2014 07:50 8926 bathtub_0137.off
-a---- 14.08.2014 07:50 138728 bathtub_0138.off
-a---- 14.08.2014 07:50 73863 bathtub_0139.off
-a---- 14.08.2014 07:50 31482 bathtub_0140.off
-a---- 14.08.2014 07:51 2774386 bathtub_0141.off
-a---- 14.08.2014 07:50 157153 bathtub_0142.off
-a---- 14.08.2014 07:51 50353 bathtub_0143.off
-a---- 14.08.2014 07:50 25148 bathtub_0144.off
-a---- 14.08.2014 07:50 304505 bathtub_0145.off
-a---- 14.08.2014 07:51 575635 bathtub_0146.off
-a---- 14.08.2014 07:50 68845 bathtub_0147.off
-a---- 14.08.2014 07:50 34615 bathtub_0148.off
-a---- 14.08.2014 07:52 5238305 bathtub_0149.off
-a---- 14.08.2014 07:51 4085485 bathtub_0150.off
-a---- 14.08.2014 07:50 243233 bathtub_0151.off
-a---- 14.08.2014 07:51 39501 bathtub_0152.off
-a---- 14.08.2014 07:51 310543 bathtub_0153.off
-a---- 14.08.2014 07:50 75717 bathtub_0154.off
-a---- 14.08.2014 07:51 390411 bathtub_0155.off
-a---- 14.08.2014 07:51 619530 bathtub_0156.off
binvox -c -d 24 -t binvox bathtub_0107.off
output
--- [binvox] mesh voxelizer, version 1.35, build #794 on 2021/10/26 11:37:14, running on Windows
--- written by Patrick Min, 2004-2021
using carving method only
Windows does not allow widths below 116, increasing dimension to 192 and performing -down as needed
Note: setting -dmin to 1
loading model file...
MeshFileIdentifier::*create_mesh_file(bathtub_0107.off)
OffMeshFile::load
starting to read 1568 vertices
1500 lines read
read 1568 vertices
3391 lines read
Read 1820 faces, 1568 vertices.
Mesh memory use is 18142 KB (17 MB)
Mesh::normalize, bounding box: [-22.8962, -46.0039, -19.47, 1] - [22.8962, 46.0039, 19.47, 1]
longest length: 92.0078
normalization transform:
(1) translate [22.8962, 46.0039, 19.47, 1], (2) scale 0.0108686, (3) translate [0, 0, 0]
got mesh bounding box [-22.8962, -46.0039, -19.47, 1] - [22.8962, 46.0039, 19.47, 1]
voxel model dimension: 192
GLwindow::init_lighting
Graphics card and driver information:
Vendor: Intel
Renderer: Intel(R) UHD Graphics 630
Version: 4.6.0 - Build 30.0.101.1153
Voxels::init(192, 192, 192, 0)
Voxelizer constructor
SimpleMeshView constructor
starting voxelization...
Voxelizer::carve_voxelize
doing x direction
doing y direction
doing z direction
Voxelizer::fill_voxels
Voxels::clear
Voxels::downsample from 7077888 to 884736
Voxels::downsample from 884736 to 110592
I didn't see bathtub_0107.binvox format
Hello,
I have just created a binvox file and managed to load it using your program :)
I had one small question: How can I retrieve the size of each voxel ? I need to know the volume of each cube so knowing the edge length would be enough.
Thanks for your help,
Hi,
I modified some codes in 'write' of binvox_rw.py to resolve the error - 'AttributeError: 'str' object has no attribute 'write' , when executing the command - model.write('dilated.binvox').
Hope it helps.
def write(voxel_model, fp):
""" Write binary binvox format.
Note that when saving a model in sparse (coordinate) format, it is first
converted to dense format.
Doesn't check if the model is 'sane'.
"""
if voxel_model.data.ndim==2:
# TODO avoid conversion to dense
dense_voxel_data = sparse_to_dense(voxel_model.data, voxel_model.dims)
else:
dense_voxel_data = voxel_model.data
print('fp : ', fp)
with open(fp, 'w') as fw:
fw.write('#binvox 1\n')
fw.write('dim '+' '.join(map(str, voxel_model.dims))+'\n')
fw.write('translate '+' '.join(map(str, voxel_model.translate))+'\n')
fw.write('scale '+str(voxel_model.scale)+'\n')
fw.write('data\n')
if not voxel_model.axis_order in ('xzy', 'xyz'):
raise ValueError('Unsupported voxel model axis order')
if voxel_model.axis_order=='xzy':
voxels_flat = dense_voxel_data.flatten()
elif voxel_model.axis_order=='xyz':
voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten()
# keep a sort of state machine for writing run length encoding
state = voxels_flat[0]
ctr = 0
for c in voxels_flat:
if c==state:
ctr += 1
# if ctr hits max, dump
if ctr==255:
fw.write(chr(state))
fw.write(chr(ctr))
ctr = 0
else:
# if switch state, dump
fw.write(chr(state))
fw.write(chr(ctr))
state = c
ctr = 1
# flush out remainders
if ctr > 0:
fw.write(chr(state))
fw.write(chr(ctr))
Nice little tool; saved me some work but this bug took some time to figure out...
The write-method does not work for python3. The problem is handling of writing bytes in Python3 has changed.
I am not sure if I was doing something wrong but I was trying to push a fix but it did not work. I am very new to git so if you can help me push the change I would appreciate it. It is probably something simple I have forgotten. What i did:
git clone https://github.com/dimatura/binvox-rw-py.git
# did my changes
git add binvox_rw.py
git commit -m "Issues with writting with Python3. Fixed by using import struct; struct.pack('B',byte_value)"
git push origin HEAD:master
remote: Permission to dimatura/binvox-rw-py.git denied to pclausen.
fatal: unable to access 'https://github.com/dimatura/binvox-rw-py.git/': The requested URL returned error: 403
Thanks. Below the patch.
diff --git a/binvox_rw.py b/binvox_rw.py
index 704fc1f..6151a1e 100644
--- a/binvox_rw.py
+++ b/binvox_rw.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# Copyright (C) 2012 Daniel Maturana
# This file is part of binvox-rw-py.
#
@@ -62,6 +63,7 @@ True
"""
import numpy as np
+import struct
class Voxels(object):
""" Holds a binvox model.
@@ -228,6 +230,16 @@ def sparse_to_dense(voxel_data, dims, dtype=np.bool):
#TODO ensure this is right when dims are not all same
#"""
#return x*(dims[1]*dims[2]) + z*dims[1] + y
+
+def bwrite(fp,s):
+ fp.write(s.encode())
+
+
+def write_pair(fp,state, ctr):
+ fp.write(struct.pack('B',state))
+ fp.write(struct.pack('B',ctr))
+
+
def write(voxel_model, fp):
""" Write binary binvox format.
@@ -244,11 +256,11 @@ def write(voxel_model, fp):
else:
dense_voxel_data = voxel_model.data
- fp.write('#binvox 1\n')
- fp.write('dim '+' '.join(map(str, voxel_model.dims))+'\n')
- fp.write('translate '+' '.join(map(str, voxel_model.translate))+'\n')
- fp.write('scale '+str(voxel_model.scale)+'\n')
- fp.write('data\n')
+ bwrite(fp,'#binvox 1\n')
+ bwrite(fp,'dim '+' '.join(map(str, voxel_model.dims))+'\n')
+ bwrite(fp,'translate '+' '.join(map(str, voxel_model.translate))+'\n')
+ bwrite(fp,'scale '+str(voxel_model.scale)+'\n')
+ bwrite(fp,'data\n')
if not voxel_model.axis_order in ('xzy', 'xyz'):
raise ValueError('Unsupported voxel model axis order')
@@ -265,19 +277,16 @@ def write(voxel_model, fp):
ctr += 1
# if ctr hits max, dump
if ctr==255:
- fp.write(chr(state))
- fp.write(chr(ctr))
+ write_pair(fp, state, ctr)
ctr = 0
else:
# if switch state, dump
- fp.write(chr(state))
- fp.write(chr(ctr))
+ write_pair(fp, state, ctr)
state = c
ctr = 1
# flush out remainders
if ctr > 0:
- fp.write(chr(state))
- fp.write(chr(ctr))
+ write_pair(fp, state, ctr)
if __name__ == '__main__':
import doctest
I am trying to convert .step file to binvox. Is it possible to convert? Can anybody give suggestions?
Thank you
If you open a file in binary 'wb' mode and call the write function you get some errors:
fp = open("test.binvox", "wb")
voxels.write(fp)
To fix the error, I have:
Not sure if it is the best fix, but at least it worked for me.
The default binvox resolution for shapenet is 128128128 for example, and for some use we may need a different size (like 323232). Can you add that resize function please? It would be really helpful
Hello thanks for the software! Can you please update to work with new Numpy to handle their bool data types.
Otherwise you get an error for using np.bool
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.