/******************************************************************************* * The MIT License (MIT) * * Copyright (c) 2015 David Williams and Matthew Williams * * 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. *******************************************************************************/ namespace PolyVox { template Mesh::Mesh() { } template Mesh::~Mesh() { } template IndexType Mesh::getNoOfVertices(void) const { return static_cast(m_vecVertices.size()); } template const VertexType& Mesh::getVertex(IndexType index) const { return m_vecVertices[index]; } template const VertexType* Mesh::getRawVertexData(void) const { return m_vecVertices.data(); } template size_t Mesh::getNoOfIndices(void) const { return m_vecIndices.size(); } template IndexType Mesh::getIndex(uint32_t index) const { return m_vecIndices[index]; } template const IndexType* Mesh::getRawIndexData(void) const { return m_vecIndices.data(); } template const Vector3DInt32& Mesh::getOffset(void) const { return m_offset; } template void Mesh::setOffset(const Vector3DInt32& offset) { m_offset = offset; } template void Mesh::addTriangle(IndexType index0, IndexType index1, IndexType index2) { //Make sure the specified indices correspond to valid vertices. POLYVOX_ASSERT(index0 < m_vecVertices.size(), "Index points at an invalid vertex."); POLYVOX_ASSERT(index1 < m_vecVertices.size(), "Index points at an invalid vertex."); POLYVOX_ASSERT(index2 < m_vecVertices.size(), "Index points at an invalid vertex."); m_vecIndices.push_back(index0); m_vecIndices.push_back(index1); m_vecIndices.push_back(index2); } template IndexType Mesh::addVertex(const VertexType& vertex) { // We should not add more vertices than our chosen index type will let us index. POLYVOX_THROW_IF(m_vecVertices.size() >= std::numeric_limits::max(), std::out_of_range, "Mesh has more vertices that the chosen index type allows."); m_vecVertices.push_back(vertex); return m_vecVertices.size() - 1; } template void Mesh::clear(void) { m_vecVertices.clear(); m_vecIndices.clear(); } template bool Mesh::isEmpty(void) const { return (getNoOfVertices() == 0) || (getNoOfIndices() == 0); } template void Mesh::removeUnusedVertices(void) { std::vector isVertexUsed(m_vecVertices.size()); std::fill(isVertexUsed.begin(), isVertexUsed.end(), false); for (uint32_t triCt = 0; triCt < m_vecIndices.size(); triCt++) { int v = m_vecIndices[triCt]; isVertexUsed[v] = true; } int noOfUsedVertices = 0; std::vector newPos(m_vecVertices.size()); for (IndexType vertCt = 0; vertCt < m_vecVertices.size(); vertCt++) { if (isVertexUsed[vertCt]) { m_vecVertices[noOfUsedVertices] = m_vecVertices[vertCt]; newPos[vertCt] = noOfUsedVertices; noOfUsedVertices++; } } m_vecVertices.resize(noOfUsedVertices); for (uint32_t triCt = 0; triCt < m_vecIndices.size(); triCt++) { m_vecIndices[triCt] = newPos[m_vecIndices[triCt]]; } } }