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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ca6912c8989e8917738c9965540ee392fce8e3c5bcfb95960b1e36d39a84b56
4
+ data.tar.gz: c38e9ab279c3e9ede439d90ff7e9153a523e189e7f5bf18867811c0356b21866
5
+ SHA512:
6
+ metadata.gz: f3499157dd5c279ba8afac41e9d245742cfd7d6a31bdf83e683ee42d9c448e6b67666b1c28b15becf18640393486658e2825e44b10ded335a37a5eb37858c722
7
+ data.tar.gz: 381dce0add3519b5b0c2661a443d34aa2cc14dfcbb1225c5b507c744b2fa9cd31d89e7fdfac5a776abf6b924e28aeb08afa66ade9e87b19d7268f0946988b7a9
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.1.0 - 2026-01-02
6
+
7
+ - Initial release.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Yudai Takada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Larb
2
+
3
+ Linear algebra library for 2D/3D graphics in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'larb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ gem install larb
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'larb'
29
+
30
+ # Vectors
31
+ v2 = Larb::Vec2.new(1, 2)
32
+ v3 = Larb::Vec3.new(1, 2, 3)
33
+ v4 = Larb::Vec4.new(1, 2, 3, 1)
34
+
35
+ # Vector operations
36
+ sum = v3 + Larb::Vec3.new(4, 5, 6)
37
+ dot = v3.dot(Larb::Vec3.new(1, 0, 0))
38
+ cross = v3.cross(Larb::Vec3.up)
39
+ normalized = v3.normalize
40
+
41
+ # Matrices
42
+ identity = Larb::Mat4.identity
43
+ translation = Larb::Mat4.translation(10, 20, 30)
44
+ rotation = Larb::Mat4.rotation_y(Math::PI / 4)
45
+ scale = Larb::Mat4.scaling(2, 2, 2)
46
+
47
+ # Matrix multiplication
48
+ transform = translation * rotation * scale
49
+ transformed_point = transform * v4
50
+
51
+ # Quaternions
52
+ quat = Larb::Quat.from_axis_angle(Larb::Vec3.up, Math::PI / 2)
53
+ rotated = quat * Larb::Vec3.new(1, 0, 0)
54
+
55
+ # Colors
56
+ red = Larb::Color.red
57
+ custom = Larb::Color.new(0.5, 0.3, 0.8, 1.0)
58
+ hex_color = Larb::Color.from_hex("#ff8800")
59
+ ```
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: :test
data/lib/larb/color.rb ADDED
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Larb
4
+ class Color
5
+ attr_accessor :r, :g, :b, :a
6
+
7
+ def initialize(r = 0.0, g = 0.0, b = 0.0, a = 1.0)
8
+ @r = r.to_f
9
+ @g = g.to_f
10
+ @b = b.to_f
11
+ @a = a.to_f
12
+ end
13
+
14
+ def self.[](r, g, b, a = 1.0)
15
+ new(r, g, b, a)
16
+ end
17
+
18
+ def self.rgb(r, g, b)
19
+ new(r, g, b, 1.0)
20
+ end
21
+
22
+ def self.rgba(r, g, b, a)
23
+ new(r, g, b, a)
24
+ end
25
+
26
+ def self.from_bytes(r, g, b, a = 255)
27
+ new(r / 255.0, g / 255.0, b / 255.0, a / 255.0)
28
+ end
29
+
30
+ def self.from_hex(hex)
31
+ hex = hex.delete("#")
32
+ r = hex[0..1].to_i(16) / 255.0
33
+ g = hex[2..3].to_i(16) / 255.0
34
+ b = hex[4..5].to_i(16) / 255.0
35
+ a = hex.length > 6 ? hex[6..7].to_i(16) / 255.0 : 1.0
36
+ new(r, g, b, a)
37
+ end
38
+
39
+ def self.black
40
+ new(0, 0, 0, 1)
41
+ end
42
+
43
+ def self.white
44
+ new(1, 1, 1, 1)
45
+ end
46
+
47
+ def self.red
48
+ new(1, 0, 0, 1)
49
+ end
50
+
51
+ def self.green
52
+ new(0, 1, 0, 1)
53
+ end
54
+
55
+ def self.blue
56
+ new(0, 0, 1, 1)
57
+ end
58
+
59
+ def self.yellow
60
+ new(1, 1, 0, 1)
61
+ end
62
+
63
+ def self.cyan
64
+ new(0, 1, 1, 1)
65
+ end
66
+
67
+ def self.magenta
68
+ new(1, 0, 1, 1)
69
+ end
70
+
71
+ def self.transparent
72
+ new(0, 0, 0, 0)
73
+ end
74
+
75
+ def +(other)
76
+ Color.new(@r + other.r, @g + other.g, @b + other.b, @a + other.a)
77
+ end
78
+
79
+ def -(other)
80
+ Color.new(@r - other.r, @g - other.g, @b - other.b, @a - other.a)
81
+ end
82
+
83
+ def *(scalar)
84
+ case scalar
85
+ when Numeric
86
+ Color.new(@r * scalar, @g * scalar, @b * scalar, @a * scalar)
87
+ when Color
88
+ Color.new(@r * scalar.r, @g * scalar.g, @b * scalar.b, @a * scalar.a)
89
+ end
90
+ end
91
+
92
+ def lerp(other, t)
93
+ Color.new(
94
+ @r + (other.r - @r) * t,
95
+ @g + (other.g - @g) * t,
96
+ @b + (other.b - @b) * t,
97
+ @a + (other.a - @a) * t
98
+ )
99
+ end
100
+
101
+ def clamp
102
+ Color.new(
103
+ @r.clamp(0.0, 1.0),
104
+ @g.clamp(0.0, 1.0),
105
+ @b.clamp(0.0, 1.0),
106
+ @a.clamp(0.0, 1.0)
107
+ )
108
+ end
109
+
110
+ def to_bytes
111
+ [(@r * 255).round.clamp(0, 255),
112
+ (@g * 255).round.clamp(0, 255),
113
+ (@b * 255).round.clamp(0, 255),
114
+ (@a * 255).round.clamp(0, 255)]
115
+ end
116
+
117
+ def to_hex
118
+ r, g, b, a = to_bytes
119
+ format("#%02x%02x%02x%02x", r, g, b, a)
120
+ end
121
+
122
+ def to_vec3
123
+ Vec3.new(@r, @g, @b)
124
+ end
125
+
126
+ def to_vec4
127
+ Vec4.new(@r, @g, @b, @a)
128
+ end
129
+
130
+ def to_a
131
+ [@r, @g, @b, @a]
132
+ end
133
+
134
+ def self.from_vec4(v)
135
+ new(v.x, v.y, v.z, v.w)
136
+ end
137
+
138
+ def self.from_vec3(v, a = 1.0)
139
+ new(v.x, v.y, v.z, a)
140
+ end
141
+
142
+ def inspect
143
+ "Color[#{@r}, #{@g}, #{@b}, #{@a}]"
144
+ end
145
+
146
+ alias to_s inspect
147
+ end
148
+ end
data/lib/larb/mat2.rb ADDED
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Larb
4
+ class Mat2
5
+ attr_reader :data
6
+
7
+ def initialize(data = nil)
8
+ @data = data || [1.0, 0.0, 0.0, 1.0]
9
+ end
10
+
11
+ def self.identity
12
+ new
13
+ end
14
+
15
+ def self.zero
16
+ new([0.0, 0.0, 0.0, 0.0])
17
+ end
18
+
19
+ def self.rotation(radians)
20
+ c = Math.cos(radians)
21
+ s = Math.sin(radians)
22
+ new([c, s, -s, c])
23
+ end
24
+
25
+ def self.scaling(x, y)
26
+ new([x.to_f, 0.0, 0.0, y.to_f])
27
+ end
28
+
29
+ def self.from_vec2(v1, v2)
30
+ new([v1.x, v1.y, v2.x, v2.y])
31
+ end
32
+
33
+ def [](i)
34
+ @data[i]
35
+ end
36
+
37
+ def []=(i, v)
38
+ @data[i] = v.to_f
39
+ end
40
+
41
+ def *(other)
42
+ case other
43
+ when Mat2
44
+ a = @data
45
+ b = other.data
46
+ Mat2.new([
47
+ a[0] * b[0] + a[2] * b[1],
48
+ a[1] * b[0] + a[3] * b[1],
49
+ a[0] * b[2] + a[2] * b[3],
50
+ a[1] * b[2] + a[3] * b[3]
51
+ ])
52
+ when Vec2
53
+ Vec2.new(
54
+ @data[0] * other.x + @data[2] * other.y,
55
+ @data[1] * other.x + @data[3] * other.y
56
+ )
57
+ when Numeric
58
+ Mat2.new(@data.map { |v| v * other })
59
+ end
60
+ end
61
+
62
+ def +(other)
63
+ Mat2.new(@data.zip(other.data).map { |a, b| a + b })
64
+ end
65
+
66
+ def -(other)
67
+ Mat2.new(@data.zip(other.data).map { |a, b| a - b })
68
+ end
69
+
70
+ def determinant
71
+ @data[0] * @data[3] - @data[2] * @data[1]
72
+ end
73
+
74
+ def inverse
75
+ det = determinant
76
+ raise "Matrix is not invertible" if det.abs < 1e-10
77
+
78
+ inv_det = 1.0 / det
79
+ Mat2.new([
80
+ @data[3] * inv_det,
81
+ -@data[1] * inv_det,
82
+ -@data[2] * inv_det,
83
+ @data[0] * inv_det
84
+ ])
85
+ end
86
+
87
+ def transpose
88
+ Mat2.new([@data[0], @data[2], @data[1], @data[3]])
89
+ end
90
+
91
+ def adjoint
92
+ Mat2.new([@data[3], -@data[1], -@data[2], @data[0]])
93
+ end
94
+
95
+ def frobenius_norm
96
+ Math.sqrt(@data.sum { |v| v * v })
97
+ end
98
+
99
+ def to_a
100
+ @data.dup
101
+ end
102
+
103
+ def ==(other)
104
+ return false unless other.is_a?(Mat2)
105
+
106
+ @data == other.data
107
+ end
108
+
109
+ def near?(other, epsilon = 1e-6)
110
+ @data.zip(other.data).all? { |a, b| (a - b).abs < epsilon }
111
+ end
112
+
113
+ def inspect
114
+ "Mat2[#{@data[0]}, #{@data[1]}, #{@data[2]}, #{@data[3]}]"
115
+ end
116
+
117
+ alias to_s inspect
118
+ end
119
+ end
data/lib/larb/mat2d.rb ADDED
@@ -0,0 +1,180 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Larb
4
+ # 2D affine transformation matrix (2x3)
5
+ # Layout: [a, b, c, d, tx, ty]
6
+ # Represents:
7
+ # | a c tx |
8
+ # | b d ty |
9
+ # | 0 0 1 |
10
+ class Mat2d
11
+ attr_reader :data
12
+
13
+ def initialize(data = nil)
14
+ @data = data || [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
15
+ end
16
+
17
+ def self.identity
18
+ new
19
+ end
20
+
21
+ def self.zero
22
+ new([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
23
+ end
24
+
25
+ def self.translation(x, y)
26
+ new([1.0, 0.0, 0.0, 1.0, x.to_f, y.to_f])
27
+ end
28
+
29
+ def self.rotation(radians)
30
+ c = Math.cos(radians)
31
+ s = Math.sin(radians)
32
+ new([c, s, -s, c, 0.0, 0.0])
33
+ end
34
+
35
+ def self.scaling(x, y)
36
+ new([x.to_f, 0.0, 0.0, y.to_f, 0.0, 0.0])
37
+ end
38
+
39
+ def self.from_rotation_translation_scale(rotation, translation, scale)
40
+ c = Math.cos(rotation)
41
+ s = Math.sin(rotation)
42
+ new([
43
+ c * scale.x,
44
+ s * scale.x,
45
+ -s * scale.y,
46
+ c * scale.y,
47
+ translation.x,
48
+ translation.y
49
+ ])
50
+ end
51
+
52
+ def [](i)
53
+ @data[i]
54
+ end
55
+
56
+ def []=(i, v)
57
+ @data[i] = v.to_f
58
+ end
59
+
60
+ def *(other)
61
+ case other
62
+ when Mat2d
63
+ a = @data
64
+ b = other.data
65
+ Mat2d.new([
66
+ a[0] * b[0] + a[2] * b[1],
67
+ a[1] * b[0] + a[3] * b[1],
68
+ a[0] * b[2] + a[2] * b[3],
69
+ a[1] * b[2] + a[3] * b[3],
70
+ a[0] * b[4] + a[2] * b[5] + a[4],
71
+ a[1] * b[4] + a[3] * b[5] + a[5]
72
+ ])
73
+ when Vec2
74
+ Vec2.new(
75
+ @data[0] * other.x + @data[2] * other.y + @data[4],
76
+ @data[1] * other.x + @data[3] * other.y + @data[5]
77
+ )
78
+ when Numeric
79
+ Mat2d.new(@data.map { |v| v * other })
80
+ end
81
+ end
82
+
83
+ def +(other)
84
+ Mat2d.new(@data.zip(other.data).map { |a, b| a + b })
85
+ end
86
+
87
+ def -(other)
88
+ Mat2d.new(@data.zip(other.data).map { |a, b| a - b })
89
+ end
90
+
91
+ def determinant
92
+ @data[0] * @data[3] - @data[1] * @data[2]
93
+ end
94
+
95
+ def inverse
96
+ a = @data
97
+ det = determinant
98
+ raise "Matrix is not invertible" if det.abs < 1e-10
99
+
100
+ inv_det = 1.0 / det
101
+ Mat2d.new([
102
+ a[3] * inv_det,
103
+ -a[1] * inv_det,
104
+ -a[2] * inv_det,
105
+ a[0] * inv_det,
106
+ (a[2] * a[5] - a[3] * a[4]) * inv_det,
107
+ (a[1] * a[4] - a[0] * a[5]) * inv_det
108
+ ])
109
+ end
110
+
111
+ def translate(x, y)
112
+ a = @data
113
+ Mat2d.new([
114
+ a[0], a[1], a[2], a[3],
115
+ a[0] * x + a[2] * y + a[4],
116
+ a[1] * x + a[3] * y + a[5]
117
+ ])
118
+ end
119
+
120
+ def rotate(radians)
121
+ self * Mat2d.rotation(radians)
122
+ end
123
+
124
+ def scale(x, y)
125
+ a = @data
126
+ Mat2d.new([
127
+ a[0] * x, a[1] * x,
128
+ a[2] * y, a[3] * y,
129
+ a[4], a[5]
130
+ ])
131
+ end
132
+
133
+ def extract_translation
134
+ Vec2.new(@data[4], @data[5])
135
+ end
136
+
137
+ def extract_rotation
138
+ Math.atan2(@data[1], @data[0])
139
+ end
140
+
141
+ def extract_scale
142
+ Vec2.new(
143
+ Math.sqrt(@data[0] * @data[0] + @data[1] * @data[1]),
144
+ Math.sqrt(@data[2] * @data[2] + @data[3] * @data[3])
145
+ )
146
+ end
147
+
148
+ def frobenius_norm
149
+ Math.sqrt(@data.sum { |v| v * v })
150
+ end
151
+
152
+ def to_mat3
153
+ Mat3.new([
154
+ @data[0], @data[1], 0,
155
+ @data[2], @data[3], 0,
156
+ @data[4], @data[5], 1
157
+ ])
158
+ end
159
+
160
+ def to_a
161
+ @data.dup
162
+ end
163
+
164
+ def ==(other)
165
+ return false unless other.is_a?(Mat2d)
166
+
167
+ @data == other.data
168
+ end
169
+
170
+ def near?(other, epsilon = 1e-6)
171
+ @data.zip(other.data).all? { |a, b| (a - b).abs < epsilon }
172
+ end
173
+
174
+ def inspect
175
+ "Mat2d[#{@data.map { |v| format('%.4f', v) }.join(', ')}]"
176
+ end
177
+
178
+ alias to_s inspect
179
+ end
180
+ end