R/remove_duplicate_materials.R

Defines functions remove_duplicate_materials

Documented in remove_duplicate_materials

#'@title Remove Duplicates
#'
#'@param scene The scene
#'@return Scene with shape added.
#'
#'@keywords internal
remove_duplicate_materials = function(scene) {
  
  #Generate unique set of materials in scene
  scene_material_hashes = scene$material_hashes
  unique_materials = unique(scene_material_hashes)
  
  #Allocate new_id vector
  new_ids = rep(0L,length(scene_material_hashes))
  
  #Generate vector for all old non-unique IDs (zero indexed)
  old_ids = seq_len(length(scene_material_hashes)) - 1L
  new_mat = list()
  
  #Go through each hash and determine which entry it is in the unique_material vector
  for(i in seq_len(length(scene_material_hashes))) {
    new_ids[i] = min(which(scene_material_hashes[i] == unique_materials)) - 1L
  }
  for(i in seq_len(length(scene$shapes))) {
    scene$shapes[[i]]$material_ids[scene$shapes[[i]]$material_ids == -1] = 0
    scene$shapes[[i]]$material_ids = new_ids[scene$shapes[[i]]$material_ids + 1L]
  }
  unique_ids = unique(new_ids)
  new_mat = list()
  for(i in seq_len(length(unique_ids))) {
    new_mat[[i]] = scene$materials[[min(which(new_ids == (i-1L)))]]
  }
  scene$materials = new_mat
  scene$material_hashes = unique_materials
  class(scene) = c("ray_mesh", "list")
  
  return(scene)
}

Try the rayvertex package in your browser

Any scripts or data that you put into this service are public.

rayvertex documentation built on July 9, 2023, 5:52 p.m.