File size: 9,340 Bytes
91daf98 | 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | import os
import sys
import numpy as np
from geometry.arc import Arc
from geometry.circle import Circle
from geometry.line import Line
from geometry import geom_utils
import pdb
class OBJParser:
"""
A class to read an OBJ file containing the sketch data
and hand it back in a form which is easy to work with.
"""
def __init__(self, pathname=None):
self.pathname = pathname
def convert_vertices(self, vertices):
"""Convert all the vertices to .obj format"""
vertex_strings = ""
for pt in vertices:
# e.g. v 0.123 0.234 0.345 1.0
vertex_string = f"v {pt[0]} {pt[1]}\n"
vertex_strings += vertex_string
return vertex_strings
def convert_curves(self, faces):
curve_strings = ""
total_curve = 0
# Faces (multiple closed regions)
for group_idx, loops in enumerate(faces):
curve_strings += f"\nface\n"
# Multiple loops (inner and outer)
for loop in loops:
if loop[0].is_outer:
curve_strings += f"out\n"
else:
curve_strings += f"in\n"
# All curves in one loop
for curve in loop:
total_curve += 1
if curve.type == 'line':
curve_strings += f"l {curve.start_idx} {curve.end_idx}\n"
elif curve.type == 'circle':
curve_strings += f"c {curve.center_idx} {curve.radius_idx}\n"
elif curve.type == 'arc':
curve_strings += f"a {curve.start_idx} {curve.mid_idx} {curve.center_idx} {curve.end_idx}\n"
return curve_strings, total_curve
def parse3d(self, point3d):
x = point3d[0]
y = point3d[1]
z = point3d[2]
return str(x)+' '+str(y)+' '+str(z)
def write_obj2(self, file, vertices, faces, meta_info, scale=None):
""" Write to .obj file """
vertex_strings = self.convert_vertices(vertices)
curve_strings, total_curve = self.convert_curves(faces)
with open(file, "w") as fh:
# Write Meta info
fh.write("# WaveFront *.obj file\n")
fh.write(f"# Vertices: {len(vertices)}\n")
fh.write(f"# Curves: {total_curve}\n")
fh.write("\n")
# Write vertex and curve
fh.write(vertex_strings)
fh.write("\n")
fh.write(curve_strings)
fh.write("\n")
#Write extrude value
fh.write("ExtrudeOperation: " + meta_info['set_op']+"\n")
extrude_string = 'Extrude '
for value in meta_info['extrude_value']:
extrude_string += str(value)+' '
fh.write(extrude_string)
fh.write("\n")
#Write refe plane transformation
p_orig = self.parse3d(meta_info['t_orig'])
x_axis = self.parse3d(meta_info['t_x'])
y_axis = self.parse3d(meta_info['t_y'])
z_axis = self.parse3d(meta_info['t_z'])
fh.write('T_origin '+p_orig)
fh.write("\n")
fh.write('T_xaxis '+x_axis)
fh.write("\n")
fh.write('T_yaxis '+y_axis)
fh.write("\n")
fh.write('T_zaxis '+z_axis)
fh.write("\n")
# Normalized object
if scale is not None:
fh.write('Scale '+str(scale))
def write_obj(self, file, curve_strings, total_curve, vertex_strings, total_v, meta_info, scale=None):
""" Write to .obj file """
#vertex_strings = self.convert_vertices(vertices)
#curve_strings, total_curve = self.convert_curves(faces)
with open(file, "w") as fh:
# Write Meta info
fh.write("# WaveFront *.obj file\n")
fh.write(f"# Vertices: {total_v}\n")
fh.write(f"# Curves: {total_curve}\n")
fh.write("\n")
# Write vertex and curve
fh.write(vertex_strings)
fh.write("\n")
fh.write(curve_strings)
fh.write("\n")
#Write extrude value
fh.write("ExtrudeOperation: " + meta_info['set_op']+"\n")
extrude_string = 'Extrude '
for value in meta_info['extrude_value']:
extrude_string += str(value)+' '
fh.write(extrude_string)
fh.write("\n")
#Write refe plane transformation
p_orig = self.parse3d(meta_info['t_orig'])
x_axis = self.parse3d(meta_info['t_x'])
y_axis = self.parse3d(meta_info['t_y'])
z_axis = self.parse3d(meta_info['t_z'])
fh.write('T_origin '+p_orig)
fh.write("\n")
fh.write('T_xaxis '+x_axis)
fh.write("\n")
fh.write('T_yaxis '+y_axis)
fh.write("\n")
fh.write('T_zaxis '+z_axis)
fh.write("\n")
# Normalized object
if scale is not None:
fh.write('Scale '+str(scale))
def parse_file(self, scale=1.0):
"""
Parse obj file
Return
vertex 2D location numpy
curve list (geometry class)
extrude parameters
"""
assert self.pathname is not None, "File is None"
assert self.pathname.exists(), "No such file"
# Parse file
vertex_list = []
loops = []
closed_loop = []
# Read vertice
with open(self.pathname) as obj_file:
for line in obj_file:
tokens = line.split()
if not tokens:
continue
line_type = tokens[0]
# Vertex
if line_type == "v":
vertex_list.append([float(x) for x in tokens[1:]])
vertices = np.array(vertex_list, dtype=np.float64) * scale
# Read curves
faces = []
loops = []
loop = []
# Read in all lines
lines = []
with open(self.pathname) as obj_file:
for line in obj_file:
lines.append(line)
# Parse all lines
faces = []
for str_idx, line in enumerate(lines):
tokens = line.split()
if not tokens:
continue
line_type = tokens[0]
# Start of a new face
if line_type == "face":
faces.append(self.read_face(lines, str_idx+1, vertices))
# Read meta data
meta_data = line.strip('# ').strip(' \n').split(' ')
meta_name = meta_data[0]
if meta_name == 'Extrude':
extrude_values = [float(x) for x in meta_data[1:]]
extrude_values = [x*scale for x in extrude_values]
elif meta_name == 'T_origin':
t_orig = [float(x) for x in meta_data[1:]]
t_orig = [x*scale for x in t_orig]
elif meta_name == 'T_xaxis':
t_x = [float(x) for x in meta_data[1:]]
elif meta_name == 'T_yaxis':
t_y = [float(x) for x in meta_data[1:]]
elif meta_name == 'T_zaxis':
t_z = [float(x) for x in meta_data[1:]]
elif meta_name == 'ExtrudeOperation:':
set_op = meta_data[1]
meta_info = {'extrude_value': extrude_values,
'set_op': set_op,
't_orig': t_orig,
't_x': t_x,
't_y': t_y,
't_z': t_z,
}
return vertices, faces, meta_info
def read_face(self, lines, str_idx, vertices):
loops = []
loop = []
for line in lines[str_idx:]:
tokens = line.split()
if not tokens:
continue
line_type = tokens[0]
if line_type == 'face':
break
# Start of a new loop
if line_type == "out" or line_type == "in":
if len(loop) > 0:
loops.append(loop)
loop = []
is_outer = (line_type == 'out')
# Line
if line_type == 'l':
c_tok = tokens[1:]
curve = Line([int(c_tok[0]), int(c_tok[1])], vertices, is_outer=is_outer)
loop.append(curve)
# Arc
if line_type == 'a':
c_tok = tokens[1:]
curve = Arc([int(c_tok[0]), int(c_tok[1]), int(c_tok[2]), int(c_tok[3])], vertices, is_outer=is_outer)
loop.append(curve)
# Circle
if line_type == 'c':
c_tok = tokens[1:]
curve = Circle([int(c_tok[0]), int(c_tok[1])], vertices, is_outer=is_outer)
loop.append(curve)
loops.append(loop)
return loops
|