TMFMesh

Struct TMFMesh 

Source
pub struct TMFMesh { /* private fields */ }
Expand description

Representation of a TMF mesh. Can be loaded from disk, imported from diffrent format, saved to disk, and exported to a diffrent format, or created using special functions. Any mesh created at run time should but does not have to be checked before saving with Self::verify call. If the mesh is known to be OK before saving this step can be skipped(even tough it is still advised).

Implementations§

Source§

impl TMFMesh

Source

pub fn reorder_data(&mut self)

Reorders mesh data to be more efficiently laid out. Reduces mesh size slightly. Not needed if Self::unify_index_data was called before.

§Example
let old_vertices = tmf_mesh.get_vertices().unwrap();
let old_vertex_triangles = tmf_mesh.get_vertex_triangles().unwrap();
// Reordering data changes how it is laid out.
tmf_mesh.reorder_data();
let new_vertices = tmf_mesh.get_vertices().unwrap();
let new_vertex_triangles = tmf_mesh.get_vertex_triangles().unwrap();
// New data is not the same as the old data
assert_ne!(old_vertices,new_vertices);
assert_ne!(old_vertex_triangles,new_vertex_triangles);
Source

pub fn unify_index_data(&mut self)

Changes mesh data to make all index arrays(e.g. vertex_triangle_array,normal_triangle_array, etc.) exactly the same. Does not support custom index segments, and will leave them unaffected. Very often drastically reduces mesh size.

§Example
// If all index arrays are different at first
assert_ne!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_normal_triangles());
assert_ne!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_uv_triangles());
assert_ne!(tmf_mesh.get_normal_triangles(),tmf_mesh.get_uv_triangles());
// Ensure mesh all index arrays in mess are exactly the same.
tmf_mesh.unify_index_data();
// All index arrays are now exactly the same.
assert_eq!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_normal_triangles());
assert_eq!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_uv_triangles());
assert_eq!(tmf_mesh.get_normal_triangles(),tmf_mesh.get_uv_triangles());
// If index arrays are exactly the same, only cost of `unify_index_data` call is the
// cost of checking that the index arrays are exactly the same.
tmf_mesh.unify_index_data();
Source

pub fn set_vertices<T: Into<Vec<Vector3>>>( &mut self, vertices: T, ) -> Option<Vec<Vector3>>

Sets mesh vertex array and returns old vertex array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call Self::verify before saving.

§Examples
 // Set the vertices of the mesh
 mesh.set_vertices(vertices);
 // Change the vertices for some other vertices...
 let old_vertices = mesh.set_vertices(new_vertices).expect("Mesh had no vertices!");
 // ... and the do something with old vertices
 do_something(&old_vertices);
Source

pub fn set_normals<T: Into<Vec<Vector3>>>( &mut self, normals: T, ) -> Option<Vec<Vector3>>

Sets mesh normal array and returns old normal array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call verify before saving.

§Examples
 // Set the normals of the mesh
 mesh.set_normals(normals);
 // Change the normals  of this mesh for some other normals...
 let old_normals = mesh.set_normals(new_normals).expect("Mesh had no normals!");
 // ... and the do something with old normals
 do_something(&old_normals);
Source

pub fn set_tangents<T: Into<Vec<Tangent>>>( &mut self, tangents: T, ) -> Option<Vec<Tangent>>

Sets mesh tangent array and returns old tangent array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call verify before saving.

§Examples
 // Set the tangents of the mesh
 mesh.set_tangents(tangents);
 // Change the tangents  of this mesh for some other tangents...
 let old_tangents = mesh.set_tangents(new_tangents).expect("Mesh had no tangents!");
 // ... and the do something with old tangents
 do_something(&old_tangents);
Source

pub fn set_uvs<T: Into<Vec<Vector2>>>(&mut self, uvs: T) -> Option<Vec<Vector2>>

Sets mesh uv array and returns old uv array if present. New mesh daata is not checked during this function call, so to ensure mesh is valid call Self::verify before saving.

