Ejemplo n.º 1
0
 def __init__(self,
              name=None,
              output_dir=get_unique_task_id(),
              overwrite=True,
              frame=0,
              scene=None,
              preset='pt',
              visualize=True,
              **kwargs):
   self.renderer_name = name
   if output_dir is not None:
     self.output_dir = taichi.settings.get_output_path(output_dir + '/')
   self.post_processor = LDRDisplay()
   self.frame = frame
   self.viewer_started = False
   self.viewer_process = None
   try:
     os.mkdir(self.output_dir)
   except Exception as e:
     if not overwrite:
       print(e)
       exit(-1)
   if scene:
     self.initialize(preset, scene=scene, **kwargs)
   self.visualize = visualize
Ejemplo n.º 2
0
 def show(self,
          title='Taichi Texture Renderer',
          res=(512, 512),
          post_processor=None):
     from taichi.gui.image_viewer import show_image
     img = self.rasterize_to_ndarray(res)
     if post_processor is None:
         post_processor = LDRDisplay()
     if post_processor:
         img = post_processor.process(img)
     show_image(title, img)
Ejemplo n.º 3
0
 def step(self, step_t):
     t = self.c.get_current_time()
     print 'Simulation time:', t
     T = time.time()
     self.update_levelset(t, t + step_t)
     print 'Update Leveset Time:', time.time() - T
     T = time.time()
     self.c.step(step_t)
     print 'Step Time:', time.time() - T
     image_buffer = tc_core.Array2DVector3(self.video_manager.width,
                                           self.video_manager.height,
                                           Vector(0, 0, 0.0))
     particles = self.c.get_render_particles()
     particles.write(self.directory + '/particles%05d.bin' % self.frame)
     res = map(float, self.resolution)
     camera = Camera('pinhole',
                     origin=(0, res[1] * 0.4, res[2] * 1.4),
                     look_at=(0, -res[1] * 0.5, 0),
                     up=(0, 1, 0),
                     fov=90,
                     width=10,
                     height=10)
     self.particle_renderer.set_camera(camera)
     self.particle_renderer.render(image_buffer, particles)
     img = image_buffer_to_ndarray(image_buffer)
     img = LDRDisplay(exposure=2.0, adaptive_exposure=False).process(img)
     show_image('Vis', img)
     self.video_manager.write_frame(img)
     self.frame += 1
Ejemplo n.º 4
0
 def step(self, step_t):
     t = self.c.get_current_time()
     print 'Simulation time:', t
     T = time.time()
     self.c.step(step_t)
     print 'Time:', time.time() - T
     image_buffer = tc_core.RGBImageFloat(self.video_manager.width,
                                          self.video_manager.height,
                                          Vector(0, 0, 0.0))
     particles = self.c.get_render_particles()
     particles.write(self.directory + '/particles%05d.bin' % self.frame)
     res = map(float, self.resolution)
     radius = res[0] * 2.5
     theta = 0
     camera = Camera('pinhole',
                     origin=(0, res[1], res[2] * 5),
                     look_at=(0, 0, 0),
                     up=(0, 1, 0),
                     fov=70,
                     width=10,
                     height=10)
     self.particle_renderer.set_camera(camera)
     self.particle_renderer.render(image_buffer, particles)
     img = image_buffer_to_ndarray(image_buffer)
     img = LDRDisplay(exposure=0.1).process(img)
     show_image('Vis', img)
     self.video_manager.write_frame(img)
     self.frame += 1
Ejemplo n.º 5
0
def render():
    # root, fov = tc.settings.get_asset_path('scenes/living-room/'), 55
    root, fov = tc.settings.get_asset_path('scenes/staircase/'), 105
    scene = load_scene(root, fov)

    renderer = Renderer(output_dir='benedikt_dataset', overwrite=True)
    renderer.set_post_processor(LDRDisplay(1.0))

    renderer.initialize(preset='pt', scene=scene)
    renderer.render(30000, cache_interval=10)
