1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
#include "TimeSeriesCanvas.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "TimeSeriesFontData.h"
#include "LinearMath/btVector3.h"
#include <stdio.h>
struct DataSource
{
unsigned char m_red;
unsigned char m_green;
unsigned char m_blue;
float m_lastValue;
bool m_hasLastValue;
DataSource()
:m_hasLastValue(false)
{
}
};
struct TimeSeriesInternalData
{
btAlignedObjectArray<DataSource> m_dataSources;
struct Common2dCanvasInterface* m_canvasInterface;
int m_canvasIndex;
int m_width;
int m_height;
float m_pixelsPerUnit;
float m_zero;
int m_timeTicks;
int m_ticksPerSecond;
float m_yScale;
int m_bar;
unsigned char m_backgroundRed;
unsigned char m_backgroundGreen;
unsigned char m_backgroundBlue;
unsigned char m_backgroundAlpha;
unsigned char m_textColorRed;
unsigned char m_textColorGreen;
unsigned char m_textColorBlue;
unsigned char m_textColorAlpha;
float getTime()
{
return m_timeTicks/(float)m_ticksPerSecond;
}
TimeSeriesInternalData(int width, int height)
:m_width(width),
m_height(height),
m_pixelsPerUnit(-100),
m_zero(height/2.0),
m_timeTicks(0),
m_ticksPerSecond(100),
m_yScale(1),
m_bar(0),
m_backgroundRed(255),
m_backgroundGreen(255),
m_backgroundBlue(255),
m_backgroundAlpha(255),
m_textColorRed(0),
m_textColorGreen(0),
m_textColorBlue(255),
m_textColorAlpha(255)
{
}
};
TimeSeriesCanvas::TimeSeriesCanvas(struct Common2dCanvasInterface* canvasInterface, int width, int height, const char* windowTitle)
{
btAssert(canvasInterface);
m_internalData = new TimeSeriesInternalData(width,height);
m_internalData->m_canvasInterface = canvasInterface;
m_internalData->m_canvasIndex = m_internalData->m_canvasInterface->createCanvas(windowTitle,m_internalData->m_width,m_internalData->m_height);
}
void TimeSeriesCanvas::addDataSource(const char* dataSourceLabel, unsigned char red,unsigned char green,unsigned char blue)
{
DataSource dataSource;
dataSource.m_red = red;
dataSource.m_green = green;
dataSource.m_blue = blue;
dataSource.m_lastValue = 0;
dataSource.m_hasLastValue = false;
if (dataSourceLabel)
{
int numSources = m_internalData->m_dataSources.size();
int row = numSources%3;
int column = numSources/3;
grapicalPrintf(dataSourceLabel, sTimeSeriesFontData, 50+200*column,m_internalData->m_height-48+row*16,
red, green,blue,255);
}
m_internalData->m_dataSources.push_back(dataSource);
}
void TimeSeriesCanvas::setupTimeSeries(float yScale, int ticksPerSecond, int startTime)
{
m_internalData->m_pixelsPerUnit = -(m_internalData->m_height/3.f)/yScale;
m_internalData->m_ticksPerSecond = ticksPerSecond;
m_internalData->m_yScale = yScale;
m_internalData->m_dataSources.clear();
for (int i=0;i<m_internalData->m_width;i++)
{
for (int j=0;j<m_internalData->m_height;j++)
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,i,j,
m_internalData->m_backgroundRed,
m_internalData->m_backgroundGreen,
m_internalData->m_backgroundBlue,
m_internalData->m_backgroundAlpha);
}
}
float zeroPixelCoord = m_internalData->m_zero;
float pixelsPerUnit = m_internalData->m_pixelsPerUnit;
float yPos = zeroPixelCoord+pixelsPerUnit*yScale;
float yNeg = zeroPixelCoord+pixelsPerUnit*-yScale;
grapicalPrintf("0", sTimeSeriesFontData, 2,zeroPixelCoord,m_internalData->m_textColorRed,m_internalData->m_textColorGreen,m_internalData->m_textColorBlue,m_internalData->m_textColorAlpha);
char label[1024];
sprintf(label,"%2.1f", yScale);
grapicalPrintf(label, sTimeSeriesFontData, 2,yPos,m_internalData->m_textColorRed,m_internalData->m_textColorGreen,m_internalData->m_textColorBlue,m_internalData->m_textColorAlpha);
sprintf(label,"%2.1f", -yScale);
grapicalPrintf(label, sTimeSeriesFontData, 2,yNeg,m_internalData->m_textColorRed,m_internalData->m_textColorGreen,m_internalData->m_textColorBlue,m_internalData->m_textColorAlpha);
m_internalData->m_canvasInterface->refreshImageData(m_internalData->m_canvasIndex);
}
TimeSeriesCanvas::~TimeSeriesCanvas()
{
if (m_internalData->m_canvasInterface && m_internalData->m_canvasIndex>=0)
{
m_internalData->m_canvasInterface->destroyCanvas(m_internalData->m_canvasIndex);
}
delete m_internalData;
}
float TimeSeriesCanvas::getCurrentTime() const
{
return m_internalData->getTime();
}
void TimeSeriesCanvas::grapicalPrintf(const char* str, void* fontData, int rasterposx,int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{
unsigned char c;
int x=0;
int xx=0;
while ((c = (unsigned char) *str++)) {
x=xx;
unsigned char* fontPtr = (unsigned char*) fontData;
char ch = c-32;
int sx=ch%16;
int sy=ch/16;
for (int i=sx*16;i<(sx*16+16);i++)
{
int y=0;
for (int j=sy*16;j<(sy*16+16);j++)
{
unsigned char packedColor = (fontPtr[i*3+255*256*3-(256*j)*3]);
//float colorf = packedColor ? 0.f : 1.f;
float colorf = packedColor/255.f;// ? 0.f : 1.f;
btVector4 rgba(colorf,colorf,colorf,1.f);
if (colorf)
{
if ((rasterposx+x>=0) && (rasterposx+x < m_internalData->m_width) &&
(rasterposy+y>=0) && (rasterposy+y<m_internalData->m_height))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,rasterposx+x,rasterposy+y,
red,green,blue,alpha);
}
}
y++;
}
x++;
}
xx+=10;
}
}
void TimeSeriesCanvas::shift1PixelToLeft()
{
int resetVal = 10;
int countdown = resetVal;
//shift pixture one pixel to the left
for (int j=0;j<m_internalData->m_height-48;j++)
{
for (int i=40;i<this->m_internalData->m_width;i++)
{
unsigned char red, green, blue, alpha;
m_internalData->m_canvasInterface->getPixel(m_internalData->m_canvasIndex,i,j,red,green,blue,alpha);
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,i-1,j,red,green,blue,alpha);
}
if (!m_internalData->m_bar)
{
if (!countdown--)
{
countdown=resetVal;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,j,0,0,0,255);
} else
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,j,255,255,255,255);
}
} else
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,j,255,255,255,255);
}
}
{
int resetVal = 2;
static int countdown = resetVal;
if (!countdown--)
{
countdown=resetVal;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,m_internalData->m_zero,0,0,0,255);
}
}
{
int resetVal = 10;
static int countdown = resetVal;
if (!countdown--)
{
countdown=resetVal;
float zeroPixelCoord = m_internalData->m_zero;
float pixelsPerUnit = m_internalData->m_pixelsPerUnit;
float yPos = zeroPixelCoord+pixelsPerUnit*m_internalData->m_yScale;
float yNeg = zeroPixelCoord+pixelsPerUnit*-m_internalData->m_yScale;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,
yPos,0,0,0,255);
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,
yNeg,0,0,0,255);
}
}
if (!m_internalData->m_bar)
{
char buf[1024];
float time = m_internalData->getTime();
sprintf(buf,"%2.0f",time);
grapicalPrintf(buf, sTimeSeriesFontData, m_internalData->m_width-25,m_internalData->m_zero+3,0,0,0,255);
m_internalData->m_bar=m_internalData->m_ticksPerSecond;
}
m_internalData->m_timeTicks++;
m_internalData->m_bar--;
}
void TimeSeriesCanvas::insertDataAtCurrentTime(float orgV, int dataSourceIndex, bool connectToPrevious)
{
btAssert(dataSourceIndex < m_internalData->m_dataSources.size());
float zero = m_internalData->m_zero;
float amp = m_internalData->m_pixelsPerUnit;
//insert some new value(s) in the right-most column
{
float time = m_internalData->getTime();
float v = zero+amp*orgV;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,v,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255
);
if (connectToPrevious && m_internalData->m_dataSources[dataSourceIndex].m_hasLastValue)
{
for (int value=m_internalData->m_dataSources[dataSourceIndex].m_lastValue;value<=v;value++)
{
if (value>=0 && value < float(m_internalData->m_height-1))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,value,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255
);
}
}
for (int value=v;value<=m_internalData->m_dataSources[dataSourceIndex].m_lastValue;value++)
{
if (value>=0 && value < float(m_internalData->m_height-1))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex,m_internalData->m_width-1,value,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255);
}
}
}
m_internalData->m_dataSources[dataSourceIndex].m_lastValue = v;
m_internalData->m_dataSources[dataSourceIndex].m_hasLastValue = true;
}
}
void TimeSeriesCanvas::nextTick()
{
shift1PixelToLeft();
m_internalData->m_canvasInterface->refreshImageData(m_internalData->m_canvasIndex);
}
|