-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_rotate_save_binvox.py
147 lines (107 loc) · 4.47 KB
/
read_rotate_save_binvox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 8 14:18:09 2022
@author: Richard
"""
print("Reading, rotating and writing .binvox file. This program takes command-line filename arguments")
import sys
import os
#From command line, read in the filename and whether to readjust an incorrectly oriented image
filename = sys.argv[1]
readjust = False
if len(sys.argv) > 2:
if sys.argv[2]== 'readjust':
readjust = True
import binvox_rw_fastwrite2 as binvox_rw
import numpy as np
import copy
with open(filename, 'rb') as f:
model = binvox_rw.read_as_3d_array(f)
print("Voxelized model dimensions: " + str(model.dims))
def rotate_90(model, rotation_matrix):
if (model.dims[0] % 2) != 0:
raise ValueError('ODD DIMENSIONS: GO BACK AND FIX THIS')
voxel_grid_side_length =model.dims[0]
a = np.ones([voxel_grid_side_length,voxel_grid_side_length, voxel_grid_side_length], dtype=bool)
a = copy.deepcopy(~a)
true_placeholder = copy.deepcopy(~a[0,0,0])
true_indices = np.where(model.data==true_placeholder)
old_x_indices = true_indices[0]
old_y_indices = true_indices[1]
old_z_indices = true_indices[2]
old_indices = np.array([old_x_indices,old_y_indices,old_z_indices])
centering_offset = np.full([3,len(old_x_indices)],int(model.dims[0]/2) )
#establishing a new origin in the center of the old grid
offset_old_indices = np.subtract(old_indices, centering_offset)
offset_new_indices = np.matmul(rotation_matrix, offset_old_indices)
#print(offset_new_indices)
new_indices = np.add(offset_new_indices, centering_offset)
#print(new_indices)
x_shift_value = np.amin(new_indices[0,:])
y_shift_value = np.amin(new_indices[1,:])
z_shift_value = np.amin(new_indices[2,:])
#shifting the image to the edge of the axes to ensure consistency and prevent cutoff
new_indices[0,:] = np.subtract(new_indices[0,:], x_shift_value)
new_indices[1,:] = np.subtract(new_indices[1,:], y_shift_value)
new_indices[2,:] = np.subtract(new_indices[2,:], z_shift_value)
a[[new_indices[0,:]], [new_indices[1,:]],[new_indices[2,:]]] = true_placeholder
model.data = copy.deepcopy(a)
return model
#The order of writing:
#No rotation
#Rotating on right side (with respect to no rotation)
ccw_x_rotation_matrix = np.array([[1,0,0],[0,0,-1],[0,1,0]])
cw_y_rotation_matrix = np.array([[0,0,1],[0,1,0],[-1,0,0]])
#readjusting and overriting the original file if necessary
if readjust:
print('readjusting')
print(filename)
model = rotate_90(model, ccw_x_rotation_matrix)
os.remove(filename)
with open (filename,'wb') as fp:
model.write(fp)
print('Rotating...')
model = rotate_90(model, cw_y_rotation_matrix)
newfilename1 = filename[0:-7] + '.stl-100.binvox'
print('Writing...')
with open (newfilename1,'wb') as fp:
model.write(fp)
print('Rotating...')
#rotating on left side (by flipping the ride ride rotated image by 180 degrees)
model = rotate_90(model, np.matmul(cw_y_rotation_matrix,cw_y_rotation_matrix))
newfilename2 = filename[0:-7] + '.stl100.binvox'
print('Writing')
with open (newfilename2,'wb') as fp:
model.write(fp)
#rotating on front face
print('Rotating')
model = rotate_90(model, np.matmul(ccw_x_rotation_matrix,cw_y_rotation_matrix))
newfilename3 = filename[0:-7] + '.stl010.binvox'
print('Writing')
with open (newfilename3,'wb') as fp:
model.write(fp)
#rotating upside down
print('Rotating')
model = rotate_90(model, ccw_x_rotation_matrix)
newfilename4 = filename[0:-7] + '.stl00-1.binvox'
print('Writing')
with open (newfilename4,'wb') as fp:
model.write(fp)
print('Rotating')
#rotating on back face
model = rotate_90(model, ccw_x_rotation_matrix)
newfilename5 = filename[0:-7] + '.stl0-10.binvox'
print('Writing')
with open (newfilename5,'wb') as fp:
model.write(fp)
#ccw_x_rotation_matrix = np.array([[1,0,0],[0,0,-1],[0,1,0]])
#model.data = scipy.ndimage.interpolation.rotate(model.data, angle = 90)
#model = rotate_90(model, ccw_x_rotation_matrix)
#model = rotate_90(model, ccw_x_rotation_matrix)
#model.data = scipy.ndimage.interpolation.rotate(model.data, 90, axes = (1,0))
#print("Rotation calculated")
#model = rotate_90(model, x_rotation_matrix)
#print("Rotation calculated")
#model = rotate_90(model, x_rotation_matrix)
#with open ('chair_2.binvox','wb') as fp:
# model.write(fp)