Module: Sumi::Geometry::Operation

Extended by:
Operation
Included in:
Operation
Defined in:
geometry/lib/sumi/geometry/operation.rb,
geometry/lib/sumi/geometry/operation.rb,
geometry/lib/sumi/geometry/operation/box.rb,
geometry/lib/sumi/geometry/operation/align.rb,
geometry/lib/sumi/geometry/operation/sweep.rb

Overview

Stateless operations that relate or derive geometry values.

alignment returns a translation offset, while align applies that offset to a copy. Center alignment works on both axes. Edge alignments change only the named axis and preserve the other coordinate. sweep derives boundary-to-boundary spans from a closed geometry element, and sweep! additionally requires at least one span.

Constant Summary collapse

OperationError =

Raised when an operation starts but cannot complete.

Class.new(Error)
OperationInapplicableError =

Raised when an operation does not apply to the target element.

Class.new(Error)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.align(element, other, alignment = :center) ⇒ Sumi::Geometry::Element

Returns an element translated to align with another element. Center alignment changes both axes. Edge alignments change only the named axis.

Examples:

Center one rectangle inside another

inner = Sumi::Geometry::Rect[4, 2]
outer = Sumi::Geometry::Rect[20, 10, position: [5, 5]]
Sumi::Geometry::Operation.align(inner, outer).position.deconstruct # => [13.0, 9.0]

Parameters:

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'geometry/lib/sumi/geometry/operation.rb', line 82

module Operation
  extend self

  # Raised when an operation starts but cannot complete.
  OperationError = Class.new(Error)

  # Raised when an operation does not apply to the target element.
  OperationInapplicableError = Class.new(Error)

  private

  # Registers one or more public operation methods.
  # @api private
  # @param handler [Module] operation handler module
  # @param operations [Array<Symbol>] operation method names
  # @return [Array<Symbol>] registered operation names
  def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) }

  def def_operation(operation, handler)
    define_singleton_method(operation) do |element, *args, **kwargs, &block|
      OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element)
      unless handler.applicable?(element)
        OperationInapplicableError.("Operation not applicable to #{element}: #{handler}")
      end

      handler.public_send(operation, element, *args, **kwargs, &block)
    end
  end
end

.alignment(element, other, alignment = :center) ⇒ Sumi::Geometry::Point

Returns the offset needed to align one element with another. Center alignment includes both axes. Edge alignments return zero on the other axis.

Examples:

Calculate an offset without moving the element

inner = Sumi::Geometry::Rect[4, 2]
outer = Sumi::Geometry::Rect[20, 10, position: [5, 5]]
Sumi::Geometry::Operation.alignment(inner, outer, :bottom).approx.deconstruct # => [0.0, 13.0]

Parameters:

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'geometry/lib/sumi/geometry/operation.rb', line 82

module Operation
  extend self

  # Raised when an operation starts but cannot complete.
  OperationError = Class.new(Error)

  # Raised when an operation does not apply to the target element.
  OperationInapplicableError = Class.new(Error)

  private

  # Registers one or more public operation methods.
  # @api private
  # @param handler [Module] operation handler module
  # @param operations [Array<Symbol>] operation method names
  # @return [Array<Symbol>] registered operation names
  def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) }

  def def_operation(operation, handler)
    define_singleton_method(operation) do |element, *args, **kwargs, &block|
      OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element)
      unless handler.applicable?(element)
        OperationInapplicableError.("Operation not applicable to #{element}: #{handler}")
      end

      handler.public_send(operation, element, *args, **kwargs, &block)
    end
  end
end

.sweep(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>

Sweeps parallel lines across a geometry element in both directions. angle is the direction of the returned lines. step is their signed perpendicular spacing. Open paths yield no interior spans.

Examples:

Generate horizontal spans through a rectangle

rect = Sumi::Geometry::Rect[10, 6]
lines = Sumi::Geometry::Operation.sweep(rect, initial: [0, 0], angle: 0, step: 2)
lines.size                # => 4
lines.map(&:length).uniq # => [10.0]

Parameters:

  • element (Sumi::Geometry::Element) —

    element to intersect

  • initial (Sumi::Geometry::Point, Array<Numeric>) —

    point on the initial sweep line

  • angle (Numeric) —

    clockwise sweep line angle in degrees

  • step (Numeric) —

    signed distance between sweep lines

  • limit (Integer) (defaults to: Sweep::LIMIT) —

    maximum iterations per direction

Yields:

  • (lines) —

    optional hook receiving the generated lines

Yield Parameters:

Yield Returns:

  • (void)

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'geometry/lib/sumi/geometry/operation.rb', line 82

module Operation
  extend self

  # Raised when an operation starts but cannot complete.
  OperationError = Class.new(Error)

  # Raised when an operation does not apply to the target element.
  OperationInapplicableError = Class.new(Error)

  private

  # Registers one or more public operation methods.
  # @api private
  # @param handler [Module] operation handler module
  # @param operations [Array<Symbol>] operation method names
  # @return [Array<Symbol>] registered operation names
  def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) }

  def def_operation(operation, handler)
    define_singleton_method(operation) do |element, *args, **kwargs, &block|
      OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element)
      unless handler.applicable?(element)
        OperationInapplicableError.("Operation not applicable to #{element}: #{handler}")
      end

      handler.public_send(operation, element, *args, **kwargs, &block)
    end
  end
end

.sweep!(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>

Sweeps parallel lines across a geometry element and requires at least one result. It has the same geometry as sweep, but raises when the result is empty.

Parameters:

  • element (Sumi::Geometry::Element) —

    element to intersect

  • initial (Sumi::Geometry::Point, Array<Numeric>) —

    point on the initial sweep line

  • angle (Numeric) —

    clockwise sweep line angle in degrees

  • step (Numeric) —

    signed distance between sweep lines

  • limit (Integer) (defaults to: Sweep::LIMIT) —

    maximum iterations per direction

Yields:

  • (lines) —

    optional hook receiving the generated lines

Yield Parameters:

Yield Returns:

  • (void)

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'geometry/lib/sumi/geometry/operation.rb', line 82

module Operation
  extend self

  # Raised when an operation starts but cannot complete.
  OperationError = Class.new(Error)

  # Raised when an operation does not apply to the target element.
  OperationInapplicableError = Class.new(Error)

  private

  # Registers one or more public operation methods.
  # @api private
  # @param handler [Module] operation handler module
  # @param operations [Array<Symbol>] operation method names
  # @return [Array<Symbol>] registered operation names
  def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) }

  def def_operation(operation, handler)
    define_singleton_method(operation) do |element, *args, **kwargs, &block|
      OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element)
      unless handler.applicable?(element)
        OperationInapplicableError.("Operation not applicable to #{element}: #{handler}")
      end

      handler.public_send(operation, element, *args, **kwargs, &block)
    end
  end
end

Instance Method Details

#box(*elements) ⇒ Sumi::Geometry::Rect

Returns the smallest axis-aligned rectangle enclosing all elements.

Each element contributes its existing Element#box, including zero-size boxes whose positions may still extend the result.

Examples:

Enclose several geometry values

a = Sumi::Geometry::Rect[10, 5, position: [2, 3]]
b = Sumi::Geometry::Line.([-4, 8], [20, 12])
Sumi::Geometry::Operation.box(a, b) # => Rect spanning both elements

Parameters:

Returns:

Raises:



18
19
20
21
22
23
# File 'geometry/lib/sumi/geometry/operation/box.rb', line 18

def box(*elements)
  validate_box_elements(elements)
  boxes = elements.map(&:box)

  Rect.from_corners(minimum_corner(boxes), maximum_corner(boxes))
end