Class: Panomosity::ControlPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/panomosity/control_point.rb

Constant Summary collapse

@@attributes =
%i(n N x y X Y t g)
@@calculated_attributes =
%i(dist px py pdist prx pry prdist conn_type i1 i2)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ControlPoint

Returns a new instance of ControlPoint.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/panomosity/control_point.rb', line 109

def initialize(attributes)
  @attributes = attributes
  # conform data types
  @attributes.each do |key, value|
    next if %i(raw).include?(key)
    next unless value.is_a?(String)
    if value.respond_to?(:include?) && value.include?('.')
      @attributes[key] = value.to_f
    else
      @attributes[key] = value.to_i
    end
  end
end

Class Method Details

.allObject



81
82
83
# File 'lib/panomosity/control_point.rb', line 81

def self.all
  @control_points
end

.calculate_distances(images, panorama_variable) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/panomosity/control_point.rb', line 43

def self.calculate_distances(images, panorama_variable)
  @control_points.each do |cp|
    image1 = images.find { |i| cp.n1 == i.id }
    image2 = images.find { |i| cp.n2 == i.id }
    point1 = image1.to_cartesian(cp.x1, cp.y1)
    point2 = image2.to_cartesian(cp.x2, cp.y2)

    product = point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2]
    product = 1.0 if product > 1.0
    angle = Math.acos(product)
    radius = (panorama_variable.w / 2.0) / Math.tan((panorama_variable.v * Math::PI / 180) / 2)

    distance = angle * radius
    cp.dist = distance

    cp.i1 = image1
    cp.i2 = image2

    # pixel distance
    # NOTE d,e distances are highest on the top-left and lowest at the bottom-right
    #      control point coordinates are highest on bottom-right and lowest on the top-left
    x1 = (cp.i1.w / 2.0) - cp.x1 + cp.i1.d
    y1 = (cp.i1.h / 2.0) - cp.y1 + cp.i1.e
    x2 = (cp.i2.w / 2.0) - cp.x2 + cp.i2.d
    y2 = (cp.i2.h / 2.0) - cp.y2 + cp.i2.e

    cp.px = x1 - x2
    cp.py = y1 - y2
    cp.pdist = Math.sqrt(cp.px ** 2 + cp.py ** 2)

    # pixel distance including roll
    r = image1.r * Math::PI / 180
    cp.prx = Math.cos(r) * cp.px - Math.sin(r) * cp.py
    cp.pry = Math.cos(r) * cp.py + Math.sin(r) * cp.px
    cp.prdist = Math.sqrt(cp.prx ** 2 + cp.pry ** 2)
  end
end

.get_detailed_info(pto_file_path, cp_type: nil) ⇒ Object



36
37
38
39
40
41
# File 'lib/panomosity/control_point.rb', line 36

def self.get_detailed_info(pto_file_path, cp_type: nil)
  exe_dir = File.expand_path('../../exe', File.dirname(__FILE__))
  control_point_info_executable = File.join(exe_dir, 'control_point_info.pl')
  result = `#{control_point_info_executable} --input #{pto_file_path}`
  parse(result, cp_type: cp_type, compact: true)
end

.merge(first_set_control_points, second_set_control_points) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/panomosity/control_point.rb', line 101

def self.merge(first_set_control_points, second_set_control_points)
  @control_points = first_set_control_points.map do |cp1|
    similar_control_point = second_set_control_points.find { |cp2| cp1 == cp2 }
    cp1.dist = similar_control_point.dist
    cp1
  end
end

.parse(pto_file, cp_type: nil, compact: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/panomosity/control_point.rb', line 9

def self.parse(pto_file, cp_type: nil, compact: false)
  @control_points = pto_file.each_line.map do |line|
    if compact
      cp_data = line.split(',').map { |part| part.split(' ') }.flatten
      n1, x1, y1, n2, x2, y2, type, dist = *cp_data.to_a.map(&:to_f)
      new(n: n1, N: n2, x: x1, y: y1, X: x2, Y: y2, t: type, dist: dist, raw: line)
    else
      parse_line(line)
    end
  end.compact

  case cp_type
    when :line
      @control_points.select!(&:line?)
    when :normal
      @control_points.select!(&:normal?)
    when :vertical
      @control_points.select!(&:vertical?)
    when :horizontal
      @control_points.select!(&:horizontal?)
    else
      @control_points
  end

  @control_points
end

.parse_line(line) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/panomosity/control_point.rb', line 85

def self.parse_line(line)
  parts = line.split(' ')
  if parts.first == 'c'
    data = parts.each_with_object({}) do |part, hash|
      attribute = @@attributes.find { |attr| part[0] == attr.to_s }
      next unless attribute
      hash[attribute] = part.sub(attribute.to_s, '')
    end

    data[:raw] = line
    data[:dist] = nil

    new data
  end
end

Instance Method Details

#==(o) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/panomosity/control_point.rb', line 179

def ==(o)
  n1 == o.n1 &&
  n2 == o.n2 &&
  x1.floor == o.x1.floor &&
  x2.floor == o.x2.floor &&
  y1.floor == o.y1.floor &&
  y2.floor == o.y2.floor
end

#[](key) ⇒ Object



123
124
125
# File 'lib/panomosity/control_point.rb', line 123

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



127
128
129
# File 'lib/panomosity/control_point.rb', line 127

def []=(key, value)
  @attributes[key] = value
end

#attributes(raw: false) ⇒ Object



199
200
201
# File 'lib/panomosity/control_point.rb', line 199

def attributes(raw: false)
  @attributes.keep_if { |k, _| raw || !%i(raw i1 i2).include?(k) }
end

#detailed_infoObject



195
196
197
# File 'lib/panomosity/control_point.rb', line 195

def detailed_info
  "#{to_s.sub(/\n/, '')} dist #{dist.round(4)} pixel_dist #{px.round(4)},#{py.round(4)},#{pdist.round(4)} pixel_r_dist #{prx.round(4)},#{pry.round(4)},#{prdist.round(4)} conn_type #{conn_type}"
end

#generated?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/panomosity/control_point.rb', line 165

def generated?
  !g.nil?
end

#horizontal?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/panomosity/control_point.rb', line 157

def horizontal?
  type == 2
end

#line?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/panomosity/control_point.rb', line 161

def line?
  vertical? || horizontal?
end

#normal?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/panomosity/control_point.rb', line 149

def normal?
  type == 0
end

#not_generated?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/panomosity/control_point.rb', line 169

def not_generated?
  !generated?
end

#recalculate_pixel_distanceObject



188
189
190
191
192
193
# File 'lib/panomosity/control_point.rb', line 188

def recalculate_pixel_distance
  r = i1.r * Math::PI / 180
  self.prx = Math.cos(r) * px - Math.sin(r) * py
  self.pry = Math.cos(r) * py + Math.sin(r) * px
  self.prdist = Math.sqrt(prx ** 2 + pry ** 2)
end

#to_sObject



173
174
175
176
177
# File 'lib/panomosity/control_point.rb', line 173

def to_s
  attrs = generated? ? @@attributes : (@@attributes - %i(g))
  line_values = attrs.map { |attribute| "#{attribute}#{self.send(attribute)}" }
  "c #{line_values.join(' ')}\n"
end

#vertical?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/panomosity/control_point.rb', line 153

def vertical?
  type == 1
end