Unify some functions in utils.
This commit is contained in:
@@ -91,10 +91,9 @@ void FlatLayerVisualisation::setVertexPositions(const int vertexNo, float *desti
|
||||
yOffset = 0;
|
||||
break;
|
||||
|
||||
case Ordering ::SQUARE:
|
||||
case Ordering::SQUARE:
|
||||
const auto nodes = faceCount / 4;
|
||||
auto columns = static_cast<int>(ceil(sqrt(nodes)));
|
||||
while (nodes % columns) ++columns;
|
||||
auto columns = static_cast<int>(numCols(nodes));
|
||||
|
||||
zOffset = -2 * (vertexNo % columns);
|
||||
yOffset = 2 * (vertexNo / columns);
|
||||
|
||||
@@ -26,6 +26,6 @@ namespace fmri
|
||||
std::unique_ptr<float[]> colorBuffer;
|
||||
std::unique_ptr<int[]> indexBuffer;
|
||||
|
||||
void setVertexPositions(const int vertexNo, float *destination);
|
||||
void setVertexPositions(int vertexNo, float *destination);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@ MultiImageVisualisation::MultiImageVisualisation(const fmri::LayerData &layer)
|
||||
|
||||
CHECK_EQ(1, images) << "Only single input image is supported" << endl;
|
||||
|
||||
int columns = ceil(sqrt(channels));
|
||||
while (channels % columns) columns++;
|
||||
int columns = numCols(channels);
|
||||
|
||||
// Create the quads for the images.
|
||||
int r = 0, c = 0;
|
||||
|
||||
14
src/utils.cpp
Normal file
14
src/utils.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
std::default_random_engine &fmri::rng()
|
||||
{
|
||||
static std::default_random_engine rng;
|
||||
static std::default_random_engine::result_type seed = 0;
|
||||
|
||||
if (seed == 0) {
|
||||
std::random_device dev;
|
||||
rng.seed(seed = dev());
|
||||
}
|
||||
|
||||
return rng;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -127,4 +128,31 @@ namespace fmri
|
||||
return val / 180 * M_PI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the ideal number of columns for dividing up a range into a rectangle.
|
||||
*
|
||||
* @tparam T
|
||||
* @param elems number of elements to space out
|
||||
* @return the ideal number of columns
|
||||
*/
|
||||
template<class T>
|
||||
inline T numCols(const T elems) {
|
||||
auto cols = static_cast<T>(ceil(sqrt(elems)));
|
||||
|
||||
while (elems % cols) {
|
||||
++cols;// TODO: this should probably be done analytically
|
||||
}
|
||||
|
||||
return cols;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a globally initialized random number generator.
|
||||
*
|
||||
* This RNG should always be used as a reference, to make sure the state actually updates.
|
||||
*
|
||||
* @return A reference to the global RNG.
|
||||
*/
|
||||
std::default_random_engine& rng();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user