Program Listing for File SceneLoader.h

Return to documentation for file (SPlisHSPlasH/Utilities/SceneLoader.h)

#ifndef __SceneLoader_h__
#define __SceneLoader_h__

#include "SPlisHSPlasH/Common.h"
#include "extern/json/json.hpp"
#include <vector>
#include "ParameterObject.h"

namespace Utilities
{
    class SceneLoader
    {
    protected:
        nlohmann::json m_jsonData;

        void readParameterObject(nlohmann::json& config, GenParam::ParameterObject* paramObj);

    public:
        struct Box
        {
            Vector3r m_minX;
            Vector3r m_maxX;
        };

        struct BoundaryData
        {
            std::string samplesFile;
            std::string meshFile;
            Vector3r translation;
            Matrix3r rotation;
            Vector3r scale;
            Real density;
            bool dynamic;
            bool isWall;
            Eigen::Matrix<float, 4, 1, Eigen::DontAlign> color;
            void *rigidBody;

            std::string mapFile;
            bool mapInvert;
            Real mapThickness;
            Eigen::Matrix<unsigned int, 3, 1, Eigen::DontAlign> mapResolution;
            unsigned int samplingMode;
            bool isAnimated;
        };

        struct FluidData
        {
            std::string id;
            std::string samplesFile;
            Vector3r translation;
            Matrix3r rotation;
            Vector3r scale;
            Vector3r initialVelocity;
            Vector3r initialAngularVelocity;
            unsigned char mode;
            bool invert;
            std::array<unsigned int, 3> resolutionSDF;
        };

        struct FluidBlock
        {
            std::string id;
            Box box;
            unsigned char mode;
            Vector3r initialVelocity;
            Vector3r initialAngularVelocity;
        };

        struct EmitterData
        {
            std::string id;
            unsigned int width;
            unsigned int height;
            Vector3r x;
            Real velocity; // emission velocity
            Matrix3r rotation;
            Real emitStartTime;
            Real emitEndTime;
            unsigned int type;
        };

        struct AnimationFieldData
        {
            std::string particleFieldName;
            std::string expression[3];
            unsigned int shapeType;
            Vector3r x;
            Matrix3r rotation;
            Vector3r scale;
            Real startTime;
            Real endTime;
        };

        struct MaterialData
        {
            std::string id;
            std::string colorField;
            unsigned int colorMapType;
            Real minVal;
            Real maxVal;
            unsigned int maxEmitterParticles;
            bool emitterReuseParticles;
            Vector3r emitterBoxMin;
            Vector3r emitterBoxMax;
        };

        struct Scene
        {
            std::vector<BoundaryData*> boundaryModels;
            std::vector<FluidData*> fluidModels;
            std::vector<FluidBlock*> fluidBlocks;
            std::vector<EmitterData*> emitters;
            std::vector<AnimationFieldData*> animatedFields;
            std::vector<MaterialData*> materials;
            Real particleRadius;
            bool sim2D;
            Real timeStepSize;
            Vector3r camPosition;
            Vector3r camLookat;
        };

        void readScene(const char *fileName, Scene &scene);

        template <typename T>
        bool readValue(const nlohmann::json &j, T &v)
        {
            if (j.is_null())
                return false;

            v = j.get<T>();
            return true;
        }

        template <typename T, int size>
        bool readVector(const nlohmann::json &j, Eigen::Matrix<T, size, 1, Eigen::DontAlign> &vec)
        {
            if (j.is_null())
                return false;

            std::vector<T> values = j.get<std::vector<T>>();
            for (unsigned int i = 0; i < values.size(); i++)
                vec[i] = values[i];
            return true;
        }

        template <typename T>
        bool readValue(const std::string &section, const std::string &key, T &v)
        {
            if (m_jsonData.find(section) != m_jsonData.end())
            {
                nlohmann::json j = m_jsonData[section];
                if (j.is_null())
                    return false;

                nlohmann::json j2 = j[key];
                if (j2.is_null())
                    return false;

                v = j2.get<T>();
                return true;
            }
            return false;
        }

        bool hasValue(const std::string& section, const std::string& key)
        {
            if (m_jsonData.find(section) != m_jsonData.end())
            {
                nlohmann::json j = m_jsonData[section];
                if (j.is_null())
                    return false;

                nlohmann::json j2 = j[key];
                if (j2.is_null())
                    return false;

                return true;
            }
            return false;
        }

        template <typename T, int size>
        bool readVector(const std::string &section, const std::string &key, Eigen::Matrix<T, size, 1, Eigen::DontAlign> &vec)
        {
            if (m_jsonData.find(section) != m_jsonData.end())
            {
                nlohmann::json j = m_jsonData[section];
                if (j.is_null())
                    return false;

                nlohmann::json j2 = j[key];
                if (j2.is_null())
                    return false;

                std::vector<T> values = j2.get<std::vector<T>>();
                for (unsigned int i = 0; i < values.size(); i++)
                    vec[i] = values[i];
                return true;
            }
            return false;
        }

        void readMaterialParameterObject(const std::string& key, GenParam::ParameterObject* paramObj);
        void readParameterObject(const std::string &key, GenParam::ParameterObject *paramObj);
    };

    template <>
    bool SceneLoader::readValue<bool>(const nlohmann::json &j, bool &v);

}

#endif