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.

Parameters:

  • candidate (Array<Mindee::Geometry::Point] Polygon to check)

    andidate [Array<Mindee::Geometry::Point] Polygon to check

  • anchor (Array<Mindee::Geometry::Point] Reference polygon)

    nchor [Array<Mindee::Geometry::Point] Reference polygon

  • margin_left (Float)

    Margin tolerance on the left of the anchor

  • margin_right (Float)

    Margin tolerance on the right of the anchor

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mindee/geometry/utils.rb', line 86

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

Parameters:

Returns:

  • (Array<Float>)


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

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 bounding box for a given set of points

Parameters:

Returns:



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

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.

Parameters:

Returns:



58
59
60
61
62
63
# File 'lib/mindee/geometry/utils.rb', line 58

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.

Parameters:

Returns:



76
77
78
79
# File 'lib/mindee/geometry/utils.rb', line 76

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.

Parameters:

Returns:



68
69
70
71
# File 'lib/mindee/geometry/utils.rb', line 68

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

.polygon_from_prediction(prediction) ⇒ Mindee::Geometry::Polygon

Transform a prediction into a Polygon.

Parameters:

  • prediction (Hash)

Returns:



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

def self.polygon_from_prediction(prediction)
  polygon = Polygon.new
  return polygon if prediction.nil?

  prediction.each do |point|
    polygon << Point.new(point[0], point[1])
  end
  polygon
end

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

Transform a prediction into a Quadrilateral.

Parameters:

  • prediction (Hash)

Returns:



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