def to_internal_value(self, value): try: zip_file = ZipFile(value) logger.debug('inspecting zip file.') found_objs = 0 # files with the .obj extension found for name in zip_file.namelist(): logger.debug('Found file with name: {}'.format(name)) if name.endswith('.obj'): found_objs += 1 if found_objs != 1: logger.debug('No .obj file in upload.') raise serializers.ValidationError( 'No single .obj file found in your uploaded zip file.', code='invalid') with ModelExtractor(zip_file) as extracted_location: try: scene = Wavefront(extracted_location['obj']) except: logger.debug('Error parsing OBJ/MTL files.') raise serializers.ValidationError( 'Error parsing OBJ/MTL files.', code='invalid') except BadZipFile: logger.debug('BadZipFile exception.') raise serializers.ValidationError( 'Uploaded file was not a valid zip file.', code='invalid') logger.debug('Custom zip validation successful.') return super().to_internal_value(value)
def __init__(self, scene, v_shader, f_shader, texture=None): """Loads a Wavefront OBJ file. """ self.vertex_info = [ ] # unique combinations of position, normal and color self.model = glm.mat4() self.perspective = PERSPECTIVE obj_list.append(self) self.position = np.array([0, 0, 0], np.float32) self.light = self self.light.color = (0, 0, 0) self.light.position = (0, 0, 0) self.shader = None self.vertex_shader = open(v_shader) self.fragment_shader = open(f_shader) self.texture_mode = 0 self.texture = texture self.translated = glm.mat4() self.rotated = glm.mat4() self.scaled = glm.mat4() scene = Wavefront(scene) scene.parse() for material in scene.materials.values(): vertex_format = 0 for i in material.vertex_format: if i.isdigit(): vertex_format += int(i) vertices = np.array(material.vertices, np.float32).reshape( len(material.vertices) // vertex_format, vertex_format) indices, vertex_info = dedup_and_index(vertices) self.color = material.diffuse self.indices = np.array(indices, np.int32) self.vertex_info = np.array(vertex_info, np.float32) self.get_box()
def load_obj(self, filename, fake_io=None): """Helper method loading files with proper mocks""" if not fake_io: self.fake_io = FakeIO() if not fake_io: scene = Wavefront(filename, cache=True, create_materials=self.create_materials) with mock.patch("pywavefront.cache.gzip.open", new=self.fake_io): with mock.patch("pywavefront.cache.open", new=self.fake_io): with mock.patch("pywavefront.cache.os.path.exists", new=self.fake_io.exisis): if fake_io: scene = Wavefront(filename, cache=True, create_materials=self.create_materials) scene.parser.post_parse() self.meta_file = self.obj_file.with_suffix(self.obj_file.suffix + '.json') self.cache_file = self.obj_file.with_suffix(self.obj_file.suffix + '.bin') return scene
def __init__(self, filename=None): self.rotation = 0.0 super(MeshViewer, self).__init__(1024, 720, caption='Mesh Viewer', resizable=True) self.meshes = Wavefront(filename) pyglet.gl.glClearColor( 1, 1, 1, 1) # Note that these are values 0.0 - 1.0 and not (0-255). self.lightfv = ctypes.c_float * 4
def load_models(self, names, vao="default"): """Load or reload models into a vao.""" glBindVertexArray(self.vertex_arrays[vao].id) while self.vertex_arrays[vao].buffer_ids: buff_id = self.vertex_arrays[vao].buffer_ids.pop() glDeleteBuffers(buff_id, 1) while self.vertex_arrays[vao].texture_ids: tex_id = self.vertex_arrays[vao].texture_ids.pop() glDeleteTextures(tex_id) all_vertices = [] model_offset = 0 models = {} for model_name in names: model_indices = 0 obj = Wavefront(f"assets/{model_name}.obj", parse=True) for name, mat in obj.materials.items(): if mat.vertex_format != "T2F_N3F_V3F": exit(f"Error in {model_name}.obj" + " vertex format must be T2F_N3F_V3F") texture = self.load_texture( mat.texture.path) if mat.texture else None if texture: self.vertex_arrays[vao].texture_ids.append(texture) verts = np.array(mat.vertices, dtype=np.float32) all_vertices.append(verts) model_indices += len(mat.vertices) // 8 models[model_name] = Model(model_offset, model_indices, texture) model_offset += model_indices vertices = np.concatenate(all_vertices).ravel() # upload to GPU new_vbo = glGenBuffers(1) self.vertex_arrays[vao].buffer_ids.append(new_vbo) glBindBuffer(GL_ARRAY_BUFFER, new_vbo) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) s = np.dtype(np.float32).itemsize * (2 + 3 + 3) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, s, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, s, ctypes.c_void_p(8)) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, s, ctypes.c_void_p(20)) self.models[vao] = models
def carica_mesh(path, texture_path): shader = compile_shaders() mesh = Wavefront(path) print(mesh.mesh_list[0].materials[0].vertex_format) data = np.array([]) for m_item in mesh.mesh_list: for mat in m_item.materials: data = np.append(data, mat.vertices) dim = len(data) // 8 data = data.reshape(dim, 8).astype(np.float32) vertici = data[:, 5:8].reshape(-1) normals = data[:, 2:5].reshape(-1) texel = data[:, 0:2].reshape(-1) vertex_bufferId = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferId) glBufferData(GL_ARRAY_BUFFER, 4 * len(vertici), vertici, GL_STATIC_DRAW) normal_bufferId = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, normal_bufferId) glBufferData(GL_ARRAY_BUFFER, 4 * len(normals), normals, GL_STATIC_DRAW) texel_bufferId = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, texel_bufferId) glBufferData(GL_ARRAY_BUFFER, 4 * len(texel), texel, GL_STATIC_DRAW) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) img = cv2.flip(cv2.imread(texture_path), 0) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.shape[1], img.shape[0], 0, GL_BGR, GL_UNSIGNED_BYTE, img.data) glGenerateMipmap(GL_TEXTURE_2D) glBindBuffer(GL_ARRAY_BUFFER, 0) glBindTexture(GL_TEXTURE_2D, 0) identity = glm.mat4(1.0) return shader | { 'v': vertex_bufferId, 'n': normal_bufferId, 't': texel_bufferId, 'texture': texture, 'type': GL_TRIANGLES, 'n_triangoli': dim, 'model_m': identity }
def __init__(self, f, v=False): self.x, self.y, self.z = 0.0, 0.0, 0.0 self.phi, self.theta, self.alpha = 0.0, 0.0, 1.0 self.verbose = v self.scene = None self.fileName = f self.name = f.split('/')[f.count('/')].rstrip(".obj") try: self.scene = Wavefront(self.fileName) if (self.verbose): print("%s loaded" % self.name) except FileNotFoundError: print("No such file %s" % self.fileName) pass
T2F_N3F_V3F Additional formats: C3F_N3F_V3F T2F_C3F_N3F_V3F """ import ctypes from pyglet.gl import * from pywavefront import visualization, Wavefront window = pyglet.window.Window(width=1280, height=720) box1 = Wavefront('data/box/box-V3F.obj') box2 = Wavefront('data/box/box-C3F_V3F.obj') box3 = Wavefront('data/box/box-N3F_V3F.obj') box4 = Wavefront('data/box/box-T2F_V3F.obj') box5 = Wavefront('data/box/box-T2F_C3F_V3F.obj') box6 = Wavefront('data/box/box-T2F_N3F_V3F.obj') rotation = 0.0 lightfv = ctypes.c_float * 4 @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45., float(width) / height, 1., 100.)
#!/usr/bin/env python """This script shows another example of using the PyWavefront module.""" # This example was created by intrepid94 import ctypes import sys sys.path.append('..') import pyglet from pyglet.gl import * from pywavefront import visualization from pywavefront import Wavefront rotation = 0.0 meshes = Wavefront('earth.obj') window = pyglet.window.Window(1024, 720, caption='Demo', resizable=True) lightfv = ctypes.c_float * 4 label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=12, x=800, y=700, anchor_x='center', anchor_y='center') @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(40.0, float(width) / height, 1.0, 100.0)
objPath = argList[1] imagePath = argList[2] precision = int(argList[3]) print( 'Welcome! I like finding blob distrubutions from 3d egg models. Feed me!') pathToHere = os.path.dirname(os.path.realpath(__file__)) if objPath[0] == '.': objPath = pathToHere + objPath[1:] if imagePath[0] == '.': imagePath = pathToHere + imagePath[1:] print('Loading egg...') meshes = Wavefront(objPath) vertices = meshes.materials['egg_model'].vertices print('Done') print('Translating and rotating egg...') mem_size = int(len(vertices) / 8) vertices_only = np.empty([3, mem_size], dtype=float) initVertices(vertices, vertices_only) mean_arr = meanVertex(vertices_only) translate(vertices_only, mean_arr)
import ctypes import os import sys sys.path.append('..') import pyglet from pyglet.gl import * from pywavefront import visualization from pywavefront import Wavefront # Create absolute path from this module file_abspath = os.path.join(os.path.dirname(__file__), 'data/earth.obj') rotation = 0.0 meshes = Wavefront(file_abspath) window = pyglet.window.Window(1024, 720, caption='Demo', resizable=True) lightfv = ctypes.c_float * 4 label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=12, x=800, y=700, anchor_x='center', anchor_y='center') @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity()
for fichier in filelist[:]: # filelist[:] makes a copy of filelist. if not (fichier.endswith(".png")): filelist.remove(fichier) boxes = [] for filename in filelist: lines = "" with open('testtex.mtl', 'r') as file: for line in file: if (line.split(" ")[0] == "map_Kd"): line = "map_Kd " + ANIM_FOLDER + "/" + filename + "\n" lines += line with open('testtex.mtl', 'w') as file: file.write(lines) boxes.append(Wavefront(os.path.join(root_path, 'testtex.obj'))) rotation = 0.0 lightfv = ctypes.c_float * 4 @window.event def on_resize(width, height): viewport_width, viewport_height = window.get_framebuffer_size() glViewport(0, 0, viewport_width, viewport_height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45., float(width) / height, 1., 100.) glMatrixMode(GL_MODELVIEW) return True
ser = serial.Serial(port=selectedDevice, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) ser.close() ser.open() ser.isOpen() out = '' roll = 0 pitch = 0 heading = 0 meshes = Wavefront('stm32f411.obj') window = pyglet.window.Window(800, 600, caption='Demo', resizable=False) glClearColor(0.8, 0.9, 1, 1) lightfv = ctypes.c_float * 4 # label = pyglet.text.Label('Hello, world', font_name = 'Times New Roman', font_size = 12, x = 100, y = 100, anchor_x = 'center', anchor_y = 'center') @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(40.0, float(width) / height, 1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW)
#!/usr/bin/env python """This script shows another example of using the PyWavefront module.""" # This example was created by intrepid94 import ctypes import sys sys.path.append('..') import pyglet from pyglet.gl import * from pywavefront import visualization from pywavefront import Wavefront rotation = 0.0 meshes = Wavefront('data/earth.obj') window = pyglet.window.Window(1024, 720, caption='Demo', resizable=True) lightfv = ctypes.c_float * 4 label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=12, x=800, y=700, anchor_x='center', anchor_y='center') @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(40.0, float(width) / height, 1.0, 100.0)
filename = 'armPath6.txt' #init openGL stuff #for desktop with graphics card # config = pyglet.gl.Config(sample_buffers=1, samples=9) #samples = number of points used for AA # window = pyglet.window.Window(width=1280, height=720, config = config) #for laptop window = pyglet.window.Window(width=1280, height=720) keys = key.KeyStateHandler() window.push_handlers(keys) base = Wavefront('base.obj') link0 = Wavefront('l0.obj') link1 = Wavefront('l1.obj') link2 = Wavefront('l2andl3Dummy.obj') greenCheck = pyglet.image.load('greenCheck.png') gc = pyglet.sprite.Sprite(img=greenCheck) gc.scale = 0.01 gc.x = -10 gc.y = 12 redX = pyglet.image.load('redX.png') rx = pyglet.sprite.Sprite(img=redX) rx.scale = 0.005 rx.x = -10 rx.y = 12 l1sim = 6.5
def load(self, filename): self.mesh = Wavefront(filename) self.setBoundingBox(filename)
# window = pyglet.window.Window(width=1280,height=720) keys = key.KeyStateHandler() window.push_handlers(keys) sg = shoulderGuesser() # path = 'armPath5.txt' #standing far to the left of arm path = 'armPath6.txt' #standing slightly to the right of arm pathArr = numpy.genfromtxt(path, delimiter=" ") global shoulderX global shoulderY global shoulderZ global bodyRot base = Wavefront('base.obj') link0 = Wavefront('l0.obj') link1 = Wavefront('l1.obj') link2 = Wavefront('l2andl3Dummy.obj') torso = Wavefront('./human/torso.obj') upperArm = Wavefront('./human/upperArm.obj') lowerArm = Wavefront('./human/lowerArm.obj') hand = Wavefront('./human/hand.obj') head = Wavefront('./human/head.obj') testPlot = pyglet.image.load('test.png') tp = pyglet.sprite.Sprite(img=testPlot) tp.scale = 0.03 tp.x = 0 tp.y = 12 greenCheck = pyglet.image.load('greenCheck.png') gc = pyglet.sprite.Sprite(img=greenCheck)
Additional formats: C3F_N3F_V3F T2F_C3F_N3F_V3F """ import ctypes import os from pyglet.gl import * from pywavefront import visualization, Wavefront window = pyglet.window.Window(width=1280, height=720) root_path = os.path.dirname(__file__) box1 = Wavefront(os.path.join(root_path, 'data/box/box-V3F.obj')) box2 = Wavefront(os.path.join(root_path, 'data/box/box-C3F_V3F.obj')) box3 = Wavefront(os.path.join(root_path, 'data/box/box-N3F_V3F.obj')) box4 = Wavefront(os.path.join(root_path, 'data/box/box-T2F_V3F.obj')) box5 = Wavefront(os.path.join(root_path, 'data/box/box-T2F_C3F_V3F.obj')) box6 = Wavefront(os.path.join(root_path, 'data/box/box-T2F_N3F_V3F.obj')) rotation = 0.0 lightfv = ctypes.c_float * 4 @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45., float(width) / height, 1., 100.)
step = 5 output_size = (64, 64) light = False obj_path = '../obj/example/earth.obj' result_folder = '../data_earth_s2_%d_%d_v2/' % (output_size[0], step) # ---------------------------------------- # alpha = 0 beta = 0 it = -1 utils.create_dir('frames') meshes = Wavefront(obj_path) window = pyglet.window.Window(output_size[0], output_size[1], caption='Demo', resizable=False, vsync=60) lightfv = ctypes.c_float * 4 frames = [] @window.event def on_resize(width, height): glMatrixMode(GL_PROJECTION)
def main(filename, output, rescale): scene = Wavefront(filename, collect_faces=True) vertex_bin = b'' vertices = scene.vertices nvertices = len(vertices) if rescale: xmin = xmax = vertices[0][0] ymin = ymax = vertices[0][1] zmin = zmax = vertices[0][2] for x, y, z in vertices: xmin = min(xmin, x) xmax = max(xmax, x) ymin = min(ymin, y) ymax = max(ymax, y) zmin = min(zmin, z) zmax = max(zmax, z) vmin = min(xmin, ymin, zmin) vmax = max(xmax, ymax, zmax) for x, y, z in vertices: if rescale: x = (x - xmin - (xmax - xmin) / 2) / (vmax - vmin) y = (y - ymin - (ymax - ymin) / 2) / (vmax - vmin) z = (z - zmin - (zmax - zmin) / 2) / (vmax - vmin) vertex_bin += pack('fff', x, y, z) index_bin = b'' nindex = 0 for name, mesh in scene.meshes.items(): indices = mesh.faces nindex += len(indices) for i, j, k in indices: assert i < nvertices or j < nvertices or k < nvertices index_bin += pack('III', i, j, k) has_vertex = True has_index = True has_extra1 = False has_extra2 = False header = pack('cccc', b'V' if has_vertex else b' ', b'I' if has_index else b' ', b' ' if has_extra1 else b' ', b' ' if has_extra2 else b' ', ) if has_vertex: vertex_header = pack('I', nvertices) if has_index: index_header = pack('I', nindex) if has_extra1: raise NotImplementedError if has_extra2: raise NotImplementedError content = header if has_vertex: content += vertex_header content += vertex_bin if has_index: content += index_header content += index_bin if has_extra1: raise NotImplementedError if has_extra2: raise NotImplementedError if output is not None: output.write_bytes(content)
import ctypes from pyglet.gl import * from pyglet.window import key from pywavefront import visualization, Wavefront import numpy #init openGL stuff window = pyglet.window.Window(width=1280, height=720) keys = key.KeyStateHandler() window.push_handlers(keys) link0 = Wavefront('link0.obj') link0Rot = 45 link1 = Wavefront('link1.obj') link1Rot = -30 link2 = Wavefront('link2.obj') link2Rot = 90 link3 = Wavefront('link3.obj') link3Rot = 90 rotation = 0.0 #count variable for spinning l3 lightfv = ctypes.c_float * 4 # xElb = 0 # yElb = 9.5 # zElb = 0 l1 = 9.5
def generate_scene_voxel_grids(cameras, suncg_dir, house_id, result_dir, obj_cache): # debug_dir = '/data/debug2/{}'.format(house_id) debug_dir = None if debug_dir: shutil.rmtree(debug_dir, ignore_errors=True) os.makedirs(debug_dir) #Generate result dir result_dir = osp.join(result_dir, 'vox') os.makedirs(result_dir, exist_ok=True) #Load house house_json = osp.join(suncg_dir, 'house', house_id, 'house.json') with open(house_json) as f: house = json.load(f) #config Params # voxSize = np.array([20,10,20]) # voxUnit = 0.3 # voxSize = np.array([240,144,240]) # voxUnit = 0.02 # voxSize = np.array([120,72,120]) # voxUnit = 0.04 # voxSize = np.array([60,40,60]) voxSize = np.array([60, 40, 60]) voxUnit = 0.08 camK = constructK() im_w = 640 im_h = 480 # Confusing facts: # Camera coordinate system has as usual Z forward and Y downward # SUNCG coordinate system has Y facing up # Output coordinate system has X facing up # Select grid based on camera location for camera_idx, cr in enumerate(cameras): cam = SUNCGCamera(cr) if debug_dir: cam_debug_dir = osp.join(debug_dir, 'camera{}'.format(camera_idx)) os.makedirs(cam_debug_dir, exist_ok=True) else: cam_debug_dir = None # Put grid center half the grid length in front of the camera, moving in the XZ-plane xz_front = cam.front * np.array([1, 0, 1]) xz_front /= np.linalg.norm(xz_front) voxOriginWorld = cam.pos + xz_front * voxSize[0] * voxUnit / 2 #Correct box center so we always get some floor. if voxOriginWorld[1] + voxUnit / 2 > voxUnit * voxSize[1] / 2: voxOriginWorld[1] = voxUnit * voxSize[1] / 2 - voxUnit / 2 voxWorldMin = voxOriginWorld - (voxSize * voxUnit / 2) voxWorldMax = voxOriginWorld + (voxSize * voxUnit / 2) gridPtsWorld = np.array( np.meshgrid(*[ np.linspace(voxWorldMin[i], voxWorldMax[i], voxSize[i]) for i in range(3) ], indexing='ij')) gridPtsWorldList = gridPtsWorld.view().reshape((3, -1)) # Create views gridPtsWorldXZ = gridPtsWorldList[[0, 2], :] gridPtsWorldXY = gridPtsWorldList[:2, :] gridPtsWorldYZ = gridPtsWorldList[1:, :] gridPtsWorldY = gridPtsWorldList[1, :] #Output gridPtsLabel = np.zeros(gridPtsWorldList.shape[1], dtype=np.uint32) xz = [0, 2] for houseLevel in house['levels']: for node in houseLevel['nodes']: if node['type'].lower() != 'room': continue #Check if we need the room try: bbox_min = np.array(node['bbox']['min']) bbox_max = np.array(node['bbox']['max']) except KeyError: continue if not boxOverlap(voxWorldMin[xz], voxWorldMax[xz], bbox_min[xz], bbox_max[xz]): continue if not sameFloor(voxOriginWorld, bbox_min, bbox_max): continue # Find grids in the room try: floorObj = Wavefront( osp.join(suncg_dir, 'room', house_id, '{}f.obj'.format(node['modelId']))) except IOError as e: floorObj = None if False and debug_dir: floor_debug_dir = osp.join(cam_debug_dir, 'floor') os.makedirs(floor_debug_dir, exist_ok=True) else: floor_debug_dir = None if floorObj: simple_vertices = np.array(floorObj.vertices) inRoom = inPolygon(gridPtsWorldXZ.T, simple_vertices[:, [0, 2]], floor_debug_dir) #Early exit if we are not actually in the room. if not inRoom.any(): continue #Find floor floorY = np.mean(simple_vertices[:, 1]) floorMask = inRoom & (np.abs(gridPtsWorldY - floorY) <= voxUnit / 2) if 'floor' not in cfg['catBlacklist']: classRootId, _ = suncg_labels.getClassRoot('floor') gridPtsLabel[floorMask] = classRootId else: #Need floor object continue # Find ceiling try: ceilObj = Wavefront( osp.join(suncg_dir, 'room', house_id, '{}c.obj'.format(node['modelId']))) except IOError as e: ceilObj = None if ceilObj: simple_vertices = np.array(ceilObj.vertices) ceilY = np.mean(simple_vertices[:, 1]) ceilMask = inRoom & (np.abs(gridPtsWorldY - ceilY) <= voxUnit / 2) if 'ceiling' not in cfg['catBlacklist']: classRootId, _ = suncg_labels.getClassRoot('ceiling') gridPtsLabel[ceilMask] = classRootId # Find walls try: wallObj = Wavefront( osp.join(suncg_dir, 'room', house_id, '{}w.obj'.format(node['modelId']))) except IOError as e: wallObj = None if False and debug_dir: wall_debug_dir = osp.join(cam_debug_dir, 'wall') os.makedirs(wall_debug_dir, exist_ok=True) else: wall_debug_dir = None wallMask = np.zeros_like(inRoom) if wallObj and ceilObj and floorObj: for m_name, material in wallObj.materials.items(): vertices = np.array( [[material.vertices[i], material.vertices[i + 2]] for i in range(5, len(material.vertices), material.vertex_size)]) if vertices.size > 0: wallMask |= inPolygon(gridPtsWorldXZ.T, vertices, wall_debug_dir, convex_hull=True) classRootId, _ = suncg_labels.getClassRoot('wall') wallMask &= gridPtsWorldY < (ceilY - voxUnit / 2) wallMask &= gridPtsWorldY > (floorY + voxUnit / 2) if 'wall' not in cfg['catBlacklist']: gridPtsLabel[wallMask] = classRootId # Visualize if False and debug_dir: t = time.time() ax_list = [] fig = getFigure() ax = plt.subplot(2, 2, 1, projection='3d') ax_list.append(ax) plt.title('inRoom, {:0.2f}%'.format(100 * np.sum(inRoom) / float(inRoom.size))) ax.plot(gridPtsWorldList[0, inRoom], gridPtsWorldList[1, inRoom], gridPtsWorldList[2, inRoom], 'g.', alpha=0.8) ax.plot(gridPtsWorldList[0, ~inRoom], gridPtsWorldList[1, ~inRoom], gridPtsWorldList[2, ~inRoom], 'r.', alpha=0.05) plt.axis('equal') ax = plt.subplot(2, 2, 2, projection='3d') ax_list.append(ax) plt.title('Wall, {:0.2f}%'.format(100 * np.sum(wallMask) / float(inRoom.size))) ax.plot(gridPtsWorldList[0, wallMask], gridPtsWorldList[1, wallMask], gridPtsWorldList[2, wallMask], 'g.', alpha=0.8) ax.plot(gridPtsWorldList[0, ~wallMask], gridPtsWorldList[1, ~wallMask], gridPtsWorldList[2, ~wallMask], 'r.', alpha=0.05) plt.axis('equal') ax = plt.subplot(2, 2, 3, projection='3d') ax_list.append(ax) plt.title('Floor, {:0.2f}%'.format( 100 * np.sum(floorMask) / float(inRoom.size))) ax.plot(gridPtsWorldList[0, floorMask], gridPtsWorldList[1, floorMask], gridPtsWorldList[2, floorMask], 'g.', alpha=0.8) ax.plot(gridPtsWorldList[0, ~floorMask], gridPtsWorldList[1, ~floorMask], gridPtsWorldList[2, ~floorMask], 'r.', alpha=0.05) plt.axis('equal') ax = plt.subplot(2, 2, 4, projection='3d') ax_list.append(ax) plt.title('Ceiling, {:0.2f}%'.format( 100 * np.sum(ceilMask) / float(inRoom.size))) ax.plot(gridPtsWorldList[0, ceilMask], gridPtsWorldList[1, ceilMask], gridPtsWorldList[2, ceilMask], 'g.', alpha=0.8) ax.plot(gridPtsWorldList[0, ~ceilMask], gridPtsWorldList[1, ~ceilMask], gridPtsWorldList[2, ~ceilMask], 'r.', alpha=0.05) plt.axis('equal') anim = FuncAnimation( fig, lambda i: [ax.view_init(30, i * 30) for ax in ax_list], frames=np.arange(0, 12), interval=1000) anim.save(osp.join(cam_debug_dir, 'Room{}.gif'.format(node['modelId'])), dpi=80, writer='imagemagick') # plt.savefig(osp.join(debug_dir, 'Room{}.png'.format(node['modelId']))) plt.close() print('Plots grid room', time.time() - t) #Check all objects for obj_idx in node.get('nodeIndices', []): obj_node = houseLevel['nodes'][obj_idx] try: model_id = obj_node['modelId'].replace('/', '__') except KeyError: continue if model_id in cfg['modelBlacklist']: continue classRootId, classRoot = suncg_labels.getClassRoot( model_id) obj_bbox_max = np.array([obj_node['bbox']['max']]).T obj_bbox_min = np.array([obj_node['bbox']['min']]).T # Work with voxels around the object objMask = np.all( (obj_bbox_min - voxUnit / 2 <= gridPtsWorldList) & (gridPtsWorldList <= obj_bbox_max + voxUnit / 2), axis=0) if not objMask.any(): continue objMaskIdx = np.flatnonzero(objMask) gridPtsWorldNear = gridPtsWorldList[:, objMask] # Get voxels binvox_filename = osp.join(suncg_dir, 'object_vox', 'object_vox_data', model_id, '{}.binvox'.format(model_id)) with open(binvox_filename, 'rb') as f: dims, translate, scale = binvox_rw.read_header(f) assert len( set(dims)) == 1, "Voxel grid assumed to be cube" #Adjust t for the standard binvox order t = np.array( [[translate[0], translate[2], translate[1]]]).T gridPtsWorldNear_h = np.vstack([ gridPtsWorldNear, np.ones([1, gridPtsWorldNear.shape[1]], dtype=gridPtsWorldNear.dtype) ]) try: T = np.linalg.inv( np.reshape(obj_node['transform'], [4, 4], order='F')) except TypeError as e: logger.error( np.reshape(obj_node['transform'], [4, 4], order='F')) raise e obj_coords_h = T @ gridPtsWorldNear_h obj_coords_h = obj_coords_h / obj_coords_h[-1] obj_coords = obj_coords_h[:3] #Transform world coordinates to voxel coords side_len = dims[0] obj_coords = side_len * (obj_coords - t) / scale # If object is a window or door, clear voxels classified as wall in bbox. if classRootId in (4, 5): gridPtsLabel[wallMask & objMask] = 0 # t = time.time() obj_mask = obj_cache.query(obj_coords, model_id, (np.sqrt(3) / 2) * side_len * voxUnit / scale) matched_indices = objMaskIdx[obj_mask] # print('Simple grid search for', gridPtsWorldNear.shape[1], 'world points:', time.time()-t) if matched_indices.size == 0: continue gridPtsLabel[matched_indices] = classRootId # if debug_dir and classRoot == 'chair': # rect = np.squeeze(np.array([vert for vert in itertools.product(*zip(obj_bbox_max, obj_bbox_min))]).T) # fig = getFigure() # ax = fig.gca(projection='3d') # ax.plot(rect[0], rect[1], rect[2], '*') # ax.plot(gridPtsWorldNear[0,match_mask], gridPtsWorldNear[1,match_mask], gridPtsWorldNear[2,match_mask], 'g.') # ax.plot(gridPtsWorldNear[0,~match_mask], gridPtsWorldNear[1,~match_mask], gridPtsWorldNear[2,~match_mask], 'r.') # # ax.plot(obj_world_coords[0], obj_world_coords[1], obj_world_coords[2], 'd', zorder=1) # plt.title('Class {}, nbr_matches {}'.format(classRoot, match_mask.sum())) # # anim = FuncAnimation(fig, lambda i: ax.view_init(30, i*30), frames=np.arange(0, 12), interval=2000) # anim.save(osp.join(cam_debug_dir, 'obj_vs_world{}.gif'.format(model_id)), dpi=fig.get_dpi(), writer='imagemagick') # # plt.savefig(osp.join(debug_dir, 'obj_vs_world{}.png'.format(model_id))) # plt.close() # # fig = getFigure() # ax = fig.gca(projection='3d') # vox_x, vox_y, vox_z = np.nonzero(obj_bv.data) # ax.plot(obj_coords[0,match_mask], obj_coords[1,match_mask], obj_coords[2,match_mask], '.b', alpha=0.5) # ax.plot(obj_coords[0,~match_mask], obj_coords[1,~match_mask], obj_coords[2,~match_mask], '.r', alpha=0.5) # ax.plot(vox_x, vox_y, vox_z, '.g', alpha=0.5) # plt.title('Class {}, nbr_matches {}'.format(classRoot, match_mask.sum())) # # plt.savefig(osp.join(cam_debug_dir, 'obj_vs_world_nnsearch{}.png'.format(model_id))) # anim = FuncAnimation(fig, lambda i: ax.view_init(30, i*30), frames=np.arange(0, 12), interval=2000) # anim.save(osp.join(cam_debug_dir, 'obj_vs_world_nnsearch{}.gif'.format(model_id)), dpi=fig.get_dpi(), writer='imagemagick') # plt.close() # # fig = getFigure() # ax = fig.gca(projection='3d') # ax.plot(gridPtsWorldList[0,objMask], gridPtsWorldList[1,objMask], gridPtsWorldList[2,objMask], '.g', alpha=0.5) # ax.plot(gridPtsWorldList[0,~objMask], gridPtsWorldList[1,~objMask], gridPtsWorldList[2,~objMask], '.r', alpha=0.5) # plt.title('Class {}, nbr_matches {}'.format(classRoot, match_mask.sum())) # # plt.savefig(osp.join(cam_debug_dir, 'obj_vs_world_nnsearch{}.png'.format(model_id))) # anim = FuncAnimation(fig, lambda i: ax.view_init(30, i*30), frames=np.arange(0, 12), interval=2000) # anim.save(osp.join(cam_debug_dir, 'obj_vs_world{}.gif'.format(model_id)), dpi=fig.get_dpi(), writer='imagemagick') # plt.close() # fig = getFigure() # plt.plot(nn_dist, '.-') # plt.title('Class {}, threshold {}'.format(classRoot, obj_bv.scale*np.sqrt(3)/2)) # plt.savefig(osp.join(debug_dir, 'obj_vs_world_dist{}.png'.format(model_id))) # plt.close() # Change coordinate axis (assume Z up) XZY -> YZX gridPtsLabel3D = gridPtsLabel.reshape(voxSize) if debug_dir: plotVoxelsList(gridPtsLabel, gridPtsWorldList, suncg_labels, save_path=osp.join( debug_dir, 'camera{}.gif'.format(camera_idx))) # plotVoxels(gridPtsLabelYZX, suncg_labels, save_path = osp.join(debug_dir, 'camera{}.gif'.format(camera_idx))) np.savez_compressed(osp.join(result_dir, '{:04}.npz'.format(camera_idx)), voxels=gridPtsLabel3D, vox_center=voxOriginWorld, vox_unit=voxUnit, vox_min=voxWorldMin, vox_max=voxWorldMax) logger.info('Done voxelizing {}'.format(house_id))
import ctypes from pyglet.gl import * from pyglet.window import key from pywavefront import visualization, Wavefront import numpy #init openGL stuff window = pyglet.window.Window(width=1280, height=720) keys = key.KeyStateHandler() window.push_handlers(keys) base = Wavefront('base.obj') link0 = Wavefront('l0.obj') link1 = Wavefront('l1.obj') link2 = Wavefront('l2.obj') link3 = Wavefront('l3.obj') link4 = Wavefront('l4.obj') link0Rot = 15 link1Rot = 30 link2Rot = 80 link3Rot = 0 link4Rot = 45 rotation = 0.0 #count variable for spinning l3 lightfv = ctypes.c_float * 4 l1 = 6.5 l2 = 6.5 l3 = 2.65 link2RotEff = link1Rot + link2Rot