Ejemplo n.º 6
0
 def step(self, step_t, camera=None):
     t = self.c.get_current_time()
     print('* Current t: %.3f' % t)
     self.update_levelset(t, t + step_t)
     T = time.time()
     if not self.start_simulation_time:
         self.start_simulation_time = T
     if not self.simulation_total_time:
         self.simulation_total_time = 0
     self.c.step(step_t)
     self.simulation_total_time += time.time() - T
     print('* Step Time: %.2f [tot: %.2f per frame %.2f]' %
           (time.time() - T, time.time() - self.start_simulation_time,
            self.simulation_total_time / (self.c.frame + 1)))
     image_buffer = tc_core.Array2DVector3(
         Vectori(self.video_manager.width, self.video_manager.height),
         Vector(0, 0, 0.0))
     '''
 particles = self.c.get_render_particles()
 try:
   os.mkdir(self.directory + '/particles')
 except:
   pass
 particles.write(self.directory + '/particles/%05d.bin' % self.c.frame)
 '''
     res = list(map(float, self.res))
     r = res[0]
     if not camera:
         camera = Camera('pinhole',
                         origin=(0, r * 0.4, r * 1.4),
                         look_at=(0, -r * 0.5, 0),
                         up=(0, 1, 0),
                         fov=90,
                         res=(10, 10))
     if False:
         self.particle_renderer.set_camera(camera)
         self.particle_renderer.render(image_buffer, particles)
         img = image_buffer_to_ndarray(image_buffer)
         img = LDRDisplay(exposure=2.0,
                          adaptive_exposure=False).process(img)
         show_image('Vis', img)
         self.video_manager.write_frame(img)
Ejemplo n.º 7
0
class Renderer(object):
    def __init__(self, name=None, output_dir=get_unique_task_id(), overwrite=True, frame=0,
                 scene=None, preset=None, **kwargs):
        self.renderer_name = name
        self.output_dir = taichi.settings.get_output_path(output_dir + '/')
        self.post_processor = LDRDisplay()
        self.frame = frame
        try:
            os.mkdir(self.output_dir)
        except Exception as e:
            if not overwrite:
                print e
                exit(-1)
        if scene:
            self.initialize(preset, scene=scene, **kwargs)

    def initialize(self, preset=None, scene=None, **kwargs):
        if preset is not None:
            args = Renderer.presets[preset]
            for key, value in kwargs.items():
                args[key] = value
            self.renderer_name = args['name']
        else:
            args = kwargs
        self.c = tc_core.create_renderer(self.renderer_name)
        if scene is not None:
            self.set_scene(scene)
        self.c.initialize(config_from_dict(args))

    def render(self, stages, cache_interval=-1):
        for i in range(1, stages + 1):
            print 'stage', i
            t = time.time()
            self.render_stage()
            print 'time:', time.time() - t
            self.show()
            if cache_interval > 0 and i % cache_interval == 0:
                self.write('img%04d-%06d.png' % (self.frame, i))

        self.write('img%04d-%06d.png' % (self.frame, stages))

    def get_full_fn(self, fn):
        return self.output_dir + fn

    def write(self, fn):
        cv2.imwrite(self.get_full_fn(fn), self.get_output() * 255)

    def get_output(self):
        output = self.c.get_output()
        output = image_buffer_to_ndarray(output)
        if self.post_processor:
            output = self.post_processor.process(output)
        return output

    def show(self):
        cv2.imshow('Rendered', self.get_output())
        cv2.waitKey(1)

    def __getattr__(self, key):
        return self.c.__getattribute__(key)

    def set_scene(self, scene):
        self.c.set_scene(scene.c)

    def set_post_processor(self, post_processor):
        self.post_processor = post_processor

    presets = {
        'sppm': {
            'name': 'sppm',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'sobol',
            'shrinking_radius': True,
            'num_threads': get_num_cores()
        },
        'vcm': {
            'name': 'vcm',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'prand',
            'stage_frequency': 10,
            'shrinking_radius': True,
            'num_threads': get_num_cores()
        },
        'pt': {
            'name': 'pt',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'sobol',
            'russian_roulette': True,
            'direct_lighting': 1,
            'direct_lighting_light': 1,
            'direct_lighting_bsdf': 1,
            'envmap_is': 1,
            'num_threads': get_num_cores()
        },
        'bdpt': {
            'name': 'bdpt',
            'min_path_length': 1,
            'max_path_length': 10,
            'stage_frequence': 3,
            'sampler': 'sobol',
            'num_threads': get_num_cores()
        }
    }
Ejemplo n.º 8
0
class Renderer(object):

  def __init__(self,
               name=None,
               output_dir=get_unique_task_id(),
               overwrite=True,
               frame=0,
               scene=None,
               preset='pt',
               visualize=True,
               **kwargs):
    self.renderer_name = name
    if output_dir is not None:
      self.output_dir = taichi.settings.get_output_path(output_dir + '/')
    self.post_processor = LDRDisplay()
    self.frame = frame
    self.viewer_started = False
    self.viewer_process = None
    try:
      os.mkdir(self.output_dir)
    except Exception as e:
      if not overwrite:
        print(e)
        exit(-1)
    if scene:
      self.initialize(preset, scene=scene, **kwargs)
    self.visualize = visualize

  def initialize(self, preset='pt', scene=None, **kwargs):
    if preset is not None:
      args = Renderer.presets[preset]
      for key, value in list(kwargs.items()):
        args[key] = value
      self.renderer_name = args['name']
    else:
      args = kwargs
    self.c = tc_core.create_renderer(self.renderer_name)
    if scene is not None:
      self.set_scene(scene)
    self.c.initialize(config_from_dict(args))


  def render(self, stages=1000, cache_interval=-1):
    for i in range(1, stages + 1):
      print('stage', i)
      t = time.time()
      self.render_stage()
      print('time:', time.time() - t)
      self.show()
      if cache_interval > 0 and i % cache_interval == 0:
        self.write('img%04d-%06d.png' % (self.frame, i))

    self.write('img%04d-%06d.png' % (self.frame, stages))

  def get_full_fn(self, fn):
    return self.output_dir + fn

  def write(self, fn):
    self.get_image_output().write(self.get_full_fn(fn))

  # Returns numpy.ndarray
  def get_output(self):
    output = self.c.get_output()
    output = image_buffer_to_ndarray(output)

    if self.post_processor:
      output = self.post_processor.process(output)

    return output

  # Returns ImageBuffer<Vector3> a.k.a. Array2DVector3
  def get_image_output(self):
    return taichi.util.ndarray_to_array2d(self.get_output())

  def show(self):
    if not self.visualize:
      return

    show_image('Taichi Renderer', self.get_output())

  def end_viewer_process(self):
    if self.viewer_process.returncode is not None:
      return

    self.viewer_process.terminate()

  def start_viewer(self, frame_path):
    path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)), '../gui/tk/viewer.py')

    self.viewer_process = Popen(['python', path, frame_path])

    atexit.register(self.end_viewer_process)

  def __getattr__(self, key):
    return self.c.__getattribute__(key)

  def set_scene(self, scene):
    self.c.set_scene(scene.c)

  def set_post_processor(self, post_processor):
    self.post_processor = post_processor

  presets = {
      'sppm': {
          'name': 'sppm',
          'min_path_length': 1,
          'max_path_length': 10,
          'initial_radius': 0.5,
          'sampler': 'sobol',
          'shrinking_radius': True,
          'num_threads': get_num_cores()
      },
      'vcm': {
          'name': 'vcm',
          'min_path_length': 1,
          'max_path_length': 10,
          'initial_radius': 0.5,
          'sampler': 'prand',
          'stage_frequency': 10,
          'shrinking_radius': True,
          'num_threads': get_num_cores()
      },
      'pt': {
          'name': 'pt',
          'min_path_length': 1,
          'max_path_length': 10,
          'initial_radius': 0.5,
          'sampler': 'sobol',
          'russian_roulette': True,
          'direct_lighting': 1,
          'direct_lighting_light': 1,
          'direct_lighting_bsdf': 1,
          'envmap_is': 1,
          'num_threads': get_num_cores()
      },
      'pt_sdf': {
          'name': 'pt_sdf',
          'min_path_length': 1,
          'max_path_length': 10,
          'initial_radius': 0.5,
          'sampler': 'sobol',
          'russian_roulette': True,
          'direct_lighting': 1,
          'direct_lighting_light': 1,
          'direct_lighting_bsdf': 1,
          'envmap_is': 1,
          'num_threads': get_num_cores()
      },
      'bdpt': {
          'name': 'bdpt',
          'min_path_length': 1,
          'max_path_length': 10,
          'stage_frequence': 3,
          'sampler': 'sobol',
          'num_threads': get_num_cores()
      }
  }
