/* ddd::space The functions declared in this section of the module deal with spacial objects and their manipulations. -- Copyright (C) 2017 Davis Remmel This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ use super::Point; use super::Translation; use super::Rotation; // A shape is a collection of points that can be rotated about an // origin. Points are relative to a shape's origin, which is absolute. pub struct Shape { pub faces: Vec>, pub rotation: Rotation, pub translation: Translation } impl Shape { // // Rotate to an absolute position // pub fn rotate_abs(&mut self, rotation: Rotation) { // self.rotation = rotation; // } // Rotate to a position relative to the current rotation pub fn rotate_rel(&mut self, rotation: Rotation) { self.rotation = Rotation { x: self.rotation.x + rotation.x, y: self.rotation.y + rotation.y, z: self.rotation.z + rotation.z }; } // pub fn translate_abs(&mut self, translation: Translation) { // self.translation = translation; // } pub fn translate_rel(&mut self, translation: Translation) { self.translation = Translation { x: self.translation.x + translation.x, y: self.translation.y + translation.y, z: self.translation.z + translation.z }; } pub fn apply_face_transformations(&self) -> Vec> { let mut absolute_faces: Vec> = vec![]; for face in &self.faces { // Figures out the absolute positions of each vertex let mut absolute_vertices: Vec = vec![]; for i in 0..face.len() { let rel_vertex = &face[i]; let origin = &self.translation; let mut x = rel_vertex.x; let mut y = rel_vertex.y; let mut z = rel_vertex.z; // Rotate about the X let z_b = (z) * (self.rotation.x).cos() - (y) * (self.rotation.x).sin(); let y_b = (y) * (self.rotation.x).cos() + (z) * (self.rotation.x).sin(); z = z_b; y = y_b; // Rotate about the Y let x_b = (x) * (self.rotation.y).cos() - (z) * (self.rotation.y).sin(); let z_b = (z) * (self.rotation.y).cos() + (x) * (self.rotation.y).sin(); x = x_b; z = z_b; // Rotate about the Z let x_b = (x) * (self.rotation.z).cos() - (y) * (self.rotation.z).sin(); let y_b = (y) * (self.rotation.z).cos() + (x) * (self.rotation.z).sin(); x = x_b; y = y_b; absolute_vertices.push(Point { x: x + origin.x, y: y + origin.y, z: z + origin.z }); } absolute_faces.push(absolute_vertices); } absolute_faces } }