-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_roof.R
1081 lines (1043 loc) · 48.9 KB
/
generate_roof.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Generate a 3D roof model
#'
#' This function generates a 3D roof model from a straight skeleton.
#'
#' @param skeleton Default `NULL`. A straight skeleton generated from the `skeletonize` function.
#' @param angle Default `45`. Angle of the roof.
#' @param max_height Default `NA`. The maximum height of the roof.
#' @param vertical_offset Default `0`. The vertical offset of the roof.
#' @param base Default `TRUE`. A logical flag that controls whether to generate the bottom of the roof.
#' @param base_height Default `vertical_offset`. Height of the base.
#' @param sides Default `FALSE`. A logical flag on whether to draw the sides. This will automatically be set to `TRUE`
#' if `base = TRUE` and the `base_height` is less than `vertical_offset`.
#' @param double_sided Default `FALSE`. A logical flag that controls whether the polygon should be double-sided.
#' @param swap_yz Default `TRUE`. A logical flag that controls whether to swap the y and z coordinates in the resulting mesh.
#' If `TRUE`, the y and z coordinates will be swapped.
#' @param progress Default `TRUE`. A logical flag to control whether a progress bar is displayed during roof generation.
#' @param verbose Default `FALSE`. A logical flag to control whether additional timing information should be displayed.
#' @param scale_all_max Default `FALSE`. If passing in a list of multiple skeletons with polygons, whether to scale each polygon to the overall
#' max height, or whether to scale each max height to the maximum internal distance in the polygon.
#' @param material Default `material_list()`. Interface to set the color/appearance/material options for the resulting `ray_mesh` mesh.
#' @param roof_material Default `NA`, uses the material specified in `material`. Interface to set the color/appearance/material options for the resulting `ray_mesh` rooftop mesh.
#'
#' @return A 3D mesh of the roof model.
#'
#' @import rayvertex
#'
#' @export
#' @examples
#' #Generate vertices and holes
#' vertices = matrix(c(0,0, 7,0, 7,7, 0,7, 0,0), ncol = 2, byrow = TRUE)-3.5
#' hole_1 = matrix(c(1,1, 2,1, 2,2, 1,2, 1,1), ncol = 2, byrow = TRUE)[5:1,]-3.5
#' hole_2 = matrix(c(5,5, 6,5, 6,6, 5,6, 5,5), ncol = 2, byrow = TRUE)[5:1,]-3.5
#' skeleton = skeletonize(vertices, holes = list(hole_1, hole_2))
#' if(run_documentation()) {
#' plot_skeleton(skeleton)
#' }
#'
#' #Generate a roof model and specify the material
#' if(run_documentation()) {
#' library(rayrender)
#' library(rayvertex)
#' roof_model = generate_roof(skeleton, material = material_list(diffuse="purple"))
#' scene_base = xz_rect(xwidth=100,zwidth=100,
#' material=diffuse(color="grey20", checkercolor="white")) |>
#' add_object(sphere(y=8,z=10,x=-3,material=light(intensity=100))) |>
#' add_object(sphere(y=800,z=10,x=-3,radius=100,material=light(intensity=5)))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Change the maximum height of the roof
#' if(run_documentation()) {
#' roof_model = generate_roof(skeleton, max_height=5)
#' raymesh_model(roof_model, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' #Add a vertical_offset to the roof, without a base
#' if(run_documentation()) {
#' roof_model = generate_roof(skeleton, vertical_offset = 2, base = FALSE)
#' raymesh_model(roof_model, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,10,20), lookat=c(0,2,0), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Add a base
#' if(run_documentation()) {
#' roof_model = generate_roof(skeleton, vertical_offset = 2, base = TRUE)
#' raymesh_model(roof_model, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,10,20), lookat=c(0,2,0), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Change the base height (note that the vertical_offset is measured from the base, not from zero)
#' if(run_documentation()) {
#' roof_model = generate_roof(skeleton, vertical_offset = 2, base = TRUE, base_height=1)
#' raymesh_model(roof_model, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,10,20), lookat=c(0,2,0), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#'
#' # Skeletonize and turn an {sf} object into a roof
#' if(run_documentation()) {
#' us_states = spData::us_states
#' cali = us_states[us_states$NAME == "California",]
#' cali_skeleton = skeletonize(cali)
#' plot_skeleton(cali_skeleton)
#' roof_model_cali = generate_roof(cali_skeleton, max_height = 2) |>
#' center_mesh() |>
#' translate_mesh(c(0,1,0))
#'
#' raymesh_model(roof_model_cali, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' add_object(sphere(x=-10,z=-10,y=4,material=light(color="red", intensity=40))) |>
#' add_object(sphere(x=10,z=-10,y=4,material=light(color="orange", intensity=40))) |>
#' render_scene(lookfrom=c(0,10,-1), sample_method = "sobol_blue",
#' width=800,height=800,fov=0, ortho_dimensions=c(12,12))
#' }
generate_roof = function(skeleton, max_height = NA, vertical_offset = 0,
base = FALSE, base_height = 0, angle = 45,
sides = FALSE, double_sided = FALSE, scale_all_max = FALSE,
swap_yz = TRUE, progress = TRUE,
material = material_list(),
roof_material = NA,
verbose = FALSE) {
if(run_documentation() || !interactive()) {
progress = FALSE
}
if(!is.list(roof_material)) {
roof_material = material
}
if(inherits(skeleton, "rayskeleton_list")) {
pb = progress::progress_bar$new(
format = ":current/:total Generating roof [:bar] eta: :eta",
total = length(skeleton), clear = TRUE, width = 60)
meshlist = list()
counter = 1
vec_default = function(length_skeleton, value) {
if(length(value) == 1) {
return(rep(value,length(skeleton)))
} else {
stopifnot(length(value) == length_skeleton)
return(value)
}
}
len_s = length(skeleton)
vertical_offset = vec_default(len_s, vertical_offset)
base_height = vec_default(len_s, base_height)
angle = vec_default(len_s, angle)
if(!is.na(max_height)) {
if(length(max_height) == 1) {
if(scale_all_max) {
max_height = rep(max_height,length(skeleton))
} else {
#Calculate proportional maximum heights
single_max_heights = unlist(lapply(skeleton, \(x) max(x$nodes$time)))
max_height = single_max_heights/max(single_max_heights) * max_height
}
} else {
stopifnot(length(max_height) == length(skeleton))
}
}
for(j in seq_len(length(skeleton))) {
if(progress) {
pb$tick()
}
meshlist[[counter]] = generate_roof(skeleton[[j]],
base = base,
max_height = max_height[j],
sides = sides,
double_sided = double_sided,
base_height = base_height[j],
vertical_offset = vertical_offset[j],
angle = angle[j],
swap_yz = swap_yz,
material = material,
roof_material = roof_material,
progress = FALSE)
counter = counter + 1
}
return(scene_from_list(meshlist))
}
if(!inherits(skeleton, "rayskeleton")) {
stop("`skeleton` must be of class `rayskeleton`")
}
angle_slope = tanpi(angle/180)
if(is.na(max_height)) {
max_height = max(skeleton$nodes$time) * angle_slope
}
polygon_ind = convert_ss_to_polygons(skeleton, progress = progress)
nodes = skeleton$nodes
index_list = list()
for(i in seq_len(length(polygon_ind))) {
tmp_ind = polygon_ind[[i]]
tmp_poly = nodes[tmp_ind,2:3]
index_list[[i]] = matrix(tmp_ind[decido::earcut(tmp_poly)],byrow=TRUE,ncol=3)
}
indices_all = do.call("rbind",index_list)
xyz = nodes[,2:4]
colnames(xyz) = c("x","y","z")
if(!is.na(max_height)) {
stopifnot(max_height >= 0)
xyz[,3] = xyz[,3]/max(xyz[,3]) * max_height
}
original_verts = attr(skeleton,"original_vertices")
original_holes = attr(skeleton,"original_holes")
side_top = vertical_offset
mesh = construct_mesh(vertices = as.matrix(xyz),
indices = as.matrix(indices_all)-1) |>
translate_mesh(c(0,0,vertical_offset)) |>
set_material(roof_material)
if(double_sided) {
xyzflip = xyz
xyzflip[,3] = -xyzflip[,3]
indices_flip = indices_all[,3:1,drop=FALSE]
double_mesh = construct_mesh(vertices = as.matrix(xyzflip),
indices = as.matrix(indices_flip)-1) |>
set_material(roof_material)
}
if(base && !double_sided) {
if(length(original_holes) > 0) {
holes = c(0,utils::head(cumsum(unlist(lapply(original_holes,nrow))),-1)) + nrow(original_verts) + 1
hole_mat = do.call("rbind", original_holes)
original_verts_holes = rbind(original_verts, hole_mat)
} else {
holes = 0
original_verts_holes = original_verts
}
base_indices = matrix(decido::earcut(original_verts_holes, holes = holes), byrow = TRUE, ncol=3)
base_indices = base_indices[,3:1,drop = FALSE]
original_verts_base = cbind(original_verts_holes, rep(0, nrow(original_verts_holes)))
base_mesh = construct_mesh(vertices = as.matrix(original_verts_base),
indices = as.matrix(base_indices)-1) |>
set_material(material)
mesh = mesh |>
add_shape(base_mesh)
}
if(double_sided) {
mesh = add_shape(mesh, double_mesh)
}
if((sides || base || double_sided) &&
((!double_sided && side_top > 0) || (double_sided && side_top > 0))) {
if(!double_sided) {
side_mesh = extrude_sides(original_verts, original_holes, bottom = 0, top = side_top)
} else {
side_mesh = extrude_sides(original_verts, original_holes, bottom = -side_top, top = side_top)
}
side_mesh = side_mesh |>
set_material(material)
mesh = add_shape(mesh, side_mesh)
}
if(!is.na(base_height)) {
mesh = translate_mesh(mesh, c(0,0,base_height))
}
if(swap_yz) {
mesh = swap_yz(mesh)
}
print_time(verbose, "Generated mesh")
return(mesh)
}
#' Generate a beveled 3D polygon
#'
#' This function generates a beveled 3D polygon from a straight skeleton.
#'
#' @param skeleton Default `NULL`. A straight skeleton generated from the `skeletonize` function.
#' @param bevel_offsets Default `NULL`. The offset(s) of the bevel.
#' @param max_height Default `1`. The maximum height of the polygon.
#' @param bevel_heights Default is set to `bevel_offsets`. Numeric vector specifying the heights of the bevels. Must be of the same length as `bevel_offsets`.
#' @param set_max_height Default `FALSE`. A logical flag that controls whether to set the max height of the roof based on the `max_height` argument.
#' @param swap_yz Default `TRUE`. A logical flag that controls whether to swap the y and z coordinates in the resulting mesh. If `TRUE`, the y and z coordinates will be swapped.
#' @param progress Default `TRUE`. A logical flag to control whether a progress bar is displayed during roof generation.
#' @param vertical_offset Default `0`. The vertical offset of the polygon.
#' @param base Default `TRUE`. A logical flag that controls whether to generate the bottom of the polygon.
#' @param base_height Default `NA`. Height of the base, defaulting to `min(bevel_heights) + vertical_offset` .
#' @param sides Default `FALSE`. A logical flag on whether to draw the sides. This will automatically be set to `TRUE`
#' if `base = TRUE` and the `base_height` is less than `vertical_offset`.
#' @param scale_all_max Default `FALSE`. If passing in a list of multiple skeletons with polygons, whether to scale each polygon to the overall
#' max height, or whether to scale each max height to the maximum internal distance in the polygon.
#' @param verbose Default `FALSE`. A logical flag to control whether additional timing information should be displayed.
#' @param raw_offsets Default `FALSE`. A logical flag indicating whether the `bevel_offsets` are already in raw format and do not need to be multiplied by the maximum time of the skeleton.
#' @param raw_heights Default `FALSE`. A logical flag indicating whether the `bevel_heights` are already in raw format and do not need to be multiplied by the maximum time of the skeleton.
#' @param double_sided Default `FALSE`. A logical flag that controls whether the polygon should be double-sided.
#' @param return_skeleton_polygons Default `FALSE`. A logical flag that controls whether to return the skeleton polygons along with the 3D mesh.
#' @param material Default `material_list()`. Interface to set the color/appearance/material options for the resulting `ray_mesh` mesh.
#' @param bevel_material Default `NA`, uses the material specified in `material`. Interface to set the color/appearance/material options for the resulting `ray_mesh` bevel mesh.
#'
#' @return A 3D mesh of the beveled polygon model.
#'
#' @import rayvertex
#'
#' @export
#' @examples
#' #Generate vertices and holes
#' vertices = matrix(c(0,0, 7,0, 7,7, 0,7, 0,0), ncol = 2, byrow = TRUE)-3.5
#' hole_1 = matrix(c(1,1, 2,1, 2,2, 1,2, 1,1), ncol = 2, byrow = TRUE)[5:1,]-3.5
#' hole_2 = matrix(c(5,5, 6,5, 6,6, 5,6, 5,5), ncol = 2, byrow = TRUE)[5:1,]-3.5
#' skeleton = skeletonize(vertices, holes = list(hole_1, hole_2))
#' plot_skeleton(skeleton)
#'
#' #Generate a roof model and specify the material
#' if(run_documentation()) {
#' library(rayrender)
#' library(rayvertex)
#' scene_base = xz_rect(xwidth=100,zwidth=100,
#' material=diffuse(color="grey20", checkercolor="white")) |>
#' add_object(sphere(y=8,z=10,x=-3,material=light(intensity=100))) |>
#' add_object(sphere(y=800,z=10,x=-3,radius=100,material=light(intensity=5))) |>
#' add_object(sphere(x=-10,z=-10,y=5,material=light(color="red", intensity=40))) |>
#' add_object(sphere(x=10,z=-10,y=5,material=light(color="orange", intensity=40)))
#'
#' bevel = generate_bevel("angled", bevel_start = 0, bevel_end = 0.2, max_height=0.25)
#' roof_model = generate_beveled_polygon(skeleton,
#' bevel_offsets = bevel,
#' material = material_list(diffuse="purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Change the bevel to be circular
#' if(run_documentation()) {
#' bevel = generate_bevel("circular", bevel_start = 0, bevel_end = 0.2, max_height=0.25)
#' roof_model = generate_beveled_polygon(skeleton,
#' bevel_offsets = bevel,
#' material = material_list(diffuse="purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Change the bevel to type "bump", change the max height, and raise it off the surface
#' if(run_documentation()) {
#' bevel = generate_bevel("bump", bevel_start = 0, bevel_end = 0.4, max_height=0.25)
#' roof_model = generate_beveled_polygon(skeleton, base_height=1,
#' bevel_offsets = bevel,
#' material = material_list(diffuse="purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Generate a complex bevel and use the exact specified heights
#' if(run_documentation()) {
#' bevel = generate_complex_bevel(c("bump", "exp", "circular","step"),
#' bevel_start = c(0,0.3,0.7,0.95),
#' bevel_end = c(0.1,0.6,0.95,1),
#' reverse = c(F,F,T,F),
#' segment_height = c(0.25,0.5,0.5,4),
#' plot_bevel = TRUE)
#'
#' roof_model = generate_beveled_polygon(skeleton, vertical_offset=0.1,
#' bevel_offsets = bevel,
#' raw_heights = TRUE,
#' material = material_list(diffuse="purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(10,30,20), sample_method = "sobol_blue",
#' width=800,height=800,fov=0,ortho_dimensions=c(10,10))
#' }
#'
#' # Turn the polygon into a ziggurat, using the step bevel type
#' if(run_documentation()) {
#' offs = seq(0, 1, by = 0.05)
#' bevel = generate_complex_bevel("step",
#' bevel_start = offs[-length(offs)],
#' bevel_end = offs[-1],
#' segment_height = 0.2)
#'
#' roof_model = generate_beveled_polygon(skeleton, vertical_offset=0.2,
#' bevel_offsets = bevel,
#' raw_heights = TRUE,
#' material = material_list(diffuse = "purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom = c(10,30,20), sample_method = "sobol_blue",
#' width = 800, height = 800, fov = 0, ortho_dimensions = c(10,10))
#' }
#'
#' # Turn the polygon into a smooth wavy slide, taking advantage of vector recycling to flip/reverse
#' if(run_documentation()) {
#' offs = seq(0, 1, by = 0.1)
#' bevel = generate_complex_bevel("exp",
#' bevel_start = offs[-length(offs)],
#' bevel_end = offs[-1],
#' reverse = c(TRUE, FALSE),
#' flip = c(TRUE, FALSE),
#' segment_height = 0.25)
#'
#' roof_model = generate_beveled_polygon(skeleton, vertical_offset=0.2,
#' bevel_offsets = bevel,
#' raw_heights = TRUE,
#' material = material_list(diffuse = "purple"))
#'
#' raymesh_model(roof_model, override_material = FALSE) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom = c(10,30,20), sample_method = "sobol_blue",
#' width = 800, height = 800, fov = 0, ortho_dimensions = c(10,10))
#' }
#'
#' # Skeletonize and turn an {sf} object into a beveled polygon
#' if(run_documentation()) {
#' us_states = spData::us_states
#' texas = us_states[us_states$NAME == "Texas",]
#' texas_skeleton = skeletonize(texas)
#' plot_skeleton(texas_skeleton)
#'
#' bevel = generate_bevel("angled" , bevel_end=0.3, max_height = 0.3)
#' roof_model_texas = generate_beveled_polygon(texas_skeleton,
#' bevel_offsets = bevel,
#' material = material_list(diffuse = "purple")) |>
#' center_mesh() |>
#' translate_mesh(c(0,0.3,0))
#'
#' raymesh_model(roof_model_texas, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' add_object(sphere(x=-10,z=-10,y=5,material=light(color="red", intensity=40))) |>
#' add_object(sphere(x=10,z=-10,y=5,material=light(color="orange", intensity=40))) |>
#' render_scene(lookfrom=c(0,10,0),camera_up=c(0,0,1), sample_method = "sobol_blue",
#' width=800,height=800,fov=0, ortho_dimensions=c(15,15))
#' }
#'
#' # Generate a smooth bevel
#' if(run_documentation()) {
#' bevel = generate_bevel("exp", bevel_start = 0, bevel_end=0.5, max_height=2)
#' roof_model_texas = generate_beveled_polygon(texas_skeleton,
#' bevel_offsets = bevel,
#' material = material_list(diffuse = "purple")) |>
#' center_mesh() |>
#' translate_mesh(c(0,0.5,0))
#'
#' raymesh_model(roof_model_texas, material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' add_object(sphere(x=-10,z=-10,y=5,material=light(color="red", intensity=40))) |>
#' add_object(sphere(x=10,z=-10,y=5,material=light(color="orange", intensity=40))) |>
#' render_scene(lookfrom=c(0,10,0),camera_up=c(0,0,1), sample_method = "sobol_blue",
#' width=800,height=800,fov=0, ortho_dimensions=c(15,15))
#' }
generate_beveled_polygon = function(skeleton,
bevel_offsets = generate_bevel(),
bevel_heights = NULL,
set_max_height = FALSE,
max_height = NA,
vertical_offset = 0,
base = TRUE,
base_height = 0,
raw_offsets = FALSE,
raw_heights = FALSE,
swap_yz = TRUE,
progress = TRUE,
double_sided = FALSE,
sides = FALSE,
return_skeleton_polygons = FALSE,
scale_all_max = FALSE,
material = material_list(),
bevel_material = NA,
verbose = FALSE) {
if(run_documentation() || !interactive()) {
progress = FALSE
}
if(!is.list(bevel_material)) {
bevel_material = material
}
init_time()
if(inherits(skeleton, "rayskeleton_list")) {
pb = progress::progress_bar$new(
format = ":current/:total Generating bevel [:bar] eta: :eta",
total = length(skeleton), clear = TRUE, width = 60)
meshlist = list()
counter = 1
vec_default = function(length_skeleton, value) {
if(length(value) == 1) {
return(rep(value,length(skeleton)))
} else {
stopifnot(length(value) == length_skeleton)
return(value)
}
}
len_s = length(skeleton)
vertical_offset = vec_default(len_s, vertical_offset)
base_height = vec_default(len_s, base_height)
if(length(max_height) == 1) {
if(scale_all_max) {
max_height = rep(max_height,length(skeleton))
} else {
#Calculate proportional maximum heights
single_max_heights = unlist(lapply(skeleton, \(x) max(x$nodes$time)))
max_height = single_max_heights/max(single_max_heights) * max_height
}
}
for(j in seq_len(length(skeleton))) {
if(progress) {
pb$tick()
}
meshlist[[counter]] = generate_beveled_polygon(skeleton[[j]],
bevel_offsets = bevel_offsets,
bevel_heights = bevel_heights,
bevel_material = bevel_material,
base = base,
vertical_offset = vertical_offset[j],
max_height = max_height[j],
raw_offsets = raw_offsets,
raw_heights = raw_heights,
sides = sides,
double_sided = double_sided,
base_height = base_height[j],
set_max_height = set_max_height,
return_skeleton_polygons = return_skeleton_polygons,
swap_yz = swap_yz,
progress = progress,
material = material)
counter = counter + 1
}
if(return_skeleton_polygons) {
class(meshlist) = c("rayskeleton_list_polygons", "list")
return(meshlist)
}
return(scene_from_list(meshlist))
}
if(!inherits(skeleton, "rayskeleton")) {
stop("`skeleton` must be of class `rayskeleton`")
}
max_time = max(skeleton$links$destination_time)
if(is.list(bevel_offsets) &&
!is.null(bevel_offsets$x) &&
!is.null(bevel_offsets$y)) {
bevel_heights = bevel_offsets$y
bevel_offsets = bevel_offsets$x
}
if(!raw_offsets) {
if(any(bevel_offsets > 1 | bevel_offsets < 0)) {
stop("If using percentage offsets, all `bevel_offsets` must be between 0 and 1.")
}
bevel_offsets = bevel_offsets * max_time
}
non_zero_offsets = bevel_offsets[bevel_offsets > 0]
if(all(non_zero_offsets >= max_time)) {
message(sprintf("All `bevel_offset` greater than max offset in polygon of %f, calculating full polygon model",
max(skeleton$nodes[,4])))
interpolated_data = stats::approx(x = c(bevel_offsets),
y = c(bevel_heights),
xout = max_time)
max_height = max(interpolated_data$y)
return(generate_roof(skeleton,
max_height = max_height,
base_height = base_height,
vertical_offset = vertical_offset,
swap_yz = swap_yz,
base = base,
sides = sides,
double_sided = double_sided,
progress = progress,
roof_material = bevel_material,
material = material))
}
if(!raw_heights) {
bevel_heights = bevel_heights * max_time
}
bevel_heights = bevel_heights[order(bevel_offsets)]
bevel_offsets = bevel_offsets[order(bevel_offsets)]
height_range = range(bevel_heights)
height_range = height_range[2]-height_range[1]
if(set_max_height && !is.na(max_height)) {
stopifnot(max_height > 0)
min_height = min(bevel_heights)
offset_bevel_heights = bevel_heights - min_height
offset_bevel_heights = max_height * offset_bevel_heights / height_range + min_height
height_range = max_height
bevel_heights = offset_bevel_heights
}
stopifnot(length(vertical_offset) == 1)
stopifnot(vertical_offset >= 0)
bevel_heights = bevel_heights + vertical_offset
valid_bevels = bevel_offsets <= max_time & bevel_offsets > 0
zero_height_val = bevel_heights[1]
bevel_offsets_polys = bevel_offsets[valid_bevels]
bevel_heights_polys = bevel_heights[valid_bevels]
stopifnot(length(bevel_offsets_polys) == length(bevel_heights_polys))
#Remove extremely close offsets (usually arising from floating point error)
for(i in rev(seq_len(length(bevel_offsets_polys)))) {
all_other_offsets = bevel_offsets_polys[-i]
if(any(abs(bevel_offsets_polys[i] - all_other_offsets) < 1e-14, na.rm = TRUE)) {
bevel_offsets_polys[i] = NA
}
}
remove_close_vals = !is.na(bevel_offsets_polys)
bevel_offsets_polys = bevel_offsets_polys[remove_close_vals]
bevel_heights_polys = bevel_heights_polys[remove_close_vals]
#Generate new nodes
beveled_ss = generate_offset_links_nodes(skeleton, bevel_offsets_polys, verbose = verbose, progress = progress)
print_time(verbose, "Generated offset links")
reordered_new_ss = recalculate_ordered_ids(beveled_ss)
print_time(verbose, "Recalculated ordered IDs")
polygon_ind = convert_ss_to_polygons(reordered_new_ss, progress = progress)
print_time(verbose, "Converted to polygons")
nodes = reordered_new_ss$nodes
index_list = list()
for(i in seq_len(length(polygon_ind))) {
tmp_ind = polygon_ind[[i]]
tmp_poly = nodes[tmp_ind,2:3]
index_list[[i]] = matrix(tmp_ind[decido::earcut(tmp_poly)],byrow=TRUE,ncol=3)
}
print_time(verbose, "Triangulated polygons")
if(length(index_list) == 1) {
indices_all = matrix(unlist(index_list),ncol=3, byrow = TRUE)
} else {
indices_all = do.call("rbind",index_list)
}
xyz = nodes[,2:4]
new_xyz = xyz
bevel_offsets_polys = c(0,bevel_offsets_polys)
bevel_heights_polys = c(zero_height_val,bevel_heights_polys)
if(bevel_offsets_polys[length(bevel_offsets_polys)] != max_time) {
bevel_offsets_with_max = c(bevel_offsets_polys, max_time)
bevel_heights_with_max = c(bevel_heights_polys, max(bevel_heights_polys))
} else {
bevel_offsets_with_max = bevel_offsets_polys
bevel_heights_with_max = bevel_heights_polys
}
side_top = bevel_heights_with_max[1]
if(return_skeleton_polygons) {
attr(reordered_new_ss, "polygons") = polygon_ind
#These offsets should always be normalized
attr(reordered_new_ss, "bevel_offsets") = bevel_offsets_with_max / max_time
attr(reordered_new_ss, "raw_offsets") = raw_offsets
class(reordered_new_ss) = c("rayskeleton_polygon")
return(reordered_new_ss)
}
#Need to account for non-zero offsets--not all start at 0
new_xyz[,3] = stats::approx(x=bevel_offsets_with_max,y=bevel_heights_with_max, xout = xyz[,3], rule=2)$y
xyz = new_xyz
colnames(xyz) = c("x","y","z")
original_verts = attr(skeleton,"original_vertices")
original_holes = attr(skeleton,"original_holes")
mesh = construct_mesh(vertices = as.matrix(xyz),
indices = as.matrix(indices_all)-1) |>
set_material(bevel_material)
if(double_sided) {
xyzflip = xyz
xyzflip[,3] = -xyzflip[,3]
indices_flip = indices_all[,3:1,drop=FALSE]
double_mesh = construct_mesh(vertices = as.matrix(xyzflip),
indices = as.matrix(indices_flip)-1) |>
set_material(bevel_material)
}
if(base && !double_sided) {
if(length(original_holes) > 0) {
holes = c(0,utils::head(cumsum(unlist(lapply(original_holes,nrow))),-1)) + nrow(original_verts) + 1
hole_mat = do.call("rbind", original_holes)
original_verts_holes = rbind(original_verts, hole_mat)
} else {
holes = 0
original_verts_holes = original_verts
}
base_indices = matrix(decido::earcut(original_verts_holes, holes = holes), byrow = TRUE, ncol=3)
base_indices = base_indices[,3:1,drop = FALSE]
original_verts_base = cbind(original_verts_holes, rep(0, nrow(original_verts_holes)))
base_mesh = construct_mesh(vertices = as.matrix(original_verts_base),
indices = as.matrix(base_indices)-1) |>
set_material(material)
mesh = mesh |>
add_shape(base_mesh)
}
if(double_sided) {
mesh = add_shape(mesh, double_mesh)
}
if((sides || base || double_sided) &&
((!double_sided && side_top > 0) || (double_sided && side_top > 0))) {
if(!double_sided) {
side_mesh = extrude_sides(original_verts, original_holes, bottom = 0, top = side_top)
} else {
side_mesh = extrude_sides(original_verts, original_holes, bottom = -side_top, top = side_top)
}
side_mesh = side_mesh |>
set_material(material)
mesh = add_shape(mesh, side_mesh)
}
if(swap_yz) {
mesh = swap_yz(mesh)
}
if(!is.na(base_height)) {
if(swap_yz) {
mesh = translate_mesh(mesh, c(0,base_height,0))
} else {
mesh = translate_mesh(mesh, c(0,0,base_height))
}
}
print_time(verbose, "Generated mesh")
return(mesh)
}
#' Change an existing polygon bevel's bevel profile.
#'
#' This function generates a beveled 3D polygon model from the modified straight skeleton with
#' pre-existing polygons generated from the `generate_beveled_polygon` function when
#' `return_skeleton_polygons = TRUE`.
#'
#' @param skeleton_polygons Default `NULL`. A straight skeleton generated from the `generate_beveled_polygon` function when
#' `return_skeleton_polygons = TRUE`.
#' @param bevel_offsets Default `NULL`. The offset(s) of the bevel.
#' @param bevel_heights Default is set to `bevel_offsets`. Numeric vector specifying the heights of the bevels. Must be of the same length as `bevel_offsets`.
#' @param max_height Default `1`. The maximum height of the polygon.
#' @param set_max_height Default `FALSE`. A logical flag that controls whether to set the max height of the polygon based on the `max_height` argument.
#' @param swap_yz Default `TRUE`. A logical flag that controls whether to swap the y and z coordinates in the resulting mesh.
#' If `TRUE`, the y and z coordinates will be swapped.
#' @param sides Default `FALSE`. A logical flag on whether to draw the sides. This will automatically be set to `TRUE`
#' if `base = TRUE` and the `base_height` is less than `vertical_offset`.
#' @param vertical_offset Default `0`. The vertical offset of the polygon.
#' @param base Default `TRUE`. A logical flag that controls whether to generate the bottom of the polygon.
#' @param base_height Default `NA`. Height of the base, defaulting to the `min(bevel_heights) + vertical_offset` .
#' @param progress Default `TRUE`. Whether to display a progress bar.
#' @param verbose Default `FALSE`. A logical flag to control whether additional timing information should be displayed.
#' @param scale_all_max Default `FALSE`. If passing in a list of multiple skeletons with polygons, whether to scale each polygon to the overall
#' max height, or whether to scale each max height to the maximum internal distance in the polygon.
#' @param raw_offsets Default `FALSE`. A logical flag indicating whether the `bevel_offsets` are already in raw format and do not need to be multiplied by the maximum time of the skeleton.
#' @param raw_heights Default `FALSE`. A logical flag indicating whether the `bevel_heights` are already in raw format and do not need to be multiplied by the maximum time of the skeleton.
#' @param double_sided Default `FALSE`. A logical flag that controls whether the polygon should be double-sided.
#' @param material Default `material_list()`. Interface to set the color/appearance/material options for the resulting `ray_mesh` mesh.
#' @param bevel_material Default `NA`, uses the material specified in `material`. Interface to set the color/appearance/material options for the resulting `ray_mesh` bevel mesh.
#'
#' @return A 3D mesh of the beveled polygon model.
#'
#' @import rayvertex
#'
#' @export
#' @examples
#' # Skeletonize a complex {sf} object and set return_skeleton_polygons = TRUE in
#' # generate_beveled_polygon(). This returns skeleton object with polygons included, which
#' # allows for quickly generating 3D models with different bevels.
#' if(run_documentation()) {
#' library(rayrender)
#' library(rayvertex)
#' us_states = spData::us_states
#' cali = us_states[us_states$NAME == "California",]
#' cali_skeleton = skeletonize(cali)
#' plot_skeleton(cali_skeleton)
#' # We add manual offsets to ensure that the polygon can be morphed all along its interior
#' bevel = generate_bevel(manual_offsets = seq(0,1,by=0.01), max_height=0.5)
#' bevel_model_cali = generate_beveled_polygon(cali_skeleton,
#' bevel_offsets = bevel,
#' return_skeleton_polygons = TRUE)
#'
#' bevel_new = change_polygon_bevel(bevel_model_cali,
#' bevel_offsets = generate_bevel(max_height=0.5,
#' bevel_end=0.5)) |>
#' center_mesh()
#'
#' scene_base = xz_rect(xwidth=100,zwidth=100,
#' material=diffuse(color="grey20", checkercolor="white")) |>
#' add_object(sphere(y=8,z=10,x=-3,material=light(intensity=100))) |>
#' add_object(sphere(y=800,z=10,x=-3,radius=100,material=light(intensity=5)))
#'
#' raymesh_model(bevel_new, y=0.5, override_material = TRUE,
#' material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(0,30,-10), sample_method = "sobol_blue",clamp_value = 10,
#' width=800,height=800,fov=0,ortho_dimensions=c(12,12))
#' }
#' # Change to a smooth bevel
#' if(run_documentation()) {
#' new_bevel = generate_bevel("circular", bevel_start = 0, bevel_end=1)
#' bevel_new = change_polygon_bevel(bevel_model_cali,
#' bevel_offsets = new_bevel, solid ) |>
#' center_mesh()
#' raymesh_model(bevel_new, override_material = TRUE, y=1,material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(0,30,-10), sample_method = "sobol_blue",clamp_value = 10,
#' width=800,height=800,fov=0,ortho_dimensions=c(12,12))
#' }
#'
#' # Make a complex bevel
#' if(run_documentation()) {
#' complex_coords = generate_complex_bevel(
#' bevel_type = c("angled","flat", "angled", "flat"),
#' bevel_start = head(seq(0,1,by=0.05),-1),
#' bevel_end = tail(seq(0,1,by=0.05),-1),
#' overall_height = 1,
#' angle = c(45,45,15,15),
#' reverse = c(FALSE, FALSE,TRUE,TRUE),
#' plot_bevel = TRUE
#' )
#' bevel_new = change_polygon_bevel(bevel_model_cali,
#' bevel_offsets = complex_coords) |>
#' center_mesh()
#' raymesh_model(bevel_new, override_material = TRUE, y=1,material = diffuse(color="purple")) |>
#' add_object(scene_base) |>
#' render_scene(lookfrom=c(0,30,-20), sample_method = "sobol_blue",clamp_value = 10,
#' width=800,height=800,fov=0,ortho_dimensions=c(12,12))
#' }
#'
#' # Quickly generate new bevels to inflate California like a balloon using the arctan function.
#' if(run_documentation()) {
#' inflate_california = function(magnitudes) {
#' for(val in magnitudes) {
#' bevel_new = change_polygon_bevel(bevel_model_cali,
#' bevel_heights = 1/2*atan(seq(0,val,length.out=100)),
#' bevel_offsets = seq(0,1, length.out=100),
#' base = TRUE) |>
#' translate_mesh(c(-120.49,0,-38.72))
#' raymesh_model(bevel_new, y = 0, override_material = TRUE,
#' material = glossy(color="darkred")) |>
#' add_object(scene_base) |>
#' add_object(sphere(x=-30,z=30,y=18,radius=30,material=light(color="white", intensity=5))) |>
#' render_scene(lookfrom=c(-1, 28, -20.32), lookat=c(-1, 1.46, -2),
#' sample_method = "sobol_blue", clamp_value = 10,
#' width=800,height=800,fov=20,samples=256)
#' }
#' }
#' inflate_california(c(1,4,16,64))
#' }
change_polygon_bevel = function(skeleton_polygons,
bevel_offsets = NULL,
bevel_heights = NULL,
set_max_height = FALSE,
max_height = 1,
vertical_offset = 0,
base = TRUE,
base_height = NA,
raw_offsets = FALSE,
raw_heights = FALSE,
swap_yz = TRUE,
progress = TRUE,
sides = FALSE,
double_sided = FALSE,
scale_all_max = FALSE,
material = material_list(),
bevel_material = NA,
verbose = FALSE) {
if(run_documentation() || !interactive()) {
progress = FALSE
}
if(!is.list(bevel_material)) {
bevel_material = material
}
init_time()
if(inherits(skeleton_polygons, "rayskeleton_list_polygons")) {
pb = progress::progress_bar$new(
format = ":current/:total Generating polygon [:bar] eta: :eta",
total = length(skeleton_polygons), clear = TRUE, width = 60)
meshlist = list()
counter = 1
if(length(vertical_offset) == 1) {
vertical_offset = rep(vertical_offset,length(skeleton_polygons))
}
if(length(max_height) == 1) {
if(scale_all_max) {
max_height = rep(max_height,length(skeleton_polygons))
} else {
#Calculate proportional maximum heights
single_max_heights = unlist(lapply(skeleton_polygons, \(x) max(x$nodes$time)))
max_height = single_max_heights/max(single_max_heights) * max_height
}
}
for(j in seq_len(length(skeleton_polygons))) {
if(progress) {
pb$tick()
}
meshlist[[counter]] = change_polygon_bevel(skeleton_polygons[[j]],
bevel_offsets = bevel_offsets,
bevel_heights = bevel_heights,
base = base,
vertical_offset = vertical_offset[j],
max_height = max_height[j],
raw_offsets = raw_offsets,
raw_heights = raw_heights,
sides = sides,
progress = FALSE,
double_sided = double_sided,
base_height = base_height,
bevel_material = bevel_material,
material = material,
swap_yz = swap_yz)
counter = counter + 1
}
return(scene_from_list(meshlist))
}
if(inherits(skeleton_polygons, "ray_mesh")) {
return(skeleton_polygons)
}
if(!inherits(skeleton_polygons, "rayskeleton_polygon")) {
stop("`skeleton_polygon` must be of class `rayskeleton_polygon`")
}
max_time = max(skeleton_polygons$links$destination_time)
if(is.list(bevel_offsets) &&
!is.null(bevel_offsets$x) &&
!is.null(bevel_offsets$y)) {
bevel_heights = bevel_offsets$y
bevel_offsets = bevel_offsets$x
# if(set_max_height) {
# warning("`set_max_height` is ignored when passing in bevel curve--set the max height when generating the bevel.")
# set_max_height = FALSE
# }
}
if(!raw_offsets) {
if(any(bevel_offsets > 1 | bevel_offsets < 0)) {
stop("If using percentage offsets, all `bevel_offsets` must be between 0 and 1.")
}
bevel_offsets_pct = bevel_offsets
bevel_offsets = bevel_offsets * max_time
} else {
bevel_offsets_pct = bevel_offsets / max_time
}
#Offsets should be in units of the coordinate system at this point
non_zero_offsets = bevel_offsets[bevel_offsets > 0]
if(all(non_zero_offsets >= max_time)) {
interpolated_data = stats::approx(x = bevel_offsets, y = bevel_heights,
xout = c(0,max_time))
bevel_offsets = c(0,max_time)
bevel_offsets_pct = c(0,1)
bevel_heights = interpolated_data$y
}
if(!raw_heights) {
bevel_heights = bevel_heights * max_time
}
if(is.na(base_height)) {
height_range = range(bevel_heights)
if(base || double_sided) {
min_offset = (height_range[2]-height_range[1]) * 1e-6
} else {
min_offset = 0
}
base_height = min(bevel_heights) + min_offset + vertical_offset
}
stopifnot(length(vertical_offset) == 1)
reordered_new_ss = skeleton_polygons
old_bevel_offsets = unique(attr(reordered_new_ss, "bevel_offsets"))
polygon_ind = attr(reordered_new_ss, "polygons")
last_height = bevel_heights[length(bevel_heights)]
if(bevel_offsets_pct[length(bevel_offsets_pct)] != 1 ) {
bevel_offsets_pct = c(bevel_offsets_pct,1)
bevel_heights_with_last = c(bevel_heights,last_height)
} else {
bevel_heights_with_last = bevel_heights
}
interpolated_data = stats::approx(x = bevel_offsets_pct, y = bevel_heights_with_last, xout = old_bevel_offsets,
rule = 2)
bevel_heights = interpolated_data$y
bevel_offsets = old_bevel_offsets * max_time
bevel_offsets = bevel_offsets[order(bevel_offsets)]
bevel_heights = bevel_heights[order(bevel_offsets)]
height_range = range(bevel_heights)
height_range = height_range[2]-height_range[1]
if(set_max_height) {
min_height = min(bevel_heights)
offset_bevel_heights = bevel_heights - min_height
offset_bevel_heights = max_height * offset_bevel_heights / height_range + min_height
height_range = max_height
bevel_heights = offset_bevel_heights
}
stopifnot(length(vertical_offset) == 1)
stopifnot(vertical_offset >= 0)
bevel_heights = bevel_heights + vertical_offset
valid_bevels = bevel_offsets <= max_time & bevel_offsets > 0
zero_height_val = bevel_heights[1]
bevel_offsets_valid = bevel_offsets[valid_bevels]
bevel_heights_valid = bevel_heights[valid_bevels]
stopifnot(length(bevel_offsets_valid) == length(bevel_heights_valid))
#Remove extremely close offsets (usually arising from floating point error)
for(i in rev(seq_len(length(bevel_offsets_valid)))) {
all_other_offsets = bevel_offsets_valid[-i]
if(any(abs(bevel_offsets_valid[i] - all_other_offsets) < 1e-14, na.rm = TRUE)) {
bevel_offsets_valid[i] = NA
}
}
remove_close_vals = !is.na(bevel_offsets_valid)
bevel_offsets_valid = bevel_offsets_valid[remove_close_vals]
bevel_heights_valid = bevel_heights_valid[remove_close_vals]
nodes = reordered_new_ss$nodes
index_list = list()
for(i in seq_len(length(polygon_ind))) {
tmp_ind = polygon_ind[[i]]
tmp_poly = nodes[tmp_ind,2:3]
index_list[[i]] = matrix(tmp_ind[decido::earcut(tmp_poly)], byrow = TRUE, ncol = 3)
}
indices_all = do.call("rbind",index_list)
xyz = nodes[,2:4]