Skip to content

Commit

Permalink
replace int_log with std::log2 and static_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
Optiligence committed Apr 28, 2018
1 parent 01a5917 commit 04b352d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 30 deletions.
4 changes: 2 additions & 2 deletions dataset.cpp
Expand Up @@ -402,7 +402,7 @@ QUrl Dataset::googleCubeUrl(const Coordinate coord) const {
} else if (type == Dataset::CubeType::RAW_JPG) {
path += "/format=singleimage";
}
path += "/scale=" + QString::number(int_log(magnification));// >= 0
path += "/scale=" + QString::number(static_cast<std::size_t>(std::log2(magnification)));// >= 0
path += "/size=" + QString("%1,%1,%1").arg(cubeEdgeLength);// <= 128³
path += "/corner=" + QString("%1,%2,%3").arg(coord.x).arg(coord.y).arg(coord.z);

Expand All @@ -419,7 +419,7 @@ QUrl Dataset::googleCubeUrl(const Coordinate coord) const {
QUrl Dataset::openConnectomeCubeUrl(Coordinate coord) const {
auto path = url.path();

path += (!path.endsWith('/') ? "/" : "") + QString::number(int_log(magnification));// >= 0
path += (!path.endsWith('/') ? "/" : "") + QString::number(static_cast<std::size_t>(std::log2(magnification)));// >= 0
coord.x /= magnification;
coord.y /= magnification;
coord.z += 1;//offset
Expand Down
9 changes: 4 additions & 5 deletions loader.cpp
Expand Up @@ -22,7 +22,6 @@

#include "loader.h"

#include "functions.h"
#include "network.h"
#include "segmentation/segmentation.h"
#include "session.h"
Expand Down Expand Up @@ -286,11 +285,11 @@ void Loader::Worker::unloadCurrentMagnification() {
}

void Loader::Worker::markOcCubeAsModified(const CoordOfCube &cubeCoord, const int magnification) {
OcModifiedCacheQueue[std::log2(magnification)].emplace(cubeCoord);
OcModifiedCacheQueue[static_cast<std::size_t>(std::log2(magnification))].emplace(cubeCoord);
}

void Loader::Worker::snappyCacheSupplySnappy(const CoordOfCube cubeCoord, const int magnification, const std::string cube) {
const auto cubeMagnification = std::log2(magnification);
const auto cubeMagnification = static_cast<std::size_t>(std::log2(magnification));
if (cubeMagnification >= snappyCache.size()) {
qWarning() << QObject::tr("ignored snappy cube (%1, %2, %3) for higher than available mag %4 ((log2(%4) = %5) ≥ %6)")
.arg(cubeCoord.x).arg(cubeCoord.y).arg(cubeCoord.z).arg(magnification).arg(cubeMagnification).arg(snappyCache.size());
Expand Down Expand Up @@ -507,7 +506,7 @@ void Loader::Worker::downloadAndLoadCubes(const unsigned int loadingNr, const Co
datasets = changedDatasets;
cleanup(center);
decltype(Dataset::magnification) magnification = datasets.front().magnification;
loaderMagnification = std::log2(magnification);
loaderMagnification = static_cast<std::size_t>(std::log2(magnification));
const auto cubeEdgeLen = datasets.front().cubeEdgeLength;
const auto Dcoi = DcoiFromPos(center.cube(cubeEdgeLen, magnification), userMoveType, direction);//datacubes of interest prioritized around the current position
//split dcoi into slice planes and rest
Expand Down Expand Up @@ -603,7 +602,7 @@ void Loader::Worker::downloadAndLoadCubes(const unsigned int loadingNr, const Co
QByteArray payload;
if (dataset.api == Dataset::API::WebKnossos) {
request.setRawHeader("Content-Type", "application/json");
payload = QString{R"json([{"position":[%1,%2,%3],"zoomStep":%4,"cubeSize":%5,"fourBit":false}])json"}.arg(globalCoord.x).arg(globalCoord.y).arg(globalCoord.z).arg(int_log(dataset.magnification)).arg(dataset.cubeEdgeLength).toUtf8();
payload = QString{R"json([{"position":[%1,%2,%3],"zoomStep":%4,"cubeSize":%5,"fourBit":false}])json"}.arg(globalCoord.x).arg(globalCoord.y).arg(globalCoord.z).arg(static_cast<std::size_t>(std::log2(dataset.magnification))).arg(dataset.cubeEdgeLength).toUtf8();
}
//request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
//request.setAttribute(QNetworkRequest::SpdyAllowedAttribute, true);
Expand Down
8 changes: 4 additions & 4 deletions network.cpp
Expand Up @@ -64,7 +64,7 @@ void Network::setCookies(const QVariantList & setting) {
std::pair<int, int> Network::checkOnlineMags(const QUrl & url) {
int lowestAvailableMag = NUM_MAG_DATASETS;
int highestAvailableMag = 0;
const int maxMagCount = int_log(NUM_MAG_DATASETS) + 1;
const auto maxMagCount = static_cast<std::size_t>(std::log2(NUM_MAG_DATASETS)) + 1;

std::vector<qint64> bytesReceivedAll(maxMagCount);
std::vector<qint64> bytesTotalAll(maxMagCount);
Expand All @@ -76,7 +76,7 @@ std::pair<int, int> Network::checkOnlineMags(const QUrl & url) {
QUrl magUrl = url;
magUrl.setPath(QString("%1/mag%2/knossos.conf").arg(url.path()).arg(currMag));
auto * replyPtr = manager.get(QNetworkRequest{magUrl});
replies[int_log(currMag)] = decltype(replies)::value_type{replyPtr};
replies[static_cast<std::size_t>(std::log2(currMag))] = decltype(replies)::value_type{replyPtr};
++downloadCounter;
QObject::connect(replyPtr, &QNetworkReply::finished, [magUrl, &pause, &downloadCounter, currMag, replyPtr, &lowestAvailableMag, &highestAvailableMag]() {
auto & reply = *replyPtr;
Expand All @@ -89,8 +89,8 @@ std::pair<int, int> Network::checkOnlineMags(const QUrl & url) {
}
});
auto processProgress = [this, currMag, &bytesReceivedAll, &bytesTotalAll](qint64 bytesReceived, qint64 bytesTotal){
bytesReceivedAll[int_log(currMag)] = bytesReceived;
bytesTotalAll[int_log(currMag)] = bytesTotal;
bytesReceivedAll[static_cast<std::size_t>(std::log2(currMag))] = bytesReceived;
bytesTotalAll[static_cast<std::size_t>(std::log2(currMag))] = bytesTotal;
const auto received = std::accumulate(std::begin(bytesReceivedAll), std::end(bytesReceivedAll), qint64{0});
const auto total = std::accumulate(std::begin(bytesTotalAll), std::end(bytesTotalAll), qint64{0});
emit Network::progressChanged(received, total);
Expand Down
2 changes: 1 addition & 1 deletion segmentation/cubeloader.cpp
Expand Up @@ -37,7 +37,7 @@ std::pair<bool, void *> getRawCube(const Coordinate & pos) {
const auto posDc = pos.cube(Dataset::current().cubeEdgeLength, Dataset::current().magnification);

state->protectCube2Pointer.lock();
auto * rawcube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, Segmentation::singleton().layerId, int_log(Dataset::current().magnification), posDc);
auto * rawcube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, Segmentation::singleton().layerId, static_cast<std::size_t>(std::log2(Dataset::current().magnification)), posDc);
state->protectCube2Pointer.unlock();

return std::make_pair(rawcube != nullptr, rawcube);
Expand Down
10 changes: 0 additions & 10 deletions stateInfo.h
Expand Up @@ -41,16 +41,6 @@ extern stateInfo * state;
// Bytes for an object ID.
#define OBJID_BYTES sizeof(uint64_t)

//custom tail recursive constexpr log implementation
//required for the following array declarations/accesses: (because std::log2 isn’t required to be constexpr yet)
// magPaths, magNames, magBoundaries, Dc2Pointer, Oc2Pointer
//TODO to be removed when the above mentioned variables vanish
//integral return value for castless use in subscripts
template<typename T>
constexpr std::size_t int_log(const T val, const T base = 2, const std::size_t res = 0) {
return val < base ? res : int_log(val/base, base, res+1);
}

/**
* @stateInfo
* @brief stateInfo holds everything we need to know about the current instance of Knossos
Expand Down
11 changes: 5 additions & 6 deletions viewer.cpp
Expand Up @@ -23,7 +23,6 @@
#include "viewer.h"

#include "file_io.h"
#include "functions.h"
#include "loader.h"
#include "segmentation/segmentation.h"
#include "session.h"
Expand Down Expand Up @@ -223,7 +222,7 @@ float Viewer::lowestScreenPxXPerDataPx(const bool ofCurrentMag) {

int Viewer::calcMag(const float screenPxXPerDataPx) {
const float exactMag = Dataset::current().highestAvailableMag * lowestScreenPxXPerDataPx() / screenPxXPerDataPx;
const int roundedPower = std::ceil(std::log(exactMag) / std::log(2));
const auto roundedPower = std::ceil(std::log2(exactMag));
return std::min(Dataset::current().highestAvailableMag, std::max(static_cast<int>(std::pow(2, roundedPower)), Dataset::current().lowestAvailableMag));
}

Expand Down Expand Up @@ -554,7 +553,7 @@ void Viewer::vpGenerateTexture(ViewportOrtho & vp, const std::size_t layerId) {
qDebug("No such slice type (%d) in vpGenerateTexture.", vp.viewportType);
}
state->protectCube2Pointer.lock();
void * const cube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layerId, int_log(Dataset::current().magnification), currentDc);
void * const cube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layerId, static_cast<std::size_t>(std::log2(Dataset::current().magnification)), currentDc);
state->protectCube2Pointer.unlock();

// Take care of the data textures.
Expand Down Expand Up @@ -734,7 +733,7 @@ void Viewer::vpGenerateTexture(ViewportArb &vp, const std::size_t layerId) {
if(currentPx.z < 0) { currentDc.z -= 1; }

state->protectCube2Pointer.lock();
void * const datacube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layerId, int_log(Dataset::datasets[layerId].magnification), {currentDc.x, currentDc.y, currentDc.z});
void * const datacube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layerId, static_cast<std::size_t>(std::log2(Dataset::datasets[layerId].magnification)), {currentDc.x, currentDc.y, currentDc.z});
state->protectCube2Pointer.unlock();

currentPxInDc_float = currentPx_float - currentDc * Dataset::current().cubeEdgeLength;
Expand Down Expand Up @@ -866,7 +865,7 @@ bool Viewer::updateDatasetMag(const int mag) {
// convert sizes in dataset coordinates to physical coordinates and back with updated scale
// save intermediates in floatCoordinate as not to truncate them
const auto prevScale = layer.scale;
layer.scale = layer.scales[std::log2(mag)] / layer.magnification;
layer.scale = layer.scales[static_cast<std::size_t>(std::log2(mag))] / layer.magnification;

const auto boundary = prevScale.componentMul(layer.boundary);
layer.boundary = boundary / layer.scale;
Expand Down Expand Up @@ -945,7 +944,7 @@ void Viewer::run() {
const auto globalCoord = pair.first.cube2Global(gpucubeedge, Dataset::current().magnification);
const auto cubeCoord = globalCoord.cube(Dataset::current().cubeEdgeLength, Dataset::current().magnification);
state->protectCube2Pointer.lock();
const auto * ptr = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layer.isOverlayData, int_log(Dataset::current().magnification), cubeCoord);
const auto * ptr = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layer.isOverlayData, static_cast<std::size_t>(std::log2(Dataset::current().magnification)), cubeCoord);
state->protectCube2Pointer.unlock();
if (ptr != nullptr) {
layer.cubeSubArray(ptr, Dataset::current().cubeEdgeLength, gpucubeedge, pair.first, pair.second);
Expand Down
2 changes: 1 addition & 1 deletion widgets/viewports/viewport3d.cpp
Expand Up @@ -168,7 +168,7 @@ void Viewport3D::updateVolumeTexture() {
auto cubeIndex = z*M*M + y*M + x;
Coordinate cubeCoordRelative{x - M_radius, y - M_radius, z - M_radius};
rawcubes[cubeIndex] = reinterpret_cast<uint64_t*>(
Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, Segmentation::singleton().layerId, int_log(Dataset::current().magnification),
Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, Segmentation::singleton().layerId, static_cast<std::size_t>(std::log2(Dataset::current().magnification)),
{currentPosDc.x + cubeCoordRelative.x, currentPosDc.y + cubeCoordRelative.y, currentPosDc.z + cubeCoordRelative.z}));
}
dcfetch_profiler.end(); // ----------------------------------------------------------- profiling
Expand Down
2 changes: 1 addition & 1 deletion widgets/zoomwidget.cpp
Expand Up @@ -255,7 +255,7 @@ void ZoomWidget::applyZoom(const float newScreenPxXPerDataPx) {
}

void ZoomWidget::reinitializeOrthoZoomWidgets() {
const auto mags = int_log(state->viewer->highestMag()) - int_log(state->viewer->lowestMag()) + 1;
const auto mags = static_cast<std::size_t>(std::log2(state->viewer->highestMag() / state->viewer->lowestMag())) + 1;
const auto interval = 50;

zoomStep = std::pow(2, 1./interval);
Expand Down

0 comments on commit 04b352d

Please sign in to comment.