obj 0.6.0

A package for loading Wavefront .obj files
//   Copyright 2017 GFX Developers
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#[cfg(feature = "genmesh")]
extern crate genmesh;

use std::fs::File;
use std::path::Path;
use std::io::{BufReader, Result as IoResult};
use std::collections::HashMap;
use std::rc::Rc;

pub use obj::{Obj, Object, Group, IndexTuple, GenPolygon, SimplePolygon};
pub use mtl::{Mtl, Material};

mod obj;
mod mtl;

pub fn load<P: GenPolygon>(path: &Path) -> IoResult<Obj<Rc<Material>,P>> {
    let f = File::open(path)?;
    let obj = Obj::load(&mut BufReader::new(f));
    let mut materials = HashMap::new();

    for m in obj.materials().iter() {
        let p = path.with_file_name(m);
        let file = File::open(&p)?;
        let mtl = Mtl::load(&mut BufReader::new(file));
        for m in mtl.materials {
            materials.insert(m.name.clone(), Rc::new(m));
        }
    }

    Ok(obj.map(|Group {name, index, material, indices}| {
        Group {
            name,
            index,
            material: material.and_then(|m| materials.get(&m).cloned()),
            indices,
        }
    }))
}