lens_protocol 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +98 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +7 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +106 -0
  10. data/Guardfile +42 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +69 -0
  13. data/Rakefile +6 -0
  14. data/bin/console +11 -0
  15. data/bin/setup +8 -0
  16. data/examples/images/R360_1.png +0 -0
  17. data/examples/oma/R1000_1.oma +213 -0
  18. data/examples/oma/R1000_2.oma +213 -0
  19. data/examples/oma/R1000_3.oma +217 -0
  20. data/examples/oma/R360_1.oma +78 -0
  21. data/examples/oma/R360_2.oma +75 -0
  22. data/examples/oma/R360_3.oma +276 -0
  23. data/examples/oma/TRCFMT6.oma +28 -0
  24. data/examples/public/styles.css +22 -0
  25. data/examples/svg.rb +10 -0
  26. data/examples/views/index.erb +26 -0
  27. data/lens_protocol.gemspec +33 -0
  28. data/lib/lens_protocol.rb +18 -0
  29. data/lib/lens_protocol/errors.rb +10 -0
  30. data/lib/lens_protocol/oma.rb +14 -0
  31. data/lib/lens_protocol/oma/formatter.rb +22 -0
  32. data/lib/lens_protocol/oma/message.rb +138 -0
  33. data/lib/lens_protocol/oma/parser.rb +24 -0
  34. data/lib/lens_protocol/oma/record.rb +19 -0
  35. data/lib/lens_protocol/oma/type/base.rb +97 -0
  36. data/lib/lens_protocol/oma/type/integer.rb +15 -0
  37. data/lib/lens_protocol/oma/type/numeric.rb +24 -0
  38. data/lib/lens_protocol/oma/type/r.rb +17 -0
  39. data/lib/lens_protocol/oma/type/text.rb +8 -0
  40. data/lib/lens_protocol/oma/type/trcfmt.rb +30 -0
  41. data/lib/lens_protocol/oma/types.rb +84 -0
  42. data/lib/lens_protocol/svg.rb +45 -0
  43. data/lib/lens_protocol/version.rb +3 -0
  44. metadata +213 -0
