Use Color type wherever possible.

This commit is contained in:
2018-04-03 19:22:14 +02:00
parent f62c2859e3
commit d4d4d6d431
5 changed files with 32 additions and 25 deletions

View File

@@ -11,12 +11,12 @@
using namespace std; using namespace std;
using namespace fmri; using namespace fmri;
ActivityAnimation::Color ActivityAnimation::colorBySign(float intensity) Color ActivityAnimation::colorBySign(float intensity)
{ {
if (intensity > 0) { if (intensity > 0) {
return {0, 1, 0}; return {0, 1, 0, 1};
} else { } else {
return {1, 0, 0}; return {1, 0, 0, 1};
} }
} }
@@ -67,7 +67,7 @@ void ActivityAnimation::draw(float timeScale)
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colorBuf.data()); glColorPointer(std::tuple_size<Color>::value, GL_FLOAT, 0, colorBuf.data());
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data()); glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data());
glDrawArrays(GL_POINTS, 0, bufferLength / 3); glDrawArrays(GL_POINTS, 0, bufferLength / 3);
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);

View File

@@ -13,7 +13,6 @@ namespace fmri
: public Animation : public Animation
{ {
public: public:
typedef std::array<float, 3> Color;
typedef std::function<Color(float)> ColoringFunction; typedef std::function<Color(float)> ColoringFunction;
ActivityAnimation( ActivityAnimation(
@@ -28,7 +27,7 @@ namespace fmri
private: private:
std::size_t bufferLength; std::size_t bufferLength;
std::vector<std::array<float, 3>> colorBuf; std::vector<Color> colorBuf;
std::vector<float> startingPos; std::vector<float> startingPos;
std::vector<float> delta; std::vector<float> delta;
std::vector<int> lineIndices; std::vector<int> lineIndices;

View File

@@ -7,7 +7,7 @@
using namespace fmri; using namespace fmri;
static inline void computeColor(float intensity, float limit, float *destination) static inline void computeColor(float intensity, float limit, Color& destination)
{ {
const float saturation = std::min(-std::log(std::abs(intensity) / limit) / 10.0f, 1.0f); const float saturation = std::min(-std::log(std::abs(intensity) / limit) / 10.0f, 1.0f);
if (intensity > 0) { if (intensity > 0) {
@@ -19,13 +19,17 @@ static inline void computeColor(float intensity, float limit, float *destination
destination[1] = saturation; destination[1] = saturation;
destination[2] = saturation; destination[2] = saturation;
} }
if constexpr (std::tuple_size<Color>::value >= 4) {
// We have an alpha channel, set it to 1.
destination[3] = 1;
}
} }
FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering ordering) : FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering ordering) :
LayerVisualisation(layer.numEntries()), LayerVisualisation(layer.numEntries()),
ordering(ordering), ordering(ordering),
vertexBuffer(layer.numEntries() * NODE_FACES.size()), vertexBuffer(layer.numEntries() * NODE_FACES.size()),
colorBuffer(layer.numEntries() * NODE_FACES.size()), colorBuffer(layer.numEntries() * VERTICES_PER_NODE),
indexBuffer(layer.numEntries() * NODE_FACES.size()) indexBuffer(layer.numEntries() * NODE_FACES.size())
{ {
auto &shape = layer.shape(); auto &shape = layer.shape();
@@ -41,20 +45,18 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
auto scalingMax = std::max(abs(*minElem), abs(*maxElem)); auto scalingMax = std::max(abs(*minElem), abs(*maxElem));
int v = 0; auto colorPos = colorBuffer.begin();
auto indexPos = indexBuffer.begin();
for (int i : Range(limit)) { for (int i : Range(limit)) {
setVertexPositions(i, vertexBuffer.data() + NODE_FACES.size() * i); setVertexPositions(i, vertexBuffer.data() + NODE_FACES.size() * i);
const auto vertexBase = static_cast<int>(i * NODE_FACES.size() / 3); Color nodeColor;
computeColor(data[i], scalingMax, nodeColor);
colorPos = std::fill_n(colorPos, NODE_FACES.size() / 3, nodeColor);
// Define the colors for the vertices auto newIndexPos = std::copy(std::begin(NODE_FACES), std::end(NODE_FACES), indexPos);
for (auto c : Range(NODE_SHAPE.size() / 3)) { std::transform(indexPos, newIndexPos, indexPos, [i](auto x) { return x + i * VERTICES_PER_NODE;});
computeColor(data[i], scalingMax, &colorBuffer[NODE_FACES.size() * i + 3 * c]); indexPos = newIndexPos;
}
// Set the face nodes indices
for (auto faceNode : NODE_FACES) {
indexBuffer[v++] = vertexBase + faceNode;
}
} }
// Compute which nodes are active, add those to the active indices // Compute which nodes are active, add those to the active indices
@@ -63,6 +65,9 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
std::copy_n(&indexBuffer[NODE_FACES.size() * i], NODE_FACES.size(), std::back_inserter(activeIndexBuffer)); std::copy_n(&indexBuffer[NODE_FACES.size() * i], NODE_FACES.size(), std::back_inserter(activeIndexBuffer));
} }
} }
assert(indexPos == indexBuffer.end());
assert(colorPos == colorBuffer.end());
} }
void FlatLayerVisualisation::render() void FlatLayerVisualisation::render()
@@ -73,7 +78,7 @@ void FlatLayerVisualisation::render()
const auto& indices = RenderingState::instance().renderActivatedOnly() ? activeIndexBuffer : indexBuffer; const auto& indices = RenderingState::instance().renderActivatedOnly() ? activeIndexBuffer : indexBuffer;
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data()); glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data());
glColorPointer(3, GL_FLOAT, 0, colorBuffer.data()); glColorPointer(std::tuple_size<Color>::value, GL_FLOAT, 0, colorBuffer.data());
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data()); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);

