changeset 1154:2795d0e114c9

deal with possibility of prototype with speed 0 that crashes motion prediction
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 09 Sep 2020 16:28:38 -0400
parents f52844c71454
children fd729e8f073c
files trafficintelligence/prediction.py
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/prediction.py	Mon Aug 24 16:02:06 2020 -0400
+++ b/trafficintelligence/prediction.py	Wed Sep 09 16:28:38 2020 -0400
@@ -69,6 +69,7 @@
         Prediction at constant speed will not work for unrealistic trajectories 
         that do not follow a slowly changing velocity (eg moving object trajectories, 
         but is good for realistic motion (eg features)'''
+        self.valid = True
         self.prototype = prototype
         self.constantSpeed = constantSpeed
         self.nFramesIgnore = nFramesIgnore
@@ -79,7 +80,12 @@
         self.theta = prototype.getVelocityAt(self.closestPointIdx).angle()
         self.initialSpeed = initialVelocity.norm2()
         if not constantSpeed:
-            self.ratio = self.initialSpeed/prototype.getVelocityAt(self.closestPointIdx).norm2()
+            while prototype.getVelocityAt(self.closestPointIdx).norm2() == 0. and self.closestPointIdx < prototype.length():
+                self.closestPointIdx += 1
+            if self.closestPointIdx < prototype.length():
+                self.ratio = self.initialSpeed/prototype.getVelocityAt(self.closestPointIdx).norm2()
+            else:
+                self.valid = False
     
     def predictPosition(self, nTimeSteps):
         if nTimeSteps > 0 and not nTimeSteps in self.predictedPositions:
@@ -570,7 +576,9 @@
             if similarities[instant-obj.getFirstInstant()] >= self.minSimilarity:
                 initialPosition = obj.getPositionAtInstant(instant)
                 initialVelocity = obj.getVelocityAtInstant(instant)
-                predictedTrajectories.append(PredictedTrajectoryPrototype(initialPosition, initialVelocity, proto.getMovingObject(), constantSpeed = self.constantSpeed, probability = proto.getNMatchings()))
+                predictedTrajectory = PredictedTrajectoryPrototype(initialPosition, initialVelocity, proto.getMovingObject(), constantSpeed = self.constantSpeed, probability = proto.getNMatchings())
+                if predictedTrajectory.valid:
+                    predictedTrajectories.append(predictedTrajectory)
         
     def generatePredictedTrajectories(self, obj, instant):
         predictedTrajectories = []