class MeshViewer(GLWindow): def __init__(self, window_name="Mesh Viewer", window_size=(640, 640)): super(MeshViewer, self).__init__(window_name, window_size) self.camera = Camera() self.origin = np.array([0, 0, 0]) self.mesh = Mesh("models/chairs/chair_0305.off") self.samples = self.mesh.get_samples(1e3) self.samples = center_samples(self.samples) self.camera_speed = 1/100. self.initialize() self.action = "" self.prev_x = 0 self.prev_y = 0 voxelize(self.mesh) glutMainLoop() def mouse(self, button, state, x, y): if button == GLUT_LEFT_BUTTON: self.action = "MOVE_CAMERA" def motion(self, x, y): if self.action == "MOVE_CAMERA": dx = self.prev_x - x dy = self.prev_y - y self.camera.theta -= dx * self.camera_speed self.camera.phi += dy * self.camera_speed self.prev_x = x self.prev_y = y def initialize(self): MAX_COORD = 2. glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LESS) glClearColor(1.0, 1.0, 1.0, 0.0) glClearDepth(1.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-MAX_COORD, MAX_COORD, -MAX_COORD, MAX_COORD, 0.1, 1000.0) glShadeModel(GL_SMOOTH) def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.camera.place() RenderUtils.color([1, 1, 0]) self.mesh.draw() RenderUtils.color([0, 0, 1]) RenderUtils.draw_points(self.samples)
class VoxelViewer(GLWindow): def __init__(self, window_name="Voxel Viewer", window_size=(640, 640)): super(VoxelViewer, self).__init__(window_name, window_size) self.camera = Camera() self.prev_x = 0 self.prev_y = 0 self.camera_speed = 1 / 100. data = np.load("voxels.npy")[0] print data.shape self.voxelgrid = VoxelGrid(size=(32, 32, 32), data=data) self.initialize() glutMainLoop() def initialize(self): MAX_COORD = 2. glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LESS) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-MAX_COORD, MAX_COORD, -MAX_COORD, MAX_COORD, 0.1, 1000.0) def mouse(self, button, state, x, y): if button == GLUT_LEFT_BUTTON: self.action = "MOVE_CAMERA" def motion(self, x, y): if self.action == "MOVE_CAMERA": dx = self.prev_x - x dy = self.prev_y - y self.camera.theta -= dx * self.camera_speed self.camera.phi += dy * self.camera_speed self.prev_x = x self.prev_y = y def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) buffer_data = glReadPixels(0, 0, self.screen_size[0], self.screen_size[1], GL_RGB, GL_FLOAT) buffer_data = np.array(buffer_data) mpimg.imsave("rendered_voxels.png", buffer_data) self.camera.place() self.voxelgrid.draw()
class PointCloudViewer(GLWindow): def __init__(self, volume_path, window_name="Mesh Viewer", window_size=(640, 640)): super(PointCloudViewer, self).__init__(window_name, window_size) self.camera = Camera() self.origin = np.array([0, 0, 0]) self.threshold = 0.1 self.volume = np.load(volume_path) self.point_cloud = volume_to_points(self.volume, threshold=self.threshold) self.camera_speed = 1 / 100. self.initialize() self.action = "" self.prev_x = 0 self.prev_y = 0 glutMainLoop() def mouse(self, button, state, x, y): if button == GLUT_LEFT_BUTTON: self.action = "MOVE_CAMERA" def motion(self, x, y): if self.action == "MOVE_CAMERA": dx = self.prev_x - x dy = self.prev_y - y self.camera.theta -= dx * self.camera_speed self.camera.phi += dy * self.camera_speed self.prev_x = x self.prev_y = y def keyboard(self, k, x, y): if k == '.': self.threshold += 0.01 np.clip(self.threshold, 0, 1) self.point_cloud = volume_to_points(self.volume, self.threshold) elif k == ',': self.threshold -= 0.01 np.clip(self.threshold, 0, 1) self.point_cloud = volume_to_points(self.volume, self.threshold) def initialize(self): MAX_COORD = 2. glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LESS) glClearColor(1.0, 1.0, 1.0, 0.0) glClearDepth(1.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-MAX_COORD, MAX_COORD, -MAX_COORD, MAX_COORD, 0.1, 1000.0) glShadeModel(GL_SMOOTH) def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.camera.place() RenderUtils.color([0, 0, 1]) RenderUtils.draw_points(self.point_cloud)
class SphericalHarmonicsViewer(GLWindow): def __init__(self, window_name='Spherical Harmonics Viewer', window_size=(640, 640)): super(SphericalHarmonicsViewer, self).__init__(window_name, window_size) self.camera = Camera() self.origin = np.array([0, 0, 0]) self.mesh = Mesh(os.path.join("..", "models", "cube.obj")) self.mesh_samples = [] self.compute_mesh_samples(500) self.n_frequencies = 10 self.sh_coeffs = np.zeros(self.n_frequencies * self.n_frequencies) self.compute_sh_coeffs() self.print_coeffs() self.sh = SphericalHarmonicsMesh(coeffs=self.sh_coeffs) self.camera_speed = 1 / 100. self.initialize() self.action = "" self.prev_x = 0 self.prev_y = 0 glutMainLoop() def compute_sh_coeffs(self): print "Computing SH coefficients" for s_i, s in enumerate(self.mesh_samples): for l in range(self.n_frequencies): for m in range(-l, l + 1): idx = l * (l + 1) + m r = np.sqrt(np.sum(np.power(s, 2))) self.sh_coeffs[idx] += sph_harm( m, l, np.arctan2(s[2], s[0]), np.arcsin(s[1] / r) + np.pi / 2.).real * self.dist_to_origin(s) renderutils.progress(s_i, len(self.mesh_samples)) norm_factor = 4 * np.pi / len(self.mesh_samples) self.sh_coeffs *= norm_factor print "Done." def print_coeffs(self): for l in range(self.n_frequencies): for m in range(-l, l + 1): idx = l * (l + 1) + m print 'l:{}, m:{}, ylm:{}'.format(l, m, self.sh_coeffs[idx]) def dist_to_origin(self, val): return np.sqrt(np.sum(np.power(val - self.origin, 2))) # Ray tracing approach - Not good def compute_mesh_samples(self, count=50): for s in range(count): xi_1 = np.random.rand() xi_2 = np.random.rand() phi = 2 * np.pi * xi_2 theta = 2 * np.arccos(np.sqrt(xi_1)) dir = renderutils.classic_sphere_to_cartesian([phi, theta, 0.1]) ray = Ray(self.origin, dir) sample = ray.intersect_mesh(self.mesh) if sample is not None: self.mesh_samples.append(sample) def draw_mesh_samples(self): glBegin(GL_POINTS) for s in self.mesh_samples: RenderUtils.vertex(s) glEnd() def mouse(self, button, state, x, y): if button == GLUT_LEFT_BUTTON: self.action = "MOVE_CAMERA" def motion(self, x, y): if self.action == "MOVE_CAMERA": dx = self.prev_x - x dy = self.prev_y - y self.camera.theta -= dx * self.camera_speed self.camera.phi += dy * self.camera_speed self.prev_x = x self.prev_y = y def initialize(self): MAX_COORD = 2. glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LESS) glClearColor(1.0, 1.0, 1.0, 0.0) glClearDepth(1.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-MAX_COORD, MAX_COORD, -MAX_COORD, MAX_COORD, 0.1, 1000.0) glShadeModel(GL_SMOOTH) def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.camera.place() # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) self.sh.draw() RenderUtils.color([0, 0, 0])