§Examples
 // Set the uvs of the mesh
 mesh.set_uvs(uvs);
 // Change the uvs  of this mesh for some other normals...
 let old_uvs = mesh.set_uvs(new_uvs).expect("Mesh had no uvs!");
 // ... and the do something with old uvs
 do_something(&old_uvs);
Source

pub fn set_vertex_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>

Sets vertex index array to triangles and returns old triangles if present.

§Example
 mesh.set_vertex_triangles(triangles);
Source

pub fn set_normal_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>

Sets normal index array to triangles and returns old triangles if present.

§Example
 mesh.set_normal_triangles(triangles);
Source

pub fn set_uv_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>

Sets uv index array to triangles and returns old triangles if present.

§Example
 mesh.set_uv_triangles(triangles);
Source

pub fn set_tangent_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>

Sets tangent index array to triangles and returns old triangles if present.

§Example
 mesh.set_tangent_triangles(triangles);
Source

pub fn get_vertices(&self) -> Option<&[Vector3]>

Gets the vertex array of this TMFMesh.

§Example
 let vertices = mesh.get_vertices();
Source

pub fn get_normals(&self) -> Option<&[Vector3]>

Gets the normal array of this TMFMesh.

§Example
 let normals = mesh.get_normals();
Source

pub fn get_tangents(&self) -> Option<&[Tangent]>

Gets the tangent array of this TMFMesh.

§Example
 let normals = mesh.get_tangents();
Source

pub fn get_uvs(&self) -> Option<&[Vector2]>

Gets the uv array of this TMFMesh.

§Example
 let uvs = mesh.get_uvs();
Source

pub fn get_vertex_triangles(&self) -> Option<&[IndexType]>

Gets the vertex triangle index array of this TMFMesh.

§Example
 let vertex_triangles = mesh.get_vertex_triangles();
Source

pub fn get_normal_triangles(&self) -> Option<&[IndexType]>

Gets the normal triangle index array of this TMFMesh.

§Example
 let normal_triangles = mesh.get_normal_triangles();
Source

pub fn get_uv_triangles(&self) -> Option<&[IndexType]>

Gets the uv triangle index array of this TMFMesh.

§Example
 let uv_triangles = mesh.get_uv_triangles();
Source

pub fn get_tangent_triangles(&self) -> Option<&[IndexType]>

Gets the tangent triangle index array of this TMFMesh.

§Example
 let tangent_triangles = mesh.get_tangent_triangles();
Source

pub fn get_vertex_buffer(&self) -> Option<Box<[Vector3]>>

Returns array containing points laid out in such a way that each 3 points create the next triangle. If mesh has no vertex array or no vertex triangle array None is returned.

§Example
 let vert_buff = mesh.get_vertex_buffer().expect("Could not create the array of points creating triangles!");
 // The same number of triangles created by points and triangles created by indices
 assert!(vert_buff.len() == vertex_triangles.len());
Source

pub fn get_normal_buffer(&self) -> Option<Box<[Vector3]>>

Returns array containing normals laid out in such a way that each 3 normals create the next triangle. If mesh has no normal array or no normal triangle array None is returned.

§Example
 let normal_buff = mesh.get_normal_buffer().expect("Could not create the array of normals creating triangles!");
 // The same number of triangles created by points and triangles created by indices
 assert!(normal_buff.len() == normal_triangles.len());
Source

pub fn get_uv_buffer(&self) -> Option<Box<[Vector2]>>

Returns array containing UV coridnates laid out in such a way that each 3 cordiantes create the next triangle. If mesh has no UV array or no uv triangle array None is returned.

§Example
 let uv_buff = mesh.get_uv_buffer().expect("Could not create the array of uvs creating triangles!");
 // The same number of triangles created by points and triangles created by indices
 assert!(uv_buff.len() == uv_triangles.len());
Source

pub fn normalize(&mut self)

Normalizes normal array of the mesh, if it is present.

