rmath3d_plain 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ChangeLog +82 -0
- data/LICENSE.txt +21 -0
- data/README.md +178 -0
- data/lib/rmath3d/rmath3d_plain.rb +3311 -0
- data/sample/opengl-bindings/load_matrix.rb +174 -0
- data/sample/opengl2/load_matrix.rb +118 -0
- data/sample/simple/transform.rb +11 -0
- data/test/test.rb +18 -0
- data/test/test_RMtx3.rb +517 -0
- data/test/test_RMtx4.rb +735 -0
- data/test/test_RQuat.rb +381 -0
- data/test/test_RVec3.rb +308 -0
- data/test/test_RVec4.rb +287 -0
- metadata +58 -0
data/test/test_RVec4.rb
ADDED
@@ -0,0 +1,287 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
class TC_RVec4 < Minitest::Test
|
3
|
+
def setup
|
4
|
+
@tolerance = RMath3D::TOLERANCE
|
5
|
+
@zero = RVec4.new( 0, 0, 0, 0 )
|
6
|
+
@ax = RVec3.new( 1, 0, 0 )
|
7
|
+
@ay = RVec3.new( 0, 1, 0 )
|
8
|
+
@az = RVec3.new( 0, 0, 1 )
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_initialize
|
15
|
+
v0 = RVec4.new
|
16
|
+
assert_in_delta( 0, v0.x, @tolerance )
|
17
|
+
assert_in_delta( 0, v0.y, @tolerance )
|
18
|
+
assert_in_delta( 0, v0.z, @tolerance )
|
19
|
+
assert_in_delta( 0, v0.w, @tolerance )
|
20
|
+
|
21
|
+
v1 = RVec4.new( 1, 2, 3, 4 )
|
22
|
+
assert_in_delta( 1, v1.x, @tolerance )
|
23
|
+
assert_in_delta( 2, v1.y, @tolerance )
|
24
|
+
assert_in_delta( 3, v1.z, @tolerance )
|
25
|
+
assert_in_delta( 4, v1.w, @tolerance )
|
26
|
+
|
27
|
+
v2 = RVec4.new( v1 )
|
28
|
+
assert_in_delta( 1, v2.x, @tolerance )
|
29
|
+
assert_in_delta( 2, v2.y, @tolerance )
|
30
|
+
assert_in_delta( 3, v2.z, @tolerance )
|
31
|
+
assert_in_delta( 4, v2.w, @tolerance )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_to_s
|
35
|
+
assert_respond_to( @zero, :to_s )
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_coerce
|
39
|
+
assert_respond_to( @zero, :coerce )
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_setElements
|
43
|
+
v = RVec4.new
|
44
|
+
v.setElements( 1, 2, 3, 4 )
|
45
|
+
assert_in_delta( 1, v.x, @tolerance )
|
46
|
+
assert_in_delta( 2, v.y, @tolerance )
|
47
|
+
assert_in_delta( 3, v.z, @tolerance )
|
48
|
+
assert_in_delta( 4, v.w, @tolerance )
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_setElement
|
52
|
+
v = RVec4.new
|
53
|
+
|
54
|
+
# x=
|
55
|
+
v.x = 1
|
56
|
+
assert_in_delta( 1, v.x, @tolerance )
|
57
|
+
v[0] = 2
|
58
|
+
assert_in_delta( 2, v.x, @tolerance )
|
59
|
+
|
60
|
+
# y=
|
61
|
+
v.y = 1
|
62
|
+
assert_in_delta( 1, v.y, @tolerance )
|
63
|
+
v[1] = 2
|
64
|
+
assert_in_delta( 2, v.y, @tolerance )
|
65
|
+
|
66
|
+
# z=
|
67
|
+
v.z = 1
|
68
|
+
assert_in_delta( 1, v.z, @tolerance )
|
69
|
+
v[2] = 2
|
70
|
+
assert_in_delta( 2, v.z, @tolerance )
|
71
|
+
|
72
|
+
# w=
|
73
|
+
v.w = 1
|
74
|
+
assert_in_delta( 1, v.w, @tolerance )
|
75
|
+
v[3] = 2
|
76
|
+
assert_in_delta( 2, v.w, @tolerance )
|
77
|
+
|
78
|
+
# xyz=
|
79
|
+
v3 = RVec3.new( 4, 5, 6 )
|
80
|
+
v.xyz = v3
|
81
|
+
assert_in_delta( 4, v.x, @tolerance )
|
82
|
+
assert_in_delta( 5, v.y, @tolerance )
|
83
|
+
assert_in_delta( 6, v.z, @tolerance )
|
84
|
+
|
85
|
+
v3 = RVec3.new( 7, 8, 9 )
|
86
|
+
v.xyz = v3
|
87
|
+
assert_in_delta( 7, v.x, @tolerance )
|
88
|
+
assert_in_delta( 8, v.y, @tolerance )
|
89
|
+
assert_in_delta( 9, v.z, @tolerance )
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_getElement
|
93
|
+
axis = RVec4.new(1,2,3,4)
|
94
|
+
assert_in_delta( 1, axis[0], @tolerance )
|
95
|
+
assert_in_delta( 2, axis[1], @tolerance )
|
96
|
+
assert_in_delta( 3, axis[2], @tolerance )
|
97
|
+
assert_in_delta( 4, axis[3], @tolerance )
|
98
|
+
|
99
|
+
assert_in_delta( 1, axis.x, @tolerance )
|
100
|
+
assert_in_delta( 2, axis.y, @tolerance )
|
101
|
+
assert_in_delta( 3, axis.z, @tolerance )
|
102
|
+
assert_in_delta( 4, axis.w, @tolerance )
|
103
|
+
|
104
|
+
# RVec4#xyz
|
105
|
+
v3 = axis.xyz
|
106
|
+
assert_kind_of( RVec3, v3 )
|
107
|
+
assert_in_delta( 1, v3.x, @tolerance )
|
108
|
+
assert_in_delta( 2, v3.y, @tolerance )
|
109
|
+
assert_in_delta( 3, v3.z, @tolerance )
|
110
|
+
|
111
|
+
assert_kind_of( RVec3, v3 )
|
112
|
+
assert_in_delta( 1, v3[0], @tolerance )
|
113
|
+
assert_in_delta( 2, v3[1], @tolerance )
|
114
|
+
assert_in_delta( 3, v3[2], @tolerance )
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_getLength
|
118
|
+
len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 4.0*4.0
|
119
|
+
len = Math.sqrt( len_sq )
|
120
|
+
|
121
|
+
v = RVec4.new( 1, 2, 3, 4 )
|
122
|
+
assert_in_delta( len_sq, v.getLengthSq, @tolerance )
|
123
|
+
assert_in_delta( len , v.getLength , @tolerance )
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_normalize
|
127
|
+
len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 4.0*4.0
|
128
|
+
len = Math.sqrt( len_sq )
|
129
|
+
x = 1.0 / len
|
130
|
+
y = 2.0 / len
|
131
|
+
z = 3.0 / len
|
132
|
+
w = 4.0 / len
|
133
|
+
v = RVec4.new( 1, 2, 3, 4 )
|
134
|
+
# getNormalized
|
135
|
+
v0 = v.getNormalized
|
136
|
+
assert_in_delta( x, v0.x, @tolerance )
|
137
|
+
assert_in_delta( y, v0.y, @tolerance )
|
138
|
+
assert_in_delta( z, v0.z, @tolerance )
|
139
|
+
assert_in_delta( w, v0.w, @tolerance )
|
140
|
+
|
141
|
+
# normalize!
|
142
|
+
v.normalize!
|
143
|
+
assert_in_delta( x, v.x, @tolerance )
|
144
|
+
assert_in_delta( y, v.y, @tolerance )
|
145
|
+
assert_in_delta( z, v.z, @tolerance )
|
146
|
+
assert_in_delta( w, v.w, @tolerance )
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_unary_operators
|
150
|
+
v = RVec4.new( 1, 2, 3, 4 )
|
151
|
+
|
152
|
+
# RVec4#+@
|
153
|
+
vp = +v
|
154
|
+
assert_in_delta( 1, vp.x, @tolerance )
|
155
|
+
assert_in_delta( 2, vp.y, @tolerance )
|
156
|
+
assert_in_delta( 3, vp.z, @tolerance )
|
157
|
+
assert_in_delta( 4, vp.w, @tolerance )
|
158
|
+
|
159
|
+
# RVec4#-@
|
160
|
+
vm = -v
|
161
|
+
assert_in_delta( -1, vm.x, @tolerance )
|
162
|
+
assert_in_delta( -2, vm.y, @tolerance )
|
163
|
+
assert_in_delta( -3, vm.z, @tolerance )
|
164
|
+
assert_in_delta( -4, vm.w, @tolerance )
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_plus_operations
|
168
|
+
v0 = RVec4.new( 1, 1, 1, 1 )
|
169
|
+
v1 = RVec4.new( 2, 2, 2, 2 )
|
170
|
+
|
171
|
+
# RVec4#+
|
172
|
+
vr = v0 + v1
|
173
|
+
assert_in_delta( 3, vr.x, @tolerance )
|
174
|
+
assert_in_delta( 3, vr.y, @tolerance )
|
175
|
+
assert_in_delta( 3, vr.z, @tolerance )
|
176
|
+
assert_in_delta( 3, vr.w, @tolerance )
|
177
|
+
|
178
|
+
# RVec4#add!
|
179
|
+
v0.add!( v1 )
|
180
|
+
assert_in_delta( 3, v0.x, @tolerance )
|
181
|
+
assert_in_delta( 3, v0.y, @tolerance )
|
182
|
+
assert_in_delta( 3, v0.z, @tolerance )
|
183
|
+
assert_in_delta( 3, v0.w, @tolerance )
|
184
|
+
|
185
|
+
assert_raises( TypeError ) { v0 + 1.0 }
|
186
|
+
assert_raises( TypeError ) { 1.0 + v0 }
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_minus_operations
|
190
|
+
v0 = RVec4.new( 1, 1, 1, 1 )
|
191
|
+
v1 = RVec4.new( 2, 2, 2, 2 )
|
192
|
+
|
193
|
+
# RVec4#-
|
194
|
+
vr = v0 - v1
|
195
|
+
assert_in_delta( -1, vr.x, @tolerance )
|
196
|
+
assert_in_delta( -1, vr.y, @tolerance )
|
197
|
+
assert_in_delta( -1, vr.z, @tolerance )
|
198
|
+
assert_in_delta( -1, vr.w, @tolerance )
|
199
|
+
|
200
|
+
# RVec4#sub!
|
201
|
+
v0.sub!( v1 )
|
202
|
+
assert_in_delta( -1, v0.x, @tolerance )
|
203
|
+
assert_in_delta( -1, v0.y, @tolerance )
|
204
|
+
assert_in_delta( -1, v0.z, @tolerance )
|
205
|
+
assert_in_delta( -1, v0.w, @tolerance )
|
206
|
+
|
207
|
+
assert_raises( TypeError ) { v0 - 1.0 }
|
208
|
+
assert_raises( TypeError ) { 1.0 - v0 }
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_mult_operations
|
212
|
+
v0 = RVec4.new( 1, 1, 1, 1 )
|
213
|
+
|
214
|
+
vr = v0 * 2.0
|
215
|
+
assert_in_delta( 2.0, vr.x, @tolerance )
|
216
|
+
assert_in_delta( 2.0, vr.y, @tolerance )
|
217
|
+
assert_in_delta( 2.0, vr.z, @tolerance )
|
218
|
+
assert_in_delta( 2.0, vr.w, @tolerance )
|
219
|
+
|
220
|
+
vr = 2.0 * v0
|
221
|
+
assert_in_delta( 2.0, vr.x, @tolerance )
|
222
|
+
assert_in_delta( 2.0, vr.y, @tolerance )
|
223
|
+
assert_in_delta( 2.0, vr.z, @tolerance )
|
224
|
+
assert_in_delta( 2.0, vr.w, @tolerance )
|
225
|
+
|
226
|
+
v0.mul!( 2.0 )
|
227
|
+
assert_in_delta( 2.0, v0.x, @tolerance )
|
228
|
+
assert_in_delta( 2.0, v0.y, @tolerance )
|
229
|
+
assert_in_delta( 2.0, v0.z, @tolerance )
|
230
|
+
assert_in_delta( 2.0, v0.w, @tolerance )
|
231
|
+
|
232
|
+
assert_raises( TypeError ) { v0 * @zero }
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_equality_operations
|
236
|
+
v = RVec4.new
|
237
|
+
assert( v == @zero )
|
238
|
+
|
239
|
+
v0 = RVec4.new(1,2,3,4)
|
240
|
+
assert( v != v0 )
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_dot
|
244
|
+
v0 = RVec4.new( 1, 1, 1, 1 )
|
245
|
+
v1 = RVec4.new( 2, 2, 2, 2 )
|
246
|
+
ax = RVec4.new( 1, 0, 0, 0 )
|
247
|
+
ay = RVec4.new( 0, 1, 0, 0 )
|
248
|
+
|
249
|
+
assert_in_delta( 8.0, RVec4.dot(v0, v1), @tolerance )
|
250
|
+
assert_in_delta( 0.0, RVec4.dot(ax, ay), @tolerance )
|
251
|
+
# assert_raises( TypeError ) { RVec4.dot(ax, 1.0) }
|
252
|
+
# assert_raises( TypeError ) { RVec4.dot(1.0, ax) }
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_transform
|
256
|
+
m = RMtx4.new.rotationX( Math::PI/4.0 )
|
257
|
+
az = RVec4.new( 0.0, 0.0, 1.0, 1.0 )
|
258
|
+
va = RVec4.new( 0.0, -Math.sqrt(2)/2, Math.sqrt(2)/2, 1.0 )
|
259
|
+
|
260
|
+
vr = az.transform( m )
|
261
|
+
assert_kind_of( RVec4, vr )
|
262
|
+
assert_in_delta( va.x, vr.x, @tolerance )
|
263
|
+
assert_in_delta( va.y, vr.y, @tolerance )
|
264
|
+
assert_in_delta( va.z, vr.z, @tolerance )
|
265
|
+
assert_in_delta( va.w, vr.w, @tolerance )
|
266
|
+
|
267
|
+
m = RMtx4.new.translation( 1.0, 1.0, 1.0 )
|
268
|
+
va = RVec4.new( 1.0, 1.0, 2.0, 1.0 )
|
269
|
+
|
270
|
+
vr = az.transform( m )
|
271
|
+
|
272
|
+
assert_kind_of( RVec4, vr )
|
273
|
+
assert_in_delta( va.x, vr.x, @tolerance )
|
274
|
+
assert_in_delta( va.y, vr.y, @tolerance )
|
275
|
+
assert_in_delta( va.z, vr.z, @tolerance )
|
276
|
+
assert_in_delta( va.w, vr.w, @tolerance )
|
277
|
+
|
278
|
+
az.transform!( m )
|
279
|
+
|
280
|
+
assert_kind_of( RVec4, vr )
|
281
|
+
assert_in_delta( va.x, az.x, @tolerance )
|
282
|
+
assert_in_delta( va.y, az.y, @tolerance )
|
283
|
+
assert_in_delta( va.z, az.z, @tolerance )
|
284
|
+
assert_in_delta( va.w, az.w, @tolerance )
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rmath3d_plain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vaiorabbit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Provides vector3/4, matrix3x3/4x4 and quaternion in plain Ruby form.
|
15
|
+
email:
|
16
|
+
- vaiorabbit@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/rmath3d/rmath3d_plain.rb
|
22
|
+
- sample/opengl-bindings/load_matrix.rb
|
23
|
+
- sample/opengl2/load_matrix.rb
|
24
|
+
- sample/simple/transform.rb
|
25
|
+
- test/test.rb
|
26
|
+
- test/test_RMtx3.rb
|
27
|
+
- test/test_RMtx4.rb
|
28
|
+
- test/test_RQuat.rb
|
29
|
+
- test/test_RVec3.rb
|
30
|
+
- test/test_RVec4.rb
|
31
|
+
- ChangeLog
|
32
|
+
- LICENSE.txt
|
33
|
+
- README.md
|
34
|
+
homepage: https://github.com/vaiorabbit/rmath3d
|
35
|
+
licenses:
|
36
|
+
- zlib/libpng
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.1.4
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Ruby Math Module for 3D Applications
|
58
|
+
test_files: []
|