Module: Mindee::Geometry

Defined in:
lib/mindee/geometry/point.rb,
lib/mindee/geometry/utils.rb,
lib/mindee/geometry/min_max.rb,
lib/mindee/geometry/polygon.rb,
lib/mindee/geometry/quadrilateral.rb

Overview

Various helper functions & classes for geometry.

Defined Under Namespace

Classes: MinMax, Point, Polygon, Quadrilateral

Class Method Summary collapse

Class Method Details

.below?(candidate, anchor, margin_left, margin_right) ⇒ Boolean

Checks whether a set of coordinates is below another on the page, with a slight margin for the lateral value.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mindee/geometry/utils.rb', line 74

def self.below?(candidate, anchor, margin_left, margin_right)
  return false if Geometry.get_min_max_y(candidate).min < Geometry.get_min_max_y(anchor).min
  if Geometry.get_min_max_x(candidate).min <
     Geometry.get_min_max_x(anchor).min - (Geometry.get_min_max_x(anchor).min * margin_left)
    return false
  end
  if Geometry.get_min_max_x(candidate).max >
     Geometry.get_min_max_x(anchor).max + (Geometry.get_min_max_x(anchor).max * margin_right)
    return false
  end

  true
end

.get_bbox(vertices) ⇒ Array<Float>

Gets the points of a bounding box for a given set of points



23
24
25
26
27
# File 'lib/mindee/geometry/utils.rb', line 23

def self.get_bbox(vertices)
  x_coords = vertices.map(&:x)
  y_coords = vertices.map(&:y)
  [x_coords.min, y_coords.min, x_coords.max, y_coords.max]
end

.get_bounding_box(vertices) ⇒ Mindee::Geometry::Quadrilateral

Creates the bounding box for a given set of points



32
33
34
35
36
37
38
39
40
# File 'lib/mindee/geometry/utils.rb', line 32

def self.get_bounding_box(vertices)
  x_min, y_min, x_max, y_max = get_bbox(vertices)
  Quadrilateral.new(
    Point.new(x_min, y_min),
    Point.new(x_max, y_min),
    Point.new(x_max, y_max),
    Point.new(x_min, y_max)
  )
end

.get_centroid(points) ⇒ Mindee::Geometry::Point

Get the central point (centroid) given a sequence of points.



45
46
47
48
49
50
# File 'lib/mindee/geometry/utils.rb', line 45

def self.get_centroid(points)
  vertices_count = points.size
  x_sum = points.map(&:x).sum
  y_sum = points.map(&:y).sum
  Point.new(x_sum / vertices_count, y_sum / vertices_count)
end

.get_min_max_x(points) ⇒ Mindee::Geometry::MinMax

Get the maximum and minimum X value given a sequence of points.



63
64
65
66
# File 'lib/mindee/geometry/utils.rb', line 63

def self.get_min_max_x(points)
  coords = points.map(&:x)
  MinMax.new(coords.min, coords.max)
end

.get_min_max_y(points) ⇒ Mindee::Geometry::MinMax

Get the maximum and minimum Y value given a sequence of points.



55
56
57
58
# File 'lib/mindee/geometry/utils.rb', line 55

def self.get_min_max_y(points)
  coords = points.map(&:y)
  MinMax.new(coords.min, coords.max)
end

.quadrilateral_from_prediction(prediction) ⇒ Mindee::Geometry::Quadrilateral

Transform a prediction into a Quadrilateral.



9
10
11
12
13
14
15
16
17
18
# File 'lib/mindee/geometry/utils.rb', line 9

def self.quadrilateral_from_prediction(prediction)
  throw "Prediction must have exactly 4 points, found #{prediction.size}" if prediction.size != 4

  Quadrilateral.new(
    Point.new(prediction[0][0], prediction[0][1]),
    Point.new(prediction[1][0], prediction[1][1]),
    Point.new(prediction[2][0], prediction[2][1]),
    Point.new(prediction[3][0], prediction[3][1])
  )
end