Program Listing for File time_surface.h

Return to documentation for file (include/cpphots/time_surface.h)

#ifndef CPPHOTS_TIME_SURFACE_H
#define CPPHOTS_TIME_SURFACE_H

#include <ostream>
#include <istream>
#include <memory>

#include "assert.h"
#include "types.h"
#include "interfaces/time_surface.h"


namespace cpphots {


class TimeSurfaceBase : public interfaces::TimeSurfaceCalculator {

public:

    TimeSurfaceBase();

    TimeSurfaceBase(uint16_t width, uint16_t height, uint16_t Rx, uint16_t Ry, TimeSurfaceScalarType tau);

    void update(uint64_t t, uint16_t x, uint16_t y) override;

    void update(const event& ev) override {
        update(ev.t, ev.x, ev.y);
    }

    std::pair<TimeSurfaceType, bool> updateAndCompute(uint64_t t, uint16_t x, uint16_t y) override {
        update(t, x, y);
        return compute(t, x, y);
    }

    std::pair<TimeSurfaceType, bool> updateAndCompute(const event& ev) override {
        update(ev.t, ev.x, ev.y);
        return compute(ev.t, ev.x, ev.y);
    }

    const TimeSurfaceType& getFullContext() const override {
        return context;
    }

    TimeSurfaceType getContext() const override;

    void reset() override;

    std::pair<uint16_t, uint16_t> getSize() const override;

    uint16_t getWx() const override {
        return Wx;
    }

    uint16_t getWy() const override {
        return Wy;
    }

    void toStream(std::ostream& out) const override;

    void fromStream(std::istream& in) override;

protected:

    TimeSurfaceType context;

    uint16_t width;

    uint16_t height;

    uint16_t Rx;

    uint16_t Ry;

    uint16_t Wx;

    uint16_t Wy;

    TimeSurfaceScalarType tau;

    uint16_t min_events;

};


class LinearTimeSurface : public interfaces::Clonable<LinearTimeSurface, TimeSurfaceBase> {

public:

    using Clonable::Clonable;

    std::pair<TimeSurfaceType, bool> compute(uint64_t t, uint16_t x, uint16_t y) const override;

    std::pair<TimeSurfaceType, bool> compute(const event& ev) const override;

    TimeSurfaceType sampleContext(uint64_t t) const override;

    void toStream(std::ostream& out) const override;

    void fromStream(std::istream& in) override;

};


class WeightedLinearTimeSurface : public interfaces::Clonable<WeightedLinearTimeSurface, LinearTimeSurface> {

public:

    WeightedLinearTimeSurface();

    WeightedLinearTimeSurface(uint16_t width, uint16_t height, uint16_t Rx, uint16_t Ry, TimeSurfaceScalarType tau, const TimeSurfaceType& weightmatrix);

    std::pair<TimeSurfaceType, bool> compute(uint64_t t, uint16_t x, uint16_t y) const override;

    std::pair<TimeSurfaceType, bool> compute(const event& ev) const override;

    TimeSurfaceType sampleContext(uint64_t t) const override;

    void toStream(std::ostream& out) const override;

    void fromStream(std::istream& in) override;

private:

    TimeSurfaceType weights;

    void setWeightMatrix(const TimeSurfaceType& weightmatrix);

};


class TimeSurfacePool : public interfaces::Clonable<TimeSurfacePool, interfaces::TimeSurfacePoolCalculator> {

public:

    TimeSurfacePool() {}

    ~TimeSurfacePool();

    TimeSurfacePool(const TimeSurfacePool& other);

    TimeSurfacePool(TimeSurfacePool&& other);

    TimeSurfacePool& operator=(const TimeSurfacePool& other);

    TimeSurfacePool& operator=(TimeSurfacePool&& other);

    template <typename TS, typename... TSArgs>
    static TimeSurfacePool create(uint16_t polarities, TSArgs... tsargs) {

        TimeSurfacePool tsp;

        for (uint16_t i = 0; i < polarities; i++) {
            tsp.surfaces.push_back(TimeSurfacePtr(new TS(std::forward<TSArgs>(tsargs)...)));
        }

        return tsp;

    }

    template <typename TS, typename... TSArgs>
    static TimeSurfacePool* create_ptr(uint16_t polarities, TSArgs... tsargs) {

        TimeSurfacePool* tsp = new TimeSurfacePool();

        for (uint16_t i = 0; i < polarities; i++) {
            tsp->surfaces.push_back(TimeSurfacePtr(new TS(std::forward<TSArgs>(tsargs)...)));
        }

        return tsp;

    }

    void update(uint64_t t, uint16_t x, uint16_t y, uint16_t p) override {
        cpphots_assert(p < surfaces.size());
        surfaces[p]->update(t, x, y);
    }

    void update(const event& ev) override {
        update(ev.t, ev.x, ev.y, ev.p);
    }

    std::pair<TimeSurfaceType, bool> compute(uint64_t t, uint16_t x, uint16_t y, uint16_t p) const override {
        cpphots_assert(p < surfaces.size());
        return surfaces[p]->compute(t, x, y);
    }

    std::pair<TimeSurfaceType, bool> compute(const event& ev) const override {
        return compute(ev.t, ev.x, ev.y, ev.p);
    }

    std::pair<TimeSurfaceType, bool> updateAndCompute(uint64_t t, uint16_t x, uint16_t y, uint16_t p) override {
        update(t, x, y, p);
        return compute(t, x, y, p);
    }

    std::pair<TimeSurfaceType, bool> updateAndCompute(const event& ev) override {
        update(ev.t, ev.x, ev.y, ev.p);
        return compute(ev.t, ev.x, ev.y, ev.p);
    }

    std::pair<uint16_t, uint16_t> getSize() const override {
        return surfaces[0]->getSize();
    }

    void reset() override {
        for (auto& ts : surfaces) {
            ts->reset();
        }
    }

    TimeSurfacePtr& getSurface(size_t idx) override {
        cpphots_assert(idx < surfaces.size());
        return surfaces[idx];
    }

    const TimeSurfacePtr& getSurface(size_t idx) const override {
        cpphots_assert(idx < surfaces.size());
        return surfaces[idx];
    }

    std::vector<TimeSurfaceType> sampleContexts(uint64_t t) const override {
        std::vector<TimeSurfaceType> ret;
        for (const auto& ts : surfaces) {
            ret.push_back(ts->sampleContext(t));
        }
        return ret;
    }

    size_t getNumSurfaces() const override {
        return surfaces.size();
    }

    void toStream(std::ostream& out) const override;

    void fromStream(std::istream& in) override;

private:
    std::vector<TimeSurfacePtr> surfaces;

    void delete_surfaces();

};


template <typename TS, typename... TSArgs>
TimeSurfacePool create_pool(uint16_t polarities, TSArgs... tsargs) {
    return TimeSurfacePool::create<TS>(polarities, std::forward<TSArgs>(tsargs)...);
}

template <typename TS, typename... TSArgs>
TimeSurfacePool* create_pool_ptr(uint16_t polarities, TSArgs... tsargs) {
    return TimeSurfacePool::create_ptr<TS>(polarities, std::forward<TSArgs>(tsargs)...);
}

}

#endif