use crate::{basic_types::RGBA, error::Error};
use super::component::{Normal, Point3D};
pub type Positions = Vec<Point3D>;
pub type Normals = Vec<Normal>;
pub type Colors = Vec<RGBA>;
pub struct Vertices {
positions: Positions,
normals: Option<Normals>,
colors: Option<Colors>,
}
impl Vertices {
pub fn new() -> Self {
Vertices {
positions: Vec::new(),
normals: None,
colors: None,
}
}
pub fn from_positions(positions: Vec<Point3D>) -> Self {
Vertices {
positions,
normals: None,
colors: None,
}
}
pub fn len(&self) -> usize {
self.positions.len()
}
pub fn set_normals(&mut self, normals: Normals) -> Result<(), Error> {
if self.positions.len() != normals.len() {
Err(Error::InvalidArgument(format!(
"Got {} vertices, but normal attribute only has {} entries",
self.positions.len(),
normals.len()
)))
} else {
self.normals = Some(normals);
Ok(())
}
}
pub fn set_colors(&mut self, colors: Colors) -> Result<(), Error> {
if self.positions.len() != colors.len() {
Err(Error::InvalidArgument(format!(
"Got {} vertices, but color attribute only has {} entries",
self.positions.len(),
colors.len()
)))
} else {
self.colors = Some(colors);
Ok(())
}
}
pub fn get_positions(&self) -> &Positions {
&self.positions
}
pub fn get_normals(&self) -> Option<&Normals> {
self.normals.as_ref()
}
pub fn get_colors(&self) -> Option<&Colors> {
self.colors.as_ref()
}
}