larb 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +14 -1
- data/ext/larb/color.c +446 -0
- data/ext/larb/color.h +35 -0
- data/ext/larb/extconf.rb +11 -0
- data/ext/larb/larb.c +27 -0
- data/ext/larb/larb.h +8 -0
- data/ext/larb/mat2.c +300 -0
- data/ext/larb/mat2.h +30 -0
- data/ext/larb/mat2d.c +380 -0
- data/ext/larb/mat2d.h +35 -0
- data/ext/larb/mat3.c +469 -0
- data/ext/larb/mat3.h +33 -0
- data/ext/larb/mat4.c +671 -0
- data/ext/larb/mat4.h +31 -0
- data/ext/larb/quat.c +523 -0
- data/ext/larb/quat.h +39 -0
- data/ext/larb/quat2.c +473 -0
- data/ext/larb/quat2.h +39 -0
- data/ext/larb/vec2.c +342 -0
- data/ext/larb/vec2.h +43 -0
- data/ext/larb/vec3.c +503 -0
- data/ext/larb/vec3.h +52 -0
- data/ext/larb/vec4.c +340 -0
- data/ext/larb/vec4.h +38 -0
- data/lib/larb/version.rb +5 -0
- data/lib/larb.rb +2 -14
- data/test/larb/color_test.rb +278 -0
- data/test/larb/mat2_test.rb +144 -0
- data/test/larb/mat2d_test.rb +172 -0
- data/test/larb/mat3_test.rb +147 -0
- data/test/larb/mat4_test.rb +270 -0
- data/test/larb/quat2_test.rb +161 -0
- data/test/larb/quat_test.rb +224 -0
- data/test/larb/vec2_test.rb +251 -0
- data/test/larb/vec3_test.rb +310 -0
- data/test/larb/vec4_test.rb +189 -0
- data/test/test_helper.rb +4 -0
- metadata +53 -14
- data/Rakefile +0 -11
- data/lib/larb/color.rb +0 -148
- data/lib/larb/mat2.rb +0 -119
- data/lib/larb/mat2d.rb +0 -180
- data/lib/larb/mat3.rb +0 -238
- data/lib/larb/mat4.rb +0 -329
- data/lib/larb/quat.rb +0 -238
- data/lib/larb/quat2.rb +0 -193
- data/lib/larb/vec2.rb +0 -150
- data/lib/larb/vec3.rb +0 -218
- data/lib/larb/vec4.rb +0 -125
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../test_helper"
|
|
4
|
+
|
|
5
|
+
class Quat2Test < Test::Unit::TestCase
|
|
6
|
+
def test_new_identity_by_default
|
|
7
|
+
q = Larb::Quat2.new
|
|
8
|
+
assert_equal 1.0, q[3]
|
|
9
|
+
assert_equal 0.0, q[7]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_new_with_data
|
|
13
|
+
data = [0, 0, 0, 1, 0, 0, 0, 0]
|
|
14
|
+
q = Larb::Quat2.new(data)
|
|
15
|
+
assert_equal data, q.data
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_identity
|
|
19
|
+
q = Larb::Quat2.identity
|
|
20
|
+
assert_equal Larb::Quat.identity, q.real
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_from_rotation_translation
|
|
24
|
+
rotation = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
25
|
+
translation = Larb::Vec3.new(1, 2, 3)
|
|
26
|
+
q = Larb::Quat2.from_rotation_translation(rotation, translation)
|
|
27
|
+
assert q.rotation.near?(rotation)
|
|
28
|
+
assert q.translation.near?(translation)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_from_translation
|
|
32
|
+
translation = Larb::Vec3.new(5, 10, 15)
|
|
33
|
+
q = Larb::Quat2.from_translation(translation)
|
|
34
|
+
assert q.translation.near?(translation)
|
|
35
|
+
assert q.rotation.near?(Larb::Quat.identity)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_from_rotation
|
|
39
|
+
rotation = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
40
|
+
q = Larb::Quat2.from_rotation(rotation)
|
|
41
|
+
assert q.rotation.near?(rotation)
|
|
42
|
+
assert q.translation.near?(Larb::Vec3.zero)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_real
|
|
46
|
+
q = Larb::Quat2.identity
|
|
47
|
+
assert_instance_of Larb::Quat, q.real
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_dual
|
|
51
|
+
q = Larb::Quat2.identity
|
|
52
|
+
assert_instance_of Larb::Quat, q.dual
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_multiply_quat2
|
|
56
|
+
q1 = Larb::Quat2.from_translation(Larb::Vec3.new(1, 0, 0))
|
|
57
|
+
q2 = Larb::Quat2.from_translation(Larb::Vec3.new(0, 1, 0))
|
|
58
|
+
result = q1 * q2
|
|
59
|
+
assert result.translation.near?(Larb::Vec3.new(1, 1, 0))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_multiply_vec3
|
|
63
|
+
q = Larb::Quat2.from_translation(Larb::Vec3.new(10, 0, 0))
|
|
64
|
+
point = Larb::Vec3.new(0, 0, 0)
|
|
65
|
+
result = q * point
|
|
66
|
+
assert result.near?(Larb::Vec3.new(10, 0, 0))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_add
|
|
70
|
+
q1 = Larb::Quat2.identity
|
|
71
|
+
q2 = Larb::Quat2.identity
|
|
72
|
+
result = q1 + q2
|
|
73
|
+
assert_equal 2.0, result[3]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_subtract
|
|
77
|
+
q1 = Larb::Quat2.identity
|
|
78
|
+
q2 = Larb::Quat2.identity
|
|
79
|
+
result = q1 - q2
|
|
80
|
+
assert_equal 0.0, result[3]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_dot
|
|
84
|
+
q = Larb::Quat2.identity
|
|
85
|
+
assert_equal 1.0, q.dot(q)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def test_length
|
|
89
|
+
q = Larb::Quat2.identity
|
|
90
|
+
assert_equal 1.0, q.length
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_normalize
|
|
94
|
+
q = Larb::Quat2.new([0, 0, 0, 2, 0, 0, 0, 0])
|
|
95
|
+
result = q.normalize
|
|
96
|
+
assert_in_delta 1.0, result.length, 1e-10
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def test_conjugate
|
|
100
|
+
rotation = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
101
|
+
q = Larb::Quat2.from_rotation(rotation)
|
|
102
|
+
conj = q.conjugate
|
|
103
|
+
result = q * conj
|
|
104
|
+
assert result.rotation.near?(Larb::Quat.identity)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def test_inverse
|
|
108
|
+
rotation = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 4)
|
|
109
|
+
translation = Larb::Vec3.new(1, 2, 3)
|
|
110
|
+
q = Larb::Quat2.from_rotation_translation(rotation, translation)
|
|
111
|
+
inv = q.inverse
|
|
112
|
+
result = q * inv
|
|
113
|
+
assert result.rotation.near?(Larb::Quat.identity)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_translation
|
|
117
|
+
t = Larb::Vec3.new(5, 10, 15)
|
|
118
|
+
q = Larb::Quat2.from_translation(t)
|
|
119
|
+
assert q.translation.near?(t)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def test_rotation
|
|
123
|
+
r = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
124
|
+
q = Larb::Quat2.from_rotation(r)
|
|
125
|
+
assert q.rotation.near?(r)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_transform_point
|
|
129
|
+
rotation = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
130
|
+
translation = Larb::Vec3.new(0, 0, 5)
|
|
131
|
+
q = Larb::Quat2.from_rotation_translation(rotation, translation)
|
|
132
|
+
point = Larb::Vec3.new(1, 0, 0)
|
|
133
|
+
result = q.transform_point(point)
|
|
134
|
+
assert_in_delta 0.0, result.x, 1e-10
|
|
135
|
+
assert_in_delta 4.0, result.z, 1e-10
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def test_lerp
|
|
139
|
+
q1 = Larb::Quat2.from_translation(Larb::Vec3.new(0, 0, 0))
|
|
140
|
+
q2 = Larb::Quat2.from_translation(Larb::Vec3.new(10, 0, 0))
|
|
141
|
+
result = q1.lerp(q2, 0.5)
|
|
142
|
+
assert_in_delta 5.0, result.translation.x, 1e-6
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_equality
|
|
146
|
+
q1 = Larb::Quat2.identity
|
|
147
|
+
q2 = Larb::Quat2.identity
|
|
148
|
+
assert_equal q1, q2
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def test_near
|
|
152
|
+
q1 = Larb::Quat2.identity
|
|
153
|
+
q2 = Larb::Quat2.new([0, 0, 0, 1.0000001, 0, 0, 0, 0])
|
|
154
|
+
assert q1.near?(q2)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def test_inspect
|
|
158
|
+
q = Larb::Quat2.identity
|
|
159
|
+
assert_match(/Quat2/, q.inspect)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../test_helper"
|
|
4
|
+
|
|
5
|
+
class QuatTest < Test::Unit::TestCase
|
|
6
|
+
def test_new_with_default_values
|
|
7
|
+
q = Larb::Quat.new
|
|
8
|
+
assert_equal 0.0, q.x
|
|
9
|
+
assert_equal 0.0, q.y
|
|
10
|
+
assert_equal 0.0, q.z
|
|
11
|
+
assert_equal 1.0, q.w
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_new_with_given_values
|
|
15
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
16
|
+
assert_equal 1.0, q.x
|
|
17
|
+
assert_equal 2.0, q.y
|
|
18
|
+
assert_equal 3.0, q.z
|
|
19
|
+
assert_equal 4.0, q.w
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_bracket_syntax
|
|
23
|
+
q = Larb::Quat[1, 2, 3, 4]
|
|
24
|
+
assert_equal 1.0, q.x
|
|
25
|
+
assert_equal 2.0, q.y
|
|
26
|
+
assert_equal 3.0, q.z
|
|
27
|
+
assert_equal 4.0, q.w
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_identity
|
|
31
|
+
q = Larb::Quat.identity
|
|
32
|
+
assert_equal 0.0, q.x
|
|
33
|
+
assert_equal 0.0, q.y
|
|
34
|
+
assert_equal 0.0, q.z
|
|
35
|
+
assert_equal 1.0, q.w
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_from_axis_angle
|
|
39
|
+
q = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
40
|
+
assert_in_delta 1.0, q.length, 1e-10
|
|
41
|
+
assert_in_delta Math.sin(Math::PI / 4), q.y, 1e-10
|
|
42
|
+
assert_in_delta Math.cos(Math::PI / 4), q.w, 1e-10
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_from_euler
|
|
46
|
+
q = Larb::Quat.from_euler(0, Math::PI / 2, 0)
|
|
47
|
+
assert_in_delta 1.0, q.length, 1e-10
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_multiply_quaternions
|
|
51
|
+
q1 = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 4)
|
|
52
|
+
q2 = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 4)
|
|
53
|
+
result = q1 * q2
|
|
54
|
+
expected = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
55
|
+
assert result.near?(expected)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_multiply_rotates_vector
|
|
59
|
+
q = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
60
|
+
v = Larb::Vec3.new(1, 0, 0)
|
|
61
|
+
result = q * v
|
|
62
|
+
assert_in_delta 0.0, result.x, 1e-10
|
|
63
|
+
assert_in_delta(-1.0, result.z, 1e-10)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_multiply_by_scalar
|
|
67
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
68
|
+
result = q * 2
|
|
69
|
+
assert_equal 2.0, result.x
|
|
70
|
+
assert_equal 4.0, result.y
|
|
71
|
+
assert_equal 6.0, result.z
|
|
72
|
+
assert_equal 8.0, result.w
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_add
|
|
76
|
+
q1 = Larb::Quat.new(1, 2, 3, 4)
|
|
77
|
+
q2 = Larb::Quat.new(5, 6, 7, 8)
|
|
78
|
+
result = q1 + q2
|
|
79
|
+
assert_equal 6.0, result.x
|
|
80
|
+
assert_equal 8.0, result.y
|
|
81
|
+
assert_equal 10.0, result.z
|
|
82
|
+
assert_equal 12.0, result.w
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_subtract
|
|
86
|
+
q1 = Larb::Quat.new(5, 6, 7, 8)
|
|
87
|
+
q2 = Larb::Quat.new(1, 2, 3, 4)
|
|
88
|
+
result = q1 - q2
|
|
89
|
+
assert_equal 4.0, result.x
|
|
90
|
+
assert_equal 4.0, result.y
|
|
91
|
+
assert_equal 4.0, result.z
|
|
92
|
+
assert_equal 4.0, result.w
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_negate
|
|
96
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
97
|
+
result = -q
|
|
98
|
+
assert_equal(-1.0, result.x)
|
|
99
|
+
assert_equal(-2.0, result.y)
|
|
100
|
+
assert_equal(-3.0, result.z)
|
|
101
|
+
assert_equal(-4.0, result.w)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_dot
|
|
105
|
+
q1 = Larb::Quat.new(1, 2, 3, 4)
|
|
106
|
+
q2 = Larb::Quat.new(5, 6, 7, 8)
|
|
107
|
+
assert_equal 70.0, q1.dot(q2)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def test_length
|
|
111
|
+
q = Larb::Quat.new(1, 2, 2, 0)
|
|
112
|
+
assert_equal 3.0, q.length
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_length_squared
|
|
116
|
+
q = Larb::Quat.new(1, 2, 2, 0)
|
|
117
|
+
assert_equal 9.0, q.length_squared
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_normalize
|
|
121
|
+
q = Larb::Quat.new(1, 2, 2, 0)
|
|
122
|
+
result = q.normalize
|
|
123
|
+
assert_in_delta 1.0, result.length, 1e-10
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def test_normalize!
|
|
127
|
+
q = Larb::Quat.new(1, 2, 2, 0)
|
|
128
|
+
q.normalize!
|
|
129
|
+
assert_in_delta 1.0, q.length, 1e-10
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def test_conjugate
|
|
133
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
134
|
+
result = q.conjugate
|
|
135
|
+
assert_equal(-1.0, result.x)
|
|
136
|
+
assert_equal(-2.0, result.y)
|
|
137
|
+
assert_equal(-3.0, result.z)
|
|
138
|
+
assert_equal 4.0, result.w
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_inverse
|
|
142
|
+
q = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 4)
|
|
143
|
+
inv = q.inverse
|
|
144
|
+
result = q * inv
|
|
145
|
+
assert result.near?(Larb::Quat.identity)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def test_lerp
|
|
149
|
+
q1 = Larb::Quat.identity
|
|
150
|
+
q2 = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
151
|
+
result = q1.lerp(q2, 0.5)
|
|
152
|
+
assert_in_delta 1.0, result.length, 1e-10
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def test_slerp
|
|
156
|
+
q1 = Larb::Quat.identity
|
|
157
|
+
q2 = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
158
|
+
result = q1.slerp(q2, 0.5)
|
|
159
|
+
expected = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 4)
|
|
160
|
+
assert result.near?(expected, 1e-6)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def test_to_axis_angle
|
|
164
|
+
q = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
165
|
+
axis, angle = q.to_axis_angle
|
|
166
|
+
assert_in_delta Math::PI / 2, angle, 1e-10
|
|
167
|
+
assert_in_delta 1.0, axis.y, 1e-10
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def test_to_euler
|
|
171
|
+
q = Larb::Quat.from_euler(0.1, 0.2, 0.3)
|
|
172
|
+
euler = q.to_euler
|
|
173
|
+
assert_in_delta 0.1, euler.x, 1e-6
|
|
174
|
+
assert_in_delta 0.2, euler.y, 1e-6
|
|
175
|
+
assert_in_delta 0.3, euler.z, 1e-6
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def test_to_mat4
|
|
179
|
+
q = Larb::Quat.from_axis_angle(Larb::Vec3.new(0, 1, 0), Math::PI / 2)
|
|
180
|
+
m = q.to_mat4
|
|
181
|
+
assert_instance_of Larb::Mat4, m
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def test_to_a
|
|
185
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
186
|
+
assert_equal [1.0, 2.0, 3.0, 4.0], q.to_a
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def test_index_access
|
|
190
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
191
|
+
assert_equal 1.0, q[0]
|
|
192
|
+
assert_equal 2.0, q[1]
|
|
193
|
+
assert_equal 3.0, q[2]
|
|
194
|
+
assert_equal 4.0, q[3]
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def test_equality
|
|
198
|
+
q1 = Larb::Quat.new(1, 2, 3, 4)
|
|
199
|
+
q2 = Larb::Quat.new(1, 2, 3, 4)
|
|
200
|
+
assert_equal q1, q2
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def test_inequality
|
|
204
|
+
q1 = Larb::Quat.new(1, 2, 3, 4)
|
|
205
|
+
q2 = Larb::Quat.new(1, 2, 3, 5)
|
|
206
|
+
assert_not_equal q1, q2
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def test_near
|
|
210
|
+
q1 = Larb::Quat.new(1.0, 2.0, 3.0, 4.0)
|
|
211
|
+
q2 = Larb::Quat.new(1.0000001, 2.0000001, 3.0000001, 4.0000001)
|
|
212
|
+
assert q1.near?(q2)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def test_inspect
|
|
216
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
217
|
+
assert_equal "Quat[1.0, 2.0, 3.0, 4.0]", q.inspect
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def test_to_s
|
|
221
|
+
q = Larb::Quat.new(1, 2, 3, 4)
|
|
222
|
+
assert_equal q.inspect, q.to_s
|
|
223
|
+
end
|
|
224
|
+
end
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../test_helper"
|
|
4
|
+
|
|
5
|
+
class Vec2Test < Test::Unit::TestCase
|
|
6
|
+
def test_new_with_default_values
|
|
7
|
+
v = Larb::Vec2.new
|
|
8
|
+
assert_equal 0.0, v.x
|
|
9
|
+
assert_equal 0.0, v.y
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_new_with_given_values
|
|
13
|
+
v = Larb::Vec2.new(3, 4)
|
|
14
|
+
assert_equal 3.0, v.x
|
|
15
|
+
assert_equal 4.0, v.y
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_bracket_syntax
|
|
19
|
+
v = Larb::Vec2[1, 2]
|
|
20
|
+
assert_equal 1.0, v.x
|
|
21
|
+
assert_equal 2.0, v.y
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_zero
|
|
25
|
+
v = Larb::Vec2.zero
|
|
26
|
+
assert_equal 0.0, v.x
|
|
27
|
+
assert_equal 0.0, v.y
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_one
|
|
31
|
+
v = Larb::Vec2.one
|
|
32
|
+
assert_equal 1.0, v.x
|
|
33
|
+
assert_equal 1.0, v.y
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_add
|
|
37
|
+
v1 = Larb::Vec2.new(1, 2)
|
|
38
|
+
v2 = Larb::Vec2.new(3, 4)
|
|
39
|
+
result = v1 + v2
|
|
40
|
+
assert_equal 4.0, result.x
|
|
41
|
+
assert_equal 6.0, result.y
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_subtract
|
|
45
|
+
v1 = Larb::Vec2.new(5, 7)
|
|
46
|
+
v2 = Larb::Vec2.new(2, 3)
|
|
47
|
+
result = v1 - v2
|
|
48
|
+
assert_equal 3.0, result.x
|
|
49
|
+
assert_equal 4.0, result.y
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_multiply_by_scalar
|
|
53
|
+
v = Larb::Vec2.new(2, 3)
|
|
54
|
+
result = v * 2
|
|
55
|
+
assert_equal 4.0, result.x
|
|
56
|
+
assert_equal 6.0, result.y
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_divide_by_scalar
|
|
60
|
+
v = Larb::Vec2.new(6, 8)
|
|
61
|
+
result = v / 2
|
|
62
|
+
assert_equal 3.0, result.x
|
|
63
|
+
assert_equal 4.0, result.y
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_negate
|
|
67
|
+
v = Larb::Vec2.new(1, -2)
|
|
68
|
+
result = -v
|
|
69
|
+
assert_equal(-1.0, result.x)
|
|
70
|
+
assert_equal 2.0, result.y
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_dot
|
|
74
|
+
v1 = Larb::Vec2.new(1, 2)
|
|
75
|
+
v2 = Larb::Vec2.new(3, 4)
|
|
76
|
+
assert_equal 11.0, v1.dot(v2)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_length
|
|
80
|
+
v = Larb::Vec2.new(3, 4)
|
|
81
|
+
assert_equal 5.0, v.length
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_length_squared
|
|
85
|
+
v = Larb::Vec2.new(3, 4)
|
|
86
|
+
assert_equal 25.0, v.length_squared
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_normalize
|
|
90
|
+
v = Larb::Vec2.new(3, 4)
|
|
91
|
+
result = v.normalize
|
|
92
|
+
assert_in_delta 0.6, result.x, 1e-10
|
|
93
|
+
assert_in_delta 0.8, result.y, 1e-10
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_normalize!
|
|
97
|
+
v = Larb::Vec2.new(3, 4)
|
|
98
|
+
v.normalize!
|
|
99
|
+
assert_in_delta 0.6, v.x, 1e-10
|
|
100
|
+
assert_in_delta 0.8, v.y, 1e-10
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def test_lerp
|
|
104
|
+
v1 = Larb::Vec2.new(0, 0)
|
|
105
|
+
v2 = Larb::Vec2.new(10, 20)
|
|
106
|
+
result = v1.lerp(v2, 0.5)
|
|
107
|
+
assert_equal 5.0, result.x
|
|
108
|
+
assert_equal 10.0, result.y
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def test_to_a
|
|
112
|
+
v = Larb::Vec2.new(1, 2)
|
|
113
|
+
assert_equal [1.0, 2.0], v.to_a
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_index_access
|
|
117
|
+
v = Larb::Vec2.new(1, 2)
|
|
118
|
+
assert_equal 1.0, v[0]
|
|
119
|
+
assert_equal 2.0, v[1]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def test_index_assignment
|
|
123
|
+
v = Larb::Vec2.new(0, 0)
|
|
124
|
+
v[0] = 5
|
|
125
|
+
v[1] = 6
|
|
126
|
+
assert_equal 5, v.x
|
|
127
|
+
assert_equal 6, v.y
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_equality
|
|
131
|
+
v1 = Larb::Vec2.new(1, 2)
|
|
132
|
+
v2 = Larb::Vec2.new(1, 2)
|
|
133
|
+
assert_equal v1, v2
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def test_inequality
|
|
137
|
+
v1 = Larb::Vec2.new(1, 2)
|
|
138
|
+
v2 = Larb::Vec2.new(1, 3)
|
|
139
|
+
assert_not_equal v1, v2
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def test_not_equal_to_array
|
|
143
|
+
v = Larb::Vec2.new(1, 2)
|
|
144
|
+
assert_not_equal v, [1, 2]
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def test_near_true
|
|
148
|
+
v1 = Larb::Vec2.new(1.0, 2.0)
|
|
149
|
+
v2 = Larb::Vec2.new(1.0000001, 2.0000001)
|
|
150
|
+
assert v1.near?(v2)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_near_false
|
|
154
|
+
v1 = Larb::Vec2.new(1.0, 2.0)
|
|
155
|
+
v2 = Larb::Vec2.new(1.1, 2.1)
|
|
156
|
+
assert_false v1.near?(v2)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_angle
|
|
160
|
+
v = Larb::Vec2.new(1, 0)
|
|
161
|
+
assert_equal 0.0, v.angle
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def test_angle_up
|
|
165
|
+
v = Larb::Vec2.new(0, 1)
|
|
166
|
+
assert_in_delta Math::PI / 2, v.angle, 1e-10
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def test_angle_to
|
|
170
|
+
v1 = Larb::Vec2.new(0, 0)
|
|
171
|
+
v2 = Larb::Vec2.new(1, 0)
|
|
172
|
+
assert_equal 0.0, v1.angle_to(v2)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def test_rotate
|
|
176
|
+
v = Larb::Vec2.new(1, 0)
|
|
177
|
+
result = v.rotate(Math::PI / 2)
|
|
178
|
+
assert_in_delta 0.0, result.x, 1e-10
|
|
179
|
+
assert_in_delta 1.0, result.y, 1e-10
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def test_distance
|
|
183
|
+
v1 = Larb::Vec2.new(0, 0)
|
|
184
|
+
v2 = Larb::Vec2.new(3, 4)
|
|
185
|
+
assert_equal 5.0, v1.distance(v2)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def test_distance_squared
|
|
189
|
+
v1 = Larb::Vec2.new(0, 0)
|
|
190
|
+
v2 = Larb::Vec2.new(3, 4)
|
|
191
|
+
assert_equal 25.0, v1.distance_squared(v2)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def test_cross
|
|
195
|
+
v1 = Larb::Vec2.new(1, 0)
|
|
196
|
+
v2 = Larb::Vec2.new(0, 1)
|
|
197
|
+
assert_equal 1.0, v1.cross(v2)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def test_perpendicular
|
|
201
|
+
v = Larb::Vec2.new(1, 0)
|
|
202
|
+
result = v.perpendicular
|
|
203
|
+
assert_equal 0.0, result.x
|
|
204
|
+
assert_equal 1.0, result.y
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def test_reflect
|
|
208
|
+
v = Larb::Vec2.new(1, -1).normalize
|
|
209
|
+
normal = Larb::Vec2.new(0, 1)
|
|
210
|
+
result = v.reflect(normal)
|
|
211
|
+
assert_in_delta v.x, result.x, 1e-10
|
|
212
|
+
assert_in_delta(-v.y, result.y, 1e-10)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def test_clamp_length
|
|
216
|
+
v = Larb::Vec2.new(3, 4)
|
|
217
|
+
result = v.clamp_length(2.5)
|
|
218
|
+
assert_in_delta 2.5, result.length, 1e-10
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def test_clamp_length_under_limit
|
|
222
|
+
v = Larb::Vec2.new(1, 1)
|
|
223
|
+
result = v.clamp_length(10)
|
|
224
|
+
assert_equal v, result
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def test_to_vec3
|
|
228
|
+
v = Larb::Vec2.new(1, 2)
|
|
229
|
+
result = v.to_vec3
|
|
230
|
+
assert_instance_of Larb::Vec3, result
|
|
231
|
+
assert_equal 1.0, result.x
|
|
232
|
+
assert_equal 2.0, result.y
|
|
233
|
+
assert_equal 0.0, result.z
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def test_to_vec3_with_custom_z
|
|
237
|
+
v = Larb::Vec2.new(1, 2)
|
|
238
|
+
result = v.to_vec3(5)
|
|
239
|
+
assert_equal 5.0, result.z
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def test_inspect
|
|
243
|
+
v = Larb::Vec2.new(1, 2)
|
|
244
|
+
assert_equal "Vec2[1.0, 2.0]", v.inspect
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def test_to_s
|
|
248
|
+
v = Larb::Vec2.new(1, 2)
|
|
249
|
+
assert_equal v.inspect, v.to_s
|
|
250
|
+
end
|
|
251
|
+
end
|