Skip to content

Commit 33d39f9

Browse files
Added note about scale transformations and emissive objects
1 parent 877648e commit 33d39f9

13 files changed

+54
-24
lines changed

R/objects.R

+22-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
1111
#' @param velocity Default `c(0, 0, 0)`. Velocity of the sphere, used for motion blur.
1212
#' @param flipped Default `FALSE`. Whether to flip the normals.
13-
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
13+
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
14+
#' Note: emissive objects may not currently function correctly when scaled.
1415
#' @importFrom grDevices col2rgb
1516
#'
1617
#' @return Single row of a tibble describing the sphere in the scene.
@@ -74,6 +75,7 @@ sphere = function(x = 0, y = 0, z = 0, radius = 1, material = lambertian(),
7475
#' @param velocity Default `c(0, 0, 0)`. Velocity of the cube, used for motion blur.
7576
#' @param flipped Default `FALSE`. Whether to flip the normals.
7677
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
78+
#' Note: emissive objects may not currently function correctly when scaled.
7779
#' @importFrom grDevices col2rgb
7880
#'
7981
#' @return Single row of a tibble describing the cube in the scene.
@@ -130,6 +132,7 @@ cube = function(x = 0, y = 0, z = 0, width = 1, xwidth = 1, ywidth = 1, zwidth =
130132
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
131133
#' @param flipped Default `FALSE`. Whether to flip the normals.
132134
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
135+
#' Note: emissive objects may not currently function correctly when scaled.
133136
#'
134137
#' @return Single row of a tibble describing the XY plane in the scene.
135138
#' @export
@@ -183,6 +186,7 @@ xy_rect = function(x = 0, y = 0, z = 0, xwidth = 1, ywidth = 1,
183186
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
184187
#' @param flipped Default `FALSE`. Whether to flip the normals.
185188
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
189+
#' Note: emissive objects may not currently function correctly when scaled.
186190
#'
187191
#' @return Single row of a tibble describing the YZ plane in the scene.
188192
#' @export
@@ -235,6 +239,7 @@ yz_rect = function(x = 0, y = 0, z = 0, ywidth = 1, zwidth = 1, material = lambe
235239
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
236240
#' @param flipped Default `FALSE`. Whether to flip the normals.
237241
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
242+
#' Note: emissive objects may not currently function correctly when scaled.
238243
#'
239244
#' @return Single row of a tibble describing the XZ plane in the scene.
240245
#' @export
@@ -296,6 +301,7 @@ xz_rect = function(x = 0, xwidth = 1, z = 0, zwidth = 1, y = 0, material = lambe
296301
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
297302
#' @param flipped Default `FALSE`. Whether to flip the normals.
298303
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
304+
#' Note: emissive objects may not currently function correctly when scaled.
299305
#'
300306
#' @return Single row of a tibble describing the XZ plane in the scene.
301307
#' @export
@@ -371,6 +377,7 @@ triangle = function(v1 = c(1, 0, 0), v2 = c(0, 1, 0), v3 = c(-1, 0, 0),
371377
#' @param velocity Default `c(0, 0, 0)`. Velocity of the disk.
372378
#' @param flipped Default `FALSE`. Whether to flip the normals.
373379
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
380+
#' Note: emissive objects may not currently function correctly when scaled.
374381
#'
375382
#' @importFrom grDevices col2rgb
376383
#'
@@ -421,19 +428,23 @@ disk = function(x = 0, y = 0, z = 0, radius = 1, inner_radius = 0, material = la
421428
}
422429

423430
#' `obj` File Object
431+
#'
432+
#' Load an obj file via a filepath. Currently only supports the diffuse texture with the `texture` argument.
433+
#' Note: light importance sampling currently not supported for this shape.
424434
#'
425435
#' @param filename Filename and path to the `obj` file. Can also be a `txt` file, if it's in the correct `obj` internally.
426436
#' @param x Default `0`. x-coordinate to offset the model.
427437
#' @param y Default `0`. y-coordinate to offset the model.
428438
#' @param z Default `0`. z-coordinate to offset the model.
429439
#' @param scale_obj Default `1`. Amount to scale the model.
430-
#' @param objcolor Default `FALSE`. Whether to load an obj file with vertex colors.
440+
#' @param texture Default `FALSE`. Whether to load the obj file texture.
431441
#' @param material Default \code{\link{lambertian}}.The material, called from one of the material
432442
#' functions \code{\link{lambertian}}, \code{\link{metal}}, or \code{\link{dielectric}}.
433443
#' @param angle Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.
434444
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".
435445
#' @param flipped Default `FALSE`. Whether to flip the normals.
436446
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
447+
#' Note: emissive objects may not currently function correctly when scaled.
437448
#'
438449
#' @return Single row of a tibble describing the obj model in the scene.
439450
#' @export
@@ -457,12 +468,12 @@ disk = function(x = 0, y = 0, z = 0, radius = 1, inner_radius = 0, material = la
457468
#' render_scene(parallel = TRUE, samples = 400,
458469
#' tonemap = "reinhold", aperture = 0.05, fov = 32, lookfrom = c(0, 2, 10))
459470
#' }
460-
obj_model = function(filename, x = 0, y = 0, z = 0, scale_obj = 1, objcolor = FALSE,
471+
obj_model = function(filename, x = 0, y = 0, z = 0, scale_obj = 1, texture = FALSE,
461472
material = lambertian(),
462473
angle = c(0, 0, 0), order_rotation = c(1, 2, 3),
463474
flipped = FALSE, scale = c(1,1,1)) {
464475
info = c(unlist(material$properties), scale_obj)
465-
if(objcolor) {
476+
if(texture) {
466477
shape = "objcolor"
467478
} else {
468479
shape = "obj"
@@ -497,6 +508,7 @@ obj_model = function(filename, x = 0, y = 0, z = 0, scale_obj = 1, objcolor = FA
497508
#' @param velocity Default `c(0, 0, 0)`. Velocity of the cylinder, used for motion blur.
498509
#' @param flipped Default `FALSE`. Whether to flip the normals.
499510
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
511+
#' Note: emissive objects may not currently function correctly when scaled.
500512
#'
501513
#' @importFrom grDevices col2rgb
502514
#'
@@ -558,8 +570,8 @@ cylinder = function(x = 0, y = 0, z = 0, radius = 1, length = 1,
558570
#' functions \code{\link{lambertian}}, \code{\link{metal}}, or \code{\link{dielectric}}.
559571
#' @param velocity Default `c(0, 0, 0)`. Velocity of the segment, used for motion blur.
560572
#' @param flipped Default `FALSE`. Whether to flip the normals.
561-
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions. Note: this will
562-
#' change the stated start/end position of the segment.
573+
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions. Notes: this will
574+
#' change the stated start/end position of the segment. Emissive objects may not currently function correctly when scaled.
563575
#'
564576
#' @importFrom grDevices col2rgb
565577
#'
@@ -628,6 +640,9 @@ segment = function(start = c(0, -1, 0), end = c(0, 1, 0), radius = 1,
628640

629641
#' Ellipsoid Object
630642
#'
643+
#' Note: light importance sampling for this shape is currently approximated by a sphere. This will fail
644+
#' for ellipsoids with large differences between axes.
645+
#'
631646
#' @param x Default `0`. x-coordinate of the center of the ellipsoid.
632647
#' @param y Default `0`. y-coordinate of the center of the ellipsoid.
633648
#' @param z Default `0`. z-coordinate of the center of the ellipsoid.
@@ -641,6 +656,7 @@ segment = function(start = c(0, -1, 0), end = c(0, 1, 0), radius = 1,
641656
#' @param velocity Default `c(0, 0, 0)`. Velocity of the segment, used for motion blur.
642657
#' @param flipped Default `FALSE`. Whether to flip the normals.
643658
#' @param scale Default `c(1,1,1)`. Scale transformation in the x, y, and z directions.
659+
#' Note: emissive objects may not currently function correctly when scaled.
644660
#'
645661
#' @importFrom grDevices col2rgb
646662
#'

man/cube.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cylinder.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/disk.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ellipsoid.Rd

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/obj_model.Rd

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/segment.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sphere.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/triangle.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xy_rect.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xz_rect.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/yz_rect.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ellipsoid.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ellipsoid: public hitable {
1010
ellipsoid(vec3 cen, float r, vec3 axes, material *mat) :
1111
center(cen), radius(r), axes(axes), mat_ptr(mat) {
1212
inv_axes = vec3(1.0f/axes.x(), 1.0f/axes.y(), 1.0f/axes.z());
13+
largest_proj_axis = axes.x() * axes.y() * axes.z() / ffmin(axes.x(), ffmin(axes.y(), axes.z()));
1314
};
1415
virtual bool hit(const ray& r, float tmin, float tmax, hit_record& rec, random_gen& rng);
1516
virtual bool bounding_box(float t0, float t1, aabb& box) const;
@@ -19,6 +20,7 @@ class ellipsoid: public hitable {
1920
float radius;
2021
vec3 axes;
2122
vec3 inv_axes;
23+
float largest_proj_axis;
2224
material *mat_ptr;
2325
};
2426

@@ -59,8 +61,8 @@ bool ellipsoid::hit(const ray& r, float t_min, float t_max, hit_record& rec, ran
5961
float ellipsoid::pdf_value(const vec3& o, const vec3& v, random_gen& rng) {
6062
hit_record rec;
6163
if(this->hit(ray(o,v), 0.001, FLT_MAX, rec, rng)) {
62-
float cos_theta_max = sqrt(1 - radius * radius/(center - o).squared_length());
63-
float solid_angle = 2 * M_PI * (1-cos_theta_max) * axes.x() * axes.y() * axes.z();
64+
float cos_theta_max = sqrt(1 - 1/(center - o).squared_length());
65+
float solid_angle = 2 * M_PI * (1-cos_theta_max) * largest_proj_axis ;
6466
return(1/solid_angle);
6567
} else {
6668
return(0);

0 commit comments

Comments
 (0)