View File

@@ -17,7 +17,7 @@ namespace fmri
private: private:
Ordering ordering; Ordering ordering;
std::vector<float> vertexBuffer; std::vector<float> vertexBuffer;
std::vector<float> colorBuffer; std::vector<Color> colorBuffer;
std::vector<int> indexBuffer; std::vector<int> indexBuffer;
std::vector<int> activeIndexBuffer; std::vector<int> activeIndexBuffer;
@@ -33,6 +33,8 @@ namespace fmri
0, 2, 3, 0, 2, 3,
1, 2, 3 1, 2, 3
}; };
constexpr const auto VERTICES_PER_NODE = NODE_SHAPE.size() / 3;
constexpr const auto FACES_PER_NODE = NODE_FACES.size() / 3;
void setVertexPositions(int vertexNo, float *destination); void setVertexPositions(int vertexNo, float *destination);

View File

@@ -170,11 +170,11 @@ static Animation *getReLUAnimation(const fmri::LayerData &prevState,
const auto maxValue = max_element(results.begin(), results.end())->first; const auto maxValue = max_element(results.begin(), results.end())->first;
return new ActivityAnimation(results, prevPositions.data(), curPositions.data(), return new ActivityAnimation(results, prevPositions.data(), curPositions.data(),
[=](float i) -> ActivityAnimation::Color { [=](float i) -> Color {
if (maxValue == 0) { if (maxValue == 0) {
return {1, 1, 1}; return {1, 1, 1, 1};
} else { } else {
return {1 - i / maxValue, 1 - i / maxValue, 1}; return {1 - i / maxValue, 1 - i / maxValue, 1, 1};
} }
}); });
} else { } else {
@@ -202,11 +202,12 @@ static Animation *getNormalizingAnimation(const fmri::LayerData &prevState, cons
auto max_val = *max_element(scaling.begin(), scaling.end()); auto max_val = *max_element(scaling.begin(), scaling.end());
return new ActivityAnimation(entries, prevPositions.data(), curPositions.data(), return new ActivityAnimation(entries, prevPositions.data(), curPositions.data(),
[=](float i) -> ActivityAnimation::Color { [=](float i) -> Color {
auto intensity = clamp((i - 1) / (max_val - 1), 0.f, 1.f); auto intensity = clamp((i - 1) / (max_val - 1), 0.f, 1.f);
return { return {
1 - intensity, 1 - intensity,
1, 1,
1,
1 1
}; };
}); });