Source

KODE / include / kode / collision / Trimesh.hpp

Full commit
/*
   KODE Physics Library
   Copyright 2013-2014 Daniel Kohler Osmari

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#ifndef KODE_TRIMESH_H
#define KODE_TRIMESH_H

#include <memory>
#include <cstdint>
#include <vector>

#include <kode/collision/Geom.hpp>

namespace kode {

    class Sphere;
    class ContactPoint;

    using TriIndex = std::uint16_t;

    class Trimesh : public Geom {
    public:
        struct Data {

            struct Impl;

            std::unique_ptr<Impl> pimpl;
        public:
            Data();
            Data(const float* verts, unsigned nVerts,
                 const TriIndex* indices, unsigned triCount);
            Data(const double* verts, unsigned nVerts,
                 const TriIndex* indices, unsigned triCount);
            ~Data() noexcept;

            Data(Data&&) noexcept = default;
            Data& operator=(Data&&) noexcept = default;


            void build(const float* verts, unsigned nVerts,
                       const TriIndex* indices, unsigned triCount);

            void build(const double* verts, unsigned nVerts,
                       const TriIndex* indices, unsigned triCount);


            void preprocess();

            const std::vector<Vector3>& getVertices() const;
            const std::vector<TriIndex>& getIndices() const;

            friend class Trimesh;
            friend  int testSphereSphere(const Sphere& sphere,
                                         const Trimesh& trimesh,
                                         std::vector<ContactPoint>& cp);
        };


        Trimesh() = default;

        Trimesh(Data&& newData);
        Trimesh(std::shared_ptr<Data>& newData);

        Type getType() const noexcept override;

        void setData(Data&& newData);

        virtual void setData(std::shared_ptr<Data> newData);

        std::shared_ptr<Data> getData();
        std::shared_ptr<const Data> getData() const;


        void getTriangle(int index, Vector3 out[3]) const;

    protected:
        std::shared_ptr<Data> data;

        void computeAABB() noexcept override;

        Real computePointDepth(const Vector3& point) const noexcept override;

        Interval computeProjected(const Vector3& axis) const override;

        Vector3 computeSupport(const Vector3& dir) const override;

        
    };

}

#endif