From 76657dddd4f37f55a3912ac44d2ba8722645b1b6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 20 Apr 2025 19:45:36 +0900 Subject: refactor: use anonymous enum for tagged-union --- src/rtw/hittable.zig | 68 ++++++++++++++++++++++------------------------------ src/rtw/material.zig | 25 +++++++------------ src/rtw/texture.zig | 17 ++++--------- 3 files changed, 42 insertions(+), 68 deletions(-) diff --git a/src/rtw/hittable.zig b/src/rtw/hittable.zig index 7240fd2..7ba5dfe 100644 --- a/src/rtw/hittable.zig +++ b/src/rtw/hittable.zig @@ -19,19 +19,7 @@ const Material = @import("material.zig").Material; const HitRecord = @import("hit_record.zig").HitRecord; const Aabb = @import("aabb.zig").Aabb; -const HittableTag = enum { - sphere, - movingSphere, - list, - xyRect, - xzRect, - yzRect, - box, - translate, - rotateY, -}; - -pub const Hittable = union(HittableTag) { +pub const Hittable = union(enum) { const Self = @This(); sphere: Sphere, @@ -58,43 +46,43 @@ pub const Hittable = union(HittableTag) { pub fn hit(self: Self, r: Ray, t_min: f64, t_max: f64, record: *HitRecord) bool { return switch (self) { - HittableTag.sphere => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.movingSphere => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.list => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.xyRect => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.xzRect => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.yzRect => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.box => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.translate => |slf| slf.hit(r, t_min, t_max, record), - HittableTag.rotateY => |slf| slf.hit(r, t_min, t_max, record), + .sphere => |slf| slf.hit(r, t_min, t_max, record), + .movingSphere => |slf| slf.hit(r, t_min, t_max, record), + .list => |slf| slf.hit(r, t_min, t_max, record), + .xyRect => |slf| slf.hit(r, t_min, t_max, record), + .xzRect => |slf| slf.hit(r, t_min, t_max, record), + .yzRect => |slf| slf.hit(r, t_min, t_max, record), + .box => |slf| slf.hit(r, t_min, t_max, record), + .translate => |slf| slf.hit(r, t_min, t_max, record), + .rotateY => |slf| slf.hit(r, t_min, t_max, record), }; } fn boudingBox(self: Self, time0: f64, time1: f64, output_box: *Aabb) bool { return switch (self) { - HittableTag.sphere => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.movingSphere => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.list => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.xyRect => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.xzRect => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.yzRect => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.box => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.translate => |slf| slf.boudingBox(time0, time1, output_box), - HittableTag.rotateY => |slf| slf.boudingBox(time0, time1, output_box), + .sphere => |slf| slf.boudingBox(time0, time1, output_box), + .movingSphere => |slf| slf.boudingBox(time0, time1, output_box), + .list => |slf| slf.boudingBox(time0, time1, output_box), + .xyRect => |slf| slf.boudingBox(time0, time1, output_box), + .xzRect => |slf| slf.boudingBox(time0, time1, output_box), + .yzRect => |slf| slf.boudingBox(time0, time1, output_box), + .box => |slf| slf.boudingBox(time0, time1, output_box), + .translate => |slf| slf.boudingBox(time0, time1, output_box), + .rotateY => |slf| slf.boudingBox(time0, time1, output_box), }; } pub fn deinit(self: *const Self) void { return switch (self.*) { - HittableTag.sphere => |slf| slf.deinit(), - HittableTag.movingSphere => |slf| slf.deinit(), - HittableTag.list => |slf| slf.deinit(), - HittableTag.xyRect => |slf| slf.deinit(), - HittableTag.xzRect => |slf| slf.deinit(), - HittableTag.yzRect => |slf| slf.deinit(), - HittableTag.box => |slf| slf.deinit(), - HittableTag.translate => |slf| slf.deinit(), - HittableTag.rotateY => |slf| slf.deinit(), + .sphere => |slf| slf.deinit(), + .movingSphere => |slf| slf.deinit(), + .list => |slf| slf.deinit(), + .xyRect => |slf| slf.deinit(), + .xzRect => |slf| slf.deinit(), + .yzRect => |slf| slf.deinit(), + .box => |slf| slf.deinit(), + .translate => |slf| slf.deinit(), + .rotateY => |slf| slf.deinit(), }; } }; diff --git a/src/rtw/material.zig b/src/rtw/material.zig index f901f26..664a225 100644 --- a/src/rtw/material.zig +++ b/src/rtw/material.zig @@ -13,14 +13,7 @@ const randomPointInUnitSphere = rand.randomPointInUnitSphere; const randomUnitVector = rand.randomUnitVector; const randomReal01 = rand.randomReal01; -const MaterialTag = enum { - diffuse, - metal, - dielectric, - diffuse_light, -}; - -pub const Material = union(MaterialTag) { +pub const Material = union(enum) { diffuse: DiffuseMaterial, metal: MetalMaterial, dielectric: DielectricMaterial, @@ -28,19 +21,19 @@ pub const Material = union(MaterialTag) { pub fn scatter(mat: Material, r_in: Ray, record: HitRecord, attenuation: *Color, scattered: *Ray, rng: Random) bool { return switch (mat) { - MaterialTag.diffuse => |diffuse_mat| diffuse_mat.scatter(r_in, record, attenuation, scattered, rng), - MaterialTag.metal => |metal_mat| metal_mat.scatter(r_in, record, attenuation, scattered, rng), - MaterialTag.dielectric => |dielectric_mat| dielectric_mat.scatter(r_in, record, attenuation, scattered, rng), - MaterialTag.diffuse_light => |diffuse_light_mat| diffuse_light_mat.scatter(r_in, record, attenuation, scattered, rng), + .diffuse => |diffuse_mat| diffuse_mat.scatter(r_in, record, attenuation, scattered, rng), + .metal => |metal_mat| metal_mat.scatter(r_in, record, attenuation, scattered, rng), + .dielectric => |dielectric_mat| dielectric_mat.scatter(r_in, record, attenuation, scattered, rng), + .diffuse_light => |diffuse_light_mat| diffuse_light_mat.scatter(r_in, record, attenuation, scattered, rng), }; } pub fn emitted(mat: Material, u: f64, v: f64, p: Vec3) Color { return switch (mat) { - MaterialTag.diffuse => rgb(0, 0, 0), - MaterialTag.metal => rgb(0, 0, 0), - MaterialTag.dielectric => rgb(0, 0, 0), - MaterialTag.diffuse_light => |diffuse_light_mat| diffuse_light_mat.emitted(u, v, p), + .diffuse => rgb(0, 0, 0), + .metal => rgb(0, 0, 0), + .dielectric => rgb(0, 0, 0), + .diffuse_light => |diffuse_light_mat| diffuse_light_mat.emitted(u, v, p), }; } }; diff --git a/src/rtw/texture.zig b/src/rtw/texture.zig index ff17579..402fb1a 100644 --- a/src/rtw/texture.zig +++ b/src/rtw/texture.zig @@ -7,14 +7,7 @@ const rgb = @import("vec.zig").rgb; const Random = @import("rand.zig").Random; const Perlin = @import("perlin.zig").Perlin; -const TextureTag = enum { - solid, - checker, - noise, - image, -}; - -pub const Texture = union(TextureTag) { +pub const Texture = union(enum) { solid: SolidTexture, checker: CheckerTexture, noise: NoiseTexture, @@ -42,10 +35,10 @@ pub const Texture = union(TextureTag) { pub fn value(tx: Texture, u: f64, v: f64, p: Vec3) Color { return switch (tx) { - TextureTag.solid => |solid_tx| solid_tx.value(u, v, p), - TextureTag.checker => |checker_tx| checker_tx.value(u, v, p), - TextureTag.noise => |noise_tx| noise_tx.value(u, v, p), - TextureTag.image => |image_tx| image_tx.value(u, v, p), + .solid => |solid_tx| solid_tx.value(u, v, p), + .checker => |checker_tx| checker_tx.value(u, v, p), + .noise => |noise_tx| noise_tx.value(u, v, p), + .image => |image_tx| image_tx.value(u, v, p), }; } }; -- cgit v1.2.3-70-g09d2