Ejemplo n.º 9
0
class Renderer(object):
    def __init__(self, name=None, output_dir=get_unique_task_id(), overwrite=True, frame=0,
                 scene=None, preset=None, **kwargs):
        self.renderer_name = name
        self.output_dir = taichi.settings.get_output_path(output_dir + '/')
        self.post_processor = LDRDisplay()
        self.frame = frame
        self.viewer_started = False
        self.viewer_process = None
        try:
            os.mkdir(self.output_dir)
        except Exception as e:
            if not overwrite:
                print e
                exit(-1)
        if scene:
            self.initialize(preset, scene=scene, **kwargs)

    def initialize(self, preset=None, scene=None, **kwargs):
        if preset is not None:
            args = Renderer.presets[preset]
            for key, value in kwargs.items():
                args[key] = value
            self.renderer_name = args['name']
        else:
            args = kwargs
        self.c = tc_core.create_renderer(self.renderer_name)
        if scene is not None:
            self.set_scene(scene)
        self.c.initialize(config_from_dict(args))

    def render(self, stages, cache_interval=-1):
        for i in range(1, stages + 1):
            print 'stage', i
            t = time.time()
            self.render_stage()
            print 'time:', time.time() - t
            self.show()
            if cache_interval > 0 and i % cache_interval == 0:
                self.write('img%04d-%06d.png' % (self.frame, i))

        self.write('img%04d-%06d.png' % (self.frame, stages))

    def get_full_fn(self, fn):
        return self.output_dir + fn

    def write(self, fn):
        self.get_image_output().write(self.get_full_fn(fn))

    # Returns numpy.ndarray
    def get_output(self):
        output = self.c.get_output()
        output = image_buffer_to_ndarray(output)

        if self.post_processor:
            output = self.post_processor.process(output)

        return output

    # Returns ImageBuffer<Vector3> a.k.a. RGBImageFloat
    def get_image_output(self):
        return taichi.util.ndarray_to_image_buffer(self.get_output())

    def show(self):
        # allow the user to opt out of the frame viewer by invoking the script
        # with --no-viewer in the command line
        if '--no-viewer' in sys.argv:
            return

        frame_path = self.get_full_fn('current-frame.png')

        # atomic write so watchers don't get a partial image
        _, temp_path = mkstemp()
        self.get_image_output().write(temp_path)
        os.rename(temp_path, frame_path)

        if not self.viewer_started:
            self.viewer_started = True

            pool = ThreadPoolExecutor(max_workers=1)
            pool.submit(self.start_viewer, frame_path)

    def end_viewer_process(self):
        if self.viewer_process.returncode is not None:
            return

        self.viewer_process.terminate()

    def start_viewer(self, frame_path):
        path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../gui/tk/viewer.py')

        self.viewer_process = Popen(['python', path, frame_path])

        atexit.register(self.end_viewer_process)

    def __getattr__(self, key):
        return self.c.__getattribute__(key)

    def set_scene(self, scene):
        self.c.set_scene(scene.c)

    def set_post_processor(self, post_processor):
        self.post_processor = post_processor

    presets = {
        'sppm': {
            'name': 'sppm',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'sobol',
            'shrinking_radius': True,
            'num_threads': get_num_cores()
        },
        'vcm': {
            'name': 'vcm',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'prand',
            'stage_frequency': 10,
            'shrinking_radius': True,
            'num_threads': get_num_cores()
        },
        'pt': {
            'name': 'pt',
            'min_path_length': 1,
            'max_path_length': 10,
            'initial_radius': 0.5,
            'sampler': 'sobol',
            'russian_roulette': True,
            'direct_lighting': 1,
            'direct_lighting_light': 1,
            'direct_lighting_bsdf': 1,
            'envmap_is': 1,
            'num_threads': get_num_cores()
        },
        'bdpt': {
            'name': 'bdpt',
            'min_path_length': 1,
            'max_path_length': 10,
            'stage_frequence': 3,
            'sampler': 'sobol',
            'num_threads': get_num_cores()
        }
    }