view scripts/safety-analysis.py @ 372:349eb1e09f45

Cleaned the methods/functions indicating if a point is in a polygon In general, shapely should be used, especially for lots of points: from shapely.geometry import Polygon, Point poly = Polygon(array([[0,0],[0,1],[1,1],[1,0]])) p = Point(0.5,0.5) poly.contains(p) -> returns True poly.contains(Point(-1,-1)) -> returns False You can convert a moving.Point to a shapely point: p = moving.Point(1,2) p.asShapely() returns the equivalent shapely point If you have several points to test, use moving.pointsInPolygon(points, polygon) where points are moving.Point and polygon is a shapely polygon.
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 16 Jul 2013 17:00:17 -0400
parents 619ae9a9a788
children 91679eb2ff2c
line wrap: on
line source

#! /usr/bin/env python

import utils, storage, prediction, events, moving

import sys, argparse

import matplotlib.pyplot as plt
import numpy as np

# todo: very slow if too many predicted trajectories
# add computation of probality of unsucessful evasive action

parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene')
parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (vector computation), constant velocity, normal adaptation, point set prediction)', choices = ['cvd', 'cv', 'na', 'ps'])
parser.add_argument('--display-cp', dest = 'displayCollisionPoints', help = 'display collision points')
args = parser.parse_args()

params = utils.TrackingParameters()
params.loadConfigFile(args.configFilename)

# parameters for prediction methods
if args.predictionMethod:
    predictionMethod = args.predictionMethod
else:
    predictionMethod = params.predictionMethod

if predictionMethod == 'cvd':
    predictionParameters = prediction.CVDirectPredictionParameters(params.maxPredictedSpeed)
elif predictionMethod == 'cv':
    predictionParameters = prediction.ConstantPredictionParameters(params.maxPredictedSpeed)
elif predictionMethod == 'na':
    predictionParameters = prediction.NormalAdaptationPredictionParameters(params.maxPredictedSpeed, 
                                                                           params.nPredictedTrajectories, 
                                                                           params.maxAcceleration,
                                                                           params.maxSteering,
                                                                           params.useFeaturesForPrediction)
elif predictionMethod == 'ps':
    predictionParameters = prediction.PointSetPredictionParameters(params.nPredictedTrajectories,
                                                                   params.maxPredictedSpeed)
else:
    print('Prediction method {} is not valid. See help.'.format(predictionMethod))
    sys.exit()

evasiveActionPredictionParameters = prediction.EvasiveActionPredictionParameters(params.maxPredictedSpeed, 
                                                                                 params.nPredictedTrajectories, 
                                                                                 params.minAcceleration,
                                                                                 params.maxAcceleration,
                                                                                 params.maxSteering,
                                                                                 params.useFeaturesForPrediction)

objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'object')
if params.useFeaturesForPrediction:
    features = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'feature') # needed if normal adaptation
    for obj in objects:
        obj.setFeatures(features)

interactions = events.createInteractions(objects)
for inter in interactions:
    inter.computeIndicators()
    inter.computeCrossingsCollisions(predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones)

storage.saveIndicators(params.databaseFilename, interactions)

if args.displayCollisionPoints:
    plt.figure()
    allCollisionPoints = []
    for inter in interactions:
        for collisionPoints in inter.collisionPoints.values():
            allCollisionPoints += collisionPoints
    moving.Point.plotAll(allCollisionPoints)
    plt.axis('equal')