def test_aa_points(): i3 = math.sqrt(1/3.0) for theta in (-0.2, 0.5): for vec in np.r_[np.eye(3), [[i3, i3, i3]]]: for point in [[0.3, 0.4, 0.5],[-0.2, 0, 4.0]]: R = axangle2aff(vec, theta) v2, t2, p2 = aff2axangle(R) assert_array_almost_equal(vec, v2) assert_array_almost_equal(theta, t2) assert_array_almost_equal(p2[:3], 0) # recovering a point point = [0.3, 0.4, 0.5] RP = axangle2aff(vec, theta, point) v3, t3, p3 = aff2axangle(RP) assert_array_almost_equal(vec, v3) assert_array_almost_equal(theta, t3) # doing the whole thing by hand T = np.eye(4) T[:3,3] = point iT = T.copy() iT[:3,3] *= -1 M_hand = np.dot(T, np.dot(R, iT)) assert_array_almost_equal(RP, M_hand) # do round trip RP_back = axangle2aff(v3, t3, p3) assert_array_almost_equal(RP, RP_back)
def align_dfmesh_scanpc(df_mesh, df_resolution, scan_pc): ''' df_mesh: trimesh df_resolution: distance field resolution scan_pc: Nx3, np array ''' pts_min = np.amin(scan_pc, axis=0) pts_max = np.amax(scan_pc, axis=0) pc_extents = pts_max - pts_min pc_bbox_center = (pts_max + pts_max) / 2.0 max_pc_size = np.max(pc_extents) df_mesh_extents = df_mesh.bounding_box.extents max_mesh_size = np.max(df_mesh_extents) scale_factor = 1.0 / df_resolution trans_v = pc_bbox_center - np.array([df_resolution/2.0, df_resolution/2.0, df_resolution/2.0]) df_mesh.apply_translation(trans_v) df_mesh.apply_scale(scale_factor) # rotate to make the face -z rot_m = axangle2aff([0,1,0], np.pi) df_mesh.apply_transform(rot_m) return df_mesh
def test_groove_direction(): '''Direction of grooves may not be parallel to z axis.''' photons = generate_test_photons(5) g = FlatGrating(d=1./500, order_selector=constant_order_factory(1)) assert np.allclose(np.dot(g.geometry['e_groove'], g.geometry['e_perp_groove']), 0.) p = g.process_photons(photons.copy()) g1 = FlatGrating(d=1./500, order_selector=constant_order_factory(1), groove_angle=.3) p1 = g1.process_photons(photons.copy()) pos3d = axangles.axangle2mat([1,0,0], .3) g2 = FlatGrating(d=1./500, order_selector=constant_order_factory(1), orientation=pos3d) p2 = g2.process_photons(photons.copy()) pos4d = axangles.axangle2aff([1,0,0], .1) g3 = FlatGrating(d=1./500, order_selector=constant_order_factory(1), groove_angle=.2, pos4d=pos4d) p3 = g3.process_photons(photons.copy()) def angle_in_yz(vec1, vec2): '''project in the y,z plane (the plane of the grating) and calculate angle.''' v1 = vec1[1:3] v2 = vec2[1:3] arccosalpha = np.dot(v1, v2) / np.sqrt(np.dot(v1, v1) * np.dot(v2, v2)) return np.arccos(arccosalpha) assert np.allclose(angle_in_yz(p1['dir'][0,:], p2['dir'][0, :]), 0) assert np.allclose(angle_in_yz(p3['dir'][0,:], p2['dir'][0, :]), 0) for px in [p1, p2, p3]: assert np.allclose(angle_in_yz(p['dir'][0,:], px['dir'][0, :]), 0.3)
def test_groove_direction(): '''Direction of grooves may not be parallel to z axis.''' photons = generate_test_photons(5) order1 = OrderSelector([1]) g = FlatGrating(d=1./500, order_selector=order1) e_groove, e_perp_groove, n = g.e_groove_coos(np.ones((2,1))) assert np.isclose(np.dot(e_groove[0, :], e_perp_groove[0, :]), 0.) p = g(photons.copy()) g1 = FlatGrating(d=1./500, order_selector=order1, groove_angle=.3) p1 = g1(photons.copy()) pos3d = axangles.axangle2mat([1,0,0], .3) g2 = FlatGrating(d=1./500, order_selector=order1, orientation=pos3d) p2 = g2(photons.copy()) pos4d = axangles.axangle2aff([1,0,0], .1) g3 = FlatGrating(d=1./500, order_selector=order1, groove_angle=.2, pos4d=pos4d) p3 = g3(photons.copy()) def angle_in_yz(vec1, vec2): '''project in the y,z plane (the plane of the grating) and calculate angle.''' v1 = vec1[1:3] v2 = vec2[1:3] arccosalpha = np.dot(v1, v2) / np.sqrt(np.dot(v1, v1) * np.dot(v2, v2)) return np.arccos(arccosalpha) assert np.allclose(angle_in_yz(p1['dir'][0,:], p2['dir'][0, :]), 0) assert np.allclose(angle_in_yz(p3['dir'][0,:], p2['dir'][0, :]), 0) for px in [p1, p2, p3]: assert np.allclose(angle_in_yz(p['dir'][0,:], px['dir'][0, :]), 0.3)
def test_pos4d_rotation(): rotation = axangle2aff(np.array([1, 1, 0]), np.deg2rad(90)) oe = marxs.optics.aperture.RectangleAperture(orientation=rotation[:3,:3]) assert np.all(oe.pos4d == rotation) assert np.all(oe.geometry['center'] == np.array([0, 0, 0, 1])) assert np.allclose(oe.geometry['e_y'], np.array([.5, .5, 1. / np.sqrt(2), 0])) assert np.allclose(oe.geometry['e_z'], np.array([1., -1, 0, 0]) / np.sqrt(2))
def rotation(self): """4x4 rotation matrix of this node relative to its parent""" r = self.fields.get('rotation', '').split() if len(r) != 4 or r[0] == 'IS': return np.identity(4) return axangle2aff([float(a) for a in r[:3]], float(r[3]))
def renderUnlabeled(BRDF, net, sess, lighting=None): sp = np.minimum(np.maximum(np.exp(BRDF[0]), 0.001), 1.0) bs = np.shape(sp)[0] if bs > 0: ro = np.minimum(np.maximum(np.exp(BRDF[1]), 0.001), 1.0) albedo = np.minimum(np.maximum(BRDF[2], 0.001), 1.0) nm = BRDF[3] * 2 - 1 normal = (nm / np.stack( [np.sqrt(nm[..., 0]**2 + nm[..., 1]**2 + nm[..., 2]**2)] * 3, axis=-1) + 1) / 2 specular = np.zeros([bs, 256, 256, 3]) roughness = np.zeros([bs, 256, 256]) normalxy = np.zeros([bs, 256, 256, 2]) mask = np.ones([bs, 256, 256]) lid = np.zeros([bs], int) matl = np.zeros([bs, 4, 4]) for b in range(bs): if lighting is None: lid[b] = np.random.randint(net.num_light) matl[b] = np.matmul( axangle2aff([1.0, 0.0, 0.0], (np.random.rand() - 0.5) * np.pi / 6), axangle2aff([0.0, 1.0, 0.0], np.random.rand() * np.pi * 2)) else: selection = np.random.randint(len(lighting[0])) lid[b] = lighting[0][selection] matl[b] = lighting[1][selection] specular[b] = np.full([256, 256, 3], sp[b][0]) roughness[b] = np.full([256, 256], ro[b][0]) normalxy[b] = normal[b][:, :, 2:0:-1] * 2 - 1 image_out = sess.run( net.render_batch, { net.albedo_batch: albedo, net.specular_batch: specular, net.roughness_batch: roughness, net.normalxy_batch: normalxy, net.mask_batch: mask, net.lightID_batch: lid, net.matLight_batch: matl }) batchUnlabeled = (image_out, np.log(np.concatenate( (sp, ro), axis=1)), albedo, normal) return (batchUnlabeled) else: return (None)
def test_pos4d_rotation(): rotation = axangle2aff(np.array([1, 1, 0]), np.deg2rad(90)) oe = marxs.optics.aperture.RectangleAperture(orientation=rotation[:3, :3]) assert np.all(oe.pos4d == rotation) assert np.all(oe.geometry['center'] == np.array([0, 0, 0, 1])) assert np.allclose(oe.geometry['e_y'], np.array([.5, .5, 1. / np.sqrt(2), 0])) assert np.allclose(oe.geometry['e_z'], np.array([1., -1, 0, 0]) / np.sqrt(2))
def test_pos4d_transforms_slit_rotated(photons1000): '''Test coordinate transforms on rotated entrance aperture.''' p = photons1000 rotation = axangle2aff(np.array([0, 1, 0]), np.deg2rad(90)) myslit = marxs.optics.aperture.RectangleAperture(orientation=rotation[:3, :3], zoom=0.5) p = myslit.process_photons(p) assert np.allclose(p['pos'][:, 2], 0) assert kstest(p['pos'][:, 0] + 0.5, "uniform")[1] > 0.01 assert kstest(p['pos'][:, 1] + 0.5, "uniform")[1] > 0.01
def test_pos4d_transforms_slit_rotated(photons1000): '''Test coordinate transforms on rotated entrance aperture.''' p = photons1000 rotation = axangle2aff(np.array([0, 1, 0]), np.deg2rad(90)) myslit = marxs.optics.aperture.RectangleAperture( orientation=rotation[:3, :3], zoom=0.5) p = myslit.process_photons(p) assert np.allclose(p['pos'][:, 2], 0) assert kstest(p['pos'][:, 0] + 0.5, "uniform")[1] > 0.01 assert kstest(p['pos'][:, 1] + 0.5, "uniform")[1] > 0.01
def __init__(self, **kwargs): self.order_selector = kwargs.pop('order_selector') self.transmission = kwargs.pop('transmission', True) if 'd' not in kwargs: raise ValueError('Input parameter "d" (Grating constant) is required.') self.d = kwargs.pop('d') self.groove_ang = kwargs.pop('groove_angle', 0.) super(FlatGrating, self).__init__(**kwargs) self.groove4d = axangles.axangle2aff(self.geometry['e_x'][:3], self.groove_ang) self.geometry['e_groove'] = np.dot(self.groove4d, self.geometry['e_z']) self.geometry['e_perp_groove'] = np.dot(self.groove4d, self.geometry['e_y'])
def __init__(self, **kwargs): self.order_selector = kwargs.pop('order_selector') self.transmission = kwargs.pop('transmission', True) if 'd' not in kwargs: raise ValueError('Input parameter "d" (Grating constant) is required.') self._d = kwargs.pop('d') self.groove_ang = kwargs.pop('groove_angle', 0.) super(FlatGrating, self).__init__(**kwargs) self.groove4d = axangles.axangle2aff(self.geometry['e_x'][:3], self.groove_ang) self.geometry['e_groove'] = np.dot(self.groove4d, self.geometry['e_z']) self.geometry['e_perp_groove'] = np.dot(self.groove4d, self.geometry['e_y'])
def test_angle_axis_imps(): for x, y, z in euler_tuples: M = ttb.euler2mat(z, y, x) q = tq.mat2quat(M) vec, theta = tq.quat2axangle(q) M1 = axangle2mat(vec, theta) M2 = axangle2aff(vec, theta)[:3,:3] assert_array_almost_equal(M1, M2) v1, t1 = mat2axangle(M1) M3 = axangle2mat(v1, t1) assert_array_almost_equal(M, M3) A = np.eye(4) A[:3,:3] = M v2, t2, point = aff2axangle(A) M4 = axangle2mat(v2, t2) assert_array_almost_equal(M, M4)
def tiltCATGratings(instrum, defaultblaze, blaze): '''Tilt Arcus gratings. In the early days of this script, my code would place the gratings on the Rowlandtorus depending on the input values in the conf dictionary. However, with the more mature design, the position and location of the gratings is read from CATfromMechanical. This function will twist those gratings to keep he position consistent, but experiment with different blaze angles. ''' rot = np.deg2rad(blaze - defaultblaze) grats = instrum.elements_of_class(CATL1L2Stack) for g in grats: rotmat = axangle2aff(g.geometry['e_z'][:3], rot) newpos = rotmat @ g.geometry.pos4d g.geometry.pos4d = newpos for ge in g.elements: g.geometry.pos4d = newpos
def test_groove_direction(): '''Direction of grooves may not be parallel to z axis.''' photons = generate_test_photons(5) order1 = OrderSelector([1]) g = FlatGrating(d=1. / 500, order_selector=order1) e_groove, e_perp_groove, n = g.e_groove_coos(np.ones((2, 1))) assert np.isclose(np.dot(e_groove[0, :], e_perp_groove[0, :]), 0.) p = g(photons.copy()) g1 = FlatGrating(d=1. / 500, order_selector=order1, groove_angle=.3) p1 = g1(photons.copy()) pos3d = axangles.axangle2mat([1, 0, 0], .3) g2 = FlatGrating(d=1. / 500, order_selector=order1, orientation=pos3d) p2 = g2(photons.copy()) pos4d = axangles.axangle2aff([1, 0, 0], .1) g3 = FlatGrating(d=1. / 500, order_selector=order1, groove_angle=.2, pos4d=pos4d) p3 = g3(photons.copy()) def angle_in_yz(vec1, vec2): '''project in the y,z plane (the plane of the grating) and calculate angle.''' v1 = vec1[1:3] v2 = vec2[1:3] arccosalpha = np.dot(v1, v2) / np.sqrt(np.dot(v1, v1) * np.dot(v2, v2)) return np.arccos(arccosalpha) assert np.allclose(angle_in_yz(p1['dir'][0, :], p2['dir'][0, :]), 0, atol=5e-8) assert np.allclose(angle_in_yz(p3['dir'][0, :], p2['dir'][0, :]), 0, atol=5e-8) for px in [p1, p2, p3]: assert np.allclose(angle_in_yz(p['dir'][0, :], px['dir'][0, :]), 0.3)
def test_groove_direction(): '''Direction of grooves may not be parallel to z axis.''' photons = generate_test_photons(5) g = FlatGrating(d=1. / 500, order_selector=constant_order_factory(1)) assert np.allclose( np.dot(g.geometry['e_groove'], g.geometry['e_perp_groove']), 0.) p = g.process_photons(photons.copy()) g1 = FlatGrating(d=1. / 500, order_selector=constant_order_factory(1), groove_angle=.3) p1 = g1.process_photons(photons.copy()) pos3d = axangles.axangle2mat([1, 0, 0], .3) g2 = FlatGrating(d=1. / 500, order_selector=constant_order_factory(1), orientation=pos3d) p2 = g2.process_photons(photons.copy()) pos4d = axangles.axangle2aff([1, 0, 0], .1) g3 = FlatGrating(d=1. / 500, order_selector=constant_order_factory(1), groove_angle=.2, pos4d=pos4d) p3 = g3.process_photons(photons.copy()) def angle_in_yz(vec1, vec2): '''project in the y,z plane (the plane of the grating) and calculate angle.''' v1 = vec1[1:3] v2 = vec2[1:3] arccosalpha = np.dot(v1, v2) / np.sqrt(np.dot(v1, v1) * np.dot(v2, v2)) return np.arccos(arccosalpha) assert np.allclose(angle_in_yz(p1['dir'][0, :], p2['dir'][0, :]), 0) assert np.allclose(angle_in_yz(p3['dir'][0, :], p2['dir'][0, :]), 0) for px in [p1, p2, p3]: assert np.allclose(angle_in_yz(p['dir'][0, :], px['dir'][0, :]), 0.3)
def align_dfmesh_scanpc(df_mesh, df_resolution): ''' df_mesh: trimesh df_resolution: distance field resolution scan_pc: Nx3, np array ''' pc_bbox_center = np.array([0., 0., 0.]) df_mesh_extents = df_mesh.bounding_box.extents max_mesh_size = np.max(df_mesh_extents) scale_factor = 1.0 / df_resolution trans_v = pc_bbox_center - np.array( [df_resolution / 2.0, df_resolution / 2.0, df_resolution / 2.0]) df_mesh.apply_translation(trans_v) df_mesh.apply_scale(scale_factor) # rotate to make the face -z, from +z to -z rot_m = axangle2aff([0, 1, 0], np.pi) df_mesh.apply_transform(rot_m) return df_mesh
def testModelByBRDF(synData, lighting, net, sess, root, outputFreq=0, step=0, times=1): splt = 0 rolt = 0 ablt = 0 nmlt = 0 relt = 0 stlt = 0 nllt = 0 cnt = 0 pt = 0 if not lighting is None: lids, matls = lighting for i in range(len(synData) * times // batch_size): BRDF = [] for b in range(batch_size): if pt >= len(synData) * times: pt = 0 BRDF += [synData[pt // times]] pt += 1 albedo = np.zeros([batch_size, 256, 256, 3]) specular = np.zeros([batch_size, 256, 256, 3]) roughness = np.zeros([batch_size, 256, 256]) normalxy = np.zeros([batch_size, 256, 256, 2]) mask = np.ones([batch_size, 256, 256]) lid = np.zeros([batch_size], int) matl = np.zeros([batch_size, 4, 4]) sr = np.zeros([batch_size, 2]) normal = np.zeros([batch_size, 256, 256, 3]) for b in range(batch_size): if not lighting is None: lid[b] = lids[i * batch_size + b] matl[b] = matls[i * batch_size + b] else: lid[b] = np.random.randint(net.num_light) matl[b] = np.matmul( axangle2aff([1.0, 0.0, 0.0], (np.random.rand() - 0.5) * np.pi / 6), axangle2aff([0.0, 1.0, 0.0], np.random.rand() * np.pi * 2)) specular[b] = np.full([256, 256, 3], BRDF[b][0]) roughness[b] = np.full([256, 256], BRDF[b][1]) albedo[b] = BRDF[b][2] normal[b] = BRDF[b][3] normalxy[b] = normal[b][:, :, 2:0:-1] * 2 - 1 sr[b, 0] = BRDF[b][0] sr[b, 1] = BRDF[b][1] image_out = sess.run( net.render_batch, { net.albedo_batch: albedo, net.specular_batch: specular, net.roughness_batch: roughness, net.normalxy_batch: normalxy, net.mask_batch: mask, net.lightID_batch: lid, net.matLight_batch: matl }) batchSyn = (image_out, np.log(sr), albedo, normal) spl, rol, abl, nml, spp, rop, alp, nmp = sess.run( [ net.loss_Sp_test, net.loss_Ro_test, net.loss_Al_test, net.loss_Nm_test, net.specular_pred_test, net.roughness_pred_test, net.albedo_pred_test, net.normal_pred_test ], feed_dict={ net.inputimage: batchSyn[0], net.sr_gt: batchSyn[1], net.albedo_gt: batchSyn[2], net.normal_gt: batchSyn[3] }) splt += spl rolt += rol ablt += abl nmlt += nml for b in range(batch_size): specular[b] = np.full([256, 256, 3], np.exp(spp[b])) roughness[b] = np.full([256, 256], np.exp(rop[b])) nm = nmp[b] * 2 - 1 normalxy[b] = (nm / np.stack( [np.sqrt(nm[..., 0]**2 + nm[..., 1]**2 + nm[..., 2]**2)] * 3, axis=-1))[:, :, 2:0:-1] rerender_out = sess.run( net.render_batch, { net.albedo_batch: alp, net.specular_batch: specular, net.roughness_batch: roughness, net.normalxy_batch: normalxy, net.mask_batch: mask, net.lightID_batch: lid, net.matLight_batch: matl }) for b in range(batch_size): rel = np.mean((rerender_out[b] - image_out[b])**2) relt += rel dssim = 1 - ssim(toLDR(rerender_out[b]).astype(np.uint8), toLDR(image_out[b]).astype(np.uint8), data_range=255, multichannel=True) stlt += dssim if outputFreq > 0: if (cnt * batch_size + b) % outputFreq == 0: saveRerender( '{}/{}_{}.png'.format(root, step, cnt * batch_size + b), rerender_out[b], image_out[b], alp[b], batchSyn[2][b], nmp[b], batchSyn[3][b], np.exp(spp[b][0]), np.exp(batchSyn[1][b][0]), np.exp(rop[b][0]), np.exp(batchSyn[1][b][1]), rel, dssim) cnt += 1 print( 'Test: avg sp_loss: {}, avg ro_loss: {}, avg al_loss: {}, avg nm_loss: {}, avg rerender_loss: {}, avg dssim: {}' .format(splt / cnt, rolt / cnt, ablt / cnt, nmlt / cnt, relt / cnt / batch_size, stlt / cnt / batch_size)) summ_test = sess.run(net.summ_op_test, feed_dict={ net.phloss_Sp_test: splt / cnt, net.phloss_Ro_test: rolt / cnt, net.phloss_Al_test: ablt / cnt, net.phloss_Nm_test: nmlt / cnt }) return (summ_test)
def getBatchShuffle(synData, pt, net, sess, lighting=None): BRDF = [] for b in range(batch_size): if pt >= len(synData): np.random.shuffle(synData) nms = [] sps = [] ros = [] for i in range(len(synData)): nms += [synData[i][3]] sps += [synData[i][0]] ros += [synData[i][1]] np.random.shuffle(nms) np.random.shuffle(sps) np.random.shuffle(ros) for i in range(len(synData)): synData[i][3] = nms[i] synData[i][0] = sps[i] synData[i][1] = ros[i] pt = 0 BRDF += [synData[pt]] pt += 1 albedo = np.zeros([batch_size, 256, 256, 3]) specular = np.zeros([batch_size, 256, 256, 3]) roughness = np.zeros([batch_size, 256, 256]) normalxy = np.zeros([batch_size, 256, 256, 2]) mask = np.ones([batch_size, 256, 256]) lid = np.zeros([batch_size], int) matl = np.zeros([batch_size, 4, 4]) sr = np.zeros([batch_size, 2]) normal = np.zeros([batch_size, 256, 256, 3]) for b in range(batch_size): if lighting is None: lid[b] = np.random.randint(net.num_light) matl[b] = np.matmul( axangle2aff([1.0, 0.0, 0.0], (np.random.rand() - 0.5) * np.pi / 6), axangle2aff([0.0, 1.0, 0.0], np.random.rand() * np.pi * 2)) else: selection = np.random.randint(len(lighting[0])) lid[b] = lighting[0][selection] matl[b] = lighting[1][selection] specular[b] = np.full([256, 256, 3], BRDF[b][0]) roughness[b] = np.full([256, 256], BRDF[b][1]) albedo[b] = BRDF[b][2] normal[b] = BRDF[b][3] normalxy[b] = normal[b][:, :, 2:0:-1] * 2 - 1 sr[b, 0] = BRDF[b][0] sr[b, 1] = BRDF[b][1] image_out = sess.run( net.render_batch, { net.albedo_batch: albedo, net.specular_batch: specular, net.roughness_batch: roughness, net.normalxy_batch: normalxy, net.mask_batch: mask, net.lightID_batch: lid, net.matLight_batch: matl }) batchSyn = (image_out, np.log(sr), albedo, normal) return (batchSyn, pt)
def plane_which_side(pts_on_plane, plane_normal, query_pts): d = - (plane_normal[0]*pts_on_plane[0] + plane_normal[1]*pts_on_plane[1] + plane_normal[2]*pts_on_plane[2]) res = np.dot(np.array([plane_normal[0], plane_normal[1], plane_normal[2], d]), np.array([query_pts[0], query_pts[1], query_pts[2], 1])) if res > 0: return 1 # positive side elif res == 0: return 0 # on else: return -1 # negtive side sdf_data = [] for mesh in REAL_TEST_DATASET.meshes: # v2 (-z) -> 3D-EPN (+z) rot_affmat = axangle2aff([0,1,0], radians(180)) mesh.apply_transform(rot_affmat) mesh_points, mesh_sample_fidx = trimesh.sample.sample_surface(mesh, 20480) mesh_point_normals = mesh.face_normals[mesh_sample_fidx] print('Construct cKD-tree from %d points.'%(mesh_points.shape[0])) tree = spatial.cKDTree(mesh_points) # get centers of volumn voxels vox_len = sdf_scale / sdf_resolution x_coords = np.linspace(-sdf_scale/2. + vox_len/2., sdf_scale/2. - vox_len/2., sdf_resolution) y_coords = np.linspace(-sdf_scale/2. + vox_len/2., sdf_scale/2. - vox_len/2., sdf_resolution) z_coords = np.linspace(-sdf_scale/2. + vox_len/2., sdf_scale/2. - vox_len/2., sdf_resolution) xv, yv, zv = np.meshgrid(x_coords, y_coords, z_coords, indexing='xy') xv = np.expand_dims(xv, axis=-1) yv = np.expand_dims(yv, axis=-1)