From ee468790485f51677b0c645681b95ffbefa3994a Mon Sep 17 00:00:00 2001 From: JZduns <130933171+JZduns@users.noreply.github.com> Date: Sun, 19 Jan 2025 17:52:23 +0100 Subject: [PATCH] Statistics (#6) * ray tracing statistics * ray counter * photonmapping ray counter * rays change * newline fix --------- Co-authored-by: Jakub <01149663@pw.edu.pl> --- code/photonmapping/python/main.py | 16 +++++++++++++++- rendering.py | 19 +++++++++++++++++-- sightpy/ray.py | 12 +++++++++++- sightpy/scene.py | 10 ++++++++-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/code/photonmapping/python/main.py b/code/photonmapping/python/main.py index 9aec82d6..92567914 100644 --- a/code/photonmapping/python/main.py +++ b/code/photonmapping/python/main.py @@ -1,5 +1,8 @@ import numpy as np import matplotlib.pyplot as plt +import time + +PHOTONS = 0 # Define basic vector operations class Vector3: @@ -103,6 +106,8 @@ def emit_photons(): trace_photon(photon, 0) def trace_photon(photon, depth): + global PHOTONS + PHOTONS += 1 if depth > max_depth: return closest_t = np.inf @@ -211,10 +216,19 @@ if __name__ == '__main__': emit_photons() print("Rendering image...") - width = 200 + width = 100 height = 100 + t0 = time.time() image = render_image(width, height) + print (f"Render Took: {round(time.time() - t0, 2)}s\n" + f"resolution: {width}x{height}\n" + f"photons (emitted): {num_photons}\n" + f"photons (reflected): {PHOTONS - num_photons}\n" + f"photons (total): {PHOTONS}\n" + f"rays: {width*height}" + ) + # Display the image plt.imshow(image) plt.axis('off') diff --git a/rendering.py b/rendering.py index f1f3d6b0..b0112517 100644 --- a/rendering.py +++ b/rendering.py @@ -2,6 +2,7 @@ """ Renders an image using raytracing """ import numpy as np import matplotlib.pyplot as plt +import time def ray_trace(num_spheres, environment, image_width=400, image_height=300, output_file="fig.png"): IMAGE_WIDTH = image_width @@ -381,10 +382,14 @@ def ray_trace(num_spheres, environment, image_width=400, image_height=300, outpu # Screen coordinates: x0, y0, x1, y1. S = (-1., -1. / r + .25, 1., 1. / r + .25) + renderTime = time.time() + reflections = 0 + rays = 0 + initialRays = 0 # Loop through all pixels. for i, x in enumerate(np.linspace(S[0], S[2], IMAGE_WIDTH)): if i % 10 == 0: - print(i / float(IMAGE_WIDTH) * 100, "%") + print(round(i / float(IMAGE_WIDTH) * 100, 2), "%") for j, y in enumerate(np.linspace(S[1], S[3], IMAGE_HEIGHT)): col[:] = 0 Q[:2] = (x, y) @@ -392,11 +397,14 @@ def ray_trace(num_spheres, environment, image_width=400, image_height=300, outpu DEPTH = 0 rayO, rayD = camera_origin, D REFLECTION = 1. + initialRays += 1 # Loop through initial and secondary rays. while DEPTH < DEPTH_MAX: traced = trace_ray(rayO, rayD) + rays += 1 if not traced: break + reflections += 1 obj, M, N, col_ray = traced # Reflection: create a new ray. rayO, rayD = M + \ @@ -405,6 +413,13 @@ def ray_trace(num_spheres, environment, image_width=400, image_height=300, outpu col += REFLECTION * col_ray REFLECTION *= obj.get('reflection', 1.) img[IMAGE_HEIGHT - j - 1, i, :] = np.clip(col, 0, 1) + renderTime = time.time() - renderTime plt.imsave(output_file, img) - print(f"Image saved as {output_file}") + print(f"Image saved as {output_file}\n" + f"resolution: {IMAGE_WIDTH}x{IMAGE_HEIGHT}\n" + f"render time: {round(renderTime, 2)} s\n" + f"reflections: {reflections}\n" + f"rays (initial): {initialRays}\n" + f"rays (secondary): {rays - initialRays}\n" + f"rays (total): {rays}") diff --git a/sightpy/ray.py b/sightpy/ray.py index 2e58b799..734c503e 100644 --- a/sightpy/ray.py +++ b/sightpy/ray.py @@ -3,6 +3,9 @@ from .utils.vector3 import vec3, extract, rgb import numpy as np from functools import reduce as reduce +RAYS = 0 +PRIMARY_RAYS = 0 + class Ray: """Info of the ray and the media it's travelling""" @@ -25,6 +28,12 @@ class Ray: self.reflections = reflections #reflections is the number of the refrections, starting at zero for camera rays self.transmissions = transmissions #transmissions is the number of the transmissions/refractions, starting at zero for camera rays self.diffuse_reflections = diffuse_reflections #reflections is the number of the refrections, starting at zero for camera rays + + global RAYS + RAYS += 1 + if self.reflections == 0 and self.transmissions == 0 and self.diffuse_reflections == 0: + global PRIMARY_RAYS + PRIMARY_RAYS += 1 def extract(self,hit_check): @@ -97,7 +106,8 @@ def get_distances(ray, scene): #Used for debugging ray-surface collisions. Retur norm_r_distance = r_distance/max_r_distance return rgb(norm_r_distance, norm_r_distance, norm_r_distance) - +def get_rays(): + return RAYS, PRIMARY_RAYS diff --git a/sightpy/scene.py b/sightpy/scene.py index d4c20a6b..57403dc5 100644 --- a/sightpy/scene.py +++ b/sightpy/scene.py @@ -4,7 +4,7 @@ import time from .utils import colour_functions as cf from .camera import Camera from .utils.vector3 import vec3, rgb -from .ray import get_raycolor, get_distances +from .ray import get_raycolor, get_distances, get_rays from . import lights from .backgrounds.skybox import SkyBox from .backgrounds.panorama import Panorama @@ -88,7 +88,13 @@ class Scene(): #gamma correction color = cf.sRGB_linear_to_sRGB(color_RGBlinear.to_array()) - print ("Render Took", time.time() - t0) + RAYS, PRIMARY_RAYS = get_rays() + + print (f"Render Took: {round(time.time() - t0, 2)}s\n" + f"resolution: {self.camera.screen_width}x{self.camera.screen_height}\n" + f"rays (total): {RAYS}\n" + f"rays (primary): {PRIMARY_RAYS}\n" + f"rays (secondary): {RAYS-PRIMARY_RAYS}") img_RGB = [] for c in color: