Skip to content

Commit

Permalink
Merge pull request #2 from strasdat/master
Browse files Browse the repository at this point in the history
Three features: labels, stacked histograms and exp-scale on sliders
  • Loading branch information
stevenlovegrove committed Sep 2, 2011
2 parents d6d4950 + 6388ee7 commit 908e7a7
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 347 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ SET(PANGOLIN_VERSION_MINOR 1)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/CMakeModules/")

IF (CMAKE_COMPILER_IS_GNUCXX )
MESSAGE("g++ compiler detected.")
ADD_DEFINITIONS("-Wall -Wno-error=deprecated-declarations -Werror")
ENDIF()

OPTION(BUILD_EXAMPLES "Build Examples" ON)

# SET(BUILD_SHARED_LIBS ON)
Expand Down
4 changes: 2 additions & 2 deletions examples/SimpleDisplay/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ int main( int /*argc*/, char* argv[] )
// Default hooks for exiting (Esc) and fullscreen (tab).
while( !pangolin::ShouldQuit() )
{
if(HasResized())
if(pangolin::HasResized())
DisplayBase().ActivateScissorAndClear();

// Safe and efficient binding of named variables.
// Specialisations mean no conversions take place for exact types
// and conversions between scalar types are cheap.
static Var<bool> a_button("ui.A Button",false,false);
static Var<double> a_double("ui.A Double",3,0,5.5);
static Var<double> a_double("ui.A Double",3,1,10000,true);
static Var<int> an_int("ui.An Int",2,0,5);
static Var<bool> a_checkbox("ui.A Checkbox",false,true);
static Var<int> an_int_no_input("ui.An Int No Input",2);
Expand Down
8 changes: 4 additions & 4 deletions examples/SimpleMultiDisplay/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ int main( int /*argc*/, char* argv[] )
View& d_panel = pangolin::CreatePanel("ui")
.SetBounds(1.0, 0.0, 0, 200);

View& d_multi = pangolin::Display("multi")
.SetBounds(1.0, 0.0, 200, 1.0)
.SetLayout(LayoutEqual)
.AddDisplay(d_cam);
// View& d_multi = pangolin::Display("multi")
// .SetBounds(1.0, 0.0, 200, 1.0)
// .SetLayout(LayoutEqual)
// .AddDisplay(d_cam);

// Default hooks for exiting (Esc) and fullscreen (tab).
while( !pangolin::ShouldQuit() )
Expand Down
11 changes: 11 additions & 0 deletions examples/SimplePlot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ int main( int /*argc*/, char* argv[] )

double t = 0;

vector<std::string> labels;

labels.push_back(std::string("sin(t)"));
labels.push_back(std::string("cos(t)"));
labels.push_back(std::string("tan(t)"));
labels.push_back(std::string("sin(t)+cos(t)"));

log.SetLabels(labels);

// Default hooks for exiting (Esc) and fullscreen (tab).
while( !pangolin::ShouldQuit() )
{
Expand All @@ -35,6 +44,8 @@ int main( int /*argc*/, char* argv[] )

static Var<double> tinc("ui.t inc",0.01,0,0.1);



log.Log(sin(t),cos(t),tan(t),sin(t)+cos(t));
t += tinc;

Expand Down
125 changes: 105 additions & 20 deletions pangolin/plotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <limits>
#include <iostream>
#include <iomanip>
#include <boost/unordered_map.hpp>

using namespace std;

Expand Down Expand Up @@ -74,7 +75,7 @@ void DataSequence::Clear()
float DataSequence::operator[](unsigned int i) const
{
if( !HasData(i) ) {
throw DataUnavailableException("Out of range");
throw DataUnavailableException("Out of range");
}
return y[(i-firstn) % y.size()];
}
Expand Down Expand Up @@ -106,7 +107,12 @@ void DataLog::Save(std::string filename)
}
}

void DataLog::Log(unsigned int N, const float vals[])
void DataLog::Log(const vector<float> & vals)
{
Log(vals.size(), &vals[0]);
}

void DataLog::Log(unsigned int N, const float * vals)
{
// Create new plots if needed
for( unsigned int i= sequences.size(); i < N; ++i )
Expand All @@ -123,6 +129,17 @@ void DataLog::Log(unsigned int N, const float vals[])
++x;
}

void DataLog::SetLabels(const std::vector<std::string> & new_labels)
{
// Create new labels if needed
for( unsigned int i= labels.size(); i < new_labels.size(); ++i )
labels.push_back(std::string("N/A"));

// Add data to existing plots
for( unsigned int i=0; i<labels.size(); ++i )
labels[i] = new_labels[i];
}

void DataLog::Log(float v)
{
const float vs[] = {v};
Expand Down Expand Up @@ -157,7 +174,7 @@ void DataLog::Log(float v1, float v2, float v3, float v4, float v5, float v6)
}

Plotter::Plotter(DataLog* log, float left, float right, float bottom, float top, float tickx, float ticky)
: log(log), track_front(true), draw_mode(0), xy(false)
: log(log), track_front(true), draw_mode(0), plot_mode(TIME_SERIES)
{
this->handler = this;
int_x[0] = int_x_dflt[0] = left;
Expand Down Expand Up @@ -229,6 +246,47 @@ void Plotter::DrawSequence(const DataSequence& seq)
glEnd();
}

void Plotter::DrawSequenceHistogram(const std::vector<DataSequence>& seq)
{
size_t vec_size
= std::min((unsigned)log->x, log->buffer_size);
int idx_subtract
= std::max(0,(int)(log->x)-(int)(log->buffer_size));
vector<float> accum_vec(vec_size,0);

for(int s=log->sequences.size()-1; s >=0; --s )
{
if( (s > 9) || show[s] )
{

const int seqint_x[2] = {seq.at(s).firstn, seq.at(s).n };
const int valid_int_x[2] = {
std::max(seqint_x[0],(int)int_x[0]),
std::min(seqint_x[1],(int)int_x[1])
};


glBegin(GL_TRIANGLE_STRIP);
glColor3fv(plot_colours[s%num_plot_colours]);


for( int x=valid_int_x[0]; x<valid_int_x[1]; ++x )
{
float val = seq.at(s)[x];

float & accum = accum_vec.at(x-idx_subtract);
float before_val = accum;
accum += val;
glVertex2f(x-0.5,before_val);
glVertex2f(x-0.5,accum);
glVertex2f(x+0.5,before_val);
glVertex2f(x+0.5,accum);
}
glEnd();
}
}
}

void Plotter::DrawSequence(const DataSequence& x,const DataSequence& y)
{
const unsigned minn = max(x.firstn,y.firstn);
Expand All @@ -240,7 +298,6 @@ void Plotter::DrawSequence(const DataSequence& x,const DataSequence& y)
glEnd();
}


void Plotter::Render()
{
if( track_front )
Expand All @@ -262,17 +319,19 @@ void Plotter::Render()

if( log && log->sequences.size() > 0 )
{
if( xy )
if( plot_mode==XY )
{
for( unsigned int s=0; s < log->sequences.size() / 2; ++s )
{
if( (s > 9) || show[s] )
{
glColor3fv(plot_colours[s]);
glColor3fv(plot_colours[s%num_plot_colours]);
DrawSequence(log->sequences[2*s],log->sequences[2*s+1]);
}
}
}else{
}
else if( plot_mode==TIME_SERIES)
{
for( unsigned int s=0; s < log->sequences.size(); ++s )
{
if( (s > 9) || show[s] )
Expand All @@ -282,11 +341,35 @@ void Plotter::Render()
}
}
}
else if( plot_mode==STACKED_HISTOGRAM )
{


DrawSequenceHistogram(log->sequences);

}
else
{
assert(false);
}
}


float ty = v.h-15;
for (size_t i=0; i<log->labels.size(); ++i)
{
glColor3fv(plot_colours[i%num_plot_colours]);

OpenGlRenderState::ApplyWindowCoords();
glRasterPos2f( v.l+5,ty);
glutBitmapString(font,(unsigned char*)log->labels[i].c_str());
ty -= 15;
}


