trilateration 0.0.1 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 350d570c17ec97de89bffff8e3757d11dc8262fa
4
- data.tar.gz: 73e954901be4f704167e4be47e2e1470ef58dd56
3
+ metadata.gz: a2e1830b0a0b59a44e16d87a46dfbe3e770ca8ff
4
+ data.tar.gz: b4ebf3a753c34beeb8ff6eaebfc0b7413fb5af64
5
5
  SHA512:
6
- metadata.gz: 11dbff7b5605c81e604ac64e7fd4edc4e39de5bd863e4abfe5a96496e7d3e86081da97b38b8694fb0d5535fc2723adc1f4c81b76cbe55b0616d7592275f1372a
7
- data.tar.gz: 818e40d6413fd5960b4bd511eea7bcddb95d7b0668552edc56ea93ae663154725571a0e759bf624e4a25324959ad801a1332048a1a7db7b52827eb84e1247cdd
6
+ metadata.gz: 891ce789e1f09357ed92a36e161a60b91ef598f406c99adc79818335919f23095bc7f94795336ee4f445a0dc7683150c8153fb96dc514a8426ae521d38b6c72c
7
+ data.tar.gz: 3b311cca08a2296f9a055c458f7be806aa84547cfaf98d6bc829e13ed06efae46c56a1e8132eb7348a57924e5180d63758a21131517ade700e6e8e77c727d44b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.7
4
+ - 2.3.4
5
+ - 2.4.1
6
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trilateration.gemspec
4
+ gemspec
data/README.md CHANGED
@@ -1,10 +1,44 @@
1
+ # Trilateration [![Build Status](https://travis-ci.org/timgentry/trilateration.svg?branch=master)](https://travis-ci.org/timgentry/trilateration)
2
+
1
3
  Trilateration calculates the position of an unknown point, using the distance from three known points to it.
2
4
 
3
- Example Usage:
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'trilateration'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trilateration
4
20
 
