<commit_before>/*********************************************************************************
 *
 * Inviwo - Interactive Visualization Workshop
 *
 * Copyright (c) 2013-2017 Inviwo Foundation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *********************************************************************************/

#include <inviwo/core/datastructures/geometry/mesh.h>
#include <inviwo/core/util/document.h>

namespace inviwo {

Mesh::Mesh(DrawType dt, ConnectivityType ct)
    : DataGroup<Mesh, MeshRepresentation>()
    , SpatialEntity<3>()
    , MetaDataOwner()
    , meshInfo_(MeshInfo(dt, ct)) {}

Mesh::Mesh(const Mesh& rhs)
    : DataGroup<Mesh, MeshRepresentation>(rhs)
    , SpatialEntity<3>(rhs)
    , MetaDataOwner(rhs)
    , meshInfo_(rhs.meshInfo_) {
    for (const auto& elem : rhs.buffers_) {
        buffers_.emplace_back(elem.first, std::shared_ptr<BufferBase>(elem.second->clone()));
    }
    for (const auto& elem : rhs.indices_) {
        indices_.emplace_back(elem.first, std::shared_ptr<IndexBuffer>(elem.second->clone()));
    }
}

Mesh& Mesh::operator=(const Mesh& that) {
    if (this != &that) {
        DataGroup<Mesh, MeshRepresentation>::operator=(that);
        SpatialEntity<3>::operator=(that);
        MetaDataOwner::operator=(that);

        BufferVector buffers;
        IndexVector indices;

        for (const auto& attr : that.buffers_) {
            buffers.emplace_back(attr.first, std::shared_ptr<BufferBase>(attr.second->clone()));
        }
        for (const auto& elem : that.indices_) {
            indices.emplace_back(elem.first, std::shared_ptr<IndexBuffer>(elem.second->clone()));
        }

        std::swap(buffers, buffers_);
        std::swap(indices, indices_);
        meshInfo_ = that.meshInfo_;
    }
    return *this;
}

Mesh* Mesh::clone() const { return new Mesh(*this); }

const Mesh::BufferVector& Mesh::getBuffers() const { return buffers_; }

const Mesh::IndexVector& Mesh::getIndexBuffers() const { return indices_; }

void Mesh::addBuffer(BufferInfo info, std::shared_ptr<BufferBase> att) {
    buffers_.emplace_back(info, att);
}

void Mesh::addBuffer(BufferType type, std::shared_ptr<BufferBase> att) {
    buffers_.emplace_back(BufferInfo(type), att);
}

void Mesh::setBuffer(size_t idx, BufferInfo info, std::shared_ptr<BufferBase> att) {
    if (idx < buffers_.size()) {
        buffers_[idx] = std::make_pair(info, att);
    }
}

void Mesh::addIndicies(MeshInfo info, std::shared_ptr<IndexBuffer> ind) {
    indices_.push_back(std::make_pair(info, ind));
}

void Mesh::reserveSizeInVertexBuffer(size_t size) {
    for (auto& buf : buffers_) {
        buf.second->getEditableRepresentation<BufferRAM>()->reserve(size);
    }
}

void Mesh::reserveIndexBuffers(size_t size) { indices_.reserve(size); }

const BufferBase* Mesh::getBuffer(size_t idx) const { return buffers_[idx].second.get(); }

const IndexBuffer* Mesh::getIndices(size_t idx) const { return indices_[idx].second.get(); }

BufferBase* Mesh::getBuffer(size_t idx) { return buffers_[idx].second.get(); }

IndexBuffer* Mesh::getIndices(size_t idx) { return indices_[idx].second.get(); }

Mesh::MeshInfo Mesh::getDefaultMeshInfo() const { return meshInfo_; }

Mesh::MeshInfo Mesh::getIndexMeshInfo(size_t idx) const { return indices_[idx].first; }

size_t Mesh::getNumberOfBuffers() const { return buffers_.size(); }

size_t Mesh::getNumberOfIndicies() const { return indices_.size(); }

void Mesh::append(const Mesh& mesh) {
    if (buffers_.size() != mesh.buffers_.size()) {
        throw Exception("Mismatched meshed, number of buffer does not match", IvwContext);
    }
    for (size_t i = 0; i < buffers_.size(); ++i) {
        if (buffers_[i].first != mesh.buffers_[i].first ||
            buffers_[i].second->getDataFormat() != mesh.buffers_[i].second->getDataFormat()) {
            throw Exception("Mismatched meshed, buffer types does not match", IvwContext);
        }
    }
    size_t size = buffers_[0].second->getSize();
    for (size_t i = 0; i < buffers_.size(); ++i) {
        buffers_[i].second->append(*(mesh.buffers_[i].second));
    }
    for (auto indbuffer : mesh.indices_) {
        const auto& inds = indbuffer.second->getRAMRepresentation()->getDataContainer();

        std::vector<std::uint32_t> newInds;
        newInds.reserve(inds.size());
        std::transform(inds.begin(), inds.end(), std::back_inserter(newInds),
                       [&](auto& i) { return i + static_cast<uint32_t>(size); });
        addIndicies(indbuffer.first, util::makeIndexBuffer(std::move(newInds)));
    }
}

const SpatialCameraCoordinateTransformer<3>& Mesh::getCoordinateTransformer(
    const Camera& camera) const {
    return SpatialEntity<3>::getCoordinateTransformer(camera);
}

inviwo::uvec3 Mesh::COLOR_CODE = uvec3(188, 188, 101);

const std::string Mesh::CLASS_IDENTIFIER = "org.inviwo.Mesh";

std::string Mesh::getDataInfo() const {
    using P = Document::PathComponent;
    using H = utildoc::TableBuilder::Header;
    Document doc;
    doc.append("b", "Mesh", {{"style", "color:white;"}});
    utildoc::TableBuilder tb(doc.handle(), P::end());

    for (const auto& elem : indices_) {
        std::stringstream ss;
        ss << elem.first.dt << " " << elem.first.ct;
        ss << " (" << elem.second->getSize() << ")";
        tb(H("IndexBuffer"), ss.str());
    }

    for (const auto& elem : buffers_) {
        std::stringstream ss;
        ss << elem.first << " " << elem.second->getBufferUsage();
        ss << " (" << elem.second->getSize() << ")";
        tb(H("Buffer"), ss.str());
    }
    return doc;
}

namespace meshutil {

bool hasPickIDBuffer(const Mesh* mesh) {
    if (!mesh) return false;
    for (auto buffer : mesh->getBuffers()) {
        // FIXME: this assumes the picking data to be stored at location 4
        if ((buffer.first.type == BufferType::NumberOfBufferTypes) &&<commit_msg>Core: updated port info for Mesh, closes inviwo/inviwo-dev#1653<commit_after>/*********************************************************************************
 *
 * Inviwo - Interactive Visualization Workshop
 *
 * Copyright (c) 2013-2017 Inviwo Foundation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *********************************************************************************/

#include <inviwo/core/datastructures/geometry/mesh.h>
#include <inviwo/core/util/document.h>

namespace inviwo {

Mesh::Mesh(DrawType dt, ConnectivityType ct)
    : DataGroup<Mesh, MeshRepresentation>()
    , SpatialEntity<3>()
    , MetaDataOwner()
    , meshInfo_(MeshInfo(dt, ct)) {}

Mesh::Mesh(const Mesh& rhs)
    : DataGroup<Mesh, MeshRepresentation>(rhs)
    , SpatialEntity<3>(rhs)
    , MetaDataOwner(rhs)
    , meshInfo_(rhs.meshInfo_) {
    for (const auto& elem : rhs.buffers_) {
        buffers_.emplace_back(elem.first, std::shared_ptr<BufferBase>(elem.second->clone()));
    }
    for (const auto& elem : rhs.indices_) {
        indices_.emplace_back(elem.first, std::shared_ptr<IndexBuffer>(elem.second->clone()));
    }
}

Mesh& Mesh::operator=(const Mesh& that) {
    if (this != &that) {
        DataGroup<Mesh, MeshRepresentation>::operator=(that);
        SpatialEntity<3>::operator=(that);
        MetaDataOwner::operator=(that);

        BufferVector buffers;
        IndexVector indices;

        for (const auto& attr : that.buffers_) {
            buffers.emplace_back(attr.first, std::shared_ptr<BufferBase>(attr.second->clone()));
        }
        for (const auto& elem : that.indices_) {
            indices.emplace_back(elem.first, std::shared_ptr<IndexBuffer>(elem.second->clone()));
        }

        std::swap(buffers, buffers_);
        std::swap(indices, indices_);
        meshInfo_ = that.meshInfo_;
    }
    return *this;
}

Mesh* Mesh::clone() const { return new Mesh(*this); }

const Mesh::BufferVector& Mesh::getBuffers() const { return buffers_; }

const Mesh::IndexVector& Mesh::getIndexBuffers() const { return indices_; }

void Mesh::addBuffer(BufferInfo info, std::shared_ptr<BufferBase> att) {
    buffers_.emplace_back(info, att);
}

void Mesh::addBuffer(BufferType type, std::shared_ptr<BufferBase> att) {
    buffers_.emplace_back(BufferInfo(type), att);
}

void Mesh::setBuffer(size_t idx, BufferInfo info, std::shared_ptr<BufferBase> att) {
    if (idx < buffers_.size()) {
        buffers_[idx] = std::make_pair(info, att);
    }
}

void Mesh::addIndicies(MeshInfo info, std::shared_ptr<IndexBuffer> ind) {
    indices_.push_back(std::make_pair(info, ind));
}

void Mesh::reserveSizeInVertexBuffer(size_t size) {
    for (auto& buf : buffers_) {
        buf.second->getEditableRepresentation<BufferRAM>()->reserve(size);
    }
}

void Mesh::reserveIndexBuffers(size_t size) { indices_.reserve(size); }

const BufferBase* Mesh::getBuffer(size_t idx) const { return buffers_[idx].second.get(); }

const IndexBuffer* Mesh::getIndices(size_t idx) const { return indices_[idx].second.get(); }

BufferBase* Mesh::getBuffer(size_t idx) { return buffers_[idx].second.get(); }

IndexBuffer* Mesh::getIndices(size_t idx) { return indices_[idx].second.get(); }

Mesh::MeshInfo Mesh::getDefaultMeshInfo() const { return meshInfo_; }

Mesh::MeshInfo Mesh::getIndexMeshInfo(size_t idx) const { return indices_[idx].first; }

size_t Mesh::getNumberOfBuffers() const { return buffers_.size(); }

size_t Mesh::getNumberOfIndicies() const { return indices_.size(); }

void Mesh::append(const Mesh& mesh) {
    if (buffers_.size() != mesh.buffers_.size()) {
        throw Exception("Mismatched meshed, number of buffer does not match", IvwContext);
    }
    for (size_t i = 0; i < buffers_.size(); ++i) {
        if (buffers_[i].first != mesh.buffers_[i].first ||
            buffers_[i].second->getDataFormat() != mesh.buffers_[i].second->getDataFormat()) {
            throw Exception("Mismatched meshed, buffer types does not match", IvwContext);
        }
    }
    size_t size = buffers_[0].second->getSize();
    for (size_t i = 0; i < buffers_.size(); ++i) {
        buffers_[i].second->append(*(mesh.buffers_[i].second));
    }
    for (auto indbuffer : mesh.indices_) {
        const auto& inds = indbuffer.second->getRAMRepresentation()->getDataContainer();

        std::vector<std::uint32_t> newInds;
        newInds.reserve(inds.size());
        std::transform(inds.begin(), inds.end(), std::back_inserter(newInds),
                       [&](auto& i) { return i + static_cast<uint32_t>(size); });
        addIndicies(indbuffer.first, util::makeIndexBuffer(std::move(newInds)));
    }
}

const SpatialCameraCoordinateTransformer<3>& Mesh::getCoordinateTransformer(
    const Camera& camera) const {
    return SpatialEntity<3>::getCoordinateTransformer(camera);
}

inviwo::uvec3 Mesh::COLOR_CODE = uvec3(188, 188, 101);

const std::string Mesh::CLASS_IDENTIFIER = "org.inviwo.Mesh";

std::string Mesh::getDataInfo() const {
    const int maxLines = 20;

    using P = Document::PathComponent;
    using H = utildoc::TableBuilder::Header;
    Document doc;
    doc.append("b", "Mesh", {{"style", "color:white;"}});

    utildoc::TableBuilder tb(doc.handle(), P::end());
    
    tb(H(std::string("Buffers (") + std::to_string(buffers_.size()) + ")"));
    
    // show all the buffers
    for (const auto& elem : buffers_) {
        std::stringstream ss1;
        ss1 << elem.first << ", " << elem.second->getBufferUsage();
        std::stringstream ss2; 
        ss2 << " (" << elem.second->getSize() << ")";
        tb(ss1.str(), ss2.str());
    }

    // ensure that at least one index buffer is shown
    int maxPrintableIndexBuf = std::max<int>(maxLines - static_cast<int>(buffers_.size()), 1);
    int numIndexBuffers = static_cast<int>(indices_.size());

    if (!indices_.empty()) {
        std::stringstream ss;
        ss << "Indexbuffers (" << indices_.size() << ")";
        tb(H(ss.str()));
    }
    int line = 0;
    for (const auto& elem : indices_) {
        std::stringstream ss1;
        ss1 << elem.first.dt << ", " << elem.first.ct;
        std::stringstream ss2;
        ss2 << " (" << elem.second->getSize() << ")";
        tb(ss1.str(), ss2.str());
        ++line;
        if (line >= maxPrintableIndexBuf) break;
    }
    if (maxPrintableIndexBuf < numIndexBuffers) {
        std::stringstream ss;
        ss << "... (" << (numIndexBuffers - maxPrintableIndexBuf) << " additional buffers)";
        tb(ss.str());
    }

    return doc;
}

namespace meshutil {

bool hasPickIDBuffer(const Mesh* mesh) {
    if (!mesh) return false;
    for (auto buffer : mesh->getBuffers()) {
        // FIXME: this assumes the picking data to be stored at location 4
        if ((buffer.first.type == BufferType::NumberOfBufferTypes) &&
{'fraction_non_alphanumeric': 0.15675320407492607, 'fraction_numerical': 0.00578376602037463, 'mean_word_length': 3.9226787447428015, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '811889', 'n_tokens_mistral': 4706, 'n_tokens_neox': 4545, 'n_words': 1159}