diff trafficintelligence/moving.py @ 1085:7853106677b7

added generate static function for CurvilinearTrajectory and modified how to create them with None list in lanes
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 25 Sep 2018 17:08:37 -0400
parents 346b41cbc81a
children 8734742c08c0
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Tue Jul 24 01:24:42 2018 -0400
+++ b/trafficintelligence/moving.py	Tue Sep 25 17:08:37 2018 -0400
@@ -1061,7 +1061,10 @@
 class CurvilinearTrajectory(Trajectory):
     '''Sub class of trajectory for trajectories with curvilinear coordinates and lane assignements
     longitudinal coordinate is stored as first coordinate (exterior name S)
-    lateral coordiante is stored as second coordinate'''
+    lateral coordinate is stored as second coordinate
+    the third "lane" coordinate is for an alignment id, 
+    whether explicit for a list/dict of alignments, 
+    or implicit for a road with lane numbers'''
 
     def __init__(self, S = None, Y = None, lanes = None):
         if S is None or Y is None or len(S) != len(Y):
@@ -1071,10 +1074,22 @@
         else:
             self.positions = [S,Y]
         if lanes is None or len(lanes) != self.length():
-            self.lanes = []
+            self.lanes = [None]*int(self.length())
         else:
             self.lanes = lanes
         
+    @staticmethod
+    def generate(s, v, nPoints, lane, y = 0):
+        '''s is initial position, v is velocity
+        0 in lateral coordinate by default
+        TODO 2D velocity for lane change?'''
+        S = [s]
+        for i in range(nPoints-1):
+            S.append(S[-1]+v)
+        Y = [y]*nPoints
+        lanes = [lane]*nPoints
+        return CurvilinearTrajectory(S, Y, lanes)
+
     def __getitem__(self,i): 
         if isinstance(i, int):
             return [self.positions[0][i], self.positions[1][i], self.lanes[i]]
@@ -1088,7 +1103,7 @@
     def getLanes(self):
         return self.lanes
 
-    def addPositionSYL(self, s, y, lane):
+    def addPositionSYL(self, s, y, lane = None):
         self.addPositionXY(s,y)
         self.lanes.append(lane)
 
@@ -1106,7 +1121,7 @@
         p1 = self[0]
         for i in range(1, self.length()):
             p2 = self[i]
-            diff.addPositionSYL(p2[0]-p1[0], p2[1]-p1[1], p1[2])
+            diff.addPositionSYL(p2[0]-p1[0], p2[1]-p1[1])
             p1=p2
         if doubleLastPosition and self.length() > 1:
             diff.addPosition(diff[-1])