§Examples
 // Some mesh with normals which are not normalzed
 mesh.set_normals(normals);
 // Normalize all normals
 mesh.normalize();
 // All normals are normalised (their magnitude is equal to 1)
 for normal in mesh.get_normals().expect("Mesh has no normals"){
     let mag = magnitude(*normal);
     // Magnitude of the normal is not exactly equal to 1
     // because of imprecision of floating-point numbers.
     assert!(mag > 0.99999 && mag < 1.00001);
 }

Normalising a mesh without normals is a NOP.

 // Mesh has no normals
 assert!(mesh.get_normals().is_none());
 // This does nothing
 mesh.normalize();
Source

pub fn verify(&self) -> Result<(), TMFIntegrityStatus>

Checks if mesh is valid and can be saved.

// Get the tmf mesh form somewhere
mesh.verify().expect("Mesh had errors!");
§Errors

Returns a TMFIntegrityStatus if mesh is invalid.

Source

pub fn read_from_obj<R: Read>(reader: &mut R) -> Result<Vec<(Self, String)>>

Reads tmf meshes from a .obj file in reader In order to enable triangulation while importing .obj files feature triangulation must be used. It is still highly experimental so read documentation before enabling. It is highly encouraged to just triangulate .obj files before importing them.

§Example
 // Open the file with the .obj model
 let mut file = File::open(dot_obj_path).expect("Could not open .obj file!");
 // And multiple meshes from it
 let meshes = TMFMesh::read_from_obj(&mut file).expect("Could not parse .obj file!");
 for (mesh,name) in meshes{
     // Do something with the mesh and name
     do_something(mesh,name);
 }
§Errors

Returns IO error if it occurs.

Source

pub fn read_from_obj_one<R: Read>(reader: &mut R) -> Result<(Self, String)>

Reads a single named tmf mesh from a .obj file in reader, if more than one mesh present an error will be returned. In order to enable triangulation while importing .obj files feature triangulation must be used. It is still highly experimental so read documentation before enabling. It is highly encouraged to just triangulate .obj files before importing them.

§Example
 // Open the file with the .obj model
 let mut file = File::open(dot_obj_path).expect("Could not open .obj file!");
 // And read a mesh from it
 let (mesh,name) = TMFMesh::read_from_obj_one(&mut file).expect("Could not parse .obj file!");
§Errors

Returns IO error if it occurs, or wrong mesh count.

Source

pub fn write_obj_one<W: Write>(&self, w: &mut W, name: &str) -> Result<()>

Writes this TMF mesh to a .obj file.

§Example
let mut obj_out = File::create(out_path).expect("Could not create obj out file!");
mesh.write_obj_one(&mut obj_out,"mesh name").expect("Could not write the .obj file!");
§Errors

Returns IO error if it occurs.

Source

pub fn write_obj<W: Write, S: Borrow<str>>( meshes: &[(TMFMesh, S)], w: &mut W, ) -> Result<()>

Writes multiple TMF meshes to a .obj file.

§Example
 let mut output = File::create(path).expect("Could not create file!");
 TMFMesh::write_obj(&meshes,&mut output).expect("Could not export to .obj");
§Errors

Returns IO error if it occurs.

Source

pub fn write_tmf_one<W: Write, S: Borrow<str>>( &self, w: &mut W, p_info: &TMFPrecisionInfo, name: S, ) -> Result<(), TMFExportError>

Writes this TMF Mesh to w.

§Example
 let mut output = File::create(path).expect("Could not create file!");
 // Settings dictating how to save the mesh
 let precision_info = TMFPrecisionInfo::default();
 mesh.write_tmf_one(&mut output,&precision_info,"mesh_name").expect("Could not save .tmf mesh!");;
§Errors

Returns IO error if occurs.

Source

pub fn write_tmf<W: Write, S: Borrow<str>>( meshes_names: &[(Self, S)], w: &mut W, p_info: &TMFPrecisionInfo, ) -> Result<(), TMFExportError>

Writes a number of TMF meshes into one file.

§Example
let mut output = File::create(path).expect("Could not create file!");
// Settings dictating how to save the mesh
let precision_info = TMFPrecisionInfo::default();
TMFMesh::write_tmf(&meshes,&mut output, &precision_info).expect("Could not save .tmf file!");
§Errors

