2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <algorithm>
|
2013-12-17 22:53:16 +01:00
|
|
|
#include <cmath>
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <cassert>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// hyperion includes
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <hyperion/ImageToLedsMap.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
using namespace hyperion;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
ImageToLedsMap::ImageToLedsMap(
|
|
|
|
const unsigned width,
|
|
|
|
const unsigned height,
|
|
|
|
const unsigned horizontalBorder,
|
|
|
|
const unsigned verticalBorder,
|
|
|
|
const std::vector<Led>& leds) :
|
2013-08-14 17:02:09 +02:00
|
|
|
_width(width),
|
|
|
|
_height(height),
|
|
|
|
mColorsMap()
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-21 17:24:42 +02:00
|
|
|
// Sanity check of the size of the borders (and width and height)
|
|
|
|
assert(width > 2*verticalBorder);
|
|
|
|
assert(height > 2*horizontalBorder);
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
// Reserve enough space in the map for the leds
|
|
|
|
mColorsMap.reserve(leds.size());
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
const unsigned xOffset = verticalBorder;
|
|
|
|
const unsigned actualWidth = width - 2 * verticalBorder;
|
|
|
|
const unsigned yOffset = horizontalBorder;
|
|
|
|
const unsigned actualHeight = height - 2 * horizontalBorder;
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
for (const Led& led : leds)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-21 17:24:42 +02:00
|
|
|
// Compute the index boundaries for this led
|
|
|
|
const unsigned minX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.minX_frac));
|
|
|
|
const unsigned maxX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.maxX_frac));
|
|
|
|
const unsigned minY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.minY_frac));
|
|
|
|
const unsigned maxY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.maxY_frac));
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
// Add all the indices in the above defined rectangle to the indices for this led
|
2013-08-14 17:02:09 +02:00
|
|
|
std::vector<unsigned> ledColors;
|
|
|
|
for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
for (unsigned x = minX_idx; x<=maxX_idx && x<width; ++x)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
ledColors.push_back(y*width + x);
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-21 17:24:42 +02:00
|
|
|
|
|
|
|
// Add the constructed vector to the map
|
2013-08-14 17:02:09 +02:00
|
|
|
mColorsMap.push_back(ledColors);
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
unsigned ImageToLedsMap::width() const
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
return _width;
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
unsigned ImageToLedsMap::height() const
|
|
|
|
{
|
|
|
|
return _height;
|
|
|
|
}
|