From c193054669ef7968983e22e50332b70c79e04378 Mon Sep 17 00:00:00 2001 From: Erik Broberg Date: Tue, 5 Jul 2016 16:56:00 -0400 Subject: [PATCH] Move definition of PixelRegion to separate file --- modules/globebrowsing/CMakeLists.txt | 3 + modules/globebrowsing/tile/pixelregion.cpp | 266 +++++++++++++++++++++ modules/globebrowsing/tile/pixelregion.h | 111 +++++++++ modules/globebrowsing/tile/tiledataset.cpp | 1 - modules/globebrowsing/tile/tiledataset.h | 197 +-------------- 5 files changed, 384 insertions(+), 194 deletions(-) create mode 100644 modules/globebrowsing/tile/pixelregion.cpp create mode 100644 modules/globebrowsing/tile/pixelregion.h diff --git a/modules/globebrowsing/CMakeLists.txt b/modules/globebrowsing/CMakeLists.txt index 5a0b907e5f..06bd849b51 100644 --- a/modules/globebrowsing/CMakeLists.txt +++ b/modules/globebrowsing/CMakeLists.txt @@ -60,6 +60,7 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tile/tileproviderfactory.h ${CMAKE_CURRENT_SOURCE_DIR}/tile/layeredtextureshaderprovider.h ${CMAKE_CURRENT_SOURCE_DIR}/tile/layeredtextures.h + ${CMAKE_CURRENT_SOURCE_DIR}/tile/pixelregion.h ${CMAKE_CURRENT_SOURCE_DIR}/other/distanceswitch.h ${CMAKE_CURRENT_SOURCE_DIR}/other/lrucache.h @@ -106,6 +107,8 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tile/tileproviderfactory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tile/layeredtextureshaderprovider.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tile/layeredtextures.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tile/pixelregion.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/other/distanceswitch.cpp ${CMAKE_CURRENT_SOURCE_DIR}/other/lrucache.inl diff --git a/modules/globebrowsing/tile/pixelregion.cpp b/modules/globebrowsing/tile/pixelregion.cpp new file mode 100644 index 0000000000..63a01fd8c6 --- /dev/null +++ b/modules/globebrowsing/tile/pixelregion.cpp @@ -0,0 +1,266 @@ + /***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#include + +#include +#include +#include + + + + + +namespace { + const std::string _loggerCat = "PixelRegion"; +} + + + +namespace openspace { + + + + PixelRegion::PixelRegion() + : start(0) + , numPixels(0) + { + + } + + PixelRegion::PixelRegion(const PixelRegion& o) + : start(o.start) + , numPixels(o.numPixels) + { + + } + + PixelRegion::PixelRegion(const PixelCoordinate& pixelStart, const PixelRange& numberOfPixels) + : start(pixelStart) + , numPixels(numberOfPixels) + { + + } + + + + + ////////////////////////////////////////////////////////////////////////////////// + // Mutators // + ////////////////////////////////////////////////////////////////////////////////// + + void PixelRegion::setSide(Side side, int pos) { + switch (side) { + case LEFT: setLeft(pos); break; + case TOP: setTop(pos); break; + case RIGHT: setRight(pos); break; + case BOTTOM: setBottom(pos); break; + } + } + + void PixelRegion::setLeft(int x) { + numPixels.x += (start.x - x); + start.x = x; + } + + void PixelRegion::setTop(int p) { + numPixels.y += (start.y - p); + start.y = p; + } + + void PixelRegion::setRight(int x) { + numPixels.x = x - start.x; + } + + void PixelRegion::setBottom(int y) { + numPixels.y = y - start.y; + } + + + void PixelRegion::align(Side side, int pos) { + switch (side) { + case LEFT: alignLeft(pos); break; + case TOP: alignTop(pos); break; + case RIGHT: alignRight(pos); break; + case BOTTOM: alignBottom(pos); break; + } + } + + void PixelRegion::alignLeft(int x) { + start.x = x; + } + + void PixelRegion::alignTop(int y) { + start.y = y; + } + + void PixelRegion::alignRight(int x) { + start.x = x - numPixels.x; + } + + void PixelRegion::alignBottom(int y) { + start.y = y - numPixels.y; + } + + + + void PixelRegion::scale(const glm::dvec2& s) { + start = PixelCoordinate(glm::round(s * glm::dvec2(start))); + numPixels = PixelCoordinate(glm::round(s * glm::dvec2(numPixels))); + } + + void PixelRegion::scale(double s) { + scale(glm::dvec2(s)); + } + + void PixelRegion::downscalePow2(int exponent) { + start.x >>= exponent; + start.y >>= exponent; + numPixels.x >>= exponent; + numPixels.y >>= exponent; + } + + void PixelRegion::upscalePow2(int exponent) { + start.x <<= exponent; + start.y <<= exponent; + numPixels.x <<= exponent; + numPixels.y <<= exponent; + } + + + + void PixelRegion::move(Side side, int amount) { + switch (side) { + case LEFT: start.x -= amount; break; + case TOP: start.y -= amount; break; + case RIGHT: start.x += amount; break; + case BOTTOM: start.y += amount; break; + } + } + + void PixelRegion::pad(const PixelRegion& padding) { + start += padding.start; + numPixels += padding.numPixels; + } + + void PixelRegion::clampTo(const PixelRegion& boundingRegion) { + start = glm::max(start, boundingRegion.start); + numPixels = glm::min(end(), boundingRegion.end()) - start; + } + + + + PixelRegion PixelRegion::globalCut(Side side, int p) { + if (!lineIntersect(side, p)) { + return PixelRegion({ 0, 0 }, { 0, 0 }); + } + + PixelRegion cutOff(*this); + int cutSize = 0; + switch (side) { + case LEFT: + setLeft(p); + cutOff.setRight(p - cutSize); + break; + case TOP: + setTop(p); + cutOff.setBottom(p - cutSize); + break; + case RIGHT: + setRight(p); + cutOff.setLeft(p + cutSize); + break; + case BOTTOM: + setBottom(p); + cutOff.setTop(p + cutSize); + break; + } + return cutOff; + } + + PixelRegion PixelRegion::localCut(Side side, int p) { + if (p < 1) { + return PixelRegion({ 0, 0 }, { 0, 0 }); + } + else return globalCut(side, edge(side) - edgeDirectionSign(side) * p); + } + + + ////////////////////////////////////////////////////////////////////////////////// + // Accessors // + ////////////////////////////////////////////////////////////////////////////////// + + int PixelRegion::area() const { + return numPixels.x * numPixels.y; + } + + + int PixelRegion::edge(Side side) const { + switch (side) { + case LEFT: return start.x; + case TOP: return start.y; + case RIGHT: return start.x + numPixels.x; + case BOTTOM: return start.y + numPixels.y; + } + } + + int PixelRegion::edgeDirectionSign(Side side) const { + return side < RIGHT ? -1 : 1; + } + + PixelCoordinate PixelRegion::end() const { + return start + numPixels; + } + + ////////////////////////////////////////////////////////////////////////////////// + // Comparators // + ////////////////////////////////////////////////////////////////////////////////// + + + bool PixelRegion::lineIntersect(Side side, int p) { + switch (side) + { + case openspace::PixelRegion::LEFT: + case openspace::PixelRegion::RIGHT: + return start.x <= p && p <= (start.x + numPixels.x); + + case openspace::PixelRegion::TOP: + case openspace::PixelRegion::BOTTOM: + return start.y <= p && p <= (start.y + numPixels.y); + } + } + + bool PixelRegion::isInside(const PixelRegion& r) const { + PixelCoordinate e = end(); + PixelCoordinate re = r.end(); + return r.start.x <= start.x && e.x <= re.x + && r.start.y <= start.y && e.y <= re.y; + } + + bool PixelRegion::equals(const PixelRegion& r) const { + return start == r.start && numPixels == r.numPixels; + } + + +} // namespace openspace diff --git a/modules/globebrowsing/tile/pixelregion.h b/modules/globebrowsing/tile/pixelregion.h new file mode 100644 index 0000000000..835506901e --- /dev/null +++ b/modules/globebrowsing/tile/pixelregion.h @@ -0,0 +1,111 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#ifndef __PIXEL_REGION_H__ +#define __PIXEL_REGION_H__ + +#include + +namespace openspace { + + typedef glm::ivec2 PixelCoordinate; + typedef glm::ivec2 PixelRange; + + struct PixelRegion { + + enum Side { + LEFT = 0, TOP, RIGHT, BOTTOM, + }; + + PixelRegion(); + PixelRegion(const PixelRegion& o); + PixelRegion(const PixelCoordinate& pixelStart, const PixelRange& numberOfPixels); + + + ////////////////////////////////////////////////////////////////////////////////// + // Mutators // + ////////////////////////////////////////////////////////////////////////////////// + + void setSide(Side side, int pos); + void setLeft(int x); + void setTop(int p); + void setRight(int x); + void setBottom(int y); + + void align(Side side, int pos); + void alignLeft(int x); + void alignTop(int y); + void alignRight(int x); + void alignBottom(int y); + + void scale(const glm::dvec2& s); + void scale(double s); + void downscalePow2(int exponent); + void upscalePow2(int exponent); + + void move(Side side, int amount); + void pad(const PixelRegion& padding); + void clampTo(const PixelRegion& boundingRegion); + + + PixelRegion globalCut(Side side, int globalPos); + PixelRegion localCut(Side side, int localPos); + + + ////////////////////////////////////////////////////////////////////////////////// + // Accessors // + ////////////////////////////////////////////////////////////////////////////////// + + int area() const; + int edge(Side side) const; + int edgeDirectionSign(Side side) const; + PixelCoordinate end() const; + + + ////////////////////////////////////////////////////////////////////////////////// + // Comparators // + ////////////////////////////////////////////////////////////////////////////////// + + bool lineIntersect(Side side, int p); + bool isInside(const PixelRegion& r) const; + bool equals(const PixelRegion& r) const; + + + ////////////////////////////////////////////////////////////////////////////////// + // Members // + ////////////////////////////////////////////////////////////////////////////////// + + PixelCoordinate start; + PixelRange numPixels; + }; + + + + + +} // namespace openspace + + + +#endif // __PIXEL_REGION_H__ \ No newline at end of file diff --git a/modules/globebrowsing/tile/tiledataset.cpp b/modules/globebrowsing/tile/tiledataset.cpp index 239f2601f3..95401b8b40 100644 --- a/modules/globebrowsing/tile/tiledataset.cpp +++ b/modules/globebrowsing/tile/tiledataset.cpp @@ -476,7 +476,6 @@ namespace openspace { } } - CPLErr err = rasterIO(rasterBand, io, dataDestination); worstError = std::max(worstError, err); diff --git a/modules/globebrowsing/tile/tiledataset.h b/modules/globebrowsing/tile/tiledataset.h index 347b176af1..be69e839b2 100644 --- a/modules/globebrowsing/tile/tiledataset.h +++ b/modules/globebrowsing/tile/tiledataset.h @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -59,197 +60,7 @@ namespace openspace { TextureFormat textureFormat; }; - - typedef glm::ivec2 PixelCoordinate; - - - - struct PixelRegion { - - enum Side { - LEFT = 0, TOP, RIGHT, BOTTOM, - }; - - PixelRegion() : start(0), numPixels(0) { } - PixelRegion(const PixelRegion& o) : start(o.start), numPixels(o.numPixels) { } - PixelRegion(const PixelCoordinate& pixelStart, const PixelCoordinate& numberOfPixels) - : start(pixelStart), numPixels(numberOfPixels) { } - - void pad(const PixelRegion& padding) { - start += padding.start; - numPixels += padding.numPixels; - } - - void clampTo(const PixelRegion& boundingRegion) { - start = glm::max(start, boundingRegion.start); - numPixels = glm::min(end(), boundingRegion.end()) - start; - } - - PixelCoordinate end() const { - return start + numPixels; - } - - void resizeToStartAt(const PixelCoordinate& c) { - numPixels -= (c - start); - start = c; - } - - void setLeft(int x) { - numPixels.x += (start.x - x); - start.x = x; - } - - void setTop(int p) { - numPixels.y += (start.y - p); - start.y = p; - } - - void setRight(int x) { - numPixels.x = x - start.x; - } - - void setBottom(int y) { - numPixels.y = y - start.y; - } - - - - void alignLeft(int x) { start.x = x; } - void alignTop(int y) { start.y = y; } - void alignRight(int x) { start.x = x - numPixels.x; } - void alignBottom(int y) { start.y = y - numPixels.y; } - - void scale(const glm::dvec2& s) { - start = PixelCoordinate(glm::round(s * glm::dvec2(start))); - numPixels = PixelCoordinate(glm::round(s * glm::dvec2(numPixels))); - } - - void scale(double s) { - scale(glm::dvec2(s)); - } - - int area() { - return numPixels.x * numPixels.y; - } - - void downscalePow2(int exponent) { - start.x >>= exponent; - start.y >>= exponent; - numPixels.x >>= exponent; - numPixels.y >>= exponent; - } - - void upscalePow2(int exponent) { - start.x <<= exponent; - start.y <<= exponent; - numPixels.x <<= exponent; - numPixels.y <<= exponent; - } - - int edge(Side side) const { - switch (side) { - case LEFT: return start.x; - case TOP: return start.y; - case RIGHT: return start.x + numPixels.x; - case BOTTOM: return start.y + numPixels.y; - } - } - - int edgeDirectionSign(Side side) const { - return side < RIGHT ? -1 : 1; - } - - void align(Side side, int pos) { - switch (side) { - case LEFT: alignLeft(pos); break; - case TOP: alignTop(pos); break; - case RIGHT: alignRight(pos); break; - case BOTTOM: alignBottom(pos); break; - } - } - - void move(Side side, int amount) { - switch (side) { - case LEFT: start.x -= amount; break; - case TOP: start.y -= amount; break; - case RIGHT: start.x += amount; break; - case BOTTOM: start.y += amount; break; - } - } - - void setSide(Side side, int pos) { - switch (side) { - case LEFT: setLeft(pos); break; - case TOP: setTop(pos); break; - case RIGHT: setRight(pos); break; - case BOTTOM: setBottom(pos); break; - } - } - - bool lineIntersect(Side side, int p) { - switch (side) - { - case openspace::PixelRegion::LEFT: - case openspace::PixelRegion::RIGHT: - return start.x <= p && p <= (start.x + numPixels.x); - - case openspace::PixelRegion::TOP: - case openspace::PixelRegion::BOTTOM: - return start.y <= p && p <= (start.y + numPixels.y); - } - } - - bool isInside(const PixelRegion& r) const { - PixelCoordinate e = end(); - PixelCoordinate re = r.end(); - return r.start.x <= start.x && e.x <= re.x - && r.start.y <= start.y && e.y <= re.y; - } - - PixelRegion cut(Side side, int p) { - if (!lineIntersect(side, p)) { - return PixelRegion({ 0, 0 }, {0, 0}); - } - - PixelRegion cutOff(*this); - int cutSize = 0; - switch (side) { - case LEFT: - setLeft(p); - cutOff.setRight(p - cutSize); - break; - case TOP: - setTop(p); - cutOff.setBottom(p - cutSize); - break; - case RIGHT: - setRight(p); - cutOff.setLeft(p + cutSize); - break; - case BOTTOM: - setBottom(p); - cutOff.setTop(p + cutSize); - break; - } - return cutOff; - } - - PixelRegion cutDelta(Side side, int p) { - if (p < 1) { - return PixelRegion({ 0, 0 }, { 0, 0 }); - } - else return cut(side, edge(side) - edgeDirectionSign(side) * p); - } - - bool equals(const PixelRegion& r) const { - return start == r.start && numPixels == r.numPixels; - } - - PixelCoordinate start; - PixelCoordinate numPixels; - }; - - + struct IODescription { @@ -258,11 +69,11 @@ namespace openspace { PixelRegion writePreCut = write.region; IODescription whatCameOff = *this; - whatCameOff.read.region = read.region.cut(side, pos); + whatCameOff.read.region = read.region.globalCut(side, pos); PixelCoordinate cutSize = whatCameOff.read.region.numPixels; int cutWidth = (side % 2 == 0) ? cutSize.x : cutSize.y; - whatCameOff.write.region = write.region.cutDelta(side, cutWidth); + whatCameOff.write.region = write.region.localCut(side, cutWidth); if (cutWidth == 0) {