def blacken_env(model, config): """.""" bwindow_path = os.path.join(config.objdir, 'blackened_window.rad') gwindow_path = os.path.join(config.objdir, 'glowing_window.rad') blackened_window = [] glowing_window = [] for _, windows in model.window_groups.items(): for window in windows: blackened_window.append( radutil.Primitive('black', window.ptype, window.identifier, window.str_arg, window.real_arg)) glowing_window.append( radutil.Primitive('glowing', window.ptype, window.identifier, window.str_arg, window.real_arg)) with open(bwindow_path, 'w') as wtr: wtr.write('\n'.join(list(map(str, blackened_window)))) with open(gwindow_path, 'w') as wtr: wtr.write('\n'.join(list(map(str, glowing_window)))) vmap_oct = os.path.join(config.rsodir, 'vmap.oct') cdmap_oct = os.path.join(config.rsodir, 'cdmap.oct') vmap = util.spcheckout(['oconv', model.material_path] + config.scene_paths + [bwindow_path]) with open(vmap_oct, 'wb') as wtr: wtr.write(vmap) cdmap = util.spcheckout(['oconv', model.material_path] + config.scene_paths + [gwindow_path]) with open(cdmap_oct, 'wb') as wtr: wtr.write(cdmap) return vmap_oct, cdmap_oct
def rcontrib(*, sender, modifier: str, octree, out, opt) -> None: """Calling rcontrib to generate the matrices. Args: sender: Sender object modifier: modifier str listing the receivers in octree octree: the octree that includes the environment and the receiver opt: option string out: output path Returns: None """ lopt = opt.split() lopt.append('-fo+') with tf.TemporaryDirectory() as tempd: modifier_path = os.path.join(tempd, 'modifier') with open(modifier_path, 'w') as wtr: wtr.write(modifier) cmd = ['rcontrib'] + lopt stdin = sender.sender if sender.form == 'p': cmd += ['-I+', '-faf', '-y', str(sender.yres)] elif sender.form == 'v': util.mkdir_p(out) out = os.path.join(out, '%04d.hdr') cmd += ['-ffc', '-x', str(sender.xres), '-y', str(sender.yres)] cmd += ['-o', out, '-M', modifier_path, octree] util.spcheckout(cmd, inp=stdin)
def pcomb(inputs): """Image operations with pcomb. Parameter: inputs, e.g: ['img1.hdr', '+', img2.hdr', '-', 'img3.hdr', 'output.hdr'] """ input_list = inputs[:-1] out_dir = inputs[-1] component_idx = range(0, len(input_list), 2) components = [input_list[i] for i in component_idx] color_op_list = [] for c in 'rgb': color_op = input_list[:] for i in component_idx: color_op[i] = '%si(%d)' % (c, i / 2 + 1) cstr = '%so=%s' % (c, ''.join(color_op)) color_op_list.append(cstr) rgb_str = ';'.join(color_op_list) cmd = ['pcomb', '-e', '%s' % rgb_str] img_name = util.basename(input_list[0], keep_ext=True) for c in components: cmd.append('-o') cmd.append(c) res = util.spcheckout(cmd) with open(os.path.join(out_dir, img_name), 'wb') as wtr: wtr.write(res)
def as_view(cls, *, vu_dict: dict, ray_cnt: int, xres: int, yres: int) -> Sender: """ Construct a sender from a view. Args: vu_dict: a dictionary containing view parameters; ray_cnt: ray count; xres, yres: image resolution c2c: Set to True to trim the fisheye corner rays. Returns: A sender object """ if None in (xres, yres): raise ValueError("Need to specify resolution") vcmd = f"vwrays {radutil.opt2str(vu_dict)} -x {xres} -y {yres} -d" res_eval = util.spcheckout(vcmd.split()).decode().split() xres, yres = int(res_eval[1]), int(res_eval[3]) logger.info("Changed resolution to %s %s", xres, yres) cmd = f"vwrays -ff -x {xres} -y {yres} " if ray_cnt > 1: vu_dict['c'] = ray_cnt vu_dict['pj'] = 0.7 # placeholder logger.debug("Ray count is %s", ray_cnt) cmd += radutil.opt2str(vu_dict) if vu_dict['vt'] == 'a': cmd += "|" + Sender.crop2circle(ray_cnt, xres) vrays = sp.run(cmd, shell=True, check=True, stdout=sp.PIPE).stdout return cls(form='v', sender=vrays, xres=xres, yres=yres)
def rfluxmtx(*, sender, receiver, env, opt=None, out=None): """Calling rfluxmtx to generate the matrices. Args: sender: Sender object receiver: Receiver object env: model environment, basically anything that's not the sender or receiver opt: option string out: output path Returns: return the stdout of the command """ if None in (sender, receiver): raise ValueError("Sender/Receiver object is None") opt = '' if opt is None else opt with tf.TemporaryDirectory() as tempd: receiver_path = os.path.join(tempd, 'receiver') with open(receiver_path, 'w') as wtr: wtr.write(receiver.receiver) if isinstance(env[0], dict): env_path = os.path.join(tempd, 'env') with open(env_path, 'w') as wtr: [wtr.write(str(prim)) for prim in env] env_paths = [env_path] else: env_paths = env cmd = ['rfluxmtx'] + opt.split() stdin = None if sender.form == 's': sender_path = os.path.join(tempd, 'sender') with open(sender_path, 'wb') as wtr: wtr.write(sender.sender) cmd.extend([sender_path, receiver_path]) elif sender.form == 'p': cmd.extend( ['-I+', '-faa', '-y', str(sender.yres), '-', receiver_path]) stdin = sender.sender elif sender.form == 'v': cmd.extend([ "-ffc", "-x", str(sender.xres), "-y", str(sender.yres), "-ld-" ]) if out is not None: util.mkdir_p(out) out = os.path.join(out, '%04d.hdr') cmd.extend(["-o", out]) cmd.extend(['-', receiver_path]) stdin = sender.sender cmd.extend(env_paths) return util.spcheckout(cmd, inp=stdin)
def rcvr_oct(receiver, env, oct_path): """Generate an octree of the environment and the receiver. Args: receiver: receiver object env: environment file paths oct_path: Path to write the octree to """ with tf.TemporaryDirectory() as tempd: receiver_path = os.path.join(tempd, 'rcvr_path') with open(receiver_path, 'w') as wtr: wtr.write(receiver.receiver) ocmd = ['oconv', '-f'] + env + [receiver_path] octree = util.spcheckout(ocmd) with open(oct_path, 'wb') as wtr: wtr.write(octree)
def gen_smx(wea_path, mfactor, outdir, onesun=False, direct=False): """Generate sky/sun matrix.""" sun_only = ' -d' if direct else '' _five = ' -5 .533' if onesun else '' oname = util.basename(wea_path) cmd = f"gendaymtx -of -m {mfactor[-1]}{sun_only}{_five}".split() cmd.append(wea_path) logger.info('Generating sku/sun matrix using command') logger.info(' '.join(cmd)) res = util.spcheckout(cmd) if direct: if onesun: smxpath = os.path.join(outdir, oname + '_d6.smx') else: smxpath = os.path.join(outdir, oname + '_d.smx') else: smxpath = os.path.join(outdir, oname + '.smx') with open(smxpath, 'wb') as wtr: wtr.write(res) return smxpath
def rmtxop(*mtxs): cmd = ["rmtxop", *mtxs] return util.spcheckout(cmd)
def direct_sun_matrix_vu(model, smx_path, vmap_oct, cdmap_oct, config): """Compute direct sun matrix for images. Args: smx_path: path to sun only sky matrix Returns: path to resulting direct sun matrix """ logger.info(f"Generating image-based direct sun matrix") rcvr_sun = radmtx.Receiver.as_sun(basis='r6', smx_path=smx_path, window_normals=model.window_normals) mod_names = [ "%04d" % (int(l[3:]) - 1) for l in rcvr_sun.modifier.splitlines() ] sun_oct = os.path.join(config.rsodir, 'sun.oct') cdsenv = [model.material_path, model.blackenvpath] + model.cfs_paths radmtx.rcvr_oct(rcvr_sun, cdsenv, sun_oct) vcdfmx = {} vcdrmx = {} vmap_paths = {} cdmap_paths = {} for view, sndr in model.sender_view.items(): vmap_paths[view] = os.path.join(config.mtxdir, f'vmap_{view}.hdr') cdmap_paths[view] = os.path.join(config.mtxdir, f'cdmap_{view}.hdr') vdict = model.views[view] vdict.pop('c', None) vdict.pop('pj', None) view_str = radutil.opt2str(vdict) cmd = ['rpict'] + view_str.split() + ['-ps', '1', '-ab', '0', '-av'] cmd.extend(['.31831', '.31831', '.31831', vmap_oct]) vmap = util.spcheckout(cmd) with open(vmap_paths[view], 'wb') as wtr: wtr.write(vmap) cmd[-1] = cdmap_oct cdmap = util.spcheckout(cmd) with open(cdmap_paths[view], 'wb') as wtr: wtr.write(cdmap) vcdfmx[view] = os.path.join(config.mtxdir, f'vcdfmx_{view}') vcdrmx[view] = os.path.join(config.mtxdir, f'vcdrmx_{view}') tempf = os.path.join(config.mtxdir, 'vcdfmx') tempr = os.path.join(config.mtxdir, 'vcdrmx') if not os.path.isdir(vcdfmx[view]) or config.overwrite: logger.info( f"Using rcontrib to generat direct sun f matrix for {view}...") radmtx.rcontrib(sender=sndr, modifier=rcvr_sun.modifier, octree=sun_oct, out=tempf, opt=config.cdsmx_opt + ' -n %s' % config.nprocess) _files = [ os.path.join(tempf, f) for f in sorted(os.listdir(tempf)) if f.endswith('.hdr') ] util.mkdir_p(vcdfmx[view]) for idx, val in enumerate(_files): os.rename(val, os.path.join(vcdfmx[view], mod_names[idx] + '.hdr')) shutil.rmtree(tempf) if not os.path.isdir(vcdrmx[view]) or config.overwrite: logger.info( f"Using rcontrib to generate direct sun r matrix for {view}") radmtx.rcontrib(sender=sndr, modifier=rcvr_sun.modifier, octree=sun_oct, out=tempr, opt=config.cdsmx_opt + ' -i -n %s' % config.nprocess) _files = [ os.path.join(tempr, f) for f in sorted(os.listdir(tempr)) if f.endswith('.hdr') ] util.mkdir_p(vcdrmx[view]) for idx, val in enumerate(_files): os.rename(val, os.path.join(vcdrmx[view], mod_names[idx] + '.hdr')) shutil.rmtree(tempr) return vcdfmx, vcdrmx, vmap_paths, cdmap_paths