view python/ubc_utils.py @ 47:e27598865af3

modified loadtrajectories to load objects and prototypes
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 23 Sep 2010 17:12:09 -0400
parents be3ae926e4e8
children 8aed225f71d8
line wrap: on
line source

#! /usr/bin/env python
'''Various utilities to load data saved by the UBC tool(s)'''

import utils
from moving import MovingObject, TimeInterval, Trajectory

__metaclass__ = type

delimiterChar = '%';

def getFileType(s):
    filePrefix = utils.removeExtension(s)
    i = filePrefix.rfind('-')
    if i>0:
        return filePrefix[i+1:]
    else:
        return ''

def getLines(f):
    '''Gets a complete entry (all the lines) in between delimiterChar.'''
    dataStrings = []
    s = utils.readline(f)
    while (len(s) > 0) and (not s.startswith(delimiterChar)):
        dataStrings += [s.strip()]
        s = utils.readline(f)
    return dataStrings

def saveTrajectorieUserTypes(inFilename, outFilename, objects):
    '''The program saves the objects, 
    by just copying the corresponding trajectory and velocity data
    from the inFilename, and saving the characteristics in objects (first line)
    into outFilename'''
    infile = utils.openCheck(inFilename)
    outfile = utils.openCheck(outFilename)

    if (not infile) | (not outfile):
        return

    lines = getLines(file)
    objNum = 0
    while lines != []:
        i = 0
        while (i<len(objects)) and (objects[i].num != objNum):
            i+=1
        if i<len(objects):
            l = lines[0].split(' ')
            outfile.write()
        # next object
        objNum += 1

def loadTrajectories(filename, nObjects = -1):
    '''Loads trajectories'''

    file = utils.openCheck(filename)
    if (not file):
        return []

    objects = []
    objNum = 0
    objectType = getFileType(filename)
    lines = getLines(file)
    while (lines != []) and ((nObjects<0) or (objNum<nObjects)):
        l = lines[0].split(' ')
        parsedLine = [int(n) for n in l[:4]]
        obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2]))
        #add = True
        if len(lines) >= 3:
            obj.positions = Trajectory.load(lines[1], lines[2])
            if len(lines) >= 5:
                obj.velocities = Trajectory.load(lines[3], lines[4])
                if objectType == 'objects':
                    obj.userType = parsedLine[3]
                    obj.nObjects = float(l[4])
                    obj.featureNumbers = [int(n) for n in l[5:]]
                    
                    # load contour data if available
                    if len(lines) >= 6:
                        obj.contourType = utils.line2Floats(lines[6])
                        obj.contourOrigins = Trajectory.load(lines[7], lines[8])
                        obj.contourSizes = Trajectory.load(lines[9], lines[10])
                elif objectType == 'prototypes':
                    obj.userType = parsedLine[3]
                    obj.nMatchings = int(l[4])

        if len(lines) != 2:
            objects.append(obj)
            objNum+=1
        else:
            print("Error two lines of data for feature %d"%(f.num))

        lines = getLines(file)

    file.close()
    return objects
   
def loadCollisionPoints(filename, nPoints = -1):
    '''Loads collision points and returns a dict
    with keys as a pair of the numbers of the two interacting objects'''
    file = utils.openCheck(filename)
    if (not file):
        return []

    points = {}
    num = 0
    lines = getLines(file)
    while (lines != []) and ((nPoints<0) or (num<nPoints)):
        parsedLine = [int(n) for n in lines[0].split(' ')]
        protagonistNums = (parsedLine[0], parsedLine[1])
        points[protagonistNums] = [[float(n) for n in lines[1].split(' ')],
                                   [float(n) for n in lines[2].split(' ')]]

        num+=1
        lines = getLines(file)

    file.close()
    return points