def loss_fun_weighted_time(index, measurement, mesh, mesh_optimization, opt, render_opt, device): transient = render(mesh, opt.lighting[index, :], opt.sensor[index, :], opt.lighting_normal[index, :], opt.sensor_normal[index, :], render_opt) phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), opt.lighting_normal[index, :]) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) #triangleIndexMap, distance_bin = find_triangle(mesh, direction, opt.lighting[index,:], opt.sensor[index,:], opt) triangleIndexMap = find_triangle(mesh, direction, opt.lighting[index, :], opt.sensor[index, :], opt) w = torch.ones((1, 1, 2 * opt.w_width + 1)).to(device).double() difference = torch.reshape( -2 * torch.from_numpy(measurement[index, :] - transient).to(device), (1, 1, opt.max_distance_bin)) difference = torch.nn.functional.conv1d(difference, w, None, 1, opt.w_width) difference = torch.nn.functional.conv1d(difference, w, None, 1, opt.w_width) weight = torch.squeeze(difference) #difference = torch.squeeze(difference) #weight = difference.index_select(0,torch.from_numpy(distance_bin).long()) return render_intensity_differentiable( mesh_optimization, direction, triangleIndexMap, weight, opt.lighting[index, :], opt.sensor[index, :], opt.lighting_normal[index, :], opt.sensor_normal[index, :], opt, device)
def run_gradient_est(mesh, opt, weight): new_mesh = type('', (), {})() new_mesh.v = Variable(torch.from_numpy(np.array(mesh.v)).cuda(), requires_grad=True) new_mesh.f = Variable(torch.from_numpy(np.array(mesh.f)).long().cuda(), requires_grad=False) phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), opt.lighting_normal) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) triangleIndexMap = mesh_intersection_grad_igl.find_triangle( mesh, direction, opt.lighting, opt.sensor) angular_transient = rendering_gpu_igl.angular_sampling( new_mesh, direction, triangleIndexMap, opt.lighting, opt.sensor, opt.lighting_normal, opt.sensor_normal, opt) angular_transient.backward(weight.cuda()) grad = new_mesh.v.grad.data.cpu() return grad
def render_differentiable_rand_direction(mesh, mesh_optimization, lighting, sensor, lighting_normal, sensor_normal, opt, device, transient_differentiable): phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), lighting_normal) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) triangleIndexMap = find_triangle(mesh, direction, lighting, sensor) render_differentiable_transient(mesh_optimization, direction, triangleIndexMap, lighting, sensor, lighting_normal, sensor_normal, opt, device, transient_differentiable)
def run_forward(mesh, opt): phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), opt.lighting_normal) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) angular_transient = rendering_igl.angular_sampling(mesh, direction, opt.lighting, opt.sensor, opt.lighting_normal, opt.sensor_normal, opt) return angular_transient
def loss_func(index, measurement, mesh, mesh_optimization, opt, render_opt, device): transient = render(mesh, opt.lighting[index, :], opt.sensor[index, :], opt.lighting_normal[index, :], opt.sensor_normal[index, :], render_opt) phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), opt.lighting_normal[index, :]) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) triangleIndexMap = find_triangle(mesh, direction, opt.lighting[index, :], opt.sensor[index, :]) transient_differentiable = render_differentiable( mesh_optimization, direction, triangleIndexMap, opt.lighting[index, :], opt.sensor[index, :], opt.lighting_normal[index, :], opt.sensor_normal[index, :], opt, device) weight = -2 * torch.from_numpy(measurement[index, :] - transient).to(device) return torch.mul(weight, transient_differentiable).sum()
def render_transient(mesh, lighting, sensor, lighting_normal, sensor_normal, opt, angular_transient): source = igl.eigen.MatrixXd(np.tile(lighting, (opt.sample_num, 1))) phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), lighting_normal) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) n = igl.eigen.MatrixXd(direction) barycoord = igl.eigen.MatrixXd() barycoord = igl.embree.line_mesh_intersection(source, n, mesh.v, mesh.f) fid = np.array(barycoord.col(0)).astype(int) idx = list(compress(range(opt.sample_num), fid != -1)) normalMap = np.empty((opt.sample_num, 3)) normalMap[:] = np.nan v11 = np.empty(opt.sample_num) v11[:] = np.nan d1 = np.empty(opt.sample_num) d1[:] = np.nan v2 = np.empty((opt.sample_num, 3)) v2[:] = np.nan d2 = np.empty(opt.sample_num) d2[:] = np.nan cos_theta2 = np.empty(opt.sample_num) cos_theta2[:] = np.nan distance_bin = np.empty(opt.sample_num) distance_bin[:] = opt.max_distance_bin + 1 intensity = np.empty(opt.sample_num) intensity[:] = np.nan intersection_p = igl.barycentric_to_global(mesh.v, mesh.f, barycoord) intersection_p = np.array(intersection_p) fn = np.array(mesh.fn) normalMap[idx, :] = fn[np.hstack(fid[idx]), :] v11[idx] = lighting[0] - intersection_p[idx, 0] d1[idx] = np.abs(np.divide(v11[idx], direction[idx, 0])) v2[idx, :] = sensor - intersection_p[idx, :] d2[idx] = np.sqrt(np.sum(v2[idx, :]**2, axis=1)) v2[idx, :] = element_divide2_np(v2[idx, :], d2[idx]) source = igl.eigen.MatrixXd(np.tile(sensor, (len(idx), 1))) n = igl.eigen.MatrixXd(-v2[idx, :]) barycoord2 = igl.eigen.MatrixXd() barycoord2 = igl.embree.line_mesh_intersection(source, n, mesh.v, mesh.f) fid2 = np.array(barycoord2.col(0)) idx = list(compress(idx, fid[idx] == fid2)) cos_theta2[idx] = np.einsum('ij,ij->i', normalMap[idx, :], v2[idx, :]) less_than_zero = list(compress(idx, cos_theta2[idx] < 0)) if len(less_than_zero) != 0: cos_theta2[less_than_zero] = 0 distance_bin[idx] = np.ceil( (d1[idx] + d2[idx]) / opt.distance_resolution) - 1 inds = list( compress(range(opt.sample_num), distance_bin < opt.max_distance_bin)) intensity[inds] = np.divide(cos_theta2[inds], d2[inds]**2) u = np.unique(distance_bin[inds]) for x in u: angular_transient[x.astype(int)] += sum(intensity[distance_bin == x]) angular_transient *= 2 * math.pi angular_transient /= opt.sample_num
f = 0.5 z = 1.8 sensor = np.array([f, 0, z]) lighting = np.array([-f, 0, z]) sensor_normal = np.array([0, 0, -1]) lighting_normal = np.array([0, 0, -1]) opt = OPT() phi = 2 * math.pi * np.random.rand(opt.sample_num) theta = np.arccos(np.random.rand(opt.sample_num)) R = np.zeros([3, 3]) rotation_matrix.R_2vect(R, np.array([0, 0, 1]), lighting_normal) direction = np.dot( np.vstack((np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta))).T, R.T) triangleIndexMap = mesh_intersection_grad_igl.find_triangle( mesh, direction, lighting, sensor) mesh.v = Variable(torch.from_numpy(np.array(mesh.v)), requires_grad=True) mesh.f = Variable(torch.from_numpy(np.array(mesh.f)).long()) tic = time.time() angular_transient = rendering_grad_igl.angular_sampling( mesh, direction, triangleIndexMap, lighting, sensor, lighting_normal, sensor_normal, opt)
#meas = np.array([.002, 0, .99]) #meas = np.array([0.002, 0.0018, .99]) #meas = np.array([0.000, 0.0018, .99]) #meas = np.array([-0.0012, 0.0018, .99]) #meas = np.array([-0.0014, 0.0014, .99]) #meas = np.array([-0.0018, 0.0014, .99]) #meas = np.array([-0.0022, 0.0010, .99]) #meas = np.array([-.003, 0.0010, 0.99]) #meas = np.array([-.004, 0.0009, 0.99]) meas = np.array([.01, 0.00, .99]) def CPP(matr): pieces = [[], [], []] for (a, b), val in np.ndenumerate(matr): pieces[a].append(int(val * 1000000000)) body = ", \n".join("%i, %i, %i" % tuple(p) for p in pieces) return "<%s, 1000000000>" % body def I3(): return np.matrix([[1., 0, 0], [0, 1., 0], [0, 0, 1.]]) m = I3() r.R_2vect(m, orig, meas) print orig, "->", meas print m print CPP(m)