wavefront_loader 0.2.4

A loader/exporter into wavefront for debugging and visualizing geometry algorithms.
Documentation
use std::fs::*;
use std::io::{BufRead, BufReader};

use crate::Vec3;

// See the specification.
#[derive(Debug, Clone, Default)]
pub struct MtlData {
    /// Name of the material.
    pub name: String,
    /// Ambient color (i.e. the light scattered in the scene).
    pub ka: Vec3,
    /// Diffuse color ("intrinsic color of the object")/
    pub kd: Vec3,
    /// Specular color ("reflective" color).
    pub ks: Vec3,
    /// "Specularity" index, value defining how big specular reflective lobes
    /// are.
    pub ns: f32,
    /// Index of refraction.
    pub ni: f32,
    /// Transparency level, 1. is opaque, 0. is fully transparent.
    pub d: f32,
    /// Illumination model. 0 is constant color (only use kd). 1 is pure
    /// Lambertian. 2 is Blinn-Phong. (This is likely legacy from the 1990's
    /// you can probably ignore it)
    pub illum: u64,
    /// Map to a texture file specifying the `kd` coloration of the object.
    /// During rendering, `kd` values should be multiplied by by the
    /// `map_kd` values to ge tthe RGB components.
    pub map_kd: String,
}

impl MtlData {
    pub fn from_disk_file(path: &str) -> Vec<Self> {
        let file = match File::open(path) {
            Ok(file) => file,
            Err(_) => {
                eprintln!("path {} not  found for mtl lib", path);
                return Vec::new();
            }
        };
        let reader = BufReader::new(file);

        let mut material_list = Vec::new();
        let mut res = Self::default();
        for line in reader.lines() {
            let line_str = line.unwrap();
            let line_str = line_str.trim();
            let tokens = line_str.split_whitespace().collect::<Vec<&str>>();
            if tokens.is_empty() {
                continue;
            }
            match tokens[0] {
                "newmtl" => {
                    material_list.push(res);

                    res = Self::default();

                    assert!(
                        res.name.is_empty(),
                        "Found duplicate name in mtl file, this is malformed."
                    );
                    res.name = tokens[1].to_string()
                }
                "Ka" => {
                    res.ka = Vec3::new(
                        tokens[1].parse::<f32>().unwrap(),
                        tokens[2].parse::<f32>().unwrap(),
                        tokens[3].parse::<f32>().unwrap(),
                    );
                }
                "Kd" => {
                    res.kd = Vec3::new(
                        tokens[1].parse::<f32>().unwrap(),
                        tokens[2].parse::<f32>().unwrap(),
                        tokens[3].parse::<f32>().unwrap(),
                    );
                }
                "Ks" => {
                    res.ks = Vec3::new(
                        tokens[1].parse::<f32>().unwrap(),
                        tokens[2].parse::<f32>().unwrap(),
                        tokens[3].parse::<f32>().unwrap(),
                    );
                }
                "Ns" => {
                    res.ns = tokens[1].parse::<f32>().unwrap();
                }
                "Ni" => {
                    res.ni = tokens[1].parse::<f32>().unwrap();
                }
                "d" => {
                    res.d = tokens[1].parse::<f32>().unwrap();
                }
                "illum" => {
                    res.illum = tokens[1].parse::<u64>().unwrap();
                }
                "map_Kd" => {
                    res.map_kd = tokens[1].to_string();
                }
                _ => {
                    continue;
                }
            }
        }

        material_list.push(res);
        material_list.remove(0);

        material_list
    }
}