Compare commits
20
Commits
09ff36dff8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8aeb643e21 | ||
|
|
4f762c7df9 | ||
|
|
1b94f8ca7a | ||
|
|
3eca1b1f7b | ||
|
|
5a23f49eae | ||
|
|
3ea1a7c5aa | ||
|
|
65bb4d4897 | ||
|
|
36c16d053e | ||
|
|
50eabecfe9 | ||
|
|
585af00665 | ||
|
|
5065596f96 | ||
|
|
8cd294c44a | ||
|
|
58a9c36d0d | ||
|
|
c4a794c4f2 | ||
|
|
2649558ecd | ||
|
|
6adb2e143f | ||
|
|
29f39a9163 | ||
|
|
0a692c01de | ||
|
|
41eb9a9dc0 | ||
|
|
5b4c205234 |
@@ -0,0 +1,72 @@
|
||||
use crate::math::{Point3, Vector3};
|
||||
|
||||
pub struct Plane<T> {
|
||||
point: Point3<T>,
|
||||
normal: Vector3<T>,
|
||||
}
|
||||
|
||||
impl<T> Plane<T> {
|
||||
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
|
||||
Plane { point, normal }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Line<T> {
|
||||
point: Point3<T>,
|
||||
direction: Vector3<T>,
|
||||
}
|
||||
|
||||
pub struct Sphere<T> {
|
||||
point: Point3<T>,
|
||||
radius: T,
|
||||
}
|
||||
|
||||
impl<T> Sphere<T> {
|
||||
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
|
||||
Sphere { point, radius }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Geometry<T> {
|
||||
Sphere(Sphere<T>),
|
||||
Plane(Plane<T>),
|
||||
Line(Line<T>),
|
||||
}
|
||||
|
||||
impl<T> Geometry<T> {
|
||||
pub fn draw(&self) {
|
||||
match self {
|
||||
Geometry::Sphere(_) => {
|
||||
println!("Drawing Sphere!");
|
||||
}
|
||||
Geometry::Line(_) => {
|
||||
println!("Drawing Line!");
|
||||
}
|
||||
Geometry::Plane(_) => {
|
||||
println!("Drawing Plane!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_plane() {
|
||||
let p = Point3::new(1.0, 2.0, 3.0);
|
||||
let n = Vector3::new(4.0, 5.0, 6.0);
|
||||
|
||||
let plane = Plane::new(p, n);
|
||||
|
||||
assert_eq!(plane.point, p);
|
||||
assert_eq!(plane.normal, n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn foo() {
|
||||
panic!("This code panics!");
|
||||
}
|
||||
}
|
||||
+2
-124
@@ -1,124 +1,2 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Vector3 {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
}
|
||||
|
||||
impl Vector3 {
|
||||
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
||||
Vector3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vector3 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn add(self, rhs: Vector3) -> Self::Output {
|
||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vector3 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn sub(self, rhs: Vector3) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f64> for Vector3 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vector3> for f64 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Vector3 {
|
||||
type Output = f64;
|
||||
|
||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f64> for Vector3 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn div(self, rhs: f64) -> Self::Output {
|
||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Point3 {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
}
|
||||
|
||||
impl Point3 {
|
||||
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
|
||||
Point3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Vector3> for Point3 {
|
||||
type Output = Point3;
|
||||
|
||||
fn add(self, rhs: Vector3) -> Self::Output {
|
||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Point3 {
|
||||
type Output = Vector3;
|
||||
|
||||
fn sub(self, rhs: Point3) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Plane {
|
||||
point: Point3,
|
||||
normal: Vector3,
|
||||
}
|
||||
|
||||
impl Plane {
|
||||
pub fn new(point: Point3, normal: Vector3) -> Plane {
|
||||
Plane { point, normal }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Line {
|
||||
point: Point3,
|
||||
direction: Vector3,
|
||||
}
|
||||
|
||||
pub struct Sphere {
|
||||
point: Point3,
|
||||
radius: f64,
|
||||
}
|
||||
|
||||
impl Sphere {
|
||||
pub fn new(point: Point3, radius: f64) -> Sphere {
|
||||
Sphere { point, radius }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Geometry {
|
||||
Sphere(Sphere),
|
||||
Plane(Plane),
|
||||
Line(Line),
|
||||
}
|
||||
pub mod geometry;
|
||||
pub mod math;
|
||||
|
||||
+118
-33
@@ -1,45 +1,130 @@
|
||||
use hello::*;
|
||||
use hello::geometry::{Geometry, Line, Plane, Sphere};
|
||||
use hello::math::{Mat4x4, Point3, Vector3};
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RGB<T> {
|
||||
red: T,
|
||||
green: T,
|
||||
blue: T,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Texture<T> {
|
||||
width: usize,
|
||||
height: usize,
|
||||
pixel: Vec<RGB<T>>,
|
||||
}
|
||||
|
||||
impl Texture<f64> {
|
||||
fn load_texture() -> Texture<f64> {
|
||||
let width = 640;
|
||||
let height = 480;
|
||||
|
||||
let mut pixel = Vec::new();
|
||||
|
||||
for _ in 0..width {
|
||||
for _ in 0..height {
|
||||
pixel.push(RGB {
|
||||
red: 1.0,
|
||||
green: 1.0,
|
||||
blue: 1.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Texture {
|
||||
width,
|
||||
height,
|
||||
pixel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RenderableGeometry<T> {
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
}
|
||||
|
||||
impl<T> RenderableGeometry<T> {
|
||||
fn new(
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
) -> RenderableGeometry<T> {
|
||||
RenderableGeometry {
|
||||
transform,
|
||||
geometry,
|
||||
texture,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Renderable {
|
||||
fn render(&self);
|
||||
}
|
||||
|
||||
impl<T> Renderable for Sphere<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Sphere!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Plane<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Plane!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Line<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Line!");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0);
|
||||
let sphere2 = Sphere::new(Point3::new(1.0, -45.4, 3.55), 2.0);
|
||||
let sphere3 = Sphere::new(Point3::new(3.0, 2.0, 1.0), 0.5);
|
||||
let tex = Rc::new(Texture::load_texture());
|
||||
|
||||
let plane1 = Plane::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0));
|
||||
let sphere = Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0));
|
||||
let geometries = vec![sphere];
|
||||
|
||||
let mut v = Vec::new();
|
||||
|
||||
v.push(Geometry::Sphere(sphere1));
|
||||
v.push(Geometry::Plane(plane1));
|
||||
v.push(Geometry::Sphere(sphere2));
|
||||
v.push(Geometry::Sphere(sphere3));
|
||||
|
||||
for geo in v {
|
||||
match geo {
|
||||
Geometry::Sphere(s) => println!("A Sphere"),
|
||||
Geometry::Plane(p) => println!("A Plane"),
|
||||
Geometry::Line(l) => println!("A Line"),
|
||||
}
|
||||
for g in geometries {
|
||||
g.draw();
|
||||
}
|
||||
|
||||
let mut lottery = [4, 8, 15, 16, 23, 42];
|
||||
let mut renderables: Vec<Box<dyn Renderable>> = Vec::new();
|
||||
renderables.push(Box::new(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)));
|
||||
renderables.push(Box::new(Plane::new(
|
||||
Point3::new(0.0, 0.0, 0.0),
|
||||
Vector3::new(0.0, 1.0, 0.0),
|
||||
)));
|
||||
|
||||
let mut sum = 0;
|
||||
|
||||
for value in lottery {
|
||||
sum += value;
|
||||
for r in renderables {
|
||||
r.render();
|
||||
}
|
||||
|
||||
println!("The sum is {}", sum);
|
||||
let geo1 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
let mut lottery_string = Vec::new();
|
||||
for value in lottery {
|
||||
lottery_string.push(value.to_string());
|
||||
}
|
||||
let geo2 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(-2.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
let lottery_string_2: Vec<String> = lottery
|
||||
.iter()
|
||||
.filter(|x| **x > 8)
|
||||
.map(|x| x.to_string())
|
||||
.collect();
|
||||
let geo3 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(2.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
println!("Reference counter = {}", Rc::strong_count(&tex));
|
||||
|
||||
let a = Vector3::new(2.0, 3.0, 1.0);
|
||||
let b = 10.0 * a;
|
||||
}
|
||||
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
macro_rules! create_vector {
|
||||
($name:ident [$($elem:ident),+] ) => {
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct $name<T> {
|
||||
$($elem: T,)*
|
||||
}
|
||||
|
||||
impl<T> $name<T> {
|
||||
pub fn new($($elem : T,)*) -> $name<T> {
|
||||
$name { $($elem,)* }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
create_vector! { Vector4 [x, y, z, w] }
|
||||
create_vector! { Vector3 [x, y, z] }
|
||||
create_vector! { Vector2 [x, y] }
|
||||
|
||||
impl<T> Add for Vector3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sub for Vector3<T>
|
||||
where
|
||||
T: Sub<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn sub(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Mul<T> for Vector3<T>
|
||||
where
|
||||
T: Mul<Output = T> + Clone + Copy,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn mul(self, rhs: T) -> Self::Output {
|
||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_mul_for_vector3 {
|
||||
($($type:ty)+) => {
|
||||
$(
|
||||
impl Mul<Vector3<$type>> for $type {
|
||||
type Output = Vector3<$type>;
|
||||
|
||||
fn mul(self, rhs: Vector3<$type>) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl_mul_for_vector3! { i32 f32 f64 }
|
||||
|
||||
impl<T> Mul for Vector3<T>
|
||||
where
|
||||
T: Mul<Output = T> + Add<Output = T>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn mul(self, rhs: Vector3<T>) -> Self::Output {
|
||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Div<T> for Vector3<T>
|
||||
where
|
||||
T: Div<Output = T> + Copy + Clone,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn div(self, rhs: T) -> Self::Output {
|
||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct Point3<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
z: T,
|
||||
}
|
||||
|
||||
impl<T> Point3<T> {
|
||||
pub fn new(x: T, y: T, z: T) -> Point3<T> {
|
||||
Point3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add<Vector3<T>> for Point3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Point3<T>;
|
||||
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sub for Point3<T>
|
||||
where
|
||||
T: Sub<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn sub(self, rhs: Point3<T>) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mat4x4 {
|
||||
v: f64,
|
||||
}
|
||||
|
||||
impl Mat4x4 {
|
||||
pub fn ident() -> Mat4x4 {
|
||||
Mat4x4 { v: 0.0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_vector3() {
|
||||
let v = Vector3::new(1.0, 2.0, 3.0);
|
||||
assert_eq!(v.x, 1.0);
|
||||
assert_eq!(v.y, 2.0);
|
||||
assert_eq!(v.z, 3.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_vector3_to_vector3() {
|
||||
let v1 = Vector3::new(1.0, 2.0, 3.0);
|
||||
let v2 = Vector3::new(4.0, 5.0, 6.0);
|
||||
|
||||
let r = v1 + v2;
|
||||
|
||||
assert_eq!(r.x, 5.0);
|
||||
assert_eq!(r.y, 7.0);
|
||||
assert_eq!(r.z, 9.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_point3() {
|
||||
let p = Point3::new(1.0, 2.0, 3.0);
|
||||
assert_eq!(p.x, 1.0);
|
||||
assert_eq!(p.y, 2.0);
|
||||
assert_eq!(p.z, 3.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#[test]
|
||||
fn very_long_running_test() {}
|
||||
Reference in New Issue
Block a user