Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import json
import matplotlib.pyplot as plt
import numpy as np
class NumpyEncoder(json.JSONEncoder):
""" Custom encoder for numpy data types """
def default(self, obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
return float(obj)
elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
return {'real': obj.real, 'imag': obj.imag}
elif isinstance(obj, (np.ndarray,)):
return obj.tolist()
elif isinstance(obj, (np.bool_)):
return bool(obj)
elif isinstance(obj, (np.void)):
return None
return json.JSONEncoder.default(self, obj)
loc="/home/leslie/Documents/Leslie/MYWORLD/"
fileloc=loc+"osm22world/"
with open(loc+"some2.obj", "r") as fp:
lines = fp.readlines()
mydict={}
vs=[]; currname=""
fs=[]
newobj=True
count=0
lastcurrname=currname
objdict={"vs":[],"fs":[],"usemtl":None,"name":None}
for l in lines:
parts=l.split(" ")
count+=1
if count==249:
print("lal")
if l[0]=="v":
inl=l.replace("\n","")
parts=inl.split(" ")
v1=np.float64(parts[2])
v2=np.float64(parts[3])
v3=np.float64(parts[4])
vs.append([v1,v2,v3])
objdict["vs"].append([v1,v2,v3])
continue
if l[0]=="f":
inl=l.replace("\n","")
parts=inl.split(" ")
f1=np.int64(parts[1])
f2=np.int64(parts[2])
f3=np.int64(parts[3])
fs.append([f1,f2,f3])
objdict["fs"].append([f1,f2,f3])
continue
if l[0]=="u":
inl=l.replace("\n","")
parts=inl.split(" ")
objdict["usemtl"]=parts[1]
continue
if l[0]=="g":
lastcurrname=currname
currname=parts[1].replace("\n","")
encountered=0
if currname in mydict:
encountered=mydict[currname]["encountered"]
mydict[currname]["encountered"]+=1
else:
mydict[currname]={"objects":[],"objectnames":[],"encountered":1}
print(currname)
continue
if l[0]=="o":
#if o means new object, save the last and reset
if newobj:
objdict={"vs":[],"fs":[],"usemtl":None,"name":None}
objdict["name"]=l.split(currname)[1].replace("\n","")
lastcurrname=currname
newobj=False
else:
mydict[lastcurrname]["objects"].append(objdict)
objdict={"vs":[],"fs":[],"usemtl":None,"name":None}
objdict["name"]=l.split(currname)[1].replace("\n","")
continue
vsids=np.arange(len(vs),dtype=np.int64)
vbool=np.zeros(len(vs),dtype=bool)
for f in fs:
for i in f:
vbool[i]=True
#with open(loc+'someIntermediat2.json', 'w') as fp:
# json.dump(mydict, fp,indent=1,cls=NumpyEncoder)
ignoredones=["RoadJunction", "Bench", "Table", "Board", "BusStop", "WasteBasket", "BuildingEntrance", "RoadConnector", "TunnelJunction", "Tree", "RoadCrossingAtConnector", "RiverJunction", "Road", "Wall", "Rail", "Bridge", "Tunnel", "Waterway", "PoleFence", "RetainingWall", "ChainLinkFence", "BicycleStands", "Hedge", "SurfaceArea", "Forest", "RoadArea", "Water", "SurfaceParking", "BeachVolleyballPitch", "SoccerPitch", "AreaFountain", "TennisPitch" ]
with open(loc+"someNew2.obj", "w") as fp:
for g in mydict:
if g not in ignoredones:
fp.write("g "+g+"\n")
for o in mydict[g]["objects"]:
fp.write("g "+g+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment