Class: NDPointList

Inherits:
Object
  • Object
show all
Defined in:
lib/kamelopard/pointlist.rb

Overview

require ‘kamelopard_classes’

Direct Known Subclasses

OneDPointList, ThreeDPointList, TwoDPointList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ NDPointList

Returns a new instance of NDPointList.



10
11
12
13
14
# File 'lib/kamelopard/pointlist.rb', line 10

def initialize(num)
    raise "Can't have an NDPointList with #{num} dimensions -- must be 1 or more" if num < 1
    @dim = num
    @points = []
end

Instance Attribute Details

#dimObject (readonly)

Contains a list of N-dimensional numeric arrays



8
9
10
# File 'lib/kamelopard/pointlist.rb', line 8

def dim
  @dim
end

Instance Method Details

#<<(a) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kamelopard/pointlist.rb', line 20

def <<(a)
    # Append points to our list
    if a.kind_of? Kamelopard::Point then
        if self.dim == 3 then
            @points << [a.longitude, a.latitude, a.altitude]
        else
            @points << [a.longitude, a.latitude]
        end
    elsif a.respond_to? 'dim' and @dim != a.dim then
        raise "Argument's dimension #{a.dim} must agree with our dimension #{@dim} to append to an NDPointList"
    else
        @points << a
    end
end

#[](i) ⇒ Object



39
40
41
# File 'lib/kamelopard/pointlist.rb', line 39

def [](i)
    @points[i]
end

#each(&blk) ⇒ Object



63
64
65
# File 'lib/kamelopard/pointlist.rb', line 63

def each(&blk)
    @points.each(&blk)
end

#interpolate(resolution = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/kamelopard/pointlist.rb', line 67

def interpolate(resolution = nil)
    # XXX Figure out how to implement the "resolution" argument
    STDERR.puts "resolution argument to NDPointList.interpolate is ignored" if resolution.nil?
    # Ruby implementation of Catmull-Rom splines (http://www.cubic.org/docs/hermite.htm)
    # Return NDPointList interpolating a path along all points in this list

    h = Matrix[
        [ 2,  -2,   1,   1 ],
        [-3,   3,  -2,  -1 ],
        [ 0,   0,   1,   0 ],
        [ 1,   0,   0,   0 ],
    ]

    result = NDPointList.new(@dim)

    # Calculate spline between every two points
    (0..(self.size-2)).each do |i|
        p1 = self[i]
        p2 = self[i+1]
        
        # Get surrounding points for calculating tangents
        if i <= 0 then pt1 = p1 else pt1 = self[i-1] end
        if i == self.size - 2 then pt2 = p2 else pt2 = self[i+2] end

        # Build tangent points into matrices to calculate tangents.
        t1 = 0.5 * ( Matrix[p2]  - Matrix[pt1] )
        t2 = 0.5 * ( Matrix[pt2] - Matrix[p1] )

        # Build matrix of Hermite parameters
        c = Matrix[p1, p2, t1.row(0), t2.row(0)]

        # Make a set of points
        (0..10).each do |t|
            r = t/10.0
            s = Matrix[[r**3, r**2, r, 1]]
            tmp = s * h
            p = tmp * c
            result << p.row(0).to_a
        end
    end
    result
end

#lastObject



35
36
37
# File 'lib/kamelopard/pointlist.rb', line 35

def last
    @points.last
end

#sizeObject



16
17
18
# File 'lib/kamelopard/pointlist.rb', line 16

def size
    return @points.size
end

#xObject



43
44
45
# File 'lib/kamelopard/pointlist.rb', line 43

def x
    @points.collect do |a| a[0] end
end

#yObject



47
48
49
50
51
52
53
# File 'lib/kamelopard/pointlist.rb', line 47

def y
    if @dim >= 2 then
        @points.collect do |a| a[1] end
    else
        raise "NDPointList of size #{@dim} has no Y element"
    end
end

#zObject



55
56
57
58
59
60
61
# File 'lib/kamelopard/pointlist.rb', line 55

def z
    if @dim >= 2 then
        @points.collect do |a| a[2] end
    else
        raise "NDPointList of size #{@dim} has no Z element"
    end
end