Returns IO error if occurs.

Source

pub fn empty() -> Self

Creates an empty TMF Mesh(mesh with no data). Equivalent to TMFMesh::default.

§Example
// Creates an empty mesh with no data
let mesh = TMFMesh::empty();
Source

pub fn read_tmf<R: Read>( reader: &mut R, ) -> Result<Vec<(Self, String)>, TMFImportError>

Reads all meshes from a .tmf file.

§Example
// Open file containg the meshes
let mut file = File::open(tmf_path).expect("Could not open .tmf file");
// Get meshes and their names
let meshes = TMFMesh::read_tmf(&mut file).expect("Could not load .tmf mesh!");
for (mesh,name) in meshes{
    // Do some thing with each mesh and name(eg. use in game)
    do_something(mesh,name);
}
§Errors

Returns: an IO error if it occurs, NotTMFFile if not tmf file, NewerVersionRequired if a newer importer is required for importing the file, and other errors for malformed tmf files.

Source

pub async fn read_tmf_async<R: Read>( reader: &mut R, ) -> Result<Vec<(Self, String)>, TMFImportError>

Async version of Self::read_tmf.

§Example
async fn load_meshes_files<R:std::io::Read>(src:&mut [R])->Result<Vec<(TMFMesh,String)>,TMFImportError>{
    let mut meshes = Vec::new();
    for source in src{
        meshes.extend(TMFMesh::read_tmf_async(source).await?);
    }
    Ok(meshes)
}
Source

pub fn read_tmf_one<R: Read>( reader: &mut R, ) -> Result<(Self, String), TMFImportError>

Reads a single mesh from a .tmf file. Returns Err if no meshes present or more than one mesh present.

// Open the file containing the mesh
let mut file = File::open(tmf_path).expect("Could not open .tmf file");
// Read mesh and mesh name form file
let (mesh,name) = TMFMesh::read_tmf_one(&mut file).expect("Could not load .tmf mesh!");
§Errors

Returns: an IO error if it occurs, NotTMFFile if not tmf file, NewerVersionRequired if a newer importer is required for importing the file, and other errors for malformed tmf files. This function also returns NoMeshes or TooManyMeshes if wrong mesh count present.

Source

pub async fn read_tmf_one_async<R: Read>( reader: &mut R, ) -> Result<(Self, String), TMFImportError>

Async version of Self::read_tmf_one.

§Example
async fn load_meshes_files<R:std::io::Read>(src:&mut [R])->Result<Vec<(TMFMesh,String)>,TMFImportError>{
    let mut meshes = Vec::new();
    for source in src{
        meshes.push(TMFMesh::read_tmf_one_async(source).await?);
    }
    Ok(meshes)
}
Source

pub fn add_custom_data( &mut self, custom_data: CustomData, name: &str, ) -> Result<(), DataSegmentError>

Adds custom data array.

§Errors

Returns Err if name is too long (over 255 bytes) or empty.

§Examples
// Add custom index data.
let custom_index_data = [0,1,2,3];
let custom_float_data = [0.0,1.2,2.4,3.5];
let custom_rgba = [(0.2,0.6,0.7,0.1),(0.5,0.6,0.1,0.9),(0.3,0.9,0.3,0.2),(0.4,0.5,0.2,0.6),(0.2,0.8,0.9,0.4)];
// Add custom index data
mesh.add_custom_data(custom_index_data.into(),"custom_indices").expect("Could not add custom data to mesh!");
// Add custom float data.
mesh.add_custom_data(custom_float_data.into(),"custom_floats").expect("Could not add custom data to mesh!");
// Add custom RGBA color data
mesh.add_custom_data(custom_rgba.into(),"custom_rgba").expect("Could not add custom data to mesh!");
Source

pub fn lookup_custom_data(&self, name: &str) -> Option<&CustomData>

Gets a custom data array with name name. Returns None, if data not present, or name too long(over 255 bytes).

