Blob Blame Raw

#ifndef RASTER_H
#define RASTER_H


#include <cassert>

#include "track.h"


class TouchPoint;
typedef std::vector<TouchPoint> TouchPointList;


class Raster {
private:
    Real *data;
    int width;
    int height;
    
public:
    explicit Raster(int width = 0, int height = 0, Real value = 0):
        data(), width(), height()
        { resize(width, height, value); }
    
    ~Raster() { reset(); }
    
    void fill(Real value) {
        if (data)
            for(Real *i = data, *e = i + width*height; i < e; ++i)
                *i = value;
    }

    void resize(int width, int height, Real value = 0) {
        if (data) delete[] data;
        data = 0;
        this->width = this->height = 0;
        
        if (width > 0 && height > 0) {
            this->width = width;
            this->height = height;
            this->data = new Real[width*height];
            fill(value);
        }
    }

    void reset() { resize(0, 0); }
    
    int get_width() const { return width; }
    int get_height() const { return height; }

    const Real* get_data() const { return data; }
    const Real* operator[](int row) const
        { assert(data); return data + row*width; }

    Real* get_data() { return data; }
    Real* operator[](int row)
        { assert(data); return data + row*width; }
    
    Real get(int x, int y, Real def = 0) const
        { return x >= 0 && y >= 0 && x < width && y < height ? (*this)[y][x] : def; }
    void set(int x, int y, Real value)
        { if (x >= 0 && y >= 0 && x < width && y < height) (*this)[y][x] = value; }
    
    void touch(int index, Real value, Real x, Real y, Real dx, Real dy, TouchPointList &points);
};


class TouchPoint {
public:
    int index;
    int row;
    int col;
    Real prev;
    Real next;
    
    TouchPoint(int index = 0, int row = 0, int col = 0, Real prev = 0, Real next = 0):
        index(index), row(row), col(col), prev(prev), next(next) { }
        
    void apply_next(Raster &raster) const
        { raster.set(col, row, next); }
    void apply_prev(Raster &raster) const
        { raster.set(col, row, prev); }
};


#endif