From 04b352d7f52961114a6a647e2e5ec92495586dac Mon Sep 17 00:00:00 2001 From: Norbert Pfeiler Date: Sat, 28 Apr 2018 18:02:26 +0200 Subject: [PATCH] replace int_log with std::log2 and static_cast --- dataset.cpp | 4 ++-- loader.cpp | 9 ++++----- network.cpp | 8 ++++---- segmentation/cubeloader.cpp | 2 +- stateInfo.h | 10 ---------- viewer.cpp | 11 +++++------ widgets/viewports/viewport3d.cpp | 2 +- widgets/zoomwidget.cpp | 2 +- 8 files changed, 18 insertions(+), 30 deletions(-) diff --git a/dataset.cpp b/dataset.cpp index 12323f6c6..03284aa48 100644 --- a/dataset.cpp +++ b/dataset.cpp @@ -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::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); @@ -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::log2(magnification)));// >= 0 coord.x /= magnification; coord.y /= magnification; coord.z += 1;//offset diff --git a/loader.cpp b/loader.cpp index 93430517a..5f2852176 100644 --- a/loader.cpp +++ b/loader.cpp @@ -22,7 +22,6 @@ #include "loader.h" -#include "functions.h" #include "network.h" #include "segmentation/segmentation.h" #include "session.h" @@ -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::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::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()); @@ -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::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 @@ -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::log2(dataset.magnification))).arg(dataset.cubeEdgeLength).toUtf8(); } //request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); //request.setAttribute(QNetworkRequest::SpdyAllowedAttribute, true); diff --git a/network.cpp b/network.cpp index f08e95638..261ba241a 100644 --- a/network.cpp +++ b/network.cpp @@ -64,7 +64,7 @@ void Network::setCookies(const QVariantList & setting) { std::pair 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::log2(NUM_MAG_DATASETS)) + 1; std::vector bytesReceivedAll(maxMagCount); std::vector bytesTotalAll(maxMagCount); @@ -76,7 +76,7 @@ std::pair 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::log2(currMag))] = decltype(replies)::value_type{replyPtr}; ++downloadCounter; QObject::connect(replyPtr, &QNetworkReply::finished, [magUrl, &pause, &downloadCounter, currMag, replyPtr, &lowestAvailableMag, &highestAvailableMag]() { auto & reply = *replyPtr; @@ -89,8 +89,8 @@ std::pair 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::log2(currMag))] = bytesReceived; + bytesTotalAll[static_cast(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); diff --git a/segmentation/cubeloader.cpp b/segmentation/cubeloader.cpp index d43c8ed86..131f6ee3a 100644 --- a/segmentation/cubeloader.cpp +++ b/segmentation/cubeloader.cpp @@ -37,7 +37,7 @@ std::pair 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::log2(Dataset::current().magnification)), posDc); state->protectCube2Pointer.unlock(); return std::make_pair(rawcube != nullptr, rawcube); diff --git a/stateInfo.h b/stateInfo.h index d992388b2..19609318f 100644 --- a/stateInfo.h +++ b/stateInfo.h @@ -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 -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 diff --git a/viewer.cpp b/viewer.cpp index 6b90b235a..dd5dd441b 100644 --- a/viewer.cpp +++ b/viewer.cpp @@ -23,7 +23,6 @@ #include "viewer.h" #include "file_io.h" -#include "functions.h" #include "loader.h" #include "segmentation/segmentation.h" #include "session.h" @@ -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(std::pow(2, roundedPower)), Dataset::current().lowestAvailableMag)); } @@ -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::log2(Dataset::current().magnification)), currentDc); state->protectCube2Pointer.unlock(); // Take care of the data textures. @@ -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::log2(Dataset::datasets[layerId].magnification)), {currentDc.x, currentDc.y, currentDc.z}); state->protectCube2Pointer.unlock(); currentPxInDc_float = currentPx_float - currentDc * Dataset::current().cubeEdgeLength; @@ -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::log2(mag))] / layer.magnification; const auto boundary = prevScale.componentMul(layer.boundary); layer.boundary = boundary / layer.scale; @@ -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::log2(Dataset::current().magnification)), cubeCoord); state->protectCube2Pointer.unlock(); if (ptr != nullptr) { layer.cubeSubArray(ptr, Dataset::current().cubeEdgeLength, gpucubeedge, pair.first, pair.second); diff --git a/widgets/viewports/viewport3d.cpp b/widgets/viewports/viewport3d.cpp index 2e89d46f3..f14f59278 100644 --- a/widgets/viewports/viewport3d.cpp +++ b/widgets/viewports/viewport3d.cpp @@ -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( - 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::log2(Dataset::current().magnification)), {currentPosDc.x + cubeCoordRelative.x, currentPosDc.y + cubeCoordRelative.y, currentPosDc.z + cubeCoordRelative.z})); } dcfetch_profiler.end(); // ----------------------------------------------------------- profiling diff --git a/widgets/zoomwidget.cpp b/widgets/zoomwidget.cpp index d6788d2a9..923859cfa 100644 --- a/widgets/zoomwidget.cpp +++ b/widgets/zoomwidget.cpp @@ -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::log2(state->viewer->highestMag() / state->viewer->lowestMag())) + 1; const auto interval = 50; zoomStep = std::pow(2, 1./interval);