-
Notifications
You must be signed in to change notification settings - Fork 42
/
create_instances.R
198 lines (194 loc) · 10.3 KB
/
create_instances.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#' Create Instances of an Object
#'
#' This creates multiple instances of the `ray_scene` passed, each with it's own transformation applied (measured
#' from the origin of the ray_scene). This means the scene only uses the memory of the object once and each
#' copy only requires a 4x4 matrix in memory.
#'
#' @param ray_scene A `ray_scene` object to be copied at the specified transformed coordinates.
#' @param x Default `0`. A vector of x-coordinates to offset the instances. Note that this can also be a 3 column matrix or
#' `data.frame()` parsable by `xyz.coords()`: if so, the other axes will be ignored.
#' @param y Default `0`. A vector of y-coordinates to offset the instances.
#' @param z Default `0`. A vector of z-coordinates to offset the instances.
#' @param angle_x Default `0`. A vector of angles around the x axis to rotate the instances.
#' @param angle_y Default `0`. A vector of angles around the y axis to rotate the instances.
#' @param angle_z Default `0`. A vector of angles around the z axis to rotate the instances.
#' @param scale_x Default `0`. A vector of values around the scale the instances on the x-axis.
#' @param scale_y Default `0`. A vector of values around the scale the instances on the y-axis.
#' @param scale_z Default `0`. A vector of values around the scale the instances on the z-axis.
#' @param material Default \code{\link{diffuse}}.The material, called from one of the material
#' functions \code{\link{diffuse}}, \code{\link{metal}}, or \code{\link{dielectric}}.
#' @param order_rotation Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z" axes.
#'
#' @return Single row of a tibble describing the instance in the scene.
#' @export
#'
#' @examples
#' if (run_documentation()) {
#' # Generate the base scene
#' base_scene = generate_ground(material = diffuse(checkercolor = "grey20")) %>%
#' add_object(sphere(z = 100, radius = 10, material = light(intensity = 70)))
#'
#' # Start with a single sphere with an R in it
#' sphere_scene = sphere(y = 0, material = glossy(color = "#2b6eff", reflectance = 0.05)) %>%
#' add_object(obj_model(r_obj(), z = 1, y = -0.05, scale_obj = 0.45, material = diffuse())) %>%
#' group_objects(scale = 0.1)
#'
#' # Render the scene
#' sphere_scene %>%
#' add_object(base_scene) %>%
#' render_scene(lookat = c(0, 1, 0), width = 800, sample_method = "sobol_blue", aperture = 0.2,
#' height = 800, samples = 128, clamp_value = 20)
#' }
#'
#' if (run_documentation()) {
#' # Create instances at different x positions, with random rotations applied
#' create_instances(sphere_scene,
#' x = seq(-1.5, 1.5, length.out = 10),
#' angle_x = 90 * (runif(10) - 0.5),
#' angle_y = 90 * (runif(10) - 0.5),
#' angle_z = 90 * (runif(10) - 0.5)) %>%
#' add_object(base_scene) %>%
#' render_scene(lookat = c(0, 1, 0), width = 800, sample_method = "sobol_blue",
#' height = 800, samples = 128, clamp_value = 20)
#' }
#'
#' if (run_documentation()) {
#' # Create instances at different x/z positions, with random scaling factors
#' create_instances(sphere_scene,
#' x = seq(-1.5, 1.5, length.out = 10),
#' y = seq(0, 1.5, length.out = 10),
#' scale_x = 0.5 + runif(10),
#' scale_y = 0.5 + runif(10),
#' scale_z = 0.5 + runif(10)) %>%
#' add_object(base_scene) %>%
#' render_scene(lookat = c(0, 1, 0), width = 800, sample_method = "sobol_blue",
#' height = 800, samples = 128, clamp_value = 20)
#' }
#'
#' if (run_documentation()) {
#' # Create instances of instances
#' create_instances(sphere_scene,
#' x = seq(-1.5, 1.5, length.out = 10),
#' angle_y = 90 * (runif(10) - 0.5)) %>%
#' create_instances(y = seq(0, 2, length.out = 10)) %>%
#' add_object(base_scene) %>%
#' render_scene(lookat = c(0, 1, 0), width = 800, sample_method = "sobol_blue",
#' height = 800, samples = 128, clamp_value = 20)
#' }
#'
#' if (run_documentation()) {
#' # Create instances of instances of instances of instances
#' create_instances(sphere_scene,
#' x = seq(-1.5, 1.5, length.out = 10),
#' angle_y = 90 * (runif(10) - 0.5)) %>%
#' create_instances(y = seq(0, 1, length.out = 5)) %>%
#' create_instances(y = seq(0, 2, length.out = 20) * 10,
#' angle_y = seq(0, 360, length.out = 20)) %>%
#' create_instances(x = c(-5, 0, 5),
#' scale_y = c(0.5, 1, 0.75)) %>%
#' add_object(base_scene) %>%
#' render_scene(lookat = c(0, 10, 0), lookfrom = c(0, 10, 50),
#' width = 800, sample_method = "sobol_blue", fov = 30,
#' height = 800, samples = 128, clamp_value = 20)
#' }
#'
#' if (run_documentation()) {
#' # Generate a complex scene in a Cornell box and replicate it in a 3x3 grid
#' # Here, a single `data.frame` with all three coordinates is passed to the `x` argument.
#' tempfileplot = tempfile()
#' png(filename = tempfileplot, height = 1600, width = 1600)
#' plot(iris$Petal.Length, iris$Sepal.Width, col = iris$Species, pch = 18, cex = 12)
#' dev.off()
#' image_array = png::readPNG(tempfileplot)
#'
#' # Note that if a instanced scene has importance sampled lights and there are many instances,
#' # it will be slow to render.
#' generate_cornell(importance_sample=FALSE) %>%
#' add_object(ellipsoid(x = 555 / 2, y = 100, z = 555 / 2, a = 50, b = 100, c = 50,
#' material = metal(color = "lightblue"))) %>%
#' add_object(cube(x = 100, y = 130 / 2, z = 200, xwidth = 130,
#' ywidth = 130, zwidth = 130, angle = c(0, 10, 0),
#' material = diffuse(checkercolor = "purple", checkerperiod = 30))) %>%
#' add_object(pig(x = 100, y = 190, z = 200, scale = 40, angle = c(0, 30, 0))) %>%
#' add_object(sphere(x = 420, y = 555 / 8, z = 100, radius = 555 / 8,
#' material = dielectric(color = "orange"))) %>%
#' add_object(yz_rect(x = 5, y = 300, z = 555 / 2, zwidth = 400, ywidth = 400,
#' material = diffuse(image_texture = image_array))) %>%
#' add_object(yz_rect(x = 555 / 2, y = 300, z = 555 - 5, zwidth = 400, ywidth = 400,
#' material = diffuse(image_texture = image_array), angle = c(0, 90, 0))) %>%
#' add_object(yz_rect(x = 555 - 5, y = 300, z = 555 / 2, zwidth = 400, ywidth = 400,
#' material = diffuse(image_texture = image_array), angle = c(0, 180, 0))) %>%
#' create_instances(x = expand.grid(x = seq(-1, 1, by = 1) * 556 - 555 / 2,
#' y = seq(-1, 1, by = 1) * 556 - 555 / 2,
#' z = 0)) %>%
#' render_scene(lookfrom = c(0, 0, -800) * 3, fov = 40,
#' samples = 128, sample_method = "sobol_blue",
#' parallel = TRUE, width = 800, height = 800)
#' }
create_instances = function(ray_scene,
x = 0, y = 0, z = 0,
angle_x = 0, angle_y = 0, angle_z = 0,
scale_x = 1, scale_y = 1, scale_z = 1,
material = diffuse(),
order_rotation = c(1, 2, 3)) {
ray_scene_processed = process_scene(ray_scene, process_material_ids = FALSE)
# Use data.frame for recycling
x_is_df = inherits(x, "data.frame") || inherits(x, "matrix")
if(x_is_df && ncol(x) == 3) {
xyz = grDevices::xyz.coords(x=x)
} else {
xyz = data.frame(x=x,y=y,z=z)
}
x = xyz$x
y = xyz$y
z = xyz$z
angle_is_df = inherits(angle_x, "data.frame") || inherits(angle_x, "matrix")
if(angle_is_df && ncol(angle_x) == 3) {
angles = grDevices::xyz.coords(angle_x)
stopifnot(length(angles$x) == length(x))
} else {
angles = data.frame(x_val=x,x=angle_x,y=angle_y,z=angle_z)
}
angle_x = angles$x
angle_y = angles$y
angle_z = angles$z
scale_is_df = inherits(scale_x, "data.frame") || inherits(scale_x, "matrix")
if(scale_is_df && ncol(scale_x) == 3) {
scales = grDevices::xyz.coords(scale_x)
stopifnot(length(scales$x) == length(x))
} else {
scales = data.frame(x_val=x,x=scale_x,y=scale_y,z=scale_z)
}
scale_x = scales$x
scale_y = scales$y
scale_z = scales$z
new_tibble_row(list(x = 0, y = 0, z = 0,
shape = "instance",
material = material,
shape_info = ray_shape_info(shape_properties = list(original_scene = list(ray_scene_processed$scene),
x_values = x,
y_values = y,
z_values = z,
angle_x = angle_x,
angle_y = angle_y,
angle_z = angle_z,
scale_x = scale_x,
scale_y = scale_y,
scale_z = scale_z,
any_light = ray_scene_processed$any_light),
tricolorinfo = list(NA),
fileinfo = NA,
material_id = NA_integer_,
csg_object = list(NA),
mesh_info = list(NA),
flipped = FALSE),
transforms = ray_transform(angle = list(c(0,0,0)),
order_rotation = list(order_rotation),
scale = list(c(1,1,1)),
group_transform = list(matrix(NA_real_))),
animation_info = ray_animated_transform(
start_transform_animation = list(matrix(NA_real_)),
end_transform_animation = list(matrix(NA_real_)),
start_time = 0, end_time = 1)
))
}