Compare commits
53
Commits
f899a55a08
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8aeb643e21 | ||
|
|
4f762c7df9 | ||
|
|
1b94f8ca7a | ||
|
|
3eca1b1f7b | ||
|
|
5a23f49eae | ||
|
|
3ea1a7c5aa | ||
|
|
65bb4d4897 | ||
|
|
36c16d053e | ||
|
|
50eabecfe9 | ||
|
|
585af00665 | ||
|
|
5065596f96 | ||
|
|
8cd294c44a | ||
|
|
58a9c36d0d | ||
|
|
c4a794c4f2 | ||
|
|
2649558ecd | ||
|
|
6adb2e143f | ||
|
|
29f39a9163 | ||
|
|
0a692c01de | ||
|
|
41eb9a9dc0 | ||
|
|
5b4c205234 | ||
|
|
09ff36dff8 | ||
|
|
45cad795a7 | ||
|
|
5daaf9e1bc | ||
|
|
bb45906753 | ||
|
|
b7a1f52360 | ||
|
|
b1963c7e6a | ||
|
|
9ef8e08936 | ||
|
|
31e224f9b0 | ||
|
|
07b2fc28d8 | ||
|
|
31c9bda5a2 | ||
|
|
794228dfce | ||
|
|
28df45da67 | ||
|
|
81205e5b29 | ||
|
|
07d7855f47 | ||
|
|
352085f8d9 | ||
|
|
b0e998fb28 | ||
|
|
7a1b0975d2 | ||
|
|
12a54c42da | ||
|
|
bd260f1769 | ||
|
|
823ec0ad00 | ||
|
|
019fbcf095 | ||
|
|
20c675ef91 | ||
|
|
6e0e031b12 | ||
|
|
d5427c96ca | ||
|
|
092e5693b7 | ||
|
|
04520ffa1e | ||
|
|
c77982c642 | ||
|
|
97e5c2fd72 | ||
|
|
6316eb4e02 | ||
|
|
c6bba1c4a7 | ||
|
|
cbc1663ad2 | ||
|
|
72541ec0a1 | ||
|
|
8779759e7a |
@@ -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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod geometry;
|
||||||
|
pub mod math;
|
||||||
+97
-81
@@ -1,114 +1,130 @@
|
|||||||
fn foo() {
|
use hello::geometry::{Geometry, Line, Plane, Sphere};
|
||||||
println!("Hello from foo()");
|
use hello::math::{Mat4x4, Point3, Vector3};
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct RGB<T> {
|
||||||
|
red: T,
|
||||||
|
green: T,
|
||||||
|
blue: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn answer_to_all_questions() -> u32 {
|
#[derive(Clone)]
|
||||||
42
|
struct Texture<T> {
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
|
pixel: Vec<RGB<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_two_numbers(a: u32, b: u32) -> u32 {
|
impl Texture<f64> {
|
||||||
a + b
|
fn load_texture() -> Texture<f64> {
|
||||||
}
|
let width = 640;
|
||||||
|
let height = 480;
|
||||||
|
|
||||||
trait Vector {
|
let mut pixel = Vec::new();
|
||||||
fn dot(&self, other: &Self) -> f32;
|
|
||||||
fn length(&self) -> f32;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Vector2 {
|
for _ in 0..width {
|
||||||
x: f32,
|
for _ in 0..height {
|
||||||
y: f32,
|
pixel.push(RGB {
|
||||||
}
|
red: 1.0,
|
||||||
|
green: 1.0,
|
||||||
|
blue: 1.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Vector2 {
|
Texture {
|
||||||
pub fn new(x: f32, y: f32) -> Vector2 {
|
width,
|
||||||
Vector2 { x, y }
|
height,
|
||||||
|
pixel,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector for Vector2 {
|
struct RenderableGeometry<T> {
|
||||||
fn dot(&self, other: &Vector2) -> f32 {
|
transform: Mat4x4,
|
||||||
self.x * other.x + self.y * other.y
|
geometry: Geometry<T>,
|
||||||
}
|
texture: Rc<Texture<f64>>,
|
||||||
|
}
|
||||||
|
|
||||||
fn length(&self) -> f32 {
|
impl<T> RenderableGeometry<T> {
|
||||||
self.dot(self).sqrt()
|
fn new(
|
||||||
|
transform: Mat4x4,
|
||||||
|
geometry: Geometry<T>,
|
||||||
|
texture: Rc<Texture<f64>>,
|
||||||
|
) -> RenderableGeometry<T> {
|
||||||
|
RenderableGeometry {
|
||||||
|
transform,
|
||||||
|
geometry,
|
||||||
|
texture,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Vector3 {
|
trait Renderable {
|
||||||
x: f32,
|
fn render(&self);
|
||||||
y: f32,
|
|
||||||
z: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector3 {
|
impl<T> Renderable for Sphere<T> {
|
||||||
pub fn new(x: f32, y: f32, z: f32) -> Vector3 {
|
fn render(&self) {
|
||||||
Vector3 { x, y, z }
|
println!("Rendering Sphere!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector for Vector3 {
|
impl<T> Renderable for Plane<T> {
|
||||||
fn dot(&self, other: &Vector3) -> f32 {
|
fn render(&self) {
|
||||||
self.x * other.x + self.y * other.y + self.z * other.z
|
println!("Rendering Plane!");
|
||||||
}
|
|
||||||
|
|
||||||
fn length(&self) -> f32 {
|
|
||||||
self.dot(self).sqrt()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Add for Vector3 {
|
impl<T> Renderable for Line<T> {
|
||||||
type Output = Vector3;
|
fn render(&self) {
|
||||||
fn add(self, other: Self) -> Self {
|
println!("Rendering Line!");
|
||||||
Vector3::new(self.x + other.x, self.y + other.y, self.z + other.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Meter {
|
|
||||||
value: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Meter {
|
|
||||||
pub fn new(value: f32) -> Meter {
|
|
||||||
Meter { value }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SquareMeter {
|
|
||||||
value: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SquareMeter {
|
|
||||||
pub fn new(value: f32) -> SquareMeter {
|
|
||||||
SquareMeter { value }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Mul for Meter {
|
|
||||||
type Output = SquareMeter;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Self) -> Self::Output {
|
|
||||||
SquareMeter::new(self.value * rhs.value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec1 = Vector3::new(2.0, 0.0, 0.0);
|
let tex = Rc::new(Texture::load_texture());
|
||||||
let vec2 = Vector3::new(1.0, 1.0, 0.0);
|
|
||||||
|
|
||||||
let vec3 = vec1 + vec2;
|
let sphere = Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0));
|
||||||
|
let geometries = vec![sphere];
|
||||||
|
|
||||||
let m1 = Meter::new(10.0);
|
for g in geometries {
|
||||||
let m2 = Meter::new(90.0);
|
g.draw();
|
||||||
|
}
|
||||||
|
|
||||||
let area = m1 * m2;
|
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),
|
||||||
|
)));
|
||||||
|
|
||||||
println!("Hello, world!");
|
for r in renderables {
|
||||||
println!("Answer to all questions: {}", answer_to_all_questions());
|
r.render();
|
||||||
println!("Add two number: {}", add_two_numbers(23, 42));
|
}
|
||||||
|
|
||||||
println!("x: {}, y: {}, z: {}", vec3.x, vec3.y, vec3.z);
|
let geo1 = RenderableGeometry::new(
|
||||||
|
Mat4x4::ident(),
|
||||||
|
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
|
||||||
|
tex.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
foo();
|
let geo2 = RenderableGeometry::new(
|
||||||
|
Mat4x4::ident(),
|
||||||
|
Geometry::Sphere(Sphere::new(Point3::new(-2.0, 0.0, 0.0), 1.0)),
|
||||||
|
tex.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
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