forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitConverter.h
200 lines (156 loc) · 6.97 KB
/
UnitConverter.h
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
//
// TM & (c) 2019 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_UNITCONVERTER_H_
#define MATERIALX_UNITCONVERTER_H_
/// @file
/// Unit converter classes
#include <MaterialXGenShader/Library.h>
#include <MaterialXCore/Definition.h>
#include <MaterialXCore/Types.h>
namespace MaterialX
{
class UnitConverter;
class LinearUnitConverter;
class UnitConverterRegistry;
/// A shared pointer to a UnitConverter
using UnitConverterPtr = shared_ptr<UnitConverter>;
/// A shared pointer to a const UnitConverter
using ConstUnitConverterPtr = shared_ptr<const UnitConverter>;
/// A shared pointer to a LinearUnitConverter
using LinearUnitConverterPtr = shared_ptr<LinearUnitConverter>;
/// A shared pointer to a const LinearUnitConverter
using ConstLinearUnitConverterPtr = shared_ptr<const LinearUnitConverter>;
/// A shared pointer to a UnitConverterRegistry
using UnitConverterRegistryPtr = shared_ptr<UnitConverterRegistry>;
/// A shared pointer to a const UnitConverterRegistry
using ConstUnitConverterRegistryPtr = shared_ptr<const UnitConverterRegistry>;
/// @class UnitConverter
/// An abstract base class for unit converters.
/// Each unit converter instance is responsible for a single unit type.
class UnitConverter
{
public:
UnitConverter() { }
virtual ~UnitConverter() { }
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
virtual float convert(float input, const string& inputUnit, const string& outputUnit) const = 0;
/// Given a unit name return a value that it can map to as an integer
/// Returns -1 value if not found
virtual int getUnitAsInteger(const string&) const { return -1; }
/// Given an integer index return the unit name in the map used by the converter
/// Returns Empty string if not found
virtual string getUnitFromInteger(int) const { return EMPTY_STRING; }
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
virtual Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const = 0;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
virtual Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const = 0;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
virtual Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const = 0;
};
/// @class LinearUnitConverter
/// A converter class for linear units that require only a scalar multiplication.
class LinearUnitConverter : public UnitConverter
{
public:
virtual ~LinearUnitConverter() { }
/// Creator
static LinearUnitConverterPtr create(UnitTypeDefPtr UnitDef);
/// Return the unit type string
const string& getUnitType() const
{
return _unitType;
}
/// @name Conversion
/// @{
/// Return the mappings from unit names to the scale value
/// defined by a linear converter.
const std::unordered_map<string, float>& getUnitScale() const
{
return _unitScale;
}
/// Ratio between the given unit to a desired unit
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
float conversionRatio(const string& inputUnit, const string& outputUnit) const;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
float convert(float input, const string& inputUnit, const string& outputUnit) const override;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const override;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const override;
/// Convert a given value in a given unit to a desired unit
/// @param input Input value to convert
/// @param inputUnit Unit of input value
/// @param outputUnit Unit for output value
Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const override;
/// @}
/// @name Shader Mapping
/// @{
/// Given a unit name return a value that it can map to as an integer.
/// Returns -1 value if not found
int getUnitAsInteger(const string& unitName) const override;
/// Given an integer index return the unit name in the map used by the converter.
/// Returns Empty string if not found
virtual string getUnitFromInteger(int index) const override;
/// @}
private:
LinearUnitConverter(UnitTypeDefPtr UnitDef);
private:
std::unordered_map<string, float> _unitScale;
std::unordered_map<string, int> _unitEnumeration;
string _unitType;
};
/// @class UnitConverterRegistry
/// A registry for unit converters.
class UnitConverterRegistry
{
public:
virtual ~UnitConverterRegistry() { }
/// Creator
static UnitConverterRegistryPtr create();
/// Add a unit converter for a given UnitDef.
/// Returns false if a converter has already been registered for the given UnitDef
bool addUnitConverter(UnitTypeDefPtr def, UnitConverterPtr converter);
/// Remove a unit converter for a given UnitDef.
/// Returns false if a converter does not exist for the given UnitDef
bool removeUnitConverter(UnitTypeDefPtr def);
/// Get a unit converter for a given UnitDef
/// Returns any empty pointer if a converter does not exist for the given UnitDef
UnitConverterPtr getUnitConverter(UnitTypeDefPtr def);
/// Clear all unit converters from the registry.
void clearUnitConverters();
/// Given a unit name return a value that it can map to as an integer
/// Returns -1 value if not found
int getUnitAsInteger(const string& unitName) const;
private:
UnitConverterRegistry(const UnitConverterRegistry&) = delete;
UnitConverterRegistry() { }
UnitConverterRegistry& operator=(const UnitConverterRegistry&) = delete;
private:
std::unordered_map<string, UnitConverterPtr> _unitConverters;
};
} // namespace MaterialX
#endif