root/branch/joao/umitMapper/Interpolation.py @ 596

Revision 596, 3.2 kB (checked in by ignotus21, 6 years ago)

Complete class documentation, bugs fix

Line 
1# Copyright (C) 2007 Adriano Monteiro Marques <py.adriano@gmail.com>
2#
3# Author: Joao Paulo de Souza Medeiros <ignotus21@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19from Numeric import arange
20
21
22class Linear2DInterpolator:
23    """
24    Implements a bidimesional linear interpolator.
25    """
26
27    def __init__(self):
28        """
29        Constructor method of Linear2DInterpolator class
30        """
31        self.__initialPoint = (0, 0)
32        """Initial point of interpolation"""
33        self.__finalPoint = (0, 0)
34        """Final point of interpolation"""
35        self.__interpolatedPoints = []
36        """Interpolated points vector"""
37
38
39    def setInitialPoint(self, a, b):
40        """
41        Set initial coordinate
42        Set final coordinate
43        @type  a: number
44        @param a: The first component of final point
45        @type  b: number
46        @param b: The second component of final point
47        """
48        self.__initialPoint = (a, b)
49
50
51    def setFinalPoint(self, a, b):
52        """
53        Set final coordinate
54        @type  a: number
55        @param a: The first component of final point
56        @type  b: number
57        @param b: The second component of final point
58        """
59        self.__finalPoint = (a, b)
60
61
62    def interpolate(self, a):
63        """
64        Return the primary point with the interpolated point
65        @type  a: number
66        @param a: The first component of point to be interpolated
67        @rtype: tuple
68        @return: Tuple with interpolated points
69        """
70        (ai, bi) = self.__initialPoint
71        (af, bf) = self.__finalPoint
72
73        return (a, bi + ((a - ai) * (bf - bi) / (af - ai)))
74
75
76    def getInterpolatedPoints(self, numberOfPass):
77        """
78        Return the vector of coordinates between the initial and final
79        coordinates with the specified size
80        @type  numberOfPass: number
81        @param numberOfPass: The number of pass of interpolation
82        @rtype: list
83        @return: A list of tuples with interpolated points
84        """
85        (ai, bi) = self.__initialPoint
86        (af, bf) = self.__finalPoint
87
88        aPass = float(af - ai) / numberOfPass
89        aList = arange(ai+aPass, af+aPass, aPass)
90
91        for i in range(numberOfPass):
92            self.__interpolatedPoints.insert( i, self.interpolate(aList[i]) )
93
94        return self.__interpolatedPoints
95
96
97
98if __name__ == "__main__":
99
100    # Testing application
101
102    i = Linear2DInterpolator()
103    i.setInitialPoint(0, 0)
104    i.setFinalPoint(1, 1)
105    print len(i.getInterpolatedPoints(10)), i.getInterpolatedPoints(10)
106
Note: See TracBrowser for help on using the browser.