forked from ocen-lang/ocen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.oc
More file actions
192 lines (153 loc) · 7.42 KB
/
Copy pathvec.oc
File metadata and controls
192 lines (153 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! A simple vector library for 2D and 3D mathematical vectors.
import std::math
import std::traits::hash::pair_hash
// TODO: formatting attribute can't work properly for generic types
[formatting "(%g, %g)" "(f64)$.x, (f64)$.y"]
//! Template for a 2D vector
struct Vec2<T> {
x, y: T
}
typedef Vec2f = Vec2<f32>
typedef Vec2i = Vec2<i32>
typedef Vec2u = Vec2<u32>
typedef Vec2f64 = Vec2<f64>
typedef Vec2i64 = Vec2<i64>
typedef Vec2u64 = Vec2<u64>
[operator "+"] def Vec2::add(this, other: Vec2<T>): Vec2<T> => Vec2<T>(.x + other.x, .y + other.y)
[operator "+"] def Vec2::adds(this, val: T): Vec2<T> => Vec2<T>(.x + val, .y + val)
[operator "+"] def Vec2::addrs(val: T, this: Vec2<T>): Vec2<T> => Vec2<T>(val + this.x, val + this.y)
[operator "-"] def Vec2::sub(this, other: Vec2<T>): Vec2<T> => Vec2<T>(.x - other.x, .y - other.y)
[operator "-"] def Vec2::subs(this, val: T): Vec2<T> => Vec2<T>(.x - val, .y - val)
[operator "-"] def Vec2::subrs(val: T, this: Vec2<T>): Vec2<T> => Vec2<T>(val - this.x, val - this.y)
[operator "*"] def Vec2::mult(this, other: Vec2<T>): Vec2<T> => Vec2<T>(.x * other.x, .y * other.y)
[operator "*"] def Vec2::mults(this, val: T): Vec2<T> => Vec2<T>(.x * val, .y * val)
[operator "*"] def Vec2::multrs(val: T, this: Vec2<T>): Vec2<T> => Vec2<T>(val * this.x, val * this.y)
[operator "/"] def Vec2::div(this, other: Vec2<T>): Vec2<T> => Vec2<T>(.x / other.x, .y / other.y)
[operator "/"] def Vec2::divs(this, val: T): Vec2<T> => Vec2<T>(.x / val, .y / val)
[operator "/"] def Vec2::divrs(val: T, this: Vec2<T>): Vec2<T> => Vec2<T>(val / this.x, val / this.y)
[operator "-"] def Vec2::negate(this): Vec2<T> => Vec2<T>(-.x, -.y)
def Vec2::dot(this, other: Vec2<T>): T => .x * other.x + .y * other.y
def Vec2::cross(this, other: Vec2<T>): T {
return .x * other.y - .y * other.x
}
def Vec2::length_sq(this): T => .x * .x + .y * .y
def Vec2::length(this): f32 => ((.x * .x + .y * .y) as f32).sqrt()
def Vec2::normalized(this): Vec2f {
let len = .length()
return Vec2f(.x as f32 / len, .y as f32 / len)
}
//! Only intended for {{Vec2}}<{{f32}}>
def Vec2::reflect(this, normal: Vec2<T>): Vec2<T> => .sub(normal.mults(2 as T * .dot(normal)))
def Vec2::rotate(&this, angle: f32): Vec2f {
let c = angle.cos()
let s = angle.sin()
return Vec2f(
.x as f32 * c - .y as f32 * s,
.x as f32 * s + .y as f32 * c,
)
}
[operator "=="]
def Vec2::eq(this, other: Vec2<T>): bool => .x == other.x and .y == other.y
def Vec2::hash(this): u32 => pair_hash(.x.hash(), .y.hash())
def Vec2::to_f32(this): Vec2f => Vec2f(.x as f32, .y as f32)
def Vec2::to_i32(this): Vec2i => Vec2i(.x as i32, .y as i32)
def Vec2::to_u32(this): Vec2u => Vec2u(.x as u32, .y as u32)
def Vec2::to_f64(this): Vec2f64 => Vec2f64(.x as f64, .y as f64)
def Vec2::to_i64(this): Vec2i64 => Vec2i64(.x as i64, .y as i64)
def Vec2::to_u64(this): Vec2u64 => Vec2u64(.x as u64, .y as u64)
/////////// Vec3
// TODO: formatting attribute can't work properly for generic types
[formatting "(%g, %g, %g)" "(f64)$.x, (f64)$.y, (f64)$.z"]
//! Template for a 3D vector
struct Vec3<T> {
x, y, z: T
}
//! Floating point vector, 3d
typedef Vec3f = Vec3<f32>
typedef Vec3i = Vec3<i32>
typedef Vec3u = Vec3<u32>
typedef Vec3f64 = Vec3<f64>
typedef Vec3i64 = Vec3<i64>
typedef Vec3u64 = Vec3<u64>
[operator "+"] def Vec3::add(this, other: Vec3<T>): Vec3<T> => Vec3<T>(.x + other.x, .y + other.y, .z + other.z)
[operator "+"] def Vec3::adds(this, val: T): Vec3<T> => Vec3<T>(.x + val, .y + val, .z + val)
[operator "+"] def Vec3::addrs(val: T, this: Vec3<T>): Vec3<T> => Vec3<T>(val + this.x, val + this.y, val + this.z)
[operator "-"] def Vec3::sub(this, other: Vec3<T>): Vec3<T> => Vec3<T>(.x - other.x, .y - other.y, .z - other.z)
[operator "-"] def Vec3::subs(this, val: T): Vec3<T> => Vec3<T>(.x - val, .y - val, .z - val)
[operator "-"] def Vec3::subrs(val: T, this: Vec3<T>): Vec3<T> => Vec3<T>(val - this.x, val - this.y, val - this.z)
[operator "*"] def Vec3::mult(this, other: Vec3<T>): Vec3<T> => Vec3<T>(.x * other.x, .y * other.y, .z * other.z)
[operator "*"] def Vec3::mults(this, val: T): Vec3<T> => Vec3<T>(.x * val, .y * val, .z * val)
[operator "*"] def Vec3::multrs(val: T, this: Vec3<T>): Vec3<T> => Vec3<T>(val * this.x, val * this.y, val * this.z)
[operator "/"] def Vec3::div(this, other: Vec3<T>): Vec3<T> => Vec3<T>(.x / other.x, .y / other.y, .z / other.z)
[operator "/"] def Vec3::divs(this, val: T): Vec3<T> => Vec3<T>(.x / val, .y / val, .z / val)
[operator "/"] def Vec3::divrs(val: T, this: Vec3<T>): Vec3<T> => Vec3<T>(val / this.x, val / this.y, val / this.z)
[operator "-"] def Vec3::negate(this): Vec3<T> => Vec3<T>(-.x, -.y, -.z)
def Vec3::dot(this, other: Vec3<T>): T => .x * other.x + .y * other.y + .z * other.z
def Vec3::cross(this, other: Vec3<T>): Vec3<T> {
return Vec3<T>(
.y * other.z - .z * other.y,
.z * other.x - .x * other.z,
.x * other.y - .y * other.x,
)
}
def Vec3::length_sq(this): T => .x * .x + .y * .y + .z * .z
def Vec3::length(this): f32 => ((.x * .x + .y * .y + .z * .z) as f32).sqrt()
def Vec3::normalized(this): Vec3f {
let len = .length()
return Vec3f(.x as f32 / len, .y as f32 / len, .z as f32 / len)
}
//! Only intended for {{Vec3}}<{{f32}}>
def Vec3::reflect(this, normal: Vec3<T>): Vec3<T> => .sub(normal.mults(2 as T * .dot(normal)))
def Vec3::rotateX(this, angle: f32): Vec3f {
let c = angle.cos()
let s = angle.sin()
let y = .y as f32 * c - .z as f32 * s
let z = .y as f32 * s + .z as f32 * c
return Vec3f(.x as f32, y, z)
}
def Vec3::rotateY(this, angle: f32): Vec3f {
let c = angle.cos()
let s = angle.sin()
let z = .z as f32 * c - .x as f32 * s
let x = .z as f32 * s + .x as f32 * c
return Vec3f(x, .y as f32, z)
}
def Vec3::rotateZ(this, angle: f32): Vec3f {
let c = angle.cos()
let s = angle.sin()
let x = .x as f32 * c - .y as f32 * s
let y = .x as f32 * s + .y as f32 * c
return Vec3f(x, y, .z as f32)
}
def Vec3::rotate_axis(this, axis: Vec3f, angle: f32): Vec3f {
let c = angle.cos()
let s = angle.sin()
let t = 1.0 - c
axis = axis.normalized()
let f = this.to_f32()
return Vec3f(
f.x * (t * axis.x * axis.x + c) + f.y * (t * axis.x * axis.y - s * axis.z) + f.z * (t * axis.x * axis.z + s * axis.y),
f.x * (t * axis.y * axis.x + s * axis.z) + f.y * (t * axis.y * axis.y + c) + f.z * (t * axis.y * axis.z - s * axis.x),
f.x * (t * axis.z * axis.x - s * axis.y) + f.y * (t * axis.z * axis.y + s * axis.x) + f.z * (t * axis.z * axis.z + c)
)
}
def Vec3::clamp01(this): Vec3<T> {
let zt = 0.0 as T
let ot = 1.0 as T
return Vec3<T>(
if .x < zt then zt else if .x > ot then ot else .x,
if .y < zt then zt else if .y > ot then ot else .y,
if .z < zt then zt else if .z > ot then ot else .z,
)
}
[operator "=="]
def Vec3::eq(this, other: Vec3<T>): bool => .x == other.x and .y == other.y and .z == other.z
def Vec3::hash(this): u32 => pair_hash(.x.hash(), pair_hash(.y.hash(), .z.hash()))
def Vec3::to_f32(this): Vec3f => Vec3f(.x as f32, .y as f32, .z as f32)
def Vec3::to_i32(this): Vec3i => Vec3i(.x as i32, .y as i32, .z as i32)
def Vec3::to_u32(this): Vec3u => Vec3u(.x as u32, .y as u32, .z as u32)
def Vec3::to_f64(this): Vec3f64 => Vec3f64(.x as f64, .y as f64, .z as f64)
def Vec3::to_i64(this): Vec3i64 => Vec3i64(.x as i64, .y as i64, .z as i64)
def Vec3::to_u64(this): Vec3u64 => Vec3u64(.x as u64, .y as u64, .z as u64)
[operator "[]"]
def Vec3::index(this, idx: u32): T => (&this as &T)[idx]