transformatrix 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +27 -0
- data/Rakefile +9 -0
- data/lib/transformatrix.rb +68 -0
- data/lib/transformatrix/version.rb +3 -0
- data/test/tmatrix_test.rb +51 -0
- data/transformatrix.gemspec +19 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjFmOTBkNWY4OWMwYzhhZjNjZDcxYWQzNmZjMDZlYWUxNmU2ODRlMA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjM2MTgwOTQzZWU0OTZlNTYxNmI2NmUyYTZkNjBkZjI5ZDNkN2FmMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YmE0NTM1MzMyOWU2NTY3ZjdmYTlmZTQyNGNmZWE1ZGVlNWQ5MTI4ZDgwYjE4
|
10
|
+
Y2Y1ZjY2NGZkNzFmZjhjY2JjOTAwYzVhNzM2N2M2OWMxMjgzNWNlMWUxZDVi
|
11
|
+
MWZmYTJkODQ1MWI3MjBmM2U4NjI2MjA0ZjM2YzQ2ZjMwODFhMjI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTY4N2VmMDU1NzZkMWFlNDA2YTUxZGY1NmJkMDE4N2IzZTNjY2FkMTA1NDEw
|
14
|
+
NjJmMDk1YzY3ZWYxMjVhNjZmOWNhNTVmMWQ1YzhlYTc3MDZmOGQ3ZjQyN2Nh
|
15
|
+
MzVmMWZlNDEwYzlhZWM4YWZkOWNiN2RjMzQ2YTJjZmQ5YmFkNWM=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fredrik Persen Fostvedt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Transformatrix
|
2
|
+
|
3
|
+
## Installation (currently not uploaded to rubygems)
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'transformatrix'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install transformatrix
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
TODO: Write usage instructions here
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
1. Fork it
|
24
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
25
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
26
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
27
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative "transformatrix/version"
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
class TMatrix < Matrix
|
5
|
+
|
6
|
+
#Multiplication with 2D vector or array
|
7
|
+
def * arg
|
8
|
+
if arg.is_a? Array
|
9
|
+
multiply_array arg
|
10
|
+
elsif arg.is_a? Vector
|
11
|
+
Vector.elements multiply_array(arg.to_a)
|
12
|
+
else
|
13
|
+
super.* arg
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def multiply_array arg
|
18
|
+
array = arg.dup
|
19
|
+
return_size = array.size
|
20
|
+
|
21
|
+
if array.size == 2
|
22
|
+
array.push 1
|
23
|
+
end
|
24
|
+
|
25
|
+
ret = []
|
26
|
+
rows.each_with_index do |row, m|
|
27
|
+
ret.push(0)
|
28
|
+
row.each_with_index do |e, n|
|
29
|
+
ret[m] += array[n] * e
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ret[0..return_size-1]
|
34
|
+
end
|
35
|
+
private :multiply_array
|
36
|
+
|
37
|
+
#Create matrix for rotating alpha radians about origo
|
38
|
+
def self.rotation alpha
|
39
|
+
rows [ \
|
40
|
+
[Math::cos(alpha), Math::sin(alpha), 0], \
|
41
|
+
[-Math::sin(alpha), Math::cos(alpha), 0], \
|
42
|
+
[0, 0, 1]]
|
43
|
+
end
|
44
|
+
|
45
|
+
#Create matrix for translating by [tx, ty]
|
46
|
+
def self.translation tx, ty
|
47
|
+
rows [\
|
48
|
+
[1, 0, tx ],\
|
49
|
+
[0, 1, ty ],\
|
50
|
+
[0, 0, 1 ]]
|
51
|
+
end
|
52
|
+
|
53
|
+
#Create matrix for reflecting about a line interpolating (0,0) and (lx,ly)
|
54
|
+
def self.reflection lx, ly
|
55
|
+
rows [\
|
56
|
+
[lx**2-ly**2, 2*lx*ly, 0 ],\
|
57
|
+
[2*lx*ly, ly**2-lx**2, 0 ],\
|
58
|
+
[0, 0, lx**2+ly**2 ]]
|
59
|
+
end
|
60
|
+
|
61
|
+
#Create matrix for scaling relative to origo
|
62
|
+
def self.scaling sx, sy
|
63
|
+
rows [\
|
64
|
+
[sx, 0, 0],\
|
65
|
+
[0, sy, 0],\
|
66
|
+
[0, 0, 1]]
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require_relative '../lib/transformatrix'
|
4
|
+
|
5
|
+
class TestTMatrix < Test::Unit::TestCase
|
6
|
+
def test_rotation
|
7
|
+
angle = 0.05 * rand(0..20) * Math::PI
|
8
|
+
assert_equal TMatrix.rotation(angle), \
|
9
|
+
Matrix[ \
|
10
|
+
[Math::cos(angle), Math::sin(angle), 0], \
|
11
|
+
[-Math::sin(angle), Math::cos(angle), 0], \
|
12
|
+
[0, 0, 1]]
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_translation
|
16
|
+
assert_equal TMatrix.translation(2,3), Matrix[[1,0,2],[0,1,3],[0,0,1]]
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_reflection
|
20
|
+
assert_equal TMatrix.reflection(1,1), Matrix[[0,2,0],[2,0,0],[0,0,2]]
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_scaling
|
24
|
+
assert_equal TMatrix.scaling(2,3), Matrix[[2,0,0],[0,3,0],[0,0,1]]
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_multiply
|
28
|
+
m = TMatrix.scaling(2,3) * TMatrix.translation(3,3)
|
29
|
+
assert m.is_a? Matrix
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_multiply_array
|
33
|
+
assert_equal TMatrix.translation(2,2) * [0,0], [2,2]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_multiplication_homogenous_array
|
37
|
+
assert_equal TMatrix.translation(2,2) * [0,0,1], [2,2,1]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_multiply_vector
|
41
|
+
assert_equal TMatrix.translation(2,3) * Vector[0,0], Vector[2,3]
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_multiplication_homogenous_vector
|
45
|
+
assert_equal TMatrix.translation(2,2) * Vector[0,0,1], Vector[2,2,1]
|
46
|
+
end
|
47
|
+
|
48
|
+
# def test_multiplication_vector
|
49
|
+
# assert_equal TMatrix.translation(2,2) * Vector[0,0], Vector[2,2]
|
50
|
+
# end
|
51
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'transformatrix/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "transformatrix"
|
8
|
+
gem.version = Transformatrix::VERSION
|
9
|
+
gem.authors = ["Fredrik Persen Fostvedt"]
|
10
|
+
gem.email = ["fpfostvedt@gmail.com"]
|
11
|
+
gem.description = "Transformation matrices"
|
12
|
+
gem.summary = "The gem generates transformation matrices"
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transformatrix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fredrik Persen Fostvedt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Transformation matrices
|
14
|
+
email:
|
15
|
+
- fpfostvedt@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/transformatrix.rb
|
26
|
+
- lib/transformatrix/version.rb
|
27
|
+
- test/tmatrix_test.rb
|
28
|
+
- transformatrix.gemspec
|
29
|
+
homepage: ''
|
30
|
+
licenses: []
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.0.3
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: The gem generates transformation matrices
|
52
|
+
test_files:
|
53
|
+
- test/tmatrix_test.rb
|