def compute_front(self, src_dict): """compute back side calculation.""" sndr_prim = [] for p in self.port_prim: np = p.copy() np['real_args'] = np['polygon'].flip().to_real() sndr_prim.append(np) sndr = rm.Sender.as_surface( prim_list=sndr_prim, basis="-"+self.rbasis, offset=None, left=False) logger.info('Computing for back side') for idx in range(len(self.win_polygon)): logger.info(f'Back transmission for window {idx}') win_polygon = self.win_polygon[idx].flip() rcvr_prim = radutil.polygon2prim(win_polygon, 'breceiver', f'window{idx}') rcvr = rm.Receiver.as_surface( prim_list=[rcvr_prim], basis="-"+self.sbasis, left=False, offset=None, source='glow', out=src_dict[f'tf{idx}']) if self.refl: logger.info(f'Back reflection for window {idx}') brcvr_prim = [ radutil.polygon2prim(self.port_prim[i]['polygon'], 'freceiver', 'window' + str(i)) for i in range(len(self.port_prim))] brcvr = rm.Receiver.as_surface( prim_list=brcvr_prim, basis=self.rbasis, left=False, offset=None, source='glow', out=src_dict[f'rf{idx}']) rcvr += brcvr rm.rfluxmtx(sender=sndr, receiver=rcvr, env=self.env, out=None, opt=self.opt)
def daylight_matrix(sender_prims, model, config, direct=False): """Call rfluxmtx to generate daylight matrices for each sender surface.""" logger.info("Computing daylight matrix...") dmxs = {} _opt = config.dmx_opt _env = [model.material_path] + config.scene_paths if direct: _opt += ' -ab 0' _env = [model.material_path, model.blackenvpath] for sname, surface_primitives in sender_prims.items(): _name = sname if direct: _name += '_d' dmxs[sname] = os.path.join(config.mtxdir, f'dmx_{_name}.mtx') sndr_window = radmtx.Sender.as_surface(prim_list=surface_primitives, basis=config.vmx_basis) if not os.path.isfile(dmxs[sname]) or config.overwrite: logger.info("Generating daylight matrix for %s", _name) dmx_res = radmtx.rfluxmtx(sender=sndr_window, receiver=model.receiver_sky, env=_env, out=None, opt=_opt) with open(dmxs[sname], 'wb') as wtr: wtr.write(dmx_res) return dmxs
def prep_2phase_vu(model, config): """Generate image-based matrices if view defined.""" logger.info("Computing for image-based 2-phase matrices...") env = [model.material_path] + config.scene_paths env += [v for v in config.windows.values()] vdsmx = {} for view_name, sender_view in model.sender_view.items(): vdsmx[view_name] = os.path.join(config.mtxdir, f"vdsmx_{view_name}") if not os.path.isdir(vdsmx[view_name]) or config.overwrite: logger.info("Generating for %s" % view_name) radmtx.rfluxmtx(sender=sender_view, receiver=model.receiver_sky, env=env, opt=config.dsmx_opt, out=vdsmx[view_name]) return vdsmx
def prep_4phase_pt(model, config, direct=False): """Prepare matrices using four-phase methods for point-based calculation.""" dmxs = {} fmxs = {} _opt = config.fmx_opt _env = config.envpath if direct: # need to add ncp path _env = [model.material_path, model.blackenvpath] _opt += ' -ab 0' ncp_prims = None for wname in model.window_groups: window_prim = model.window_groups[wname] if direct: _name = wname + '_d' fmxs[wname] = os.path.join(config.mtxdir, 'fmx_{_name}.mtx') port_prims = mfacade.genport(wpolys=window_prim, npolys=ncp_prims, depth=None, scale=None) mfacade.Genfmtx(win_polygons=window_prim['polygon'], port_prim=port_prims, out=fmxs[wname], env=_env, sbasis=config.vmx_basis, rbasis=config.fmx_basis, opt=_opt, refl=False, forw=False, wrap=False) logger.info(f"Generating daylight matrix for {wname}") dmxs[wname] = os.path.join(config.mtxdir, f'dmx_{wname}.mtx') sndr_port = radmtx.Sender.as_surface(prim_list=port_prims, basis=config.fmx_basis, offset=None) radmtx.rfluxmtx(sender=sndr_port, receiver=model.receiver_sky, env=_env, out=dmxs[wname], opt=config.dmx_opt) return fmxs, dmxs
def compute_back(self, src_dict): """compute front side calculation(backwards).""" logger.info('Computing for front side') for idx, win_polygon in enumerate(self.win_polygon): logger.info(f'Front transmission for window {idx}') front_rcvr = rm.Receiver.as_surface( prim_list=self.port_prim, basis=self.rbasis, left=True, offset=None, source='glow', out=src_dict[f'tb{idx}']) sndr_prim = radutil.polygon2prim(win_polygon, 'fsender', f'window{idx}') sndr = rm.Sender.as_surface( prim_list=[sndr_prim], basis=self.sbasis, left=True, offset=None) if self.refl: logger.info(f'Front reflection for window {idx}') back_window = win_polygon.flip() back_window_prim = radutil.polygon2prim( back_window, 'breceiver', f'window{idx}') back_rcvr = rm.Receiver.as_surface( prim_list=[back_window_prim], basis="-"+self.rbasis, left=False, offset=None, source='glow', out=src_dict[f'rb{idx}']) front_rcvr += back_rcvr rm.rfluxmtx(sender=sndr, receiver=front_rcvr, env=self.env, out=None, opt=self.opt)
def view_matrix_pt(model, config, direct=False): """.""" _opt = config.vmx_opt _env = [model.material_path] + config.scene_paths if direct: logger.info("Computing direct view matrix for sensor grid:") _opt += ' -ab 1' _env = [model.material_path, model.blackenvpath] else: logger.info("Computing view matrix for sensor grid:") vmxs = {} receiver_windows = radmtx.Receiver(receiver='', basis=config.vmx_basis, modifier=None) for grid_name, sender_grid in model.sender_grid.items(): for wname, window_prim in model.window_groups.items(): _name = grid_name + wname if direct: _name += '_d' vmxs[grid_name + wname] = os.path.join(config.mtxdir, f'pvmx_{_name}.mtx') receiver_windows += radmtx.Receiver.as_surface( prim_list=window_prim, basis=config.vmx_basis, offset=None, left=None, source='glow', out=vmxs[grid_name + wname]) files_exist = all([os.path.isfile(f) for f in vmxs.values()]) if not files_exist or config.overwrite: logger.info("Generating vmx for %s", grid_name) radmtx.rfluxmtx(sender=sender_grid, receiver=receiver_windows, env=_env, opt=_opt, out=None) return vmxs
def view_matrix_vu(model, config, direct=False): """Prepare matrices using three-phase methods.""" _opt = config.vmx_opt _env = [model.material_path] + config.scene_paths if direct: _opt += ' -i -ab 1' _env = [model.material_path, model.blackenvpath] vmxs = {} vrcvr_windows = {} for view in model.sender_view: for wname in model.window_groups: vrcvr_windows[view + wname] = radmtx.Receiver( receiver='', basis=config.vmx_basis, modifier=None) for view in model.sender_view: logger.info("Computing image-based view matrix:") for wname, window_prim in model.window_groups.items(): _name = view + wname if direct: _name += '_d' vmxs[view + wname] = os.path.join(config.mtxdir, f'vvmx_{_name}', '%04d.hdr') vrcvr_windows[view + wname] += radmtx.Receiver.as_surface( prim_list=window_prim, basis=config.vmx_basis, out=vmxs[view + wname]) for view, sender_view in model.sender_view.items(): for wname in model.window_groups: if not os.path.isdir(vmxs[view + wname][:-8]) or config.overwrite: logger.info("Generating for %s to %s", view, wname) util.mkdir_p(os.path.dirname(vmxs[view + wname])) radmtx.rfluxmtx(sender=sender_view, receiver=vrcvr_windows[view + wname], env=_env, opt=_opt, out=None) return vmxs
def facade_matrix(model, config, direct=False): """Generate facade matrices. Args: model (namedtuple): model assembly config (namedtuple): model configuration Returns: facade matrices file path """ logger.info("Computing facade matrix...") fmxs = {} _opt = config.dmx_opt _env = [model.material_path] + config.scene_paths if direct: _opt += ' -ab 0' _env = [model.material_path, model.blackenvpath] ncp_prims = {} for ncppath in config.ncppath: name = util.basename(ncppath) ncp_prims[name] = radutil.unpack_primitives(ncppath) all_ncp_prims = [prim for key, prim in ncp_prims.items()] all_window_prims = [prim for key, prim in model.window_groups.items()] port_prims = mfacade.genport(wpolys=all_window_prims, npolys=all_ncp_prims, depth=None, scale=None) port_rcvr = radmtx.Receiver.as_surface(prim_list=port_prims, basis=config.fmx_basis, out=None) for wname in model.window_groups: _name = wname + '_d' if direct else wname fmxs[wname] = os.path.join(config.mtxdir, f'fmx_{_name}.mtx') window_prim = model.window_groups[wname] sndr_window = radmtx.Sender.as_surface(prim_list=window_prim, basis=config.fmx_basis) if not os.path.isfile(fmxs[wname]) or config.overwrite: logger.info("Generating facade matrix for %s", _name) fmx_res = radmtx.rfluxmtx(sender=sndr_window, receiver=port_rcvr, env=_env, out=None, opt=_opt) with open(fmxs[wname], 'wb') as wtr: wtr.write(fmx_res) return fmxs
def prep_2phase_pt(model, config): """Prepare matrices two phase methods.""" logger.info('Computing for 2-phase sensor point matrices...') env = [model.material_path] + config.scene_paths env += [v for v in config.windows.values()] pdsmx = os.path.join(config.mtxdir, 'pdsmx.mtx') opt = config.dsmx_opt + ' -n %s' % config.nprocess pdsmx = {} for grid_name, sender_grid in model.sender_grid.items(): pdsmx[grid_name] = os.path.join(config.mtxdir, f"pdsmx_{grid_name}") if not os.path.isfile(pdsmx[grid_name]) or config.overwrite: res = radmtx.rfluxmtx(sender=sender_grid, receiver=model.receiver_sky, env=env, opt=opt) with open(pdsmx[grid_name], 'wb') as wtr: wtr.write(res) return pdsmx
def main(): """Generate a matrix.""" description = 'Generate flux transport matrix' parser = argparse.ArgumentParser(prog='genmtx', description=description) genmtx_r = genmtx_args(parser) args = genmtx_r.parse_args() logger = logging.getLogger('frads') formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_handler = logging.StreamHandler() _level = args.verbose * 10 logger.setLevel(_level) console_handler.setLevel(_level) console_handler.setFormatter(formatter) logger.addHandler(console_handler) assert len(args.receiver) == len(args.outpath) env = args.env sender = None outpath = None if args.octree is not None: env.extend(['-i', args.octree]) # what's the sender with open(args.sender) as rdr: sndrlines = rdr.readlines() if args.sender_type == 's': prim_list = radutil.parse_primitive(sndrlines) sender = rm.Sender.as_surface(prim_list=prim_list, basis=args.sender_basis, offset=args.sender_offset) elif args.sender_type == 'v': vudict = util.parse_vu(sndrlines[-1]) # use the last view sender = rm.Sender.as_view(vu_dict=vudict, ray_cnt=args.ray_count, xres=args.resolu[0], yres=args.resolu[1]) elif args.sender_type == 'p': pts_list = [line.split() for line in sndrlines] sender = rm.Sender.as_pts(pts_list=pts_list, ray_cnt=args.ray_count) # figure out receiver if args.receiver[0] == 'sky': logger.info('Sky is the receiver') receiver = rm.Receiver.as_sky(args.receiver_basis) outpath = args.outpath[0] if args.sender_type == 'v': util.mkdir_p(outpath) elif args.receiver[0] == 'sun': full_modifier = False if args.sender_type != 'v': full_modifier = True window_normals: Union[None, Set[radgeom.Vector]] = None if args.wpths is not None: window_normals = radutil.primitive_normal(args.wpths) receiver = rm.Receiver.as_sun(basis=args.receiver_basis, smx_path=args.smx, window_normals=window_normals, full_mod=full_modifier) else: # assuming multiple receivers rcvr_prims = [] for path in args.receiver: rcvr_prims.extend(radutil.unpack_primitives(path)) modifiers = set([prim.modifier for prim in rcvr_prims]) receiver = rm.Receiver(receiver='', basis=args.receiver_basis, modifier=None) for mod, op in zip(modifiers, args.outpath): _receiver = [ prim for prim in rcvr_prims if prim.modifier == mod and prim.ptype in ('polygon', 'ring') ] if _receiver != []: if args.sender_type == 'v': _outpath = os.path.join(op, '%04d.hdr') else: _outpath = op receiver += rm.Receiver.as_surface(prim_list=_receiver, basis=args.receiver_basis, offset=args.receiver_offset, left=None, source='glow', out=_outpath) outpath = None # generate matrices if args.receiver[0] == 'sun': logger.info('Suns are the receivers.') outpath = os.path.join(os.getcwd(), args.outpath[0]) sun_oct = 'sun.oct' rm.rcvr_oct(receiver, env, sun_oct) with tf.TemporaryDirectory(dir=os.getcwd()) as tempd: mod_names = [ "%04d" % (int(l[3:]) - 1) for l in receiver.modifier.splitlines() ] rm.rcontrib(sender=sender, modifier=receiver.modifier, octree=sun_oct, out=tempd, opt=args.option) _files = [ os.path.join(tempd, f) for f in sorted(os.listdir(tempd)) if f.endswith('.hdr') ] util.mkdir_p(outpath) for idx, val in enumerate(_files): os.rename(val, os.path.join(outpath, mod_names[idx] + '.hdr')) else: res = rm.rfluxmtx(sender=sender, receiver=receiver, env=env, opt=args.option, out=outpath) if (outpath is not None) and (args.sender_type != 'v'): with open(outpath, 'wb') as wtr: wtr.write(res)