use .entry().or_insert_with() pattern everywhere

This commit is contained in:
Quaternions 2024-02-16 06:04:24 -08:00
parent b1d860edf1
commit 4a53040011
2 changed files with 23 additions and 56 deletions

View File

@ -218,11 +218,8 @@ impl GraphicsState{
color:GraphicsModelColor4::new(model.color), color:GraphicsModelColor4::new(model.color),
}; };
//get or create owned mesh map //get or create owned mesh map
let owned_mesh_map=if let Some(owned_mesh_map)=owned_mesh_id_from_mesh_id_render_config_id.get(&model.mesh){ let owned_mesh_map=owned_mesh_id_from_mesh_id_render_config_id
//the mesh has already been split into a set of unique renderconfig meshes .entry(model.mesh).or_insert_with(||{
//simply add one instance to each of them
owned_mesh_map
}else{
let mut owned_mesh_map=HashMap::new(); let mut owned_mesh_map=HashMap::new();
//add mesh if renderid never before seen for this model //add mesh if renderid never before seen for this model
//add instance //add instance
@ -231,12 +228,10 @@ impl GraphicsState{
if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){ if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){
for graphics_group in mesh.graphics_groups.iter(){ for graphics_group in mesh.graphics_groups.iter(){
//get or create owned mesh //get or create owned mesh
let owned_mesh_id=if let Some(&owned_mesh_id)=owned_mesh_map.get(&graphics_group.render){ let owned_mesh_id=owned_mesh_map
owned_mesh_id .entry(graphics_group.render).or_insert_with(||{
}else{
//create //create
let owned_mesh_id=IndexedGraphicsMeshOwnedRenderConfigId::new(unique_render_config_models.len() as u32); let owned_mesh_id=IndexedGraphicsMeshOwnedRenderConfigId::new(unique_render_config_models.len() as u32);
owned_mesh_map.insert(graphics_group.render,owned_mesh_id);
unique_render_config_models.push(IndexedGraphicsMeshOwnedRenderConfig{ unique_render_config_models.push(IndexedGraphicsMeshOwnedRenderConfig{
unique_pos:mesh.unique_pos.iter().map(|&v|*Into::<glam::Vec3>::into(v).as_ref()).collect(), unique_pos:mesh.unique_pos.iter().map(|&v|*Into::<glam::Vec3>::into(v).as_ref()).collect(),
unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(), unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(),
@ -248,7 +243,7 @@ impl GraphicsState{
instances:Vec::new(), instances:Vec::new(),
}); });
owned_mesh_id owned_mesh_id
}; });
let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap(); let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap();
match &mut owned_mesh.polys{ match &mut owned_mesh.polys{
model::PolygonGroup::PolygonList(polygon_list)=>polygon_list.extend( model::PolygonGroup::PolygonList(polygon_list)=>polygon_list.extend(
@ -262,9 +257,8 @@ impl GraphicsState{
} }
} }
} }
owned_mesh_id_from_mesh_id_render_config_id.insert(model.mesh,owned_mesh_map); owned_mesh_map
owned_mesh_id_from_mesh_id_render_config_id.get(&model.mesh).unwrap() });
};
for owned_mesh_id in owned_mesh_map.values(){ for owned_mesh_id in owned_mesh_map.values(){
let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap(); let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap();
let render_config=&map.render_configs[owned_mesh.render_config.get() as usize]; let render_config=&map.render_configs[owned_mesh.render_config.get() as usize];
@ -291,24 +285,14 @@ impl GraphicsState{
continue; continue;
} }
//populate hashmap //populate hashmap
let unique_color=if let Some(unique_color)=unique_texture_color.get_mut(&model.render_config){ let unique_color=unique_texture_color
unique_color .entry(model.render_config)
}else{ .or_insert_with(||HashMap::new());
//make new hashmap
let unique_color=HashMap::new();
unique_texture_color.insert(model.render_config,unique_color);
unique_texture_color.get_mut(&model.render_config).unwrap()
};
//separate instances by color //separate instances by color
for (instance_id,instance) in model.instances.iter().enumerate(){ for (instance_id,instance) in model.instances.iter().enumerate(){
let model_instance_list=if let Some(model_instance_list)=unique_color.get_mut(&instance.color){ let model_instance_list=unique_color
model_instance_list .entry(instance.color)
}else{ .or_insert_with(||Vec::new());
//make new hashmap
let model_instance_list=Vec::new();
unique_color.insert(instance.color.clone(),model_instance_list);
unique_color.get_mut(&instance.color).unwrap()
};
//add model instance to list //add model instance to list
model_instance_list.push((model_id,instance_id)); model_instance_list.push((model_id,instance_id));
} }
@ -344,46 +328,34 @@ impl GraphicsState{
let map_pos_id:Vec<PositionId>=model.unique_pos.iter().map(|untransformed_pos|{ let map_pos_id:Vec<PositionId>=model.unique_pos.iter().map(|untransformed_pos|{
let pos=instance.transform.transform_point3(glam::Vec3::from_array(untransformed_pos.clone())).to_array(); let pos=instance.transform.transform_point3(glam::Vec3::from_array(untransformed_pos.clone())).to_array();
let h=bytemuck::cast::<[f32;3],[u32;3]>(pos); let h=bytemuck::cast::<[f32;3],[u32;3]>(pos);
PositionId::new((if let Some(&pos_id)=pos_id_from.get(&h){ PositionId::new(*pos_id_from.entry(h).or_insert_with(||{
pos_id
}else{
let pos_id=unique_pos.len(); let pos_id=unique_pos.len();
unique_pos.push(pos); unique_pos.push(pos);
pos_id_from.insert(h,pos_id);
pos_id pos_id
}) as u32) }) as u32)
}).collect(); }).collect();
let map_tex_id:Vec<TextureCoordinateId>=model.unique_tex.iter().map(|&tex|{ let map_tex_id:Vec<TextureCoordinateId>=model.unique_tex.iter().map(|&tex|{
let h=bytemuck::cast::<[f32;2],[u32;2]>(tex); let h=bytemuck::cast::<[f32;2],[u32;2]>(tex);
TextureCoordinateId::new((if let Some(&tex_id)=tex_id_from.get(&h){ TextureCoordinateId::new(*tex_id_from.entry(h).or_insert_with(||{
tex_id
}else{
let tex_id=unique_tex.len(); let tex_id=unique_tex.len();
unique_tex.push(tex); unique_tex.push(tex);
tex_id_from.insert(h,tex_id);
tex_id tex_id
}) as u32) }) as u32)
}).collect(); }).collect();
let map_normal_id:Vec<NormalId>=model.unique_normal.iter().map(|untransformed_normal|{ let map_normal_id:Vec<NormalId>=model.unique_normal.iter().map(|untransformed_normal|{
let normal=(instance.normal_transform*glam::Vec3::from_array(untransformed_normal.clone())).to_array(); let normal=(instance.normal_transform*glam::Vec3::from_array(untransformed_normal.clone())).to_array();
let h=bytemuck::cast::<[f32;3],[u32;3]>(normal); let h=bytemuck::cast::<[f32;3],[u32;3]>(normal);
NormalId::new((if let Some(&normal_id)=normal_id_from.get(&h){ NormalId::new(*normal_id_from.entry(h).or_insert_with(||{
normal_id
}else{
let normal_id=unique_normal.len(); let normal_id=unique_normal.len();
unique_normal.push(normal); unique_normal.push(normal);
normal_id_from.insert(h,normal_id);
normal_id normal_id
}) as u32) }) as u32)
}).collect(); }).collect();
let map_color_id:Vec<ColorId>=model.unique_color.iter().map(|&color|{ let map_color_id:Vec<ColorId>=model.unique_color.iter().map(|&color|{
let h=bytemuck::cast::<[f32;4],[u32;4]>(color); let h=bytemuck::cast::<[f32;4],[u32;4]>(color);
ColorId::new((if let Some(&color_id)=color_id_from.get(&h){ ColorId::new(*color_id_from.entry(h).or_insert_with(||{
color_id
}else{
let color_id=unique_color.len(); let color_id=unique_color.len();
unique_color.push(color); unique_color.push(color);
color_id_from.insert(h,color_id);
color_id color_id
}) as u32) }) as u32)
}).collect(); }).collect();
@ -396,12 +368,9 @@ impl GraphicsState{
normal:map_normal_id[unmapped_vertex.normal.get() as usize], normal:map_normal_id[unmapped_vertex.normal.get() as usize],
color:map_color_id[unmapped_vertex.color.get() as usize], color:map_color_id[unmapped_vertex.color.get() as usize],
}; };
VertexId::new((if let Some(&vertex_id)=vertex_id_from.get(&vertex){ VertexId::new(*vertex_id_from.entry(vertex.clone()).or_insert_with(||{
vertex_id
}else{
let vertex_id=unique_vertices.len(); let vertex_id=unique_vertices.len();
unique_vertices.push(vertex.clone()); unique_vertices.push(vertex);
vertex_id_from.insert(vertex,vertex_id);
vertex_id vertex_id
}) as u32) }) as u32)
}).collect(); }).collect();
@ -447,9 +416,7 @@ impl GraphicsState{
for end_index in 2..poly.len(){ for end_index in 2..poly.len(){
for index in [0,end_index-1,end_index]{ for index in [0,end_index-1,end_index]{
let vertex_index=poly[index]; let vertex_index=poly[index];
if let Some(&i)=index_from_vertex.get(&vertex_index){ indices.push(*index_from_vertex.entry(vertex_index).or_insert_with(||{
indices.push(i);
}else{
let i=vertices.len(); let i=vertices.len();
let vertex=&model.unique_vertices[vertex_index.get() as usize]; let vertex=&model.unique_vertices[vertex_index.get() as usize];
vertices.push(GraphicsVertex{ vertices.push(GraphicsVertex{
@ -458,9 +425,8 @@ impl GraphicsState{
normal:model.unique_normal[vertex.normal.get() as usize], normal:model.unique_normal[vertex.normal.get() as usize],
color:model.unique_color[vertex.color.get() as usize], color:model.unique_color[vertex.color.get() as usize],
}); });
index_from_vertex.insert(vertex_index,i); i
indices.push(i); }));
}
} }
} }
} }

View File

@ -992,6 +992,7 @@ impl PhysicsContext{
let mut used_meshes=Vec::new(); let mut used_meshes=Vec::new();
let mut physics_mesh_id_from_model_mesh_id=HashMap::<MeshId,PhysicsMeshId>::new(); let mut physics_mesh_id_from_model_mesh_id=HashMap::<MeshId,PhysicsMeshId>::new();
self.data.models.models=map.models.iter().enumerate().filter_map(|(model_id,model)|{ self.data.models.models=map.models.iter().enumerate().filter_map(|(model_id,model)|{
//TODO: use .entry().or_insert_with(||{
let attr_id=if let Some(&attr_id)=physics_attr_id_from_model_attr_id.get(&model.attributes){ let attr_id=if let Some(&attr_id)=physics_attr_id_from_model_attr_id.get(&model.attributes){
attr_id attr_id
}else{ }else{