/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš 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 "CompressIndices.h" #include #include #include #include "Magnum/Math/FunctionsBatch.h" namespace Magnum { namespace MeshTools { namespace { template inline Containers::Array compress(const Containers::StridedArrayView1D& indices) { /* Can't use Utility::copy() here because we're copying from a larger type to a smaller one */ Containers::Array buffer(indices.size()*sizeof(T)); for(std::size_t i = 0; i != indices.size(); ++i) { T index = static_cast(indices[i]); std::memcpy(buffer.begin()+i*sizeof(T), &index, sizeof(T)); } return buffer; } template std::pair, MeshIndexType> compressIndicesImplementation(const Containers::StridedArrayView1D& indices, const MeshIndexType atLeast) { const T max = Math::max(indices); Containers::Array out; MeshIndexType type; const UnsignedInt log = Math::log(256, max); /* If it fits into 8 bytes and 8 bytes are allowed, pack into 8 */ if(log == 0 && atLeast == MeshIndexType::UnsignedByte) { out = compress(indices); type = MeshIndexType::UnsignedByte; /* Otherwise, if it fits into either 8 or 16 bytes and we allow either 8 or 16, pack into 16 */ } else if(log <= 1 && atLeast != MeshIndexType::UnsignedInt) { out = compress(indices); type = MeshIndexType::UnsignedShort; /* Otherwise pack into 32 */ } else { out = compress(indices); type = MeshIndexType::UnsignedInt; } return {std::move(out), type}; } } std::pair, MeshIndexType> compressIndices(const Containers::StridedArrayView1D& indices, const MeshIndexType atLeast) { return compressIndicesImplementation(indices, atLeast); } std::pair, MeshIndexType> compressIndices(const Containers::StridedArrayView1D& indices, const MeshIndexType atLeast) { return compressIndicesImplementation(indices, atLeast); } std::pair, MeshIndexType> compressIndices(const Containers::StridedArrayView1D& indices, const MeshIndexType atLeast) { return compressIndicesImplementation(indices, atLeast); } #ifdef MAGNUM_BUILD_DEPRECATED std::tuple, MeshIndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector& indices) { /** @todo Performance hint when range can be represented by smaller value? */ const auto minmax = Math::minmax(indices); Containers::Array data; MeshIndexType type; std::tie(data, type) = compressIndices(indices, MeshIndexType::UnsignedByte); return std::make_tuple(std::move(data), type, minmax.first, minmax.second); } #endif template Containers::Array compressIndicesAs(const std::vector& indices) { #if !defined(CORRADE_NO_ASSERT) || defined(CORRADE_GRACEFUL_ASSERT) const auto max = Math::max(indices); CORRADE_ASSERT(Math::log(256, max) < sizeof(T), "MeshTools::compressIndicesAs(): type too small to represent value" << max, {}); #endif Containers::Array buffer(indices.size()); for(std::size_t i = 0; i != indices.size(); ++i) buffer[i] = T(indices[i]); return buffer; } template Containers::Array compressIndicesAs(const std::vector&); template Containers::Array compressIndicesAs(const std::vector&); template Containers::Array compressIndicesAs(const std::vector&); }}