/
lines.R
271 lines (263 loc) · 11.1 KB
/
lines.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#'@title Generate Lines
#'
#'@param start Default `c(0,0,0)`. Start of the line segment.
#'@param end Default `c(0,1,0)`. End of the line segment..
#'@param color Default `white`. Color of the line segment.
#'@return Line matrix
#'@export
#'@examples
#'if(run_documentation()) {
#' # Make a spiral of lines
#' t = seq(0,8*pi,length.out=361)
#' line_mat = matrix(nrow=0,ncol=9)
#'
#' for(i in 1:360) {
#' line_mat = add_lines(line_mat,
#' generate_line(start = c(0.5*sin(t[i]), t[i]/(8*pi), 0.5*cos(t[i])),
#' end = c(0.5*sin(t[i+1]), t[i+1]/(8*pi), 0.5*cos(t[i+1]))))
#' }
#' rasterize_lines(line_mat)
#' }
#' if(run_documentation()) {
#' #Change the line color
#' line_mat = matrix(nrow=0,ncol=9)
#' cols = hsv(seq(0,1,length.out=360))
#' for(i in 1:360) {
#' line_mat = add_lines(line_mat,
#' generate_line(start = c(sin(t[i]), 2*t[i]/(8*pi), cos(t[i])),
#' end = c(sin(t[i+1]), 2*t[i+1]/(8*pi), cos(t[i+1])),
#' color = cols[i]))
#' }
#' rasterize_lines(line_mat,lookfrom=c(0,10,10),fov=15)
#' }
#' if(run_documentation()) {
#' #Use in a scene with a mesh
#' obj_mesh(r_obj(),material=material_list(diffuse="dodgerblue")) |>
#' rasterize_scene(line_info = line_mat, light_info = directional_light(c(0,1,1)),
#' lookfrom=c(0,5,10),lookat=c(0,0.8,0),fov=15)
#' }
generate_line = function(start = c(0,0,0), end = c(0,1,0), color = "white") {
color = convert_color(color)
start = as.numeric(start)
end = as.numeric(end)
returnmat = matrix(c(start, end, color), nrow=1,ncol=9)
colnames(returnmat) = c("x0","y0","z0","x1","y1","z1","r","g","b")
returnmat
}
#'@title Rotate Lines
#'
#'@param lines The existing line scene.
#'@param angle Default `c(0,0,0)`. The rotation amount for the x/y/z axes, in degrees.
#'@param pivot_point Default `c(0,0,0)`. The pivot point of the rotation.
#'@param order_rotation Default `c(1,2,3)`. The order in which to perform the rotations.#'
#'@return Rotated lines.
#'
#'@export
#'@examples
#'if(run_documentation()) {
#' #Generate a cube out of lines
#' cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1)))
#' rasterize_lines(cube_outline,lookfrom=c(0,6,10))
#' }
#' if(run_documentation()) {
#' #Rotate the cube 30 degrees around the y-axis
#' rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(0,30,0)),color="red")
#' rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10))
#' }
#' if(run_documentation()) {
#' #Rotate the cube 30 degrees around each axis, in this order: x,y,z
#' rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30)),color="red")
#' rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10))
#' }
#' if(run_documentation()) {
#' #Rotate the cube 30 degrees around each axis, in this order: z,y,x
#' rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30),
#' order_rotation = c(3,2,1)),color="red")
#' rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10))
#' }
rotate_lines = function(lines, angle = c(0,0,0), pivot_point = c(0,0,0), order_rotation = c(1,2,3)) {
angle = angle*pi/180
lines[,1] = lines[,1] - pivot_point[1]
lines[,2] = lines[,2] - pivot_point[2]
lines[,3] = lines[,3] - pivot_point[3]
lines[,4] = lines[,4] - pivot_point[1]
lines[,5] = lines[,5] - pivot_point[2]
lines[,6] = lines[,6] - pivot_point[3]
rot_mat = generate_rot_matrix(angle, order_rotation)
for(i in seq_len(nrow(lines))) {
lines[i,1:3] = lines[i,1:3] %*% rot_mat
lines[i,4:6] = lines[i,4:6] %*% rot_mat
}
lines[,1] = lines[,1] + pivot_point[1]
lines[,2] = lines[,2] + pivot_point[2]
lines[,3] = lines[,3] + pivot_point[3]
lines[,4] = lines[,4] + pivot_point[1]
lines[,5] = lines[,5] + pivot_point[2]
lines[,6] = lines[,6] + pivot_point[3]
return(lines)
}
#'@title Scale Lines
#'
#'@param lines The line scene.
#'@param scale Default `c(1,1,1)`. The scale amount, per axis.
#'
#'@return Scaled line matrix.
#'@export
#'@examples
#'if(run_documentation()) {
#' #Generate a cube out of lines
#' cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1)))
#' rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3))
#' }
#' if(run_documentation()) {
#' #Scale the cube uniformly
#' scaled_cube = color_lines(scale_lines(cube_outline,scale=0.5),color="red")
#' rasterize_lines(add_lines(cube_outline,scaled_cube),fov=90,lookfrom=c(0,0,3))
#' }
#' if(run_documentation()) {
#' #Scale the cube non-uniformly
#' scaled_cube = color_lines(scale_lines(cube_outline,scale=c(0.8,2,0.4)),color="red")
#' rasterize_lines(add_lines(cube_outline,scaled_cube),fov=60,lookfrom=c(3,3,3))
#' }
scale_lines = function(lines, scale = 1) {
if(length(scale) == 1) {
scale = rep(scale,3)
}
lines[,1] = lines[,1] * scale[1]
lines[,2] = lines[,2] * scale[2]
lines[,3] = lines[,3] * scale[3]
lines[,4] = lines[,4] * scale[1]
lines[,5] = lines[,5] * scale[2]
lines[,6] = lines[,6] * scale[3]
return(lines)
}
#'@title Translate Lines
#'
#'@param lines The line scene.
#'@param position Default `c(0,0,0)`. The translation vector.
#'
#'@return Translated line matrix.
#'@export
#'@examples
#'if(run_documentation()) {
#' #Generate a cube out of lines
#' cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1)))
#' rasterize_lines(cube_outline,fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0))
#' }
#' if(run_documentation()) {
#' #Scale the cube uniformly
#' translated_cube = color_lines(translate_lines(cube_outline,c(1,1,1)),"red")
#' translated_cube2 = color_lines(translate_lines(cube_outline,c(-1,-1,-1)),"green")
#'
#' cube_outline |>
#' add_lines(translated_cube) |>
#' add_lines(translated_cube2) |>
#' rasterize_lines(fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0))
#'}
translate_lines = function(lines, position = 1) {
lines[,1] = lines[,1] + position[1]
lines[,2] = lines[,2] + position[2]
lines[,3] = lines[,3] + position[3]
lines[,4] = lines[,4] + position[1]
lines[,5] = lines[,5] + position[2]
lines[,6] = lines[,6] + position[3]
return(lines)
}
#'@title Color Lines
#'
#'@param lines The line scene.
#'@param color Default `white`. The color to convert the lines to.
#'
#'@return Colored line matrix.
#'@export
#'@examples
#'if(run_documentation()) {
#' #Generate a cube out of lines
#' cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1)))
#'
#' cube_outline |>
#' color_lines(color="red") |>
#' rasterize_lines()
#'}
color_lines = function(lines, color = "white") {
color = convert_color(color)
lines[,7] = color[1]
lines[,8] = color[2]
lines[,9] = color[3]
return(lines)
}
#'@title Add Line
#'
#'@param lines Existing lines or empty (0-row) matrix.
#'@param line Line to add, generated with `generate_line()`
#'
#'@return New line matrix.
#'@export
#'@examples
#'if(run_documentation()) {
#' #Generate a cube out of lines
#' cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |>
#' add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |>
#' add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |>
#' add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |>
#' add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1)))
#'
#' rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3))
#' }
add_lines = function(lines, line) {
if(nrow(lines) == 0) {
return(line)
}
return(rbind(lines,line))
}