paramdict.h 8.6 KB
Newer Older
M
Matt Pharr 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
// pbrt is Copyright(c) 1998-2020 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
// The pbrt source code is licensed under the Apache License, Version 2.0.
// SPDX: Apache-2.0

#ifndef PBRT_PARAMDICT_H
#define PBRT_PARAMDICT_H

#include <pbrt/pbrt.h>

#include <pbrt/base/texture.h>
#include <pbrt/parser.h>
#include <pbrt/util/containers.h>
#include <pbrt/util/error.h>
#include <pbrt/util/memory.h>
#include <pbrt/util/pstd.h>
#include <pbrt/util/spectrum.h>
#include <pbrt/util/vecmath.h>

#include <limits>
#include <map>
#include <memory>
#include <string>
#include <vector>

namespace pbrt {

// ParameterType Definition
enum class ParameterType {
    Boolean,
    Float,
    Integer,
    Point2f,
    Vector2f,
    Point3f,
    Vector3f,
    Normal3f,
    Spectrum,
    String,
    Texture
};

// SpectrumType Definition
enum class SpectrumType { Reflectance, General };

template <ParameterType PT>
struct ParameterTypeTraits {};

// ParameterDictionary Definition
class ParameterDictionary {
  public:
    // ParameterDictionary Public Methods
    ParameterDictionary() = default;
    ParameterDictionary(ParsedParameterVector params, const RGBColorSpace *colorSpace);

    ParameterDictionary(ParsedParameterVector params0,
                        const ParsedParameterVector &params1,
                        const RGBColorSpace *colorSpace);

    std::string GetTexture(const std::string &name) const;

    std::vector<RGB> GetRGBArray(const std::string &name) const;

    // For --upgrade only
    pstd::optional<RGB> GetOneRGB(const std::string &name) const;
    void RemoveFloat(const std::string &);
    void RemoveInt(const std::string &);
    void RemoveBool(const std::string &);
    void RemovePoint2f(const std::string &);
    void RemoveVector2f(const std::string &);
    void RemovePoint3f(const std::string &);
    void RemoveVector3f(const std::string &);
    void RemoveNormal3f(const std::string &);
    void RemoveString(const std::string &);
    void RemoveTexture(const std::string &);
    void RemoveSpectrum(const std::string &);

    void RenameParameter(const std::string &before, const std::string &after);
    void RenameUsedTextures(const std::map<std::string, std::string> &m);

    const RGBColorSpace *ColorSpace() const { return colorSpace; }

    std::string ToParameterList(int indent = 0) const;
    std::string ToParameterDefinition(const std::string &) const;
    std::string ToString() const;

    const FileLoc *loc(const std::string &) const;

    Float GetOneFloat(const std::string &name, Float def) const;
    int GetOneInt(const std::string &name, int def) const;
    bool GetOneBool(const std::string &name, bool def) const;
    Point2f GetOnePoint2f(const std::string &name, const Point2f &def) const;
    Vector2f GetOneVector2f(const std::string &name, const Vector2f &def) const;
    Point3f GetOnePoint3f(const std::string &name, const Point3f &def) const;
    Vector3f GetOneVector3f(const std::string &name, const Vector3f &def) const;
    Normal3f GetOneNormal3f(const std::string &name, const Normal3f &def) const;
    std::string GetOneString(const std::string &name, const std::string &def) const;

    SpectrumHandle GetOneSpectrum(const std::string &name, SpectrumHandle def,
                                  SpectrumType spectrumType, Allocator alloc) const;

    std::vector<Float> GetFloatArray(const std::string &name) const;
    std::vector<int> GetIntArray(const std::string &name) const;
    std::vector<uint8_t> GetBoolArray(const std::string &name) const;
    std::vector<Point2f> GetPoint2fArray(const std::string &name) const;
    std::vector<Vector2f> GetVector2fArray(const std::string &name) const;
    std::vector<Point3f> GetPoint3fArray(const std::string &name) const;
    std::vector<Vector3f> GetVector3fArray(const std::string &name) const;
    std::vector<Normal3f> GetNormal3fArray(const std::string &name) const;
    std::vector<SpectrumHandle> GetSpectrumArray(const std::string &name,
                                                 SpectrumType spectrumType,
                                                 Allocator alloc) const;
    std::vector<std::string> GetStringArray(const std::string &name) const;

