sfml_types/
vector3.rs

1/*
2* Rust-SFML - Copyright (c) 2013 Letang Jeremy.
3*
4* The original software, SFML library, is provided by Laurent Gomila.
5*
6* This software is provided 'as-is', without any express or implied warranty.
7* In no event will the authors be held liable for any damages arising from
8* the use of this software.
9*
10* Permission is granted to anyone to use this software for any purpose,
11* including commercial applications, and to alter it and redistribute it
12* freely, subject to the following restrictions:
13*
14* 1. The origin of this software must not be misrepresented; you must not claim
15*    that you wrote the original software. If you use this software in a product,
16*    an acknowledgment in the product documentation would be appreciated but is
17*    not required.
18*
19* 2. Altered source versions must be plainly marked as such, and must not be
20*    misrepresented as being the original software.
21*
22* 3. This notice may not be removed or altered from any source distribution.
23*/
24
25//! Utility Class providing 3 dimensional vectors for f32.
26
27use std::ops::{Add, Sub, Mul, Div};
28
29/// Vector3f definition
30#[repr(C)]
31#[derive(Clone, PartialOrd, PartialEq, Debug, Copy)]
32pub struct Vector3<T> {
33    /// X coordinate of the vector.
34    pub x: T,
35    /// Y coordinate of the vector.
36    pub y: T,
37    /// Z coordinate of the vector.
38    pub z: T
39}
40
41impl<T> Vector3<T> {
42    /// Create a new Vector3f with the given values.
43    pub fn new(x: T, y: T, z: T) -> Vector3<T> {
44        Vector3 {
45            x: x,
46            y: y,
47            z: z
48        }
49    }
50}
51
52/// export Vector3<f32> as Vector3f
53pub type Vector3f = Vector3<f32>;
54/// export Vector3<i32> as Vector3i
55pub type Vector3i = Vector3<i32>;
56/// export Vector3<u32> as Vector3u
57pub type Vector3u = Vector3<u32>;
58
59impl<T: Add + Copy> Add<T> for Vector3<T> {
60    type Output = Vector3<T::Output>;
61
62    fn add(self, rhs: T) -> Vector3<T::Output> {
63        Vector3 {
64            x: self.x + rhs,
65            y: self.y + rhs,
66            z: self.z + rhs
67        }
68    }
69}
70
71impl<T: Sub + Copy> Sub<T> for Vector3<T> {
72    type Output = Vector3<T::Output>;
73
74    fn sub(self, rhs: T) -> Vector3<T::Output> {
75        Vector3 {
76            x: self.x - rhs,
77            y: self.y - rhs,
78            z: self.z - rhs
79        }
80    }
81}
82
83impl<T: Mul + Copy> Mul<T> for Vector3<T> {
84    type Output = Vector3<T::Output>;
85
86    fn mul(self, rhs: T) -> Vector3<T::Output> {
87        Vector3 {
88            x: self.x * rhs,
89            y: self.y * rhs,
90            z: self.z * rhs
91        }
92    }
93}
94
95impl<T: Div + Copy> Div<T> for Vector3<T> {
96    type Output = Vector3<T::Output>;
97
98    fn div(self, rhs: T) -> Vector3<T::Output> {
99        Vector3 {
100            x: self.x / rhs,
101            y: self.y / rhs,
102            z: self.z / rhs
103        }
104    }
105}
106
107
108impl<T: Add> Add for Vector3<T> {
109    type Output = Vector3<T::Output>;
110
111    fn add(self, rhs: Vector3<T>) -> Vector3<T::Output> {
112        Vector3 {
113            x: self.x + rhs.x,
114            y: self.y + rhs.y,
115            z: self.z + rhs.z
116        }
117    }
118}
119
120impl<T: Sub> Sub for Vector3<T> {
121    type Output = Vector3<T::Output>;
122
123    fn sub(self, rhs: Vector3<T>) -> Vector3<T::Output> {
124        Vector3 {
125            x: self.x - rhs.x,
126            y: self.y - rhs.y,
127            z: self.z - rhs.z
128        }
129    }
130}
131
132impl<T: Mul> Mul for Vector3<T> {
133    type Output = Vector3<T::Output>;
134
135    fn mul(self, rhs: Vector3<T>) -> Vector3<T::Output> {
136        Vector3 {
137            x: self.x * rhs.x,
138            y: self.y * rhs.y,
139            z: self.z * rhs.z
140        }
141    }
142}
143
144impl<T: Div> Div for Vector3<T> {
145    type Output = Vector3<T::Output>;
146
147    fn div(self, rhs: Vector3<T>) -> Vector3<T::Output> {
148        Vector3 {
149            x: self.x / rhs.x,
150            y: self.y / rhs.y,
151            z: self.z / rhs.z
152        }
153    }
154}