Class: Envelope3

Inherits:
BaseEnvelope
  • Object
show all
Defined in:
lib/envelope3.rb

Overview

Envelope3 is the minimum 3D envelope that will completely contain a set of 3D points. 3D cartersian points or other 3D envelopes may be added to an instance, and it can return the number or points considered so far, the x, y and z envelopes, and the high and low Point3 boundaries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, y = 0, z = 0) ⇒ Envelope3

Creates and returns an instance. If arguments are given, they are passed to the set method to initialize the new instance.



21
22
23
24
25
26
27
# File 'lib/envelope3.rb', line 21

def initialize(x=nil,y=0,z=0)
  super()
  @x = Envelope.new
  @y = Envelope.new
  @z = Envelope.new
  add(x,y,z) unless x == nil
end

Instance Attribute Details

#xObject (readonly)

3D cartesian x coordinate envelope reader.



13
14
15
# File 'lib/envelope3.rb', line 13

def x
  @x
end

#yObject (readonly)

3D cartesian y coordinate envelope reader.



15
16
17
# File 'lib/envelope3.rb', line 15

def y
  @y
end

#zObject (readonly)

3D cartesian z coordinate envelope reader.



17
18
19
# File 'lib/envelope3.rb', line 17

def z
  @z
end

Instance Method Details

#add(x = 0, y = 0, z = 0) ⇒ Object

Adds a value to the instance. When

  • x is an Envelope3, it is coalesced into the instance.

  • x responds like a Point3, its coordinates are added.

  • x us Numeric, the arguments are added.

  • otherwise, an EnvelopeException is raised.

The modified instance is returned.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/envelope3.rb', line 41

def add(x=0,y=0,z=0)
  if x.kind_of? Envelope3
    count = x.count
    if count > 0
      add x.high
      add x.low
      @count += (count-2)
    end
    self
  elsif x.point3_like?
    add x.x, x.y, x.z
  elsif x.kind_of? Numeric
    @x.add x
    @y.add y
    @z.add z
    @count += 1
    self
  else
    raise_no_compare x
  end
end

#contains?(x = 0, y = 0, z = 0) ⇒ Boolean

Returns true if the instance completely contains the arguments:

  • x is an Envelope3, its high and low are contained.

  • x responds like a Point3, its coordinates are contained.

  • x is Numeric, the arguments are contained.

  • otherwise, false is returned.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/envelope3.rb', line 84

def contains?(x=0,y=0,z=0)
  if x.kind_of? Envelope3
    (contains? x.high) && (contains? x.low)
  elsif x.point3_like?
    contains?(x.x,x.y,x.z)
  elsif x.kind_of? Numeric
    (@x.contains? x) && (@y.contains? y) && (@z.contains? z)
  else
    raise_no_compare x
  end
end

#highObject

Returns a Point3 whose coordinates are the high values of the x, y, and z envelopes.

  • if there are no boundaries, an EnvelopeException is raised.



66
67
68
69
# File 'lib/envelope3.rb', line 66

def high
  raise_no_envelope if @count == 0
  (@high ||= Point3.new).set @x.high, @y.high, @z.high
end

#lowObject

Returns a Point3 whose coordinates are the low values of the x, y, and z envelopes.

  • if there are no boundaries, an EnvelopeException is raised.



74
75
76
77
# File 'lib/envelope3.rb', line 74

def low
  raise_no_envelope if @count == 0
  (@low ||= Point3.new).set @x.low, @y.low, @z.low
end

#to_sObject

Returns a string representation of the instance.



30
31
32
33
# File 'lib/envelope3.rb', line 30

def to_s
  values = (count > 0)? "\n  high  #{high}\n  low   #{low}" : ""
  "Envelope3: count #{count}#{values}"
end