overlap 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3d4dabbb1d2f5b4271c383006c00cd3b5da83e7
4
+ data.tar.gz: 30234480bf410c5f2df3440cb658a3d9561b823b
5
+ SHA512:
6
+ metadata.gz: a41667c84d3e2cb30bd77f7ef5eff2d6288fb0b889ec3226a0a9b6471ecc4e81903bf3a5f36ee9dbeb5284ca6d075beaaffe77531f05bfd012ffc7a7d5043a1d
7
+ data.tar.gz: 3ab1286d13326784599e917903718bac56f361bfacf570d4b9b0d76f4110a4a2bea82aaeef63c87a33c07bfae057b18da2e8ad8cda9c3c63b5aee63f4e637858
@@ -0,0 +1,8 @@
1
+ exit
2
+ ).collection
3
+ ]
4
+ [2, 4]
5
+ [1, 3],
6
+ [
7
+ Collection.new(
8
+ collection
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,7 @@
1
+ ### VERSION 0.2.0
2
+
3
+ * First version was wrong, algorithm didn't take in account merge
4
+
5
+ ### VERSION 0.1.0
6
+
7
+ * First version
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at joel.azemar@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in overlap.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'byebug'
8
+ gem 'pry-byebug'
9
+ gem 'coveralls', require: false
10
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joel AZEMAR
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.
@@ -0,0 +1,118 @@
1
+ # Overlap
2
+
3
+ [![Code Climate](https://codeclimate.com/github/FinalCAD/overlap.png)](https://codeclimate.com/github/FinalCAD/overlap)
4
+
5
+ [![Dependency Status](https://gemnasium.com/FinalCAD/overlap.svg)](https://gemnasium.com/FinalCAD/overlap)
6
+
7
+ [![Build Status](https://travis-ci.org/FinalCAD/overlap.svg?branch=master)](https://travis-ci.org/FinalCAD/overlap) (Travis CI)
8
+
9
+ [![Coverage Status](https://coveralls.io/repos/FinalCAD/overlap/badge.svg?branch=master&service=github)](https://coveralls.io/github/FinalCAD/overlap?branch=master)
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/overlap.svg)](https://badge.fury.io/rb/overlap)
12
+
13
+ The Overlap module provides methods to remove overlapping for linear segments, also provide some informations about them:
14
+
15
+ You can give a collection of object with a start position and end position and Overlap give you Unions, Overlap spot overlapped segments and merge them to one continuous segment, and provide to you some informations like intersection quantities, new quantity/distance of your segment
16
+
17
+ The main purpose of Overlaps -- given a set of Segment objects -- is to find all possible overlaps and merge overlapped segment to ones.
18
+
19
+ This gem is inspired by [overlaps](https://github.com/h0tl33t/overlaps) so thanks [Ryan Stenberg](https://github.com/h0tl33t)
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'overlap'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install overlap
36
+
37
+ ## Usage
38
+
39
+ Require the Overlap module and call #find on a collection. You can feed them two main types of input:
40
+
41
+ Array of Array objects
42
+
43
+ input_data = [ [1,4], [2,3], [5,6] ]
44
+ Overlaps.find(input_data)
45
+
46
+ Array of whatever object where each object is of the same class and the start and end point attribute/accessors are passed as values in an options hash to the :start and :end keys:
47
+
48
+ DataObject = Struct.new(:start_point, :end_point)
49
+
50
+ input_data = [ DataObject.new(1,10), DataObject.new(1,5), DataObject.new(3,6) ]
51
+ analysis = Overlaps.find(input_data, start: :start_point, end: :end_point)
52
+
53
+ Overlaps.find returns an Object Analysis, this contains all unions and informations about the global data analysis
54
+
55
+ New Segment
56
+
57
+ analysis.unions.each do |union|
58
+ union.segment => Overlap::Segment#... [1, 4]
59
+ union.segment => Overlap::Segment#... [5, 6]
60
+ end
61
+ analysis.segments => [ Overlap::Segment#... [1, 4], Overlap::Segment#... [5, 6] ]
62
+
63
+ All Segments overlapped
64
+
65
+ analysis.unions.each do |union|
66
+ union.segments => [ Overlap::Segment#... [1, 4], Overlap::Segment#... [2, 3] ]
67
+ union.segments => [ Overlap::Segment#... [5, 6] ]
68
+ end
69
+
70
+ Intersections
71
+
72
+ analysis.unions.each do |union|
73
+ union.intersections => [1]
74
+ union.intersections => []
75
+ end
76
+ analysis.intersections => [ 1, 0 ]
77
+
78
+ Quantity/Distance
79
+
80
+ analysis.unions.each do |union|
81
+ union.quantity => 3
82
+ union.quantity => 1
83
+ end
84
+ analysis.quantities => [ 3, 1 ]
85
+ analysis.quantity => 4
86
+
87
+ Quantity/Distance With Intersections
88
+
89
+ analysis.unions.each do |union|
90
+ union.quantity_with_intersections => 4
91
+ union.quantity_with_intersections => 1
92
+ end
93
+ analysis.quantities_with_intersections => [ 4, 1 ]
94
+ analysis.quantity_with_intersections => 5
95
+
96
+ Intersections Quantity/Distance
97
+
98
+ analysis.unions.each do |union|
99
+ union.intersection_quantity => 1
100
+ union.intersection_quantity => 0
101
+ end
102
+ analysis.intersections => [ 1, 0 ]
103
+ analysis.intersection_quantity => 1
104
+
105
+ ## Development
106
+
107
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
108
+
109
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/FinalCAD/overlap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
114
+
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ ### VERSION 0.1.0 => 0.2.0
2
+
3
+ Really important change, last version was wrong for complicated case
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "overlap"
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ require 'overlap/version'
2
+ require 'overlap/union'
3
+ require 'overlap/segment'
4
+ require 'overlap/collection'
5
+ require 'overlap/detect'
6
+ require 'overlap/analysis'
7
+
8
+ module Overlap
9
+
10
+ def self.find(data, options = {})
11
+ Analysis.new(data, options)
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module Overlap
2
+ class Analysis
3
+
4
+ attr_reader :unions, :segments, :intersections, :quantity_with_intersections, :quantities_with_intersections,
5
+ :intersection_quantity, :quantities, :quantity
6
+
7
+ def initialize(input_data, options={})
8
+ @input_data = input_data
9
+ @options = options
10
+ build!
11
+ end
12
+
13
+ private
14
+
15
+ def build!
16
+ @unions = Detect.new(
17
+ Collection.new(@input_data, @options).collection
18
+ ).overlaps.map do |overlapped_segments|
19
+ Union.new(overlapped_segments)
20
+ end
21
+ @segments = unions.map { |union| union.segment.to_a }
22
+ @intersections = unions.map { |union| union.intersection_quantity }
23
+ @intersection_quantity = intersections.reduce(:+)
24
+ @quantities_with_intersections = unions.map { |union| union.quantity_with_intersections }
25
+ @quantity_with_intersections = quantities_with_intersections.reduce(:+)
26
+ @quantities = unions.map { |union| (union.quantity&.round(3) || 0) }
27
+ @quantity = quantities.reduce(:+)&.round(3) || 0
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module Overlap
2
+ class Collection
3
+
4
+ attr_reader :collection
5
+
6
+ def initialize(input_collection, options={})
7
+ @input_collection = input_collection
8
+ @options = options
9
+ build!
10
+ end
11
+
12
+ def to_a
13
+ collection.map { |e| e.to_a }
14
+ end
15
+
16
+ private
17
+
18
+ def build!
19
+ _collection = @input_collection.map do |entry|
20
+ _start = entry.send(@options.fetch(:start, :first))
21
+ _end = entry.send(@options.fetch(:end, :last))
22
+ [ _start, _end ].sort if _start && _end
23
+ end.compact.sort { |x,y| x[0] <=> y[0] }
24
+ @collection = _collection.map { |x, y| Segment.new(x, y) }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ module Overlap
2
+ class Detect
3
+
4
+ attr_reader :overlaps
5
+
6
+ def initialize(collection)
7
+ @collection = collection
8
+ @overlaps = []
9
+ build!
10
+ end
11
+
12
+ private
13
+
14
+ def build!
15
+ destructive_collection = @collection.dup.to_a
16
+ current_segment = nil
17
+
18
+ while (one = destructive_collection.shift) do
19
+ current_segment = Segment.new(one.start_position, one.end_position)
20
+ segments = []
21
+ segments << one
22
+
23
+ @collection.map do |other|
24
+ next if current_segment.same?(other)
25
+ if current_segment.overlap?(other)
26
+ current_segment.merge!(other)
27
+ segments << other
28
+ end
29
+ end.compact
30
+
31
+ segments.each { |e| @collection.delete(e) }
32
+ segments.each { |e| destructive_collection.delete(e) }
33
+
34
+ @overlaps << { union: current_segment, segments: segments }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,75 @@
1
+ module Overlap
2
+ class Segment
3
+
4
+ attr_reader :center, :radius, :start_position, :end_position, :quantity, :key
5
+
6
+ def initialize(start_position, end_position)
7
+ @start_position = start_position
8
+ @end_position = end_position
9
+ build!
10
+ end
11
+
12
+ def merge!(other)
13
+ if other.start_position < start_position
14
+ @start_position = other.start_position
15
+ end
16
+
17
+ if end_position < other.end_position
18
+ @end_position = other.end_position
19
+ end
20
+
21
+ build!
22
+ end
23
+
24
+ def to_s
25
+ "[#{start_position.to_f}, #{end_position.to_f}]"
26
+ end
27
+ alias_method :inspect, :to_s
28
+
29
+ def inspect
30
+ "#{self.class.name}##{self.object_id} #{to_s}"
31
+ end
32
+
33
+ def to_a
34
+ [ start_position, end_position ]
35
+ end
36
+
37
+ def same?(other)
38
+ self == other
39
+ end
40
+
41
+ def overlap?(other)
42
+ (center - other.center).abs.round(3) <= (radius + other.radius).round(3)
43
+ end
44
+
45
+ def ==(other)
46
+ self.class == other.class && to_a == other.to_a
47
+ end
48
+ alias :eql? :==
49
+
50
+ def hash
51
+ to_a.hash
52
+ end
53
+
54
+ def intersection(other)
55
+ end_position - other.start_position
56
+ end
57
+
58
+ def <=>(other)
59
+ if start_position == other.start_position
60
+ end_position <=> other.end_position
61
+ else
62
+ start_position <=> other.start_position
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def build!
69
+ @quantity = end_position - start_position
70
+ @radius = quantity / 2
71
+ @center = start_position + radius
72
+ @key = [ start_position, end_position ]
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,32 @@
1
+ module Overlap
2
+ class Union
3
+
4
+ attr_reader :segment, :quantity, :intersections, :quantity_with_intersections, :intersection_quantity, :segments
5
+
6
+ def initialize(overlapped_segments)
7
+ @overlapped_segments = overlapped_segments
8
+ @segments = overlapped_segments[:segments].sort
9
+ build!
10
+ end
11
+
12
+ private
13
+
14
+ def build!
15
+ @segment = @overlapped_segments[:union]
16
+ @quantity = segment.quantity
17
+
18
+ previous_value = nil
19
+ @intersections = segments.sort.map do |_segment|
20
+ intersection = nil
21
+ if previous_value
22
+ intersection = previous_value - _segment.start_position
23
+ end
24
+ previous_value = _segment.end_position
25
+ intersection
26
+ end.compact
27
+
28
+ @intersection_quantity = intersections.reduce(:+)&.round(3) || 0
29
+ @quantity_with_intersections = segments.map(&:quantity).reduce(:+)&.round(3) || 0
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Overlap
2
+ VERSION = '0.2.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'overlap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "overlap"
8
+ spec.version = Overlap::VERSION
9
+ spec.authors = ["Joel AZEMAR"]
10
+ spec.email = ["joel.azemar@gmail.com"]
11
+
12
+ spec.summary = %q{Detect Overlaps}
13
+ spec.description = %q{Overlaps Tool}
14
+ spec.homepage = "https://github.com/FinalCAD/overlap"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: overlap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel AZEMAR
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-12 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Overlaps Tool
56
+ email:
57
+ - joel.azemar@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".byebug_history"
63
+ - ".coveralls.yml"
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CHANGELOG.md
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - UPGRADE.md
74
+ - bin/console
75
+ - bin/setup
76
+ - lib/overlap.rb
77
+ - lib/overlap/analysis.rb
78
+ - lib/overlap/collection.rb
79
+ - lib/overlap/detect.rb
80
+ - lib/overlap/segment.rb
81
+ - lib/overlap/union.rb
82
+ - lib/overlap/version.rb
83
+ - overlap.gemspec
84
+ homepage: https://github.com/FinalCAD/overlap
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.6
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Detect Overlaps
108
+ test_files: []