    void ReportUnused() const;

  private:
    friend class TextureParameterDictionary;
    // ParameterDictionary Private Methods
    template <ParameterType PT>
    typename ParameterTypeTraits<PT>::ReturnType lookupSingle(
        const std::string &name,
        typename ParameterTypeTraits<PT>::ReturnType defaultValue) const;

    template <ParameterType PT>
    std::vector<typename ParameterTypeTraits<PT>::ReturnType> lookupArray(
        const std::string &name) const;

    template <typename ReturnType, typename G, typename C>
    std::vector<ReturnType> lookupArray(const std::string &name, ParameterType type,
                                        const char *typeName, int nPerItem, G getValues,
                                        C convert) const;

    std::vector<SpectrumHandle> extractSpectrumArray(const ParsedParameter &param,
                                                     SpectrumType spectrumType,
                                                     Allocator alloc) const;

    void remove(const std::string &name, const char *typeName);
    void checkParameterTypes();
    static std::string ToParameterDefinition(const ParsedParameter *p, int indentCount);

    // ParameterDictionary Private Members
    ParsedParameterVector params;
    const RGBColorSpace *colorSpace = nullptr;
};

// TextureParameterDictionary Definition
class TextureParameterDictionary {
  public:
    // TextureParameterDictionary Public Methods
    TextureParameterDictionary(
        const ParameterDictionary *dict,
        const std::map<std::string, FloatTextureHandle> *floatTextures,
        const std::map<std::string, SpectrumTextureHandle> *spectrumTextures);

    operator const ParameterDictionary &() const { return *dict; }

    Float GetOneFloat(const std::string &name, Float def) const;
    int GetOneInt(const std::string &name, int def) const;
    bool GetOneBool(const std::string &name, bool def) const;
    Point2f GetOnePoint2f(const std::string &name, const Point2f &def) const;
    Vector2f GetOneVector2f(const std::string &name, const Vector2f &def) const;
    Point3f GetOnePoint3f(const std::string &name, const Point3f &def) const;
    Vector3f GetOneVector3f(const std::string &name, const Vector3f &def) const;
    Normal3f GetOneNormal3f(const std::string &name, const Normal3f &def) const;
    SpectrumHandle GetOneSpectrum(const std::string &name, SpectrumHandle def,
                                  SpectrumType spectrumType, Allocator alloc) const;
    std::string GetOneString(const std::string &name, const std::string &def) const;

    std::vector<Float> GetFloatArray(const std::string &name) const;
    std::vector<int> GetIntArray(const std::string &name) const;
    std::vector<uint8_t> GetBoolArray(const std::string &name) const;
    std::vector<Point2f> GetPoint2fArray(const std::string &name) const;
    std::vector<Vector2f> GetVector2fArray(const std::string &name) const;
    std::vector<Point3f> GetPoint3fArray(const std::string &name) const;
    std::vector<Vector3f> GetVector3fArray(const std::string &name) const;
    std::vector<Normal3f> GetNormal3fArray(const std::string &name) const;
    std::vector<SpectrumHandle> GetSpectrumArray(const std::string &name,
                                                 SpectrumType spectrumType,
                                                 Allocator alloc) const;
    std::vector<std::string> GetStringArray(const std::string &name) const;

    FloatTextureHandle GetFloatTexture(const std::string &name, Float defaultValue,
                                       Allocator alloc) const;
    FloatTextureHandle GetFloatTextureOrNull(const std::string &name,
                                             Allocator alloc) const;

    void ReportUnused() const;

    SpectrumTextureHandle GetSpectrumTexture(const std::string &name,
                                             SpectrumHandle defaultValue,
                                             SpectrumType spectrumType,
                                             Allocator alloc) const;
    SpectrumTextureHandle GetSpectrumTextureOrNull(const std::string &name,
                                                   SpectrumType spectrumType,
                                                   Allocator alloc) const;

  private:
    // TextureParameterDictionary Private Members
    const ParameterDictionary *dict;
    const std::map<std::string, FloatTextureHandle> *floatTextures;
    const std::map<std::string, SpectrumTextureHandle> *spectrumTextures;
};

}  // namespace pbrt

#endif  // PBRT_PARAMDICT_H