Skip to content

Commit

Permalink
obj format works
Browse files Browse the repository at this point in the history
  • Loading branch information
ChHarding committed Oct 22, 2021
1 parent 15f7ce3 commit a61f63f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
47 changes: 28 additions & 19 deletions touchterrain/common/grid_tesselate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,15 +1181,24 @@ def make_OBJfile_buffer(self, no_bottom=False, temp_file=None, no_normals=False)

assert self.vi != -1, "make_OBJfile_buffer: need grid with vertex indices!"

# initially, store each line in output file as string in a list
buf_as_list = []
buf_as_list.append("g vert") # header for vertex indexing section
# make a stream s, either on disk or memory and append to it via write()
if temp_file != None:
try:
s = open(temp_file, "a") # append
except Exception as e:
print("Error opening:", temp_file, e, file=sys.stderr)
return e
else:
s = io.StringIO()


s.write("g vert\n")
for v in list(self.vi.keys()): # self.vi is a dict of vertex indices
#vstr = "v %f %f %f #%d" % (v[0], v[1], v[2], i+1) # putting the vert index behind a comment makes some programs crash when loading the obj file!
vstr = f"v {v[0]}, {v[1]}, {v[2]}"
buf_as_list.append(vstr)
vstr = f"v {v[0]}, {v[1]}, {v[2]}\n"
s.write(vstr)

buf_as_list.append("g tris")
s.write("\ng tris\n")

# number of cells in x and y grid is cells[y,x]
ncells_x = self.cells.shape[1]
Expand All @@ -1214,18 +1223,17 @@ def make_OBJfile_buffer(self, no_bottom=False, temp_file=None, no_normals=False)

for f in (t0,t1): # output the 2 facets (triangles)
if f == None: continue # for "tri quads" one tri is None
fstr = "f %d %d %d" % (f[0]+1, f[1]+1, f[2]+1) # OBJ indices start at 1!
buf_as_list.append(fstr)
fstr = "f %d %d %d\n" % (f[0]+1, f[1]+1, f[2]+1) # OBJ indices start at 1!
#buf_as_list.append(fstr)
s.write(fstr)
del cell

# concat list of strings to a single string
buf = "\n".join(buf_as_list).encode("UTF-8")

if temp_file == None:
return buf
return s.getvalue() # return stream as string
else:
r = self.write_into_temp_file(temp_file, buf, ascii=True)
return r # if OK, just returns temp filename
s.close()
return temp_file

# MAIN (left this in so I can test stuff ...)
if __name__ == "__main__":
Expand Down Expand Up @@ -1294,10 +1302,11 @@ def make_OBJfile_buffer(self, no_bottom=False, temp_file=None, no_normals=False)
[ 1, 50, 10, 10, 20, 10 , 1 ],
])
'''
top = np.array([ [1,2, 3],
[3, 4, 5],
])
'''

#bottom = np.zeros((4, 3)) # num along x, num along y
top = top.astype(float)
Expand Down Expand Up @@ -1330,8 +1339,9 @@ def make_OBJfile_buffer(self, no_bottom=False, temp_file=None, no_normals=False)
"ntilesx": 1,
"ntilesy": 1,
"tile_centered" : False, # True: each tile's center is 0/0, False: global (all-tile) 0/0
"fileformat": "stlb", # folder/zip file name for all tiles
"base_thickness_mm": 0, # thickness between bottom and lowest elevation, NOT including the bottom relief.
#"fileformat": "stlb", # folder/zip file name for all tiles
"fileformat": "obj",
"base_thickness_mm": 1, # thickness between bottom and lowest elevation, NOT including the bottom relief.
"tile_width": 100,
"use_geo_coords": None,

Expand All @@ -1346,11 +1356,10 @@ def make_OBJfile_buffer(self, no_bottom=False, temp_file=None, no_normals=False)


#b = g.make_STLfile_buffer(ascii=True, no_normals=True, temp_file="STLtest_asc6.stl")
b = g.make_STLfile_buffer(ascii=False, no_normals=False, temp_file="STLtest_new_b3.stl")
#b = g.make_STLfile_buffer(ascii=False, no_normals=False, temp_file="STLtest_new_b3.stl")
#f = open("STLtest_new.stl", 'wb');f.write(b);f.close()

#b = g.make_OBJfile_buffer()
#f = open("OBJtest.obj", 'wb');f.write(b);f.close()
b = g.make_OBJfile_buffer(no_bottom=False, temp_file="OBJtest2.obj", no_normals=False)
print("done")


Expand Down
2 changes: 1 addition & 1 deletion touchterrain/server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ <h4 class="text-truncate">
<div class="form-inline text-truncate">
<div class="form-group">
<select class="form-control custom-select-sm"
id="options_fileformat" name="fileformat" onchange="update_options_hidden();"
id="options_fileformat" name="fileformat" onchange="update_options_hidden();">
<option value="obj" > Obj</option>
<option value="STLb" selected> STL binary</option>
<option value="STLa"> STL ascii</option>
Expand Down

0 comments on commit a61f63f

Please sign in to comment.