comparison python/moving.py @ 2:de5642925615

started implementation of TimeInterval and Spatio-temporal object
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 19 Oct 2009 13:16:21 -0400
parents aed8eb63cdde
children 597d61c1eebe
comparison
equal deleted inserted replaced
1:f5806717cc36 2:de5642925615
4 import utils; 4 import utils;
5 5
6 __metaclass__ = type 6 __metaclass__ = type
7 7
8 #class MovingObject: 8 #class MovingObject:
9
10 class TimeInterval:
11 '''Temporal interval'''
12 def __init__(self, first=0, last=-1):
13 self.first=first
14 self.last=last
15
16 def __str__(self):
17 return '%d %d'%(self.first, self.last)
18
19 def empty(self):
20 '''
21 >>> TimeInterval().empty()
22 True
23 >>> TimeInterval(0,1).empty()
24 False
25 '''
26 return self.first > self.last
27
28 def length(self):
29 '''Returns the length of the interval
30
31 >>> TimeInterval(0,1).length()
32 2
33 >>> TimeInterval(10,8).length()
34 0
35 '''
36 return max(0,self.last-self.first+1)
37
38 def getList(self):
39 return [self.first, self.last]
40
41 def contains(self, instant):
42 return (self.first<=instant and self.last>=instant)
43
44 def inside(self, interval2):
45 'indicates if the temporal interval of self is comprised in interval2'
46 return (self.first >= interval2.first) and (self.last <= interval2.last)
47
48 def union(self, interval2):
49 '''Largest interval comprising self and interval2'''
50 return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last))
51
52 def intersection(self, interval2):
53 '''Largest interval comprising self and interval2'''
54 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last))
55
56 class STObject:
57 '''Class for spatio-temporal object
58 i.e. with temporal and spatial existence
59 (time interval and bounding polygon for positions (e.g. rectangle)'''
60
61 def __init__(self, timeInterval = TimeInterval(), boundingPolygon = None, num = None):
62 self.timeInterval = timeInterval
63 self.boundingPolygon = boundingPolygon
64 self.num = num
65
66 def empty(self):
67 return self.timeInterval.empty()
68
69 def getFirstInstant(self):
70 return self.timeInterval.first()
71
72 def getLastInstant(self):
73 return self.timeInterval.first()
74
75
76
77 # return bounding box, and generic polygon, that defaults to box
78
79 class Track(STObject):
80 '''Class for trajectories
81 i.e. sequence of positions'''
82 pass
83
84
85
86 if __name__ == "__main__":
87 import doctest
88 import unittest
89 #suite = doctest.DocFileSuite('tests/ubc_utils.txt')
90 suite = doctest.DocTestSuite()
91 unittest.TextTestRunner().run(suite)
92 #doctest.testmod()
93 #doctest.testfile("example.txt")