if( mouse_state & MouseButtonLeft )
{
if( xy )
if( plot_mode==XY )
{
glColor3fv(colour_ms);
glBegin(GL_LINE_STRIP);
Expand Down Expand Up @@ -360,9 +443,9 @@ void Plotter::Keyboard(View&, unsigned char key, int x, int y, bool pressed)
}else if( key == 'm' ) {
draw_mode = (draw_mode+1)%draw_modes_n;
}else if( key == 'p' ) {
xy = !xy;
plot_mode = (plot_mode+1)%modes_n;
ResetView();
if( xy ) {
if( plot_mode==XY ) {
int_x[0] = int_y[0];
int_x[1] = int_y[1];
track_front = false;
Expand All @@ -372,7 +455,7 @@ void Plotter::Keyboard(View&, unsigned char key, int x, int y, bool pressed)
ResetView();
}else if( key == 'a' || key == ' ' ) {
cout << "Plotter: Auto scale" << endl;
if( xy && log->sequences.size() >= 2)
if( plot_mode==XY && log->sequences.size() >= 2)
{
int_x[0] = log->sequences[0].min_y;
int_x[1] = log->sequences[0].max_y;
Expand Down Expand Up @@ -415,10 +498,12 @@ void Plotter::Mouse(View&, MouseButton button, int x, int y, bool pressed, int b

if(button == MouseWheelUp || button == MouseWheelDown)
{
const float mean = (int_y[0] + int_y[1])/2.0;
//const float mean = (int_y[0] + int_y[1])/2.0;
const float scale = 1.0f + ((button == MouseWheelDown) ? 0.1 : -0.1);
int_y[0] = scale*(int_y[0] - mean) + mean;
int_y[1] = scale*(int_y[1] - mean) + mean;
// int_y[0] = scale*(int_y[0] - mean) + mean;
// int_y[1] = scale*(int_y[1] - mean) + mean;
int_y[0] = scale*(int_y[0]) ;
int_y[1] = scale*(int_y[1]) ;
}

ScreenToPlot(x,y);
Expand All @@ -436,8 +521,8 @@ void Plotter::MouseMotion(View&, int x, int y, int button_state)
track_front = false;
int_x[0] -= df[0];
int_x[1] -= df[0];
// interval_y[0] -= df[1];
// interval_y[1] -= df[1];
// interval_y[0] -= df[1];
// interval_y[1] -= df[1];
}else if(button_state == MouseButtonMiddle )
{
int_y[0] -= df[1];
Expand All @@ -464,10 +549,10 @@ void Plotter::MouseMotion(View&, int x, int y, int button_state)

Plotter& CreatePlotter(const string& name, DataLog* log)
{
Plotter* v = new Plotter(log);
context->all_views[name] = v;
context->base.views.push_back(v);
return *v;
Plotter* v = new Plotter(log);
context->all_views[name] = v;
context->base.views.push_back(v);
return *v;
}

} // namespace pangolin
25 changes: 20 additions & 5 deletions pangolin/plotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,36 @@ struct DataLog
void Log(float v1, float v2, float v3, float v4);
void Log(float v1, float v2, float v3, float v4, float v5);
void Log(float v1, float v2, float v3, float v4, float v5, float v6);
void Log(unsigned int N, const float vals[]);
void Log(unsigned int N, const float * vals);
void Log(const std::vector<float> & vals);
void SetLabels(const std::vector<std::string> & labels);
void Clear();
void Save(std::string filename);

unsigned int buffer_size;
int x;
std::vector<DataSequence> sequences;
std::vector<std::string> labels;
};

const static int num_plot_colours = 6;
const static int num_plot_colours = 12;
const static float plot_colours[][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0},
{1.0, 0.0, 1.0},
{0.0, 1.0, 1.0},
{1.0, 1.0, 0.0}
{0.5, 0.5, 0.0},
{0.5, 0.0, 0.0},
{0.0, 0.5, 0.0},
{0.0, 0.0, 0.5},
{0.5, 0.0, 1.0},
{0.0, 1.0, 0.5},
{1.0, 0.0, 0.5},
{0.0, 0.5, 1.0}
};


const static float colour_bg[3] = {0.0,0.0,0.0};
const static float colour_tk[3] = {0.1,0.1,0.1};
const static float colour_ms[3] = {0.3,0.3,0.3};
Expand All @@ -116,6 +127,7 @@ struct Plotter : public View, Handler
void Render();
void DrawSequence(const DataSequence& seq);
void DrawSequence(const DataSequence& x,const DataSequence& y);
void DrawSequenceHistogram(const std::vector<DataSequence>& seq);
void DrawTicks();

void ResetView();
Expand All @@ -138,7 +150,10 @@ struct Plotter : public View, Handler
float mouse_xy[2];

int draw_mode;
bool xy;

enum PLOT_MODES { TIME_SERIES, XY, STACKED_HISTOGRAM};
static const unsigned modes_n = 3;
unsigned plot_mode;
const static unsigned int show_n = 9;
bool show[show_n];
};
Expand Down
Loading

0 comments on commit 908e7a7

Please sign in to comment.