@@ -0,0 +1,15 @@
1
+ module LensProtocol
2
+ module OMA
3
+ module Type
4
+ class Integer < Base
5
+ def parse_value value
6
+ Integer(value) rescue Float(value).round rescue nil
7
+ end
8
+
9
+ def format_value value
10
+ value.round if value
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module LensProtocol
2
+ module OMA
3
+ module Type
4
+ class Numeric < Base
5
+ def initialize decimals: nil, **opts
6
+ super **opts
7
+ @decimals = decimals
8
+ end
9
+
10
+ def parse_value value
11
+ Float(value) rescue nil
12
+ end
13
+
14
+ def format_value value
15
+ if value && @decimals
16
+ "%.#{@decimals}f" % value
17
+ else
18
+ value
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module LensProtocol
2
+ module OMA
3
+ module Type
4
+ class R < Base
5
+ def parse line, message
6
+ label, values = label_and_values line
7
+ side = message.context(:last_trcfmt_side) or raise ParsingError.new('Could not found a corresponding TRCFMT record', line)
8
+ message.add_record_side_values(label, side, values.map(&:to_i))
9
+ end
10
+
11
+ def format _record, _message
12
+ [] # Formatted in Type::Trcfmt
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ module LensProtocol
2
+ module OMA
3
+ module Type
4
+ class Text < Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ module LensProtocol
2
+ module OMA
3
+ module Type
4
+ class Trcfmt < Base
5
+ def parse line, message
6
+ label, values = label_and_values line
7
+ side = side_position_from_trcfmt values
8
+ message.add_record_side_values(label, side, values).set_context(:last_trcfmt_side, side)
9
+ end
10
+
11
+ def format record, message
12
+ Array(record.value).select { |v| v&.any? }.flat_map do |values|
13
+ trcfmt_line = format_line(record.label, values)
14
+
15
+ side = side_position_from_trcfmt values
16
+ r_lines = message.value_of('R', [[], []])[side].each_slice(10).map do |group|
17
+ format_line('R', group)
18
+ end
19
+
20
+ [trcfmt_line, *r_lines]
21
+ end
22
+ end
23
+
24
+ def side_position_from_trcfmt values
25
+ values[3] == 'R' ? 0 : 1
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,84 @@
1
+ module LensProtocol
2
+ module OMA
3
+ TYPES = Hash.new { Type::Text.new }.merge(
4
+ 'ACOAT' => Type::Text.new(mode: :chiral),
5
+ 'ADD' => Type::Numeric.new(mode: :chiral, decimals: 2),
6
+ 'AX' => Type::Numeric.new(mode: :chiral, decimals: 2),
7
+ 'BACK' => Type::Numeric.new(mode: :chiral, decimals: 2),
8
+ 'BCTHK' => Type::Numeric.new(mode: :chiral, decimals: 2),
9
+ 'BEVM' => Type::Integer.new(mode: :chiral),
10
+ 'BEVP' => Type::Integer.new(mode: :chiral),
11
+ 'BSIZ' => Type::Integer.new(mode: :chiral),
12
+ 'BVD' => Type::Integer.new(mode: :chiral),
13
+ 'CRIB' => Type::Numeric.new(mode: :chiral, decimals: 2),
14
+ 'CTHICK' => Type::Numeric.new(mode: :chiral, decimals: 3),
15
+ 'CYL' => Type::Numeric.new(mode: :chiral, decimals: 2),
16
+ 'DBL' => Type::Numeric.new,
17
+ 'DIA' => Type::Numeric.new(mode: :chiral, decimals: 2),
18
+ 'DRILLE' => Type::Text.new(mode: :matrix_of_values),
19
+ 'ETYP' => Type::Integer.new,
20
+ 'FCOCIN' => Type::Numeric.new(mode: :chiral, decimals: 2),
21
+ 'FCOCUP' => Type::Numeric.new(mode: :chiral, decimals: 2),
22
+ 'FCSGIN' => Type::Numeric.new(mode: :chiral, decimals: 2),
23
+ 'FCSGUP' => Type::Numeric.new(mode: :chiral, decimals: 2),
24
+ 'FCRV' => Type::Numeric.new(mode: :chiral, decimals: 2),
25
+ 'FED' => Type::Numeric.new(mode: :chiral, decimals: 2),
26
+ 'FPINB' => Type::Numeric.new(mode: :chiral, decimals: 2),
27
+ 'FRNT' => Type::Numeric.new(mode: :chiral, decimals: 3),
28
+ 'FTYP' => Type::Integer.new,
29
+ 'FWD' => Type::Numeric.new(mode: :chiral),
30
+ 'GDEPTH' => Type::Numeric.new(mode: :chiral, decimals: 2),
31
+ 'GWIDTH' => Type::Numeric.new(mode: :chiral, decimals: 2),
32
+ 'GRADIENT' => Type::Integer.new(mode: :chiral),
33
+ 'HBOX' => Type::Numeric.new(mode: :chiral, decimals: 2),
34
+ 'IPD' => Type::Numeric.new(mode: :chiral, decimals: 2),
35
+ 'LDADD' => Type::Numeric.new(mode: :chiral),
36
+ 'LDDRAX' => Type::Numeric.new(mode: :chiral),
37
+ 'LDDRCYL' => Type::Numeric.new(mode: :chiral),
38
+ 'LDDRSPH' => Type::Numeric.new(mode: :chiral),
39
+ 'LDNAM' => Type::Text.new(mode: :chiral),
40
+ 'LDNRAX' => Type::Numeric.new(mode: :chiral),
41
+ 'LDNRCYL' => Type::Numeric.new(mode: :chiral),
42
+ 'LDNRSPH' => Type::Numeric.new(mode: :chiral),
43
+ 'LDVEN' => Type::Text.new(mode: :chiral),
44
+ 'LIND' => Type::Numeric.new(mode: :chiral, decimals: 3),
45
+ 'LMATID' => Type::Integer.new(mode: :chiral),
46
+ 'LMATTYPE' => Type::Text.new(mode: :chiral),
47
+ 'LNAM' => Type::Text.new(mode: :chiral),
48
+ 'LTYPE' => Type::Text.new(mode: :chiral),
49
+ 'MAXFRT' => Type::Numeric.new(mode: :chiral),
50
+ 'MBASE' => Type::Numeric.new(mode: :chiral, decimals: 2),
51
+ 'MINFRT' => Type::Numeric.new(mode: :chiral),
52
+ 'MINEDG' => Type::Numeric.new(mode: :chiral, decimals: 2),
53
+ 'MPD' => Type::Integer.new(mode: :chiral),
54
+ 'NPD' => Type::Numeric.new(mode: :chiral, decimals: 2),
55
+ 'OCHT' => Type::Numeric.new(mode: :chiral, decimals: 2),
56
+ 'OPC' => Type::Text.new(mode: :chiral),
57
+ 'OPTFRNT' => Type::Numeric.new(mode: :chiral),
58
+ 'PANTO' => Type::Integer.new(mode: :chiral),
59
+ 'PINB' => Type::Numeric.new(mode: :chiral, decimals: 2),
60
+ 'POLAR' => Type::Integer.new(mode: :chiral),
61
+ 'PRVA' => Type::Numeric.new(mode: :chiral, decimals: 1),
62
+ 'PRVM' => Type::Numeric.new(mode: :chiral, decimals: 2),
63
+ 'R' => Type::R.new,
64
+ 'SEGHT' => Type::Numeric.new(mode: :chiral, decimals: 2),
65
+ 'SGOCIN' => Type::Numeric.new(mode: :chiral, decimals: 2),
66
+ 'SGOCUP' => Type::Numeric.new(mode: :chiral, decimals: 2),
67
+ 'SPH' => Type::Numeric.new(mode: :chiral, decimals: 2),
68
+ 'STATUS' => Type::Integer.new,
69
+ 'THKP' => Type::Numeric.new(mode: :chiral, decimals: 2),
70
+ 'TINT' => Type::Text.new(mode: :chiral),
71
+ 'TRCFMT' => Type::Trcfmt.new(mode: :chiral),
72
+ 'VIEWP' => Type::Text.new(mode: :array_of_values),
73
+ 'VBOX' => Type::Numeric.new(mode: :chiral, decimals: 2),
74
+ 'XSTATUS' => Type::Text.new(mode: :matrix_of_values),
75
+ 'ZTILT' => Type::Integer.new(mode: :chiral),
76
+ '_BLANK' => Type::Text.new(mode: :matrix_of_values),
77
+ '_LLVAL' => Type::Integer.new(mode: :chiral),
78
+ '_PRVA1' => Type::Integer.new(mode: :chiral),
79
+ '_PRVA2' => Type::Integer.new(mode: :chiral),
80
+ '_PRVM1' => Type::Numeric.new(mode: :chiral, decimals: 1),
81
+ '_PRVM2' => Type::Numeric.new(mode: :chiral, decimals: 1),
82
+ )
83
+ end
84
+ end
@@ -0,0 +1,45 @@
1
+ module LensProtocol
2
+ module SVG
3
+ module_function
4
+
5
+ def from_message message, **opts
6
+ message
7
+ .tracing_in_rectangular_coordinates
8
+ .select(&:any?)
9
+ .map { |coordinates| from_rectangular_coordinates coordinates, **opts }
10
+ end
11
+
12
+ def from_rectangular_coordinates coordinates, polygon: {}, cross: {}, cross_size: 200
13
+ polygon_opts = {
14
+ 'stroke-width' => 50,
15
+ 'points' => polygon_points_from_coordinates(coordinates)
16
+ }.merge(polygon)
17
+
18
+ cross_opts = {
19
+ 'stroke-width' => 20
20
+ }.merge(cross)
21
+
22
+ Nokogiri::XML::Builder.new do |xml|
23
+ xml.svg(width: '100%', viewBox: view_box_from_coordinates(coordinates), xmlns: 'http://www.w3.org/2000/svg') do |xml|
24
+ xml.polygon polygon_opts
25
+ xml.line cross_opts.merge(x1: 0, y1: -cross_size, x2: 0, y2: cross_size)
26
+ xml.line cross_opts.merge(x1: -cross_size, y1: 0, x2: cross_size, y2: 0)
27
+ end
28
+ end.doc.root.to_s
29
+ end
30
+
31
+ # @param coordinates [Array] in rectangular form (+X to the right and +Y to the top)
32
+ def polygon_points_from_coordinates coordinates
33
+ coordinates.map { |(x, y)| [x.round, -y.round].join(' ') }.join(', ')
34
+ end
35
+
36
+ def view_box_from_coordinates coordinates
37
+ if coordinates.any?
38
+ # Double the max coordinates plus a 10% to leave some margin for the border
39
+ width = coordinates.map { |(x, _)| x.abs }.max * 2.1
40
+ height = coordinates.map { |(_, y)| y.abs }.max * 2.1
41
+ [-width / 2, -height / 2, width, height].join ' '
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module LensProtocol
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lens_protocol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emmanuel Nicolau
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
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: pry
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: pry-byebug
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
+ - !ruby/object:Gem::Dependency
126
+ name: sinatra
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: A Ruby parser and builder for the OMA protocol (a.k.a. Data Communication
140
+ Standard) that was developed by the Lens Processing & Technology Division of The
141
+ Vision Council for interconnection of optical laboratory equipment.
142
+ email:
143
+ - emmanicolau@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - ".rubocop.yml"
151
+ - ".ruby-version"
152
+ - ".travis.yml"
153
+ - CODE_OF_CONDUCT.md
154
+ - Gemfile
155
+ - Gemfile.lock
156
+ - Guardfile
157
+ - LICENSE.txt
158
+ - README.md
159
+ - Rakefile
160
+ - bin/console
161
+ - bin/setup
162
+ - examples/images/R360_1.png
163
+ - examples/oma/R1000_1.oma
164
+ - examples/oma/R1000_2.oma
165
+ - examples/oma/R1000_3.oma
166
+ - examples/oma/R360_1.oma
167
+ - examples/oma/R360_2.oma
168
+ - examples/oma/R360_3.oma
169
+ - examples/oma/TRCFMT6.oma
170
+ - examples/public/styles.css
171
+ - examples/svg.rb
172
+ - examples/views/index.erb
173
+ - lens_protocol.gemspec
174
+ - lib/lens_protocol.rb
175
+ - lib/lens_protocol/errors.rb
176
+ - lib/lens_protocol/oma.rb
177
+ - lib/lens_protocol/oma/formatter.rb
178
+ - lib/lens_protocol/oma/message.rb
179
+ - lib/lens_protocol/oma/parser.rb
180
+ - lib/lens_protocol/oma/record.rb
181
+ - lib/lens_protocol/oma/type/base.rb
182
+ - lib/lens_protocol/oma/type/integer.rb
183
+ - lib/lens_protocol/oma/type/numeric.rb
184
+ - lib/lens_protocol/oma/type/r.rb
185
+ - lib/lens_protocol/oma/type/text.rb
186
+ - lib/lens_protocol/oma/type/trcfmt.rb
187
+ - lib/lens_protocol/oma/types.rb
188
+ - lib/lens_protocol/svg.rb
189
+ - lib/lens_protocol/version.rb
190
+ homepage: https://github.com/eeng/lens_protocol
191
+ licenses: []
192
+ metadata: {}
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.7.3
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: LensProtocol is a Ruby parser and builder for the OMA protocol.
213
+ test_files: []