def __init__(self): learning_root_dir='../../' self.shared_data_dir=os.path.join(learning_root_dir,'shared_data') # self.data_root_dir='/data/njin19/generated_models_v2' self.data_root_dir='/data/zhenglin/poses_v3' self.offsets_dir=os.path.join(self.data_root_dir,'lowres_offset_npys') self.offsets_T_dir='/data/zhenglin/poses_v3/lowres_offset_T_npys' self.skin_dir=os.path.join(self.data_root_dir,'lowres_skin_npys') self.rotation_dir=os.path.join(self.data_root_dir,'rotation_matrices') bone_names_path=os.path.join(self.shared_data_dir,'rotation_readme.txt') self.bone_names=load_bone_names(bone_names_path) skin_weights_path=os.path.join(self.shared_data_dir,'skin_weights.txt') self.bone_structure,self.whole_body_bone_weights=load_skin_weights(skin_weights_path) modelT_path=os.path.join(self.shared_data_dir,'modelT.obj') modelT=read_obj(modelT_path) self.body_fcs=modelT.f cloth_embeddings_path=os.path.join(self.shared_data_dir,'dressed_TshirtW_embedding.txt') self.whole_cloth_body_weights=load_cloth_embeddings(cloth_embeddings_path) self.skin_cloth_T_vts=get_skin_cloth_vts(modelT,self.whole_cloth_body_weights) # flat_tshirt_path=os.path.join(self.shared_data_dir,'flat_tshirt.obj') flat_tshirt_path=os.path.join(self.shared_data_dir,'flat_TshirtW_remesh3_lowres_tri.obj') flat_tshirt_obj=read_obj(flat_tshirt_path) self.tshirt_fcs=flat_tshirt_obj.f self.whole_cloth_bone_weights=get_whole_cloth_bone_weights(self.whole_cloth_body_weights,self.whole_body_bone_weights,self.body_fcs)
def compare_loss(sample_ids,gt_pattern,pd_pattern1,pd_pattern2,loss_fn=avg_vt_loss): loss_list=[] for sample_id in sample_ids: gt_v=read_obj(gt_pattern.format(sample_id)).v pd1_v=read_obj(pd_pattern1.format(sample_id)).v loss_1=loss_fn(pd1_v,gt_v) pd2_v=read_obj(pd_pattern2.format(sample_id)).v loss_2=loss_fn(pd2_v,gt_v) loss_list.append((loss_2/loss_1,loss_2,loss_1,sample_id)) loss_list.sort(key=lambda t:t[0]) for t in loss_list: print(t)
def compute_loss_stats(samples_dir,gt_pattern,pd_pattern,loss_fn): sample_dirs=os.listdir(samples_dir) loss_list=[] for sample_dir in sample_dirs: sample_id=int(sample_dir) gt_path=join(samples_dir,sample_dir,gt_pattern.format(sample_id)) gt_v=read_obj(gt_path).v pd_path=join(samples_dir,sample_dir,pd_pattern.format(sample_id)) # print('pd_path',pd_path) pd_v=read_obj(pd_path).v loss_list.append(loss_fn(pd_v,gt_v)) loss_list=np.array(loss_list) print('mean:',np.mean(loss_list),'max',np.max(loss_list),'min',np.min(loss_list),'med',np.median(loss_list),'#',len(loss_list))
def compute_energy_stats(samples_dir,gt_pattern,pd_pattern): energy_utils=EnergyUitls() sample_dirs=os.listdir(samples_dir) energy_list=[] for sample_dir in sample_dirs: sample_id=int(sample_dir) gt_path=join(samples_dir,sample_dir,gt_pattern.format(sample_id)) gt_v=read_obj(gt_path).v pd_path=join(samples_dir,sample_dir,pd_pattern.format(sample_id)) pd_v=read_obj(pd_path).v energy_diff=np.abs(energy_utils.compute_energy(pd_v)-energy_utils.compute_energy(gt_v)) energy_list.append(energy_diff) energy_list=np.array(energy_list) print('mean:',np.mean(energy_list),'max',np.max(energy_list),'min',np.min(energy_list),'med',np.median(energy_list),'#',len(energy_list))
def load_opt_data(res_ctx, ctx): max_num_constraints = ctx['max_num_constraints'] tshirt_obj_path = join(res_ctx['shared_data_dir'], 'flat_tshirt.obj') tshirt_obj = read_obj(tshirt_obj_path) n_vts = len(tshirt_obj.v) linear_edges = np.loadtxt( join(res_ctx['shared_data_dir'], 'linear_edges.txt')).astype(int) linear_rest_lengths = np.loadtxt( join(res_ctx['shared_data_dir'], 'mat_or_med_linear.txt')) edges = linear_edges l0 = linear_rest_lengths if 'use_spring' in ctx and ctx['use_spring']: bend_edges = np.loadtxt( join(res_ctx['shared_data_dir'], 'bend_edges.txt')).astype(int) bend_rest_lengths = np.loadtxt( join(res_ctx['shared_data_dir'], 'mat_or_med_bend.txt')) edges = np.concatenate([linear_edges, bend_edges], axis=0) l0 = np.concatenate([linear_rest_lengths, bend_rest_lengths]) n_linear_edges = len(linear_edges) n_bend_edges = len(bend_edges) res_ctx['stiffness'] = np.array([10 / (1 + np.sqrt(2))] * n_linear_edges + [2 / (1 + np.sqrt(2))] * n_bend_edges) if max_num_constraints >= 0: edges = edges[:max_num_constraints] l0 = l0[:max_num_constraints] # m=np.ones(n_vts) m = np.loadtxt(join(res_ctx['shared_data_dir'], 'm.txt')) * 1e4 # l0*=1.01 return m, edges, l0
def write_proto_scenes(in_dirs, out_dir, offsets): # write out_dir if not os.path.isdir(out_dir): os.mkdir(out_dir) common_dir = os.path.join(out_dir, 'common') if not os.path.isdir(common_dir): os.mkdir(common_dir) proto_debug_utils = PROTO_DEBUG_UTILS() first_frame = 1 frame = 1 in_dir0 = in_dirs[0] files = os.listdir(in_dir0) for file in files: if not file.endswith('obj'): continue file_name = file[:-4] for i in range(len(in_dirs)): in_dir = in_dirs[i] file_path = os.path.join(in_dir, file) if not os.path.isfile(file_path): continue obj = read_obj(file_path) proto_debug_utils.Add_Mesh(obj.v + offsets[i], obj.f, [[1, 1, 0]]) frame_dir = os.path.join(out_dir, '{}'.format(frame)) if not os.path.isdir(frame_dir): os.mkdir(frame_dir) proto_debug_utils.Write_Output_Files(out_dir, frame) print('frame', frame, 'file_name', file_name) frame += 1 last_frame = frame with open(os.path.join(common_dir, 'first_frame'), 'w') as f: f.write('{}\n'.format(first_frame)) with open(os.path.join(common_dir, 'last_frame'), 'w') as f: f.write('{}\n'.format(last_frame))
def write_skin_img_cloth_from_id(self, test_id, prefix='cloth', out_dir=None): device = self.device offset_img = np.load( os.path.join(self.offset_img_dir, 'offset_img_{:08d}.npy'.format(test_id))) skin_img = np.load( os.path.join(self.skin_img_dir, 'skin_img_{:08d}.npy'.format(test_id))) cloth_img = offset_img + skin_img cloth_imgs = torch.from_numpy(cloth_img).permute(2, 0, 1).unsqueeze(0) cloth_vt = self.get_offsets_torch(cloth_imgs)[0].detach().cpu().numpy() gt_cloth_path = os.path.join(self.data_root_dir, 'lowres_tshirts', 'tshirt_{:08d}.obj'.format(test_id)) gt_cloth_vt = read_obj(gt_cloth_path).v print('error', self.get_error(gt_cloth_vt - cloth_vt)) obj = Obj(v=cloth_vt, f=self.fcs) if out_dir is None: out_dir = self.out_dir out_path = os.path.join(self.out_dir, '{}_{:08d}.obj'.format(prefix, test_id)) write_obj(obj, out_path)
def __init__(self, **kwargs): # load fcs self.shared_data_dir = kwargs['shared_data_dir'] self.data_root_dir = kwargs['data_root_dir'] self.skin_dir = kwargs['skin_dir'] self.cloth_type = kwargs.get('cloth_type', 'lowres_tshirt') assert (self.cloth_type in ['lowres_tshirt', 'midres_tshirt', 'tie']) self.cloth_prefix = 'necktie' if self.cloth_type == 'tie' else 'tshirt' if self.cloth_type == 'lowres_tshirt': self.cloth_npys_dir = os.path.join(self.data_root_dir, 'lowres_tshirt_npys') elif self.cloth_type == 'midres_tshirt': self.cloth_npys_dir = os.path.join(self.data_root_dir, 'midres_tshirt_npys') else: self.cloth_npys_dir = os.path.join(self.data_root_dir, 'necktie_npys') if 'cloth_npys_dir' in kwargs: self.cloth_npys_dir = kwargs['cloth_npys_dir'] if self.cloth_type == 'lowres_tshirt' or self.cloth_type == 'midres_tshirt': rest_cloth_path = os.path.join(self.shared_data_dir, 'flat_tshirt.obj') else: rest_cloth_path = os.path.join(self.shared_data_dir, 'necktie_rest.obj') rest_cloth_obj = read_obj(rest_cloth_path) self.fcs = torch.from_numpy(rest_cloth_obj.f).long() self.out_dir = 'normal_test' # only used for test purpose
def run_pp(self,in_path,out_path): if self.cudaqs_utils is None: self.cudaqs_utils=CudaqsUtils('../../pixel_network/shared_data_hr_front') obj=read_obj(in_path) obj.v=self.cudaqs_utils.forward(obj.v) print('write to',out_path) write_obj(obj,out_path)
def write_area_ratio_ply(samples_dir, in_pattern, out_pattern, ratio_range=0.5): sample_dirs = os.listdir(samples_dir) area_utils = AreaUtils() for sample_dir in sample_dirs: sample_id = int(sample_dir) obj_path = join(samples_dir, sample_dir, in_pattern.format(sample_id)) obj = read_obj(obj_path) v = obj.v r = area_utils.compute_vt_area_ratios(v) # print('min:',np.min(r),'max:',np.max(r)) t = ((r - 1) / ratio_range).clip(-1, 1) color0 = np.array([[1, 1, 1]]) color_neg = np.array([[0, 0, 1]]) color_pos = np.array([[1, 0, 0]]) colors = np.zeros_like(v) # print('colors',colors.shape,'t',t.shape) pos_i = t >= 0 neg_i = t < 0 pos_t = (t[pos_i]).reshape((-1, 1)) neg_t = (-t[neg_i]).reshape((-1, 1)) colors[pos_i] = (1 - pos_t) * color0 + pos_t * color_pos colors[neg_i] = (1 - neg_t) * color0 + neg_t * color_neg colors = np.uint8(colors * 255) out_path = join(samples_dir, sample_dir, out_pattern.format(sample_id)) print('write to', out_path) write_ply(out_path, v, obj.f, colors)
def test_samples_dir(self, samples_dir): sample_dirs = os.listdir(samples_dir) pd_pattern = 'pd_cloth_{:08d}.obj' cr_pattern = 'cr_ineq_cloth_{:08d}.obj' # pd_pattern='pd_{:08d}.obj' # cr_pattern='cr_ineq_{:08d}.obj' anchor = [] ids = [] for i, sample_dir in enumerate(sample_dirs): sample_id = int(sample_dir) pd_path = join(samples_dir, sample_dir, pd_pattern.format(sample_id)) pd_obj = read_obj(pd_path) v, f = pd_obj.v, pd_obj.f v = torch.from_numpy(v).to(device=self.device, dtype=self.dtype) v = v.unsqueeze(0) anchor.append(v) ids.append(sample_id) if len(anchor) == self.batch_size or i == len(sample_dirs) - 1: anchor = torch.cat(anchor, dim=0) start_time = time.time() x = self.proj_module(anchor) end_time = time.time() # print('ellapse:',end_time-start_time) for j, sample_id in enumerate(ids): x_save = x[j].detach().cpu().numpy() cr_path = join(samples_dir, '{:08d}'.format(sample_id), cr_pattern.format(sample_id)) print('write to', cr_path) write_obj(Obj(v=x_save, f=f), cr_path) anchor = [] ids = []
def test_loss(self, sample_id): pd_vt, f = self.read_obj(join( self.pd_dir, '{:08d}/pd_cloth_{:08d}.obj'.format(sample_id, sample_id)), patch_id=self.patch_id) cr_gt_vt, _ = self.read_obj(join( 'opt_test/cpp_test/cr_gt_{:08d}_bak.obj'.format(sample_id)), patch_id=self.patch_id) agg_tgt_vt, _ = self.read_obj(join( 'opt_test/cpp_test/opt/cr_tgt_{:08d}_lap0.obj'.format(sample_id)), patch_id=self.patch_id) tgt_vt = read_obj( join(self.opt_dir, 'tgt_015_{:08d}.obj'.format(sample_id))).v[self.patch_vt_ids] print(np.linalg.norm(tgt_vt - cr_gt_vt, axis=1)) print(np.linalg.norm(agg_tgt_vt - cr_gt_vt, axis=1)) print('agg Ninf:', np.max(np.linalg.norm(agg_tgt_vt - cr_gt_vt, axis=1)), 'N2:', np.linalg.norm(agg_tgt_vt - cr_gt_vt)) print('patch Ninf:', np.max(np.linalg.norm(tgt_vt - cr_gt_vt, axis=1)), 'N2:', np.linalg.norm(tgt_vt - cr_gt_vt)) self.write_obj(cr_gt_vt, f, prefix=join(self.opt_dir, 'cr_gt'), sample_id=sample_id, patch_id=self.patch_id) self.write_obj(agg_tgt_vt, f, prefix=join(self.opt_dir, 'agg_tgt'), sample_id=sample_id, patch_id=self.patch_id)
def __init__(self, shared_data_dir='../../shared_data'): # get vts # shared_data_dir='../../shared_data' obj_path = os.path.join(shared_data_dir, 'flat_tshirt.obj') obj = read_obj(obj_path) vts = obj.v m = get_vts_normalize_m(vts) vts = normalize_vts(vts[:, :2], m) self.vts = vts self.fcs = obj.f # get_region_names self.region_path = os.path.join(shared_data_dir, 'regions') region_names_path = os.path.join(self.region_path, 'region_names.txt') self.region_names = self.load_region_names(region_names_path) self.n_regions = len(self.region_names) print('num_regions', len(self.region_names)) self.img_size = 512 self.crop_size = 512 * 5 // 16 region_crop_dir = os.path.join(self.region_path, 'region_crops') if not os.path.isdir(region_crop_dir): os.mkdir(region_crop_dir) self.region_crop_dir = region_crop_dir self.mask_margin_size = 16 mask_path = os.path.join(shared_data_dir, 'offset_img_mask.npy') mask = np.load(mask_path) self.front_mask = mask[:, :, 0] self.back_mask = mask[:, :, 1]
def read_obj(self, path, patch_id=-1): pd_obj = read_obj(path) pd_vt = pd_obj.v f = pd_obj.f if patch_id >= 0: pd_vt = pd_vt[self.patch_vt_ids] f = f[self.patch_fc_ids] return pd_vt, f
def __init__(self, lambda0=0, cg_tol=1e-3, cg_max_iter=1000, n_newton_steps=1, beta=0.7): self.shared_data_dir = '../../shared_data_midres' self.data_root_dir = '/data/zhenglin/poses_v3' self.res = 128 self.lambda0 = lambda0 self.cg_tol = cg_tol self.cg_max_iter = cg_max_iter self.n_newton_steps = n_newton_steps self.beta = beta self.pd_offset_dir = 'opt_test' self.gt_offset_dir = join(self.data_root_dir, 'midres_offset_npys') self.skin_vt_dir = join(self.data_root_dir, 'midres_skin_npys') self.cr_vt_dir = 'opt_test' self.out_obj_dir = 'opt_test' self.device = torch.device('cuda:0') self.dtype = torch.double self.offset_manager = OffsetManager(self.shared_data_dir, ctx={ 'data_root_dir': self.data_root_dir, 'offset_img_size': self.res, 'device': self.device }) self.offset_io_manager = OffsetIOManager( res_ctx={ 'skin_dir': None, 'shared_data_dir': self.shared_data_dir }) tshirt_obj_path = os.path.join(self.shared_data_dir, 'flat_tshirt.obj') tshirt_obj = read_obj(tshirt_obj_path) self.rest_vts = torch.from_numpy(tshirt_obj.v).to(device=self.device, dtype=self.dtype) self.fcs = torch.from_numpy(tshirt_obj.f).to(device=self.device, dtype=torch.long) front_vt_ids = np.loadtxt( join(self.shared_data_dir, 'front_vertices.txt')).astype(np.int32) back_vt_ids = np.loadtxt( join(self.shared_data_dir, 'back_vertices.txt')).astype(np.int32) self.stepper = NewtonStepper(self.rest_vts, self.fcs, front_vt_ids=front_vt_ids, back_vt_ids=back_vt_ids, bdry_ids=None, lambda0=lambda0, cg_tol=self.cg_tol, cg_max_iter=self.cg_max_iter) self.system = self.stepper.system self.verbose = True
def __init__(self): self.shared_data_dir = '../../shared_data' # self.shared_data_dir='../../shared_data_midres' front_obj_path = os.path.join(self.shared_data_dir, 'flat_tshirt_front.obj') front_obj = read_obj(front_obj_path) front_vts, front_fcs = front_obj.v, front_obj.f front_vts_normalize_m = get_vts_normalize_m(front_vts) front_obj_w_pad_path = os.path.join(self.shared_data_dir, 'front_tshirt_w_pad.obj') front_obj_w_pad = read_obj(front_obj_w_pad_path) front_vts_w_pad, front_fcs_w_pad = front_obj_w_pad.v, front_obj_w_pad.f front_vts_w_pad = normalize_vts(front_vts_w_pad[:, :2], front_vts_normalize_m) back_obj_path = os.path.join(self.shared_data_dir, 'flat_tshirt_back.obj') back_obj = read_obj(back_obj_path) back_vts, back_fcs = back_obj.v, back_obj.f back_vts_normalize_m = get_vts_normalize_m(back_vts) back_obj_w_pad_path = os.path.join(self.shared_data_dir, 'back_tshirt_w_pad.obj') back_obj_w_pad = read_obj(back_obj_w_pad_path) back_vts_w_pad, back_fcs_w_pad = back_obj_w_pad.v, back_obj_w_pad.f back_vts_w_pad = normalize_vts(back_vts_w_pad[:, :2], back_vts_normalize_m) front_crpds_path = os.path.join(self.shared_data_dir, 'front_vert_crpds.txt') front_crpds = cvt_crpds(load_crpds(front_crpds_path)) back_crpds_path = os.path.join(self.shared_data_dir, 'back_vert_crpds.txt') back_crpds = cvt_crpds(load_crpds(back_crpds_path)) print('len(front)', len(front_vts), 'len(back)', len(back_vts)) self.front_vts = front_vts_w_pad self.back_vts = back_vts_w_pad self.front_fcs = front_fcs_w_pad self.back_fcs = back_fcs_w_pad self.front_crpds = front_crpds self.back_crpds = back_crpds self.img_dir = 'opt_test/rasterize'
def write_diff_ply(samples_dir,gt_pattern,pd_pattern,out_pattern,max_diff=0.04): sample_dirs=os.listdir(samples_dir) for sample_dir in sample_dirs: sample_id=int(sample_dir) gt_path=join(samples_dir,sample_dir,gt_pattern.format(sample_id)) gt_obj=read_obj(gt_path) gt_v=gt_obj.v pd_path=join(samples_dir,sample_dir,pd_pattern.format(sample_id)) pd_v=read_obj(pd_path).v d=norm(pd_v-gt_v,axis=1) t=np.clip(d/max_diff,0,1).reshape((-1,1)) color0=np.array([[1,1,1]]) color1=np.array([[1,0,0]]) colors=color0*(1-t)+color1*t colors=np.uint8(colors*255) out_path=join(samples_dir,sample_dir,out_pattern.format(sample_id)) print('write to',out_path) write_ply(out_path,pd_v,gt_obj.f,colors)
def read_obj(self, path, patch_id=-1): pd_obj = read_obj(path) pd_vt = pd_obj.v assert (len(pd_vt) == self.agg_n_vts) f = pd_obj.f if patch_id >= 0: pd_vt = pd_vt[self.patch_vt_ids] f = f[self.patch_fc_ids] return pd_vt, f
def __init__(self,sample_id=16107,reconstruct_type='_square',net_mode='test'): self.sample_id=sample_id self.type=reconstruct_type # self.type='' self.sample_dir='reconstruct_test/{:08d}{}'.format(self.sample_id,self.type) self.frames_dir=join(self.sample_dir,'frames') if not isdir(self.frames_dir): os.makedirs(self.frames_dir) self.keys_dir=self.frames_dir self.use_ff_prune=False self.use_visible_prune=False # self.use_gt_camera=self.type.find('pd')!=-1 self.use_gt_camera=False self.use_avg_reconst=False self.use_screen_bc=False self.use_obj_mat=False print('use_ff_prune',self.use_ff_prune,'use_visible_prune',self.use_visible_prune,'use_gt_camera',self.use_gt_camera) if self.use_gt_camera: self.gt_reconstruct_info_dir=self.sample_dir[:-3] self.obj2tri_path='/data/yxjin/PhysBAM/Tools/obj2tri/obj2tri-relwithdebinfo' self.prepare_objs() if self.type=='mocap': self.gt_v=pd_obj.v self.gt_vt=read_vt('../vt_groundtruth_div.txt') else: self.gt_path=join(self.sample_dir,'gt_{}_div.obj'.format(self.sample_id)) gt_obj=read_obj(self.gt_path) self.gt_v=gt_obj.v self.gt_vt=gt_obj.vt shared_data_dir='../../pixel_network/shared_data_highres/' self.shared_data_dir=shared_data_dir self.front_vt_ids=np.loadtxt(join(shared_data_dir,'front_vertices.txt')).astype(np.int) front_obj=read_obj(join(shared_data_dir,'flat_tshirt_front.obj')) self.front_fcs=front_obj.f self.vt_gt=read_vt('../vt_groundtruth_div.txt') # self.net_out_dir_pattern='../../pixel_network/rundir/highres_tex/l2pix_final_{}/eval_'+net_mode if self.type=='mocap': self.net_out_dir_pattern='/phoenix/yxjin/mocap_result/mocap_result_{}' else: self.net_out_dir_pattern='/data/zhenglin/dataset_subdivision/tsnn_output/test_result_{}' self.cudaqs_utils=None
def test_opt(self, sample_id, save_obj=False): pd_obj = read_obj(join(self.pd_dir, 'pd_{:08d}.obj'.format(sample_id))) pd_vt = torch.from_numpy(pd_obj.v).to(device=self.device, dtype=self.dtype) cr_vt = self.solver.solve(pd_vt) if save_obj: cr_vt = cr_vt.cpu().numpy() cr_obj = Obj(v=cr_vt, f=pd_obj.f) write_obj(cr_obj, join(self.cr_dir, 'cr_{:08d}.obj'.format(sample_id)))
def __init__(self): shared_data_dir = '../../shared_data' flat_tshirt = read_obj(join(shared_data_dir, 'flat_tshirt.obj')) self.fcs = flat_tshirt.f linear_edges = np.loadtxt(join(shared_data_dir, 'linear_edges.txt')) linear_l0 = np.loadtxt(join(shared_data_dir, 'mat_or_med_linear.txt')) rest_fc_areas = self.compute_areas_from_edge_lengths( self.fcs, linear_edges, linear_l0) self.rest_vt_areas = self.compute_vt_areas(flat_tshirt.v, rest_fc_areas)
def avg_samples_dir(self, samples_dir): sample_dirs = os.listdir(samples_dir) pd_pattern = 'pd_cloth_{:08d}.obj' cr_pattern = 'cr_cloth_{:08d}.obj' avg_pattern = 'avg_cloth_{:08d}.obj' for i, sample_dir in enumerate(sample_dirs): sample_id = int(sample_dir) pd_path = join(samples_dir, sample_dir, pd_pattern.format(sample_id)) pd_obj = read_obj(pd_path) v0, f = pd_obj.v, pd_obj.f cr_path = join(samples_dir, sample_dir, cr_pattern.format(sample_id)) cr_obj = read_obj(cr_path) v1 = cr_obj.v avg_path = join(samples_dir, sample_dir, avg_pattern.format(sample_id)) print('write to', avg_path) write_obj(Obj(v=(v0 + v1) / 2, f=f), avg_path)
def save_projected_obj(self, in_path, out_dir, test_id, n_pcs=2048): if not os.path.isdir(out_dir): os.makedirs(out_dir) in_obj = read_obj(in_path) skin = np.load( os.path.join(self.skin_dir, 'skin_{:08d}.npy'.format(test_id))) offsets = in_obj.v - skin proj_offsets = self.project_pc(offsets, n_pcs=n_pcs) out_obj = Obj(v=skin + proj_offsets, f=in_obj.f) out_path = os.path.join(out_dir, 'proj_{}.obj'.format(n_pcs)) write_obj(out_obj, out_path)
def save_region_from_whole_obj(self, whole_obj_path, out_dir, region_id, prefix=''): whole_obj = read_obj(whole_obj_path) region_obj = Obj(v=whole_obj.v, f=self.all_region_intr_fcs[region_id]) region_name = self.region_names[region_id] region_obj_path = os.path.join(out_dir, '{}{}.obj'.format(prefix, region_name)) write_obj(region_obj, region_obj_path)
def analyze_pp_error(self): print('analyze_reconstruct_error') out_dir=join(self.sample_dir,'error_analysis') if not isdir(out_dir): os.makedirs(out_dir) v_id_set=set(self.front_vt_ids.tolist()) reconstruct_v=read_obj(join(self.sample_dir,'reconstruct_front_pp.obj')).v max_v_id,max_dis=self.get_max_v_error(reconstruct_v,v_id_set) out_path=join(out_dir,'pp_error.txt') print('write to',out_path) with open(out_path,'w') as f: f.write('{} {}\n'.format(max_v_id,max_dis))
def compute_item_energy_diff_stats(samples_dir,pd_pattern,gt_pattern): energy_utils=EnergyUitls() sample_dirs=os.listdir(samples_dir) compress_energy_list,stretch_energy_list,total_energy_list=[],[],[] for sample_dir in sample_dirs: # for sample_dir in ['00016469']: sample_id=int(sample_dir) gt_path=join(samples_dir,sample_dir,gt_pattern.format(sample_id)) gt_v=read_obj(gt_path).v gt_compress_energy,gt_stretch_energy,gt_total_energy=energy_utils.compute_item_energy(gt_v) pd_path=join(samples_dir,sample_dir,pd_pattern.format(sample_id)) pd_v=read_obj(pd_path).v pd_compress_energy,pd_stretch_energy,pd_total_energy=energy_utils.compute_item_energy(pd_v) compress_energy_diff=np.abs(gt_compress_energy-pd_compress_energy) compress_energy_list.append(compress_energy_diff) stretch_energy_diff=np.abs(gt_stretch_energy-pd_stretch_energy) stretch_energy_list.append(stretch_energy_diff) total_energy_list.append(compress_energy_diff+stretch_energy_diff) compress_energy_list,stretch_energy_list,total_energy_list=np.array(compress_energy_list),np.array(stretch_energy_list),np.array(total_energy_list) print('compress',np.mean(compress_energy_list),'stretch',np.mean(stretch_energy_list),'total',np.mean(total_energy_list),'#',len(total_energy_list))
def trim_obj(self,in_path,out_pattern,n_iters=1): def trim_vts(ids,trim_id_set): left_ids=[] for i in ids: if not i in trim_id_set: left_ids.append(i) return left_ids def add_to_set(trim_id_set,next_trim_list): for v in next_trim_list: trim_id_set.add(v) def next_trim(trim_id_set,edges): next_trim_id_set=set() for i0,i1 in edges: i0_in=i0 in trim_id_set i1_in=i1 in trim_id_set if i0_in and not i1_in: next_trim_id_set.add(i1) if not i0_in and i1_in: next_trim_id_set.add(i0) return list(next_trim_id_set) def trim_fcs(fcs,ids): id_set=set(ids) result_fcs=[] for fc in fcs: i0,i1,i2=fc if i0 in id_set and i1 in id_set and i2 in id_set: result_fcs.append(fc) return np.array(result_fcs).astype(np.int) bdry_ids=np.loadtxt(join(self.shared_data_dir,'bdry_vertices.txt')).astype(np.int) trim_id_set=set(bdry_ids) left_ids=trim_vts(self.front_vt_ids,trim_id_set) obj=read_obj(in_path) edges=get_edge_from_fcs(self.front_fcs) fcs=obj.f iters=0 while True: fcs=trim_fcs(fcs,left_ids) out_path=out_pattern.format(iters) print('write to',out_path) write_obj(Obj(obj.v,f=fcs),out_path) iters+=1 if iters>=n_iters: break next_trim_list=next_trim(trim_id_set,edges) add_to_set(trim_id_set,next_trim_list) left_ids=trim_vts(left_ids,trim_id_set)
def __init__(self,res_ctx): self.shared_data_dir=res_ctx['shared_data'] self.vt_offset_dir=res_ctx['vt_offset_dir'] self.skin_dir=res_ctx['skin_dir'] obj_path=join(shared_data_dir,'flat_tshirt.obj') self.fcs=read_obj(obj_path).f self.patch_path=join(self.shared_data_dir,'patches') patch_names_path=join(self.patch_path,'patch_names.txt') self.patch_names=self.load_patch_names(patch_names_path) self.n_patches=len(self.patch_names) self.all_patch_vt_ids=self.load_all_patch_vt_ids()
def compute_edge_lengths(samples_dir,pattern,edges,l0): sample_dirs=os.listdir(samples_dir) sample_dirs.sort() ratios=[] for sample_dir in sample_dirs: sample_id=int(sample_dir) path=join(samples_dir,sample_dir,pattern.format(sample_id)) obj=read_obj(path) v=obj.v l=np.linalg.norm(v[edges[:,0]]-v[edges[:,1]],axis=1) ratio=l/l0 ratios+=ratio.tolist() return ratios
def draw_vt_error(self,error_path,cloth_obj_path,out_path): draw_obj_utils=DrawObjUtils() with open(error_path) as f: line=f.readline() parts=line.split() max_v_id=int(parts[0]) v=read_obj(cloth_obj_path).v p1,p2=v[max_v_id],self.gt_v[max_v_id] draw_obj_utils.add_sphere(p1,0.005) draw_obj_utils.add_sphere(p2,0.005) draw_obj_utils.add_cylinder(p1,p2,0.002) print('write to',out_path) draw_obj_utils.write_obj(out_path)