§Examples
// Add custom index data.
let custom_index_data = [0,1,2,3];
let custom_float_data = [0.0,1.2,2.4,3.5];
let custom_rgba = [(0.2,0.6,0.7,0.1),(0.5,0.6,0.1,0.9),(0.3,0.9,0.3,0.2),(0.4,0.5,0.2,0.6),(0.2,0.8,0.9,0.4)];
// Add custom index data
mesh.add_custom_data(custom_index_data.into(),"custom_indices").expect("Could not add custom data to mesh!");
// Add custom float data.
mesh.add_custom_data(custom_float_data.into(),"custom_floats").expect("Could not add custom data to mesh!");
// Add custom RGBA color data
mesh.add_custom_data(custom_rgba.into(),"custom_rgba").expect("Could not add custom data to mesh!");
// lookup data
let indices = mesh.lookup_custom_data("custom_indices").expect("Could not find custom indices");
let floats = mesh.lookup_custom_data("custom_floats").expect("Could not find custom floats");
let rgba = mesh.lookup_custom_data("custom_rgba").expect("Could not find custom rgba colors");
Source

pub fn append_vertices(&mut self, vertices: &[Vector3])

Appends vertices to this meshes vertex array.

§Example
let vertices_len = tmf_mesh.get_vertices().unwrap().len();
tmf_mesh.append_vertices(&[(0.2,4.5,4.4)]);
assert!(vertices_len < tmf_mesh.get_vertices().unwrap().len());
Source

pub fn append_normals(&mut self, normals: &[Vector3])

Appends normals to this meshes normal array.

§Example
let normals_len = tmf_mesh.get_normals().unwrap().len();
tmf_mesh.append_normals(&[(0.2,4.5,4.4)]);
assert!(normals_len < tmf_mesh.get_normals().unwrap().len());
Source

pub fn append_tangents(&mut self, tangents: &[Tangent])

Appends tangents to this meshes tangent array.

§Example
let tangents_len = tmf_mesh.get_tangents().unwrap().len();
tmf_mesh.append_tangents(&[((0.311649, 0.733673, -0.60382),-1.0)]);
assert!(tangents_len < tmf_mesh.get_tangents().unwrap().len());
Source

pub fn append_uvs(&mut self, uvs: &[Vector2])

Appends uvs to this meshes uv array.

§Example
let uvs_len = tmf_mesh.get_uvs().unwrap().len();
tmf_mesh.append_uvs(&[(0.2,0.5),(0.12,0.78)]);
assert!(uvs_len < tmf_mesh.get_uvs().unwrap().len());
Source

pub fn append_vertex_triangles(&mut self, triangles: &[IndexType])

Appends indices to this meshes vertex triangle array.

§Example
let triangle_len = tmf_mesh.get_vertex_triangles().unwrap().len();
tmf_mesh.append_vertex_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_vertex_triangles().unwrap().len());
Source

pub fn append_normal_triangles(&mut self, triangles: &[IndexType])

Appends indices to this meshes normal triangle array.

§Example
let triangle_len = tmf_mesh.get_normal_triangles().unwrap().len();
tmf_mesh.append_normal_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_normal_triangles().unwrap().len());
Source

pub fn append_uv_triangles(&mut self, triangles: &[IndexType])

Appends indices to this meshes uv triangle array.

§Example
let triangle_len = tmf_mesh.get_uv_triangles().unwrap().len();
tmf_mesh.append_uv_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_uv_triangles().unwrap().len());
Source

pub fn append_tangent_triangles(&mut self, triangles: &[IndexType])

Appends indices to this meshes tangent triangle array.

§Example
let triangle_len = tmf_mesh.get_tangent_triangles().unwrap().len();
tmf_mesh.append_tangent_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_tangent_triangles().unwrap().len());

Trait Implementations§

Source§

impl Clone for TMFMesh

Source§

fn clone(&self) -> TMFMesh

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for TMFMesh

Source§

fn default() -> Self

Creates default, empty TMFMesh. Equivalent to TMFMesh::empty call.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.