larb 0.1.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.
data/lib/larb/vec3.rb ADDED
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Larb
4
+ class Vec3
5
+ attr_accessor :x, :y, :z
6
+
7
+ def initialize(x = 0.0, y = 0.0, z = 0.0)
8
+ @x = x.to_f
9
+ @y = y.to_f
10
+ @z = z.to_f
11
+ end
12
+
13
+ def self.[](x, y, z)
14
+ new(x, y, z)
15
+ end
16
+
17
+ def self.zero
18
+ new(0, 0, 0)
19
+ end
20
+
21
+ def self.one
22
+ new(1, 1, 1)
23
+ end
24
+
25
+ def self.up
26
+ new(0, 1, 0)
27
+ end
28
+
29
+ def self.down
30
+ new(0, -1, 0)
31
+ end
32
+
33
+ def self.forward
34
+ new(0, 0, -1)
35
+ end
36
+
37
+ def self.back
38
+ new(0, 0, 1)
39
+ end
40
+
41
+ def self.right
42
+ new(1, 0, 0)
43
+ end
44
+
45
+ def self.left
46
+ new(-1, 0, 0)
47
+ end
48
+
49
+ def +(other)
50
+ Vec3.new(@x + other.x, @y + other.y, @z + other.z)
51
+ end
52
+
53
+ def -(other)
54
+ Vec3.new(@x - other.x, @y - other.y, @z - other.z)
55
+ end
56
+
57
+ def *(scalar)
58
+ case scalar
59
+ when Numeric
60
+ Vec3.new(@x * scalar, @y * scalar, @z * scalar)
61
+ when Vec3
62
+ Vec3.new(@x * scalar.x, @y * scalar.y, @z * scalar.z)
63
+ end
64
+ end
65
+
66
+ def /(scalar)
67
+ Vec3.new(@x / scalar, @y / scalar, @z / scalar)
68
+ end
69
+
70
+ def -@
71
+ Vec3.new(-@x, -@y, -@z)
72
+ end
73
+
74
+ def dot(other)
75
+ @x * other.x + @y * other.y + @z * other.z
76
+ end
77
+
78
+ def cross(other)
79
+ Vec3.new(
80
+ @y * other.z - @z * other.y,
81
+ @z * other.x - @x * other.z,
82
+ @x * other.y - @y * other.x
83
+ )
84
+ end
85
+
86
+ def length
87
+ Math.sqrt(@x * @x + @y * @y + @z * @z)
88
+ end
89
+
90
+ def length_squared
91
+ @x * @x + @y * @y + @z * @z
92
+ end
93
+
94
+ def normalize
95
+ self / length
96
+ end
97
+
98
+ def reflect(normal)
99
+ self - normal * (2 * dot(normal))
100
+ end
101
+
102
+ def xy
103
+ Vec2.new(@x, @y)
104
+ end
105
+
106
+ def xz
107
+ Vec2.new(@x, @z)
108
+ end
109
+
110
+ def yz
111
+ Vec2.new(@y, @z)
112
+ end
113
+
114
+ def lerp(other, t)
115
+ self + (other - self) * t
116
+ end
117
+
118
+ def to_a
119
+ [@x, @y, @z]
120
+ end
121
+
122
+ def to_vec4(w = 1.0)
123
+ Vec4.new(@x, @y, @z, w)
124
+ end
125
+
126
+ def [](i)
127
+ to_a[i]
128
+ end
129
+
130
+ def ==(other)
131
+ return false unless other.is_a?(Vec3)
132
+
133
+ @x == other.x && @y == other.y && @z == other.z
134
+ end
135
+
136
+ def near?(other, epsilon = 1e-6)
137
+ (@x - other.x).abs < epsilon &&
138
+ (@y - other.y).abs < epsilon &&
139
+ (@z - other.z).abs < epsilon
140
+ end
141
+
142
+ def distance(other)
143
+ Math.sqrt(distance_squared(other))
144
+ end
145
+
146
+ def distance_squared(other)
147
+ dx = @x - other.x
148
+ dy = @y - other.y
149
+ dz = @z - other.z
150
+ dx * dx + dy * dy + dz * dz
151
+ end
152
+
153
+ def angle_between(other)
154
+ d = dot(other) / (length * other.length)
155
+ Math.acos(d.clamp(-1.0, 1.0))
156
+ end
157
+
158
+ def project(onto)
159
+ onto * (dot(onto) / onto.length_squared)
160
+ end
161
+
162
+ def reject(from)
163
+ self - project(from)
164
+ end
165
+
166
+ def slerp(other, t)
167
+ dot_val = normalize.dot(other.normalize).clamp(-1.0, 1.0)
168
+ theta = Math.acos(dot_val) * t
169
+ relative = (other - self * dot_val).normalize
170
+ self * Math.cos(theta) + relative * Math.sin(theta)
171
+ end
172
+
173
+ def clamp_length(max_length)
174
+ len_sq = length_squared
175
+ return self if len_sq <= max_length * max_length
176
+
177
+ self * (max_length / Math.sqrt(len_sq))
178
+ end
179
+
180
+ def normalize!
181
+ l = length
182
+ @x /= l
183
+ @y /= l
184
+ @z /= l
185
+ self
186
+ end
187
+
188
+ def min(other)
189
+ Vec3.new([@x, other.x].min, [@y, other.y].min, [@z, other.z].min)
190
+ end
191
+
192
+ def max(other)
193
+ Vec3.new([@x, other.x].max, [@y, other.y].max, [@z, other.z].max)
194
+ end
195
+
196
+ def abs
197
+ Vec3.new(@x.abs, @y.abs, @z.abs)
198
+ end
199
+
200
+ def floor
201
+ Vec3.new(@x.floor, @y.floor, @z.floor)
202
+ end
203
+
204
+ def ceil
205
+ Vec3.new(@x.ceil, @y.ceil, @z.ceil)
206
+ end
207
+
208
+ def round
209
+ Vec3.new(@x.round, @y.round, @z.round)
210
+ end
211
+
212
+ def inspect
213
+ "Vec3[#{@x}, #{@y}, #{@z}]"
214
+ end
215
+
216
+ alias to_s inspect
217
+ end
218
+ end
data/lib/larb/vec4.rb ADDED
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Larb
4
+ class Vec4
5
+ attr_accessor :x, :y, :z, :w
6
+
7
+ def initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0)
8
+ @x = x.to_f
9
+ @y = y.to_f
10
+ @z = z.to_f
11
+ @w = w.to_f
12
+ end
13
+
14
+ def self.[](x, y, z, w = 1.0)
15
+ new(x, y, z, w)
16
+ end
17
+
18
+ def self.zero
19
+ new(0, 0, 0, 0)
20
+ end
21
+
22
+ def self.one
23
+ new(1, 1, 1, 1)
24
+ end
25
+
26
+ def +(other)
27
+ Vec4.new(@x + other.x, @y + other.y, @z + other.z, @w + other.w)
28
+ end
29
+
30
+ def -(other)
31
+ Vec4.new(@x - other.x, @y - other.y, @z - other.z, @w - other.w)
32
+ end
33
+
34
+ def *(scalar)
35
+ Vec4.new(@x * scalar, @y * scalar, @z * scalar, @w * scalar)
36
+ end
37
+
38
+ def /(scalar)
39
+ Vec4.new(@x / scalar, @y / scalar, @z / scalar, @w / scalar)
40
+ end
41
+
42
+ def -@
43
+ Vec4.new(-@x, -@y, -@z, -@w)
44
+ end
45
+
46
+ def dot(other)
47
+ @x * other.x + @y * other.y + @z * other.z + @w * other.w
48
+ end
49
+
50
+ def length
51
+ Math.sqrt(length_squared)
52
+ end
53
+
54
+ def length_squared
55
+ @x * @x + @y * @y + @z * @z + @w * @w
56
+ end
57
+
58
+ def normalize
59
+ self / length
60
+ end
61
+
62
+ def normalize!
63
+ l = length
64
+ @x /= l
65
+ @y /= l
66
+ @z /= l
67
+ @w /= l
68
+ self
69
+ end
70
+
71
+ def perspective_divide
72
+ return Vec3.new(@x, @y, @z) if @w == 0 || @w == 1
73
+
74
+ Vec3.new(@x / @w, @y / @w, @z / @w)
75
+ end
76
+
77
+ def xyz
78
+ Vec3.new(@x, @y, @z)
79
+ end
80
+
81
+ def xy
82
+ Vec2.new(@x, @y)
83
+ end
84
+
85
+ def rgb
86
+ Vec3.new(@x, @y, @z)
87
+ end
88
+
89
+ def to_a
90
+ [@x, @y, @z, @w]
91
+ end
92
+
93
+ def [](i)
94
+ to_a[i]
95
+ end
96
+
97
+ def ==(other)
98
+ return false unless other.is_a?(Vec4)
99
+
100
+ @x == other.x && @y == other.y && @z == other.z && @w == other.w
101
+ end
102
+
103
+ def near?(other, epsilon = 1e-6)
104
+ (@x - other.x).abs < epsilon &&
105
+ (@y - other.y).abs < epsilon &&
106
+ (@z - other.z).abs < epsilon &&
107
+ (@w - other.w).abs < epsilon
108
+ end
109
+
110
+ def lerp(other, t)
111
+ Vec4.new(
112
+ @x + (other.x - @x) * t,
113
+ @y + (other.y - @y) * t,
114
+ @z + (other.z - @z) * t,
115
+ @w + (other.w - @w) * t
116
+ )
117
+ end
118
+
119
+ def inspect
120
+ "Vec4[#{@x}, #{@y}, #{@z}, #{@w}]"
121
+ end
122
+
123
+ alias to_s inspect
124
+ end
125
+ end
data/lib/larb.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "larb/vec2"
4
+ require_relative "larb/vec3"
5
+ require_relative "larb/vec4"
6
+ require_relative "larb/quat"
7
+ require_relative "larb/quat2"
8
+ require_relative "larb/mat2"
9
+ require_relative "larb/mat2d"
10
+ require_relative "larb/mat3"
11
+ require_relative "larb/mat4"
12
+ require_relative "larb/color"
13
+
14
+ module Larb
15
+ VERSION = "0.1.0"
16
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: larb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai Takada
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Larb provides vectors, matrices, quaternions, and Color classes for 2D/3D
13
+ graphics and mathematical computations
14
+ email:
15
+ - t.yudai92@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - lib/larb.rb
25
+ - lib/larb/color.rb
26
+ - lib/larb/mat2.rb
27
+ - lib/larb/mat2d.rb
28
+ - lib/larb/mat3.rb
29
+ - lib/larb/mat4.rb
30
+ - lib/larb/quat.rb
31
+ - lib/larb/quat2.rb
32
+ - lib/larb/vec2.rb
33
+ - lib/larb/vec3.rb
34
+ - lib/larb/vec4.rb
35
+ homepage: https://github.com/ydah/larb
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ source_code_uri: https://github.com/ydah/larb
40
+ changelog_uri: https://github.com/ydah/larb/blob/main/CHANGELOG.md
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.1.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 4.0.3
56
+ specification_version: 4
57
+ summary: Linear algebra library for 2D/3D graphics in Ruby
58
+ test_files: []