5
- p1 = Trilateration::Point.new(1,2)
6
- p2 = Trilateration::Point.new(8,6)
7
- p3 = Trilateration::Point.new(1,6)
21
+ ## Usage
22
+
23
+ ```ruby
24
+ p1 = Vector[1, 2, 0]
25
+ p2 = Vector[8, 6, 0]
26
+ p3 = Vector[1, 6, 0]
8
27
  tri = Trilateration::Calculate.new(p1, p2, p3)
9
28
 
10
- output = tri.calculate_from_distances(4.02305854, 4.43677811, 3.87104637)
29
+ output = tri.calculate_from_distances(4.02305854, 4.29941857, 3.87104637)
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/timgentry/trilateration/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ desc 'Run tests'
12
+ task default: :test
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'trilateration'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require 'pry'
10
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -1,2 +1,6 @@
1
+ require 'trilateration/version'
1
2
  require 'trilateration/calculate'
2
- require 'trilateration/point'
3
+
4
+ module Trilateration
5
+ # Your code goes here...
6
+ end
@@ -1,62 +1,55 @@
1
+ require 'matrix'
2
+
1
3
  module Trilateration
2
4
  class Calculate
3
5
  attr_accessor :point1, :point2, :point3, :rel_point1, :rel_point2, :rel_point3
4
6
 
5
7
  # where point1 is our origin, and p2 and p3 are two other
6
8
  # sensors in which we know the relative positions
7
- def initialize p1, p2, p3
9
+ def initialize(p1, p2, p3)
8
10
  @point1 = p1
9
11
  @point2 = p2
10
12
  @point3 = p3
11
13
 
12
14
  # adjust all the points so that they are relative to p1
13
- @rel_point1 = Point.new(0, 0, 0)
14
- @rel_point2 = get_relative_point(@point2, @point1)
15
- @rel_point3 = get_relative_point(@point3, @point1)
15
+ @rel_point1 = Vector[0, 0, 0]
16
+ @rel_point2 = @point2 - @point1
17
+ @rel_point3 = @point3 - @point1
16
18
  end
17
19
 
18
- # Trilaterating an X,Y coordinate of an object based on its distance
20
+ # Trilaterating an X,Y coordinate of an object based on its distance
19
21
  # from three sensor points which are at known coordinate positions
20
22
  # input: target point relative to the first sensor
21
- def calculate_from_distances dist1, dist2, dist3
22
- x = ((dist1 ** 2) - (dist2 ** 2) + (@rel_point2.x ** 2)) / (2 * @rel_point2.x)
23
+ def calculate_from_distances(dist1, dist2, dist3)
24
+ # From Wikipedia: https://en.wikipedia.org/wiki/Trilateration
25
+ ex = @rel_point2 / @rel_point2.norm
26
+ i = ex.dot(@rel_point3)
27
+ ey = (@rel_point3 - (i * ex)) / (@rel_point3 - (i * ex)).norm
28
+ ez = ex.cross(ey)
29
+ d = @rel_point2.norm
30
+ j = ey.dot(@rel_point3)
23
31
 
24
- a = ((dist1 ** 2) - (dist3 ** 2) + (@rel_point3.x ** 2) + (@rel_point3.y ** 2)) / (2 * @rel_point3.y)
25
- b = (@rel_point3.x * dist1) / @rel_point3.y
32
+ x = (dist1**2 - dist2**2 + d**2) / (2 * d)
33
+ y = ((dist1**2 - dist3**2 + i**2 + j**2) / (2 * j)) - ((i / j) * x)
26
34
 
27
- y = a - b
35
+ z = Math.sqrt((dist1**2 - x**2 - y**2).abs)
28
36
 
29
- z = ((dist1 ** 2) - (x ** 2) - (y ** 2)) ** 0.5
30
-
31
- Point.new(x, y, z)
37
+ @point1 + (x * ex) + (y * ey) + (z * ez)
32
38
  end
33
39
 
34
40
  # used similarly as calculate_from_distances, except this is a test method
35
41
  # input: a target point whose coordinates from @point1 are already known,
36
42
  # in order to test the trilateration algorithm quickly
37
- def calculate_from_test_point target_point
38
- rel_target_point = get_relative_point(target_point, @point1)
39
- dist1 = calculate_dist1(rel_target_point)
40
- dist2 = calculate_dist2(rel_target_point)
41
- dist3 = calculate_dist3(rel_target_point)
42
-
43
- calculate_from_distances(dist1, dist2, dist3)
44
- end
45
-
46
- def get_relative_point relative_point, origin_point
47
- Point.new(relative_point.x - origin_point.x, relative_point.y - origin_point.y, relative_point.z - origin_point.z)
48
- end
49
-
50
- def calculate_dist1 target_point
51
- ((target_point.x ** 2) + (target_point.y ** 2) + (target_point.z ** 2)) ** 0.5
52
- end
43
+ def calculate_from_test_point(target_point)
44
+ dist1 = calculate_distance(target_point, @point1)
45
+ dist2 = calculate_distance(target_point, @point2)
46
+ dist3 = calculate_distance(target_point, @point3)
53
47
 
54
- def calculate_dist2 target_point
55
- (((target_point.x - @rel_point2.x) ** 2) + (target_point.y ** 2) + (target_point.z ** 2)) ** 0.5
48
+ calculate_from_distances(dist1, dist2, dist3)
56
49
  end
57
50
 
58
- def calculate_dist3 target_point
59
- (((target_point.x - @rel_point3.x) ** 2) + ((target_point.y - @rel_point3.y) ** 2) + (target_point.z ** 2)) ** 0.5
51
+ def calculate_distance(target_point, origin_point)
52
+ (target_point - origin_point).r
60
53
  end
61
54
  end
62
- end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Trilateration
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -1,15 +1,25 @@
1
- Gem::Specification.new do |s|
2
- s.name = 'trilateration'
3
- s.version = '0.0.1'
4
- s.date = '2014-05-29'
5
- s.summary = "Performs a trilateration operation in order to find an unknown point in a Cartesian coordinate system."
6
- s.description = "Trilateration calculates the position of an unknown point, using the distance from three known points to it."
7
- s.authors = ["Patrick Read"]
8
- s.email = 'pread13@gmail.com'
9
- s.files = %w(LICENSE README.md trilateration.gemspec)
10
- s.files += Dir.glob('lib/**/*.rb')
11
- s.files += Dir.glob('spec/**/*')
12
- s.test_files = Dir.glob('tests/**/*')
13
- s.homepage = 'https://github.com/patrickread/trilateration'
14
- s.license = 'MIT'
15
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trilateration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'trilateration'
8
+ spec.version = Trilateration::VERSION
9
+ spec.authors = ['Patrick Read', 'Tim Gentry']
10
+
11
+ spec.summary = %q(Performs a trilateration operation in order to find an unknown point in a Cartesian coordinate system.)
12
+ spec.description = %q(Trilateration calculates the position of an unknown point, using the distance from three known points to it.)
13
+ spec.homepage = 'https://github.com/patrickread/trilateration'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.9'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency 'pry'
25
+ end
metadata CHANGED
@@ -1,28 +1,90 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trilateration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Read
8
+ - Tim Gentry
8
9
  autorequire:
9
- bindir: bin
10
+ bindir: exe
10
11
  cert_chain: []
11
- date: 2014-05-29 00:00:00.000000000 Z
12
- dependencies: []
12
+ date: 2017-06-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.9'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pry
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
13
70
  description: Trilateration calculates the position of an unknown point, using the
14
71
  distance from three known points to it.
15
- email: pread13@gmail.com
72
+ email:
16
73
  executables: []
17
74
  extensions: []
18
75
  extra_rdoc_files: []
19
76
  files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
20
80
  - LICENSE
21
81
  - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
22
85
  - lib/trilateration.rb
23
86
  - lib/trilateration/calculate.rb
24
- - lib/trilateration/point.rb
25
- - tests/test_trilateration.rb
87
+ - lib/trilateration/version.rb
26
88
  - trilateration.gemspec
27
89
  homepage: https://github.com/patrickread/trilateration
28
90
  licenses:
@@ -44,10 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
106
  version: '0'
45
107
  requirements: []
46
108
  rubyforge_project:
47
- rubygems_version: 2.2.2
109
+ rubygems_version: 2.4.8
48
110
  signing_key:
49
111
  specification_version: 4
50
112
  summary: Performs a trilateration operation in order to find an unknown point in a
51
113
  Cartesian coordinate system.
52
- test_files:
53
- - tests/test_trilateration.rb
114
+ test_files: []
@@ -1,24 +0,0 @@
1
- module Trilateration
2
- class Point
3
- attr_accessor :x, :y, :z
4
-
5
- # Third dimension is optional
6
- def initialize pointX, pointY, pointZ=0
7
- @x = pointX
8
- @y = pointY
9
- @z = pointZ
10
- end
11
-
12
- def x_rounded
13
- @x.round(3)
14
- end
15
-
16
- def y_rounded
17
- @y.round(3)
18
- end
19
-
20
- def z_rounded
21
- @z.round(3)
22
- end
23
- end
24
- end
@@ -1,26 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "minitest/autorun"
4
- require_relative '../lib/trilateration.rb'
5
- require_relative '../lib/point.rb'
6
-
7
- class TrilaterationTest < Minitest::Test
8
-
9
- def test_trilateration
10
- p1 = Point.new(1,2)
11
- p2 = Point.new(8,6)
12
- p3 = Point.new(1,6)
13
- tri = Trilateration.new(p1, p2, p3)
14
-
15
- test_output = tri.calculate_from_test_point(Point.new(4.25, 4.15, 1))
16
- output = tri.calculate_from_distances(4.02305854, 4.43677811, 3.87104637)
17
-
18
- assert_equal(test_output.x_rounded, output.x_rounded, "X Coordinate is not correct")
19
- assert_equal(test_output.y_rounded, output.y_rounded, "Y Coordinate is not correct")
20
-
21
- if (!test_output.z.is_a?(Complex))
22
- refute_kind_of(Complex, output.z, "Z Coordinate shouldn't be a Complex")
23
- assert_equal(test_output.z_rounded, output.z_rounded, "Z Coordinate is not correct")
24
- end
25
- end
26
- end