cglm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cglm.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "cglm/version"
2
+
3
+ class Numeric
4
+ def to_radians
5
+ self * Math::PI / 180.0
6
+ end
7
+
8
+ def to_degrees
9
+ self * 180.0 / Math::PI
10
+ end
11
+ end
12
+
13
+ module CGLM
14
+ require 'cglm/base'
15
+ require 'cglm/vector_type'
16
+ require 'cglm/aabb'
17
+ require 'cglm/vec3'
18
+ require 'cglm/vec4'
19
+ require 'cglm/mat3'
20
+ require 'cglm/mat4'
21
+ require 'cglm/quat'
22
+ require 'cglm/frustum'
23
+ require 'cglm/plane'
24
+ end
25
+
26
+ require "cglm/cglm"
data/lib/cglm/aabb.rb ADDED
@@ -0,0 +1,4 @@
1
+ module CGLM
2
+ class AABB < Base
3
+ end
4
+ end
data/lib/cglm/base.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'fiddle'
2
+
3
+ module CGLM
4
+ class Base
5
+ attr_accessor :addr
6
+ alias to_ptr addr
7
+
8
+ def initialize(addr: CGLM.alloc(self.class).tap { |ptr|
9
+ # Init malloc'd values to 0; there is a PR to fiddle for this, then we
10
+ # can delete this code. https://github.com/ruby/fiddle/pull/14
11
+ ptr[0, self.class.size] = "\x00".b * self.class.size
12
+ })
13
+ # Even if addr is present, but points to NULL, this is a problem that
14
+ # will lead to segfaults. If not present, `nil.to_i == 0`.
15
+ raise ArgumentError, 'BUG: object initialized without a backing store?' if addr.to_i == 0
16
+ self.addr = addr
17
+ end
18
+
19
+ def copy_to(other)
20
+ size = addr.size > other.addr.size ? other.addr.size : addr.size
21
+ other.addr[0, size] = addr[0, size]
22
+ end
23
+
24
+ def dup(other = nil)
25
+ if other
26
+ copy_to other
27
+ else
28
+ super()
29
+ end
30
+ end
31
+
32
+ def initialize_dup(other)
33
+ super
34
+ @addr = CGLM.alloc(self.class)
35
+ other.copy_to(self)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module CGLM
2
+ class Frustum
3
+ attr_accessor :matrix
4
+
5
+ def initialize(matrix = Mat4.identity)
6
+ @matrix = matrix
7
+ end
8
+ end
9
+ end
data/lib/cglm/mat3.rb ADDED
@@ -0,0 +1,27 @@
1
+ module CGLM
2
+ class Mat3 < Base
3
+ # Performs multiplication with `other` and returns the result.
4
+ #
5
+ # * `other` is a Mat3 or Vec3.
6
+ def *(other)
7
+ case other
8
+ when Mat3 then mul_mat3(other)
9
+ when Vec3 then mul_vec3(other)
10
+ when Numeric then mul_scalar(other)
11
+ else raise ArgumentError, "Don't know how to multiply Mat3 with object: #{other.inspect}"
12
+ end
13
+ end
14
+
15
+ def to_a
16
+ 3.times.map { |i| self[i].to_a }
17
+ end
18
+
19
+ def inspect
20
+ vals = to_a
21
+ longest_val_size = vals.flatten.reduce(0) { |a, v| a < v.to_s.size ? v.to_s.size : a }
22
+ vals.map! { |row| row.map { |val| val.to_s.rjust(longest_val_size) }.join(', ') }
23
+ left = "#<#{self.class}@#{addr.to_i.to_s(16)} ["
24
+ left + vals.join(",\n" + (" " * left.size)) + "]>"
25
+ end
26
+ end
27
+ end
data/lib/cglm/mat4.rb ADDED
@@ -0,0 +1,46 @@
1
+ module CGLM
2
+ class Mat4 < Base
3
+ def initialize(initial_values = nil, **args)
4
+ super(**args)
5
+ case initial_values
6
+ when Mat3
7
+ initial_values.to_a.each_with_index { |row, i| self[i] = row }
8
+ when Mat4
9
+ addr[0, self.class.size] = initial_values.addr[0, self.class.size]
10
+ when Array
11
+ initial_values.each_with_index do |row, i|
12
+ self[i] = row
13
+ end
14
+ when nil
15
+ else
16
+ raise ArgumentError, 'initial values should be a Mat4, a Mat3 or an array of Array/Vec3/Vec4'
17
+ end
18
+ end
19
+
20
+ def to_a
21
+ 4.times.map { |i| self[i].to_a }
22
+ end
23
+
24
+ def inspect
25
+ vals = to_a
26
+ longest_val_size = vals.flatten.reduce(0) { |a, v| a < v.to_s.size ? v.to_s.size : a }
27
+ vals.map! { |row| row.map { |val| val.to_s.rjust(longest_val_size) }.join(', ') }
28
+ left = "#<#{self.class}@#{addr.to_i.to_s(16)} ["
29
+ left + vals.join(",\n" + (" " * left.size)) + "]>"
30
+ end
31
+
32
+ # Performs multiplication with `other` and returns the result.
33
+ #
34
+ # * `other` is a Mat3 or Vec3.
35
+ def *(other)
36
+ case other
37
+ when Mat4 then mul_mat4(other)
38
+ when Mat3 then mul_mat3(other)
39
+ when Vec3 then mul_vec3(other)
40
+ when Vec4 then mul_vec4(other)
41
+ when Numeric then mul_scalar(other)
42
+ else raise ArgumentError, "Don't know how to multiply Mat4 with object: #{other.inspect}"
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/cglm/plane.rb ADDED
@@ -0,0 +1,9 @@
1
+ module CGLM
2
+ class Plane < Vec4
3
+ # Returns a new Vec4 representing a plane with the given normal (a Vec3)
4
+ # and distance (a number).
5
+ def initialize(normal, distance)
6
+ super [*normal, distance]
7
+ end
8
+ end
9
+ end
data/lib/cglm/quat.rb ADDED
@@ -0,0 +1,10 @@
1
+ module CGLM
2
+ class Quat < Vec4
3
+ def *(other)
4
+ case other
5
+ when Quat then mul_quat(other)
6
+ else Quat.new(super)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module CGLM
2
+ # Sphere Representation in cglm: `[center.x, center.y, center.z, radius]`
3
+ #
4
+ # You could use this representation or you can convert it to Vec4 before
5
+ # calling any function.
6
+ class Sphere < Vec4
7
+ def radius
8
+ self[3]
9
+ end
10
+ end
11
+ end
data/lib/cglm/vec3.rb ADDED
@@ -0,0 +1,88 @@
1
+ module CGLM
2
+ class Vec3 < VectorType
3
+ def to_a
4
+ [self[0], self[1], self[2]]
5
+ end
6
+
7
+ def inspect
8
+ "#<#{self.class}@#{addr.to_i.to_s(16)} #{to_a.inspect}>"
9
+ end
10
+
11
+ def ==(other)
12
+ case other
13
+ when VectorType then equals_vec3(other)
14
+ when Numeric then equals_scalar(other)
15
+ else false
16
+ end
17
+ end
18
+
19
+ # Returns true if the given value is very close to this Vec3. If `other`
20
+ # is a scalar (number), each component in this vector must be very close
21
+ # to the scalar. If it's a vector, it must be a Vec3 or Vec4 and each
22
+ # component of this Vec3 must be very close to the corresponding component
23
+ # of `other`. See #equalish_vec3 and #equalish_scalar.
24
+ def =~(other)
25
+ case other
26
+ when VectorType then equalish_vec3(other)
27
+ when Numeric then equalish_scalar(other)
28
+ else false
29
+ end
30
+ end
31
+
32
+ def +(other)
33
+ case other
34
+ when VectorType then add_vec3(other)
35
+ when Numeric then add_scalar(other)
36
+ else false
37
+ end
38
+ end
39
+
40
+ def -(other)
41
+ case other
42
+ when VectorType then sub_vec3(other)
43
+ when Numeric then sub_scalar(other)
44
+ else false
45
+ end
46
+ end
47
+
48
+ def *(other)
49
+ case other
50
+ when VectorType then mul_vec3(other)
51
+ when Numeric then mul_scalar(other)
52
+ else false
53
+ end
54
+ end
55
+
56
+ def /(other)
57
+ case other
58
+ when VectorType then div_vec3(other)
59
+ when Numeric then div_scalar(other)
60
+ else false
61
+ end
62
+ end
63
+
64
+ def addadd(other, dest)
65
+ case other
66
+ when VectorType then addadd_vec3(other, dest)
67
+ when Numeric then addadd_scalar(other, dest)
68
+ else false
69
+ end
70
+ end
71
+
72
+ def subadd(other, dest)
73
+ case other
74
+ when VectorType then subadd_vec3(other, dest)
75
+ when Numeric then subadd_scalar(other, dest)
76
+ else false
77
+ end
78
+ end
79
+
80
+ def muladd(other, dest)
81
+ case other
82
+ when VectorType then muladd_vec3(other, dest)
83
+ when Numeric then muladd_scalar(other, dest)
84
+ else false
85
+ end
86
+ end
87
+ end
88
+ end
data/lib/cglm/vec4.rb ADDED
@@ -0,0 +1,104 @@
1
+ module CGLM
2
+ class Vec4 < VectorType
3
+ def to_a
4
+ [self[0], self[1], self[2], self[3]]
5
+ end
6
+
7
+ def inspect
8
+ "#<#{self.class}@#{addr.to_i.to_s(16)} #{to_a.inspect}>"
9
+ end
10
+
11
+ def ==(other)
12
+ case other
13
+ when VectorType then equals_vec4(other)
14
+ when Numeric then equals_scalar(other)
15
+ else false
16
+ end
17
+ end
18
+
19
+ # Returns true if the given value is very close to this Vec4. If `other`
20
+ # is a scalar (number), each component in this vector must be very close
21
+ # to the scalar. If it's a vector, it must be a Vec4 and each component
22
+ # of this Vec4 must be very close to the corresponding component of
23
+ # `other`. See #equalish_vec4 and #equalish_scalar.
24
+ def =~(other)
25
+ case other
26
+ when VectorType then equalish_vec4(other)
27
+ when Numeric then equalish_scalar(other)
28
+ else false
29
+ end
30
+ end
31
+
32
+ def +(other)
33
+ case other
34
+ when VectorType then add_vec4(other)
35
+ when Numeric then add_scalar(other)
36
+ else false
37
+ end
38
+ end
39
+
40
+ def -(other)
41
+ case other
42
+ when VectorType then sub_vec4(other)
43
+ when Numeric then sub_scalar(other)
44
+ else false
45
+ end
46
+ end
47
+
48
+ def *(other)
49
+ case other
50
+ when VectorType then mul_vec4(other)
51
+ when Numeric then mul_scalar(other)
52
+ else false
53
+ end
54
+ end
55
+
56
+ def /(other)
57
+ case other
58
+ when VectorType then div_vec4(other)
59
+ when Numeric then div_scalar(other)
60
+ else false
61
+ end
62
+ end
63
+
64
+ def addadd(other, dest)
65
+ case other
66
+ when VectorType then addadd_vec4(other, dest)
67
+ when Numeric then addadd_scalar(other, dest)
68
+ else false
69
+ end
70
+ end
71
+
72
+ def subadd(other, dest)
73
+ case other
74
+ when VectorType then subadd_vec4(other, dest)
75
+ when Numeric then subadd_scalar(other, dest)
76
+ else false
77
+ end
78
+ end
79
+
80
+ def muladd(other, dest)
81
+ case other
82
+ when VectorType then muladd_vec4(other, dest)
83
+ when Numeric then muladd_scalar(other, dest)
84
+ else false
85
+ end
86
+ end
87
+
88
+ def clamp(min, max, *extra)
89
+ case min
90
+ when VectorType then clamp_vec4(min, max, *extra)
91
+ when Numeric then clamp_scalar(min, max, *extra)
92
+ else raise ArgumentError, 'need Vec4 or'
93
+ end
94
+ end
95
+
96
+ def clamp!(min, max)
97
+ case min
98
+ when VectorType then clamp_vec4!(min, max)
99
+ when Numeric then clamp_scalar!(min, max)
100
+ else raise ArgumentError, 'need Vec4 or Scalar'
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,17 @@
1
+ module CGLM
2
+ class VectorType < Base
3
+ def initialize(initial = nil, **args)
4
+ super(**args)
5
+ case initial
6
+ when Array
7
+ initial.each_with_index { |v, i| self[i] = v; break if i >= 3 }
8
+ when Quat, Vec3, Vec4
9
+ size = initial.class.size > self.class.size ? self.class.size : initial.class.size
10
+ addr[0, size] = initial.addr[0, size]
11
+ when nil
12
+ else
13
+ raise ArgumentError, 'initial values must be an Array, Quat, Vec3 or Vec4'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module CGLM
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cglm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Colin MacKenzie IV
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: redcarpet
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: github-markup
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - sinisterchipmunk@gmail.com
128
+ executables: []
129
+ extensions:
130
+ - ext/cglm/extconf.rb
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
135
+ - ".yardopts"
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - cglm.gemspec
144
+ - ext/cglm/extconf.rb
145
+ - ext/cglm/rb_cglm.c
146
+ - ext/cglm/rb_cglm.h
147
+ - ext/cglm/rb_cglm_aabb.c
148
+ - ext/cglm/rb_cglm_affine.c
149
+ - ext/cglm/rb_cglm_cam.c
150
+ - ext/cglm/rb_cglm_color.c
151
+ - ext/cglm/rb_cglm_ease.c
152
+ - ext/cglm/rb_cglm_euler.c
153
+ - ext/cglm/rb_cglm_frustum.c
154
+ - ext/cglm/rb_cglm_mat3.c
155
+ - ext/cglm/rb_cglm_mat4.c
156
+ - ext/cglm/rb_cglm_plane.c
157
+ - ext/cglm/rb_cglm_project.c
158
+ - ext/cglm/rb_cglm_quat.c
159
+ - ext/cglm/rb_cglm_sphere.c
160
+ - ext/cglm/rb_cglm_vec3.c
161
+ - ext/cglm/rb_cglm_vec4.c
162
+ - lib/cglm.rb
163
+ - lib/cglm/aabb.rb
164
+ - lib/cglm/base.rb
165
+ - lib/cglm/frustum.rb
166
+ - lib/cglm/mat3.rb
167
+ - lib/cglm/mat4.rb
168
+ - lib/cglm/plane.rb
169
+ - lib/cglm/quat.rb
170
+ - lib/cglm/sphere.rb
171
+ - lib/cglm/vec3.rb
172
+ - lib/cglm/vec4.rb
173
+ - lib/cglm/vector_type.rb
174
+ - lib/cglm/version.rb
175
+ homepage: https://github.com/sinisterchipmunk/cglm-ruby
176
+ licenses:
177
+ - MIT
178
+ metadata:
179
+ allowed_push_host: https://rubygems.org
180
+ homepage_uri: https://github.com/sinisterchipmunk/cglm-ruby
181
+ source_code_uri: https://github.com/sinisterchipmunk/cglm-ruby
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.7.8
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: CGLM bindings for Ruby
202
+ test_files: []