Commit a8208909 authored by Damien Leroux's avatar Damien Leroux
Browse files

Added new means to plot data in the terminal.

Idea based loosely on some python and julia packages.
parent 43ca8be5
#ifndef _SPELL_BRAILLE_PLOT_H_
#define _SPELL_BRAILLE_PLOT_H_
#include <iostream>
#include "eigen.h"
struct braille_plot {
int m_width;
int m_height;
typedef Eigen::Matrix<unsigned long, Eigen::Dynamic, Eigen::Dynamic> plot_data_type;
plot_data_type m_data;
double m_xmin;
double m_xmax;
double m_ymin;
double m_ymax;
double m_xrange_inv;
double m_yrange_inv;
braille_plot(int w, int h, double min_x, double max_x, double min_y, double max_y)
: m_width((w << 1)), m_height((h << 2)), m_data(plot_data_type::Constant(h + 1, w + 1, 0x0080A0E2)),
m_xmin(min_x), m_xmax(max_x), m_ymin(min_y), m_ymax(max_y),
m_xrange_inv(1. / (m_xmax - m_xmin)),
m_yrange_inv(1. / (m_ymax - m_ymin))
{}
braille_plot&
put_pixel(double x, double y)
{
int ix = x2plot(x);
int iy = y2plot(y);
unsigned long bit = xy2bit(ix, iy);
int r = y2data(iy);
int c = x2data(ix);
if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
m_data(r, c) |= bit;
/*std::cout << "put_pixel(" << std::dec << x << ", " << y << ") ix=" << ix << " iy=" << iy << " bit=" << std::hex << bit << std::dec << " r=" << r << " c=" << c << " data=" << std::hex << m_data(r, c) << std::endl;*/
}
return *this;
}
braille_plot&
clear_pixel(double x, double y)
{
int ix = x2plot(x);
int iy = y2plot(y);
unsigned char bit = xy2bit(ix, iy);
int r = y2data(iy);
int c = x2data(ix);
if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
m_data(r, c) &= ~bit;
}
return *this;
}
bool
get_pixel(double x, double y) const
{
int ix = x2plot(x);
int iy = y2plot(y);
unsigned char bit = xy2bit(ix, iy);
int r = y2data(iy);
int c = x2data(ix);
if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
return !!(m_data(r, c) & bit);
}
return false;
}
template <typename FUN>
braille_plot&
plot(FUN fun, double step)
{
for (double x = m_xmin; x < m_xmax; x += step) {
put_pixel(x, fun(x));
}
return *this;
}
friend
std::ostream& operator << (std::ostream& os, const braille_plot& plot)
{
/*static Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols,*/
/*"", "\n",*/
/*"", "",*/
/*"", "");*/
/*return os << plot.m_data.format(fmt);*/
/*static Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols,*/
/*"\x28", "\n",*/
/*"\x28", "",*/
/*"", "");*/
/*return os << plot.m_data.format(fmt);*/
for (int r = 0; r < plot.m_data.rows(); ++r) {
for (int c = 0; c < plot.m_data.cols(); ++c) {
os.write((char*) &plot.m_data(r, c), 3);
}
os << std::endl;
}
return os;
}
private:
int x2data(int x) const { return x >> 1; }
int y2data(int y) const { return y >> 2; }
unsigned long xy2bit(int x, int y) const
{
static unsigned int bitval[8] = {
0x010000, 0x080000,
0x020000, 0x100000,
0x040000, 0x200000,
0x000100, 0x000200
};
return bitval[ ((y & 0x3) << 1) | (x & 1) ];
}
int x2plot(double x) const
{
double rel_x = (x - m_xmin) * m_xrange_inv;
return (int) round(m_width * rel_x);
}
int y2plot(double y) const
{
double rel_y = 1 - (y - m_ymin) * m_yrange_inv;
return (int) round(m_height * rel_y);
}
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment