MaterialX 1.39.1
Loading...
Searching...
No Matches
Unit.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_UNIT_H_
7#define MATERIALX_UNIT_H_
8
11
13
15
16MATERIALX_NAMESPACE_BEGIN
17
18class UnitConverter;
21
23using UnitConverterPtr = shared_ptr<UnitConverter>;
25using ConstUnitConverterPtr = shared_ptr<const UnitConverter>;
26
28using LinearUnitConverterPtr = shared_ptr<LinearUnitConverter>;
30using ConstLinearUnitConverterPtr = shared_ptr<const LinearUnitConverter>;
31
33using UnitConverterRegistryPtr = shared_ptr<UnitConverterRegistry>;
35using ConstUnitConverterRegistryPtr = shared_ptr<const UnitConverterRegistry>;
36
40class MX_CORE_API UnitConverter
41{
42 public:
43 UnitConverter() { }
44 virtual ~UnitConverter() { }
45
50 virtual float convert(float input, const string& inputUnit, const string& outputUnit) const = 0;
51
54 virtual int getUnitAsInteger(const string&) const { return -1; }
55
58 virtual string getUnitFromInteger(int) const { return EMPTY_STRING; }
59
64 virtual Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const = 0;
65
70 virtual Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const = 0;
71
76 virtual Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const = 0;
77
79 virtual void write(DocumentPtr doc) const = 0;
80};
81
84class MX_CORE_API LinearUnitConverter : public UnitConverter
85{
86 public:
87 virtual ~LinearUnitConverter() { }
88
91
93 const string& getUnitType() const
94 {
95 return _unitType;
96 }
97
99 void write(DocumentPtr doc) const override;
100
103
106 const std::unordered_map<string, float>& getUnitScale() const
107 {
108 return _unitScale;
109 }
110
114 float conversionRatio(const string& inputUnit, const string& outputUnit) const;
115
120 float convert(float input, const string& inputUnit, const string& outputUnit) const override;
121
126 Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const override;
127
132 Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const override;
133
138 Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const override;
139
143
146 int getUnitAsInteger(const string& unitName) const override;
147
150 virtual string getUnitFromInteger(int index) const override;
151
153
154 private:
156
157 private:
158 std::unordered_map<string, float> _unitScale;
159 std::unordered_map<string, int> _unitEnumeration;
160 string _unitType;
161};
162
164using UnitConverterPtrMap = std::unordered_map<string, UnitConverterPtr>;
165
168class MX_CORE_API UnitConverterRegistry
169{
170 public:
171 virtual ~UnitConverterRegistry() { }
172
175
179
183
187
190
193 int getUnitAsInteger(const string& unitName) const;
194
196 void write(DocumentPtr doc) const;
197
200 bool convertToUnit(DocumentPtr doc, const string& unitType, const string& targetUnit);
201
202 private:
205
206 UnitConverterRegistry& operator=(const UnitConverterRegistry&) = delete;
207
208 private:
209 UnitConverterPtrMap _unitConverters;
210};
211
212MATERIALX_NAMESPACE_END
213
214#endif
shared_ptr< UnitTypeDef > UnitTypeDefPtr
A shared pointer to a UnitTypeDef.
Definition: Definition.h:67
The top-level Document class.
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
Import and export declarations for the Core library.
shared_ptr< const LinearUnitConverter > ConstLinearUnitConverterPtr
A shared pointer to a const LinearUnitConverter.
Definition: Unit.h:30
shared_ptr< UnitConverter > UnitConverterPtr
A shared pointer to a UnitConverter.
Definition: Unit.h:23
shared_ptr< const UnitConverterRegistry > ConstUnitConverterRegistryPtr
A shared pointer to a const UnitConverterRegistry.
Definition: Unit.h:35
shared_ptr< const UnitConverter > ConstUnitConverterPtr
A shared pointer to a const UnitConverter.
Definition: Unit.h:25
shared_ptr< UnitConverterRegistry > UnitConverterRegistryPtr
A shared pointer to a UnitConverterRegistry.
Definition: Unit.h:33
std::unordered_map< string, UnitConverterPtr > UnitConverterPtrMap
Map of unit converters.
Definition: Unit.h:164
shared_ptr< LinearUnitConverter > LinearUnitConverterPtr
A shared pointer to a LinearUnitConverter.
Definition: Unit.h:28
A converter class for linear units that require only a scalar multiplication.
Definition: Unit.h:85
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.
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.
virtual string getUnitFromInteger(int index) const override
Given an integer index return the unit name in the map used by the converter.
int getUnitAsInteger(const string &unitName) const override
Given a unit name return a value that it can map to as an integer.
void write(DocumentPtr doc) const override
Create unit definitions in a document based on the converter.
static LinearUnitConverterPtr create(UnitTypeDefPtr UnitDef)
Creator.
float conversionRatio(const string &inputUnit, const string &outputUnit) const
Ratio between the given unit to a desired unit.
float convert(float input, const string &inputUnit, const string &outputUnit) const override
Convert a given value in a given unit to a desired unit.
Vector4 convert(const Vector4 &input, const string &inputUnit, const string &outputUnit) const override
Convert a given value in a given unit to a desired unit.
const string & getUnitType() const
Return the unit type string.
Definition: Unit.h:93
const std::unordered_map< string, float > & getUnitScale() const
Return the mappings from unit names to the scale value defined by a linear converter.
Definition: Unit.h:106
An abstract base class for unit converters.
Definition: Unit.h:41
virtual string getUnitFromInteger(int) const
Given an integer index return the unit name in the map used by the converter Returns Empty string if ...
Definition: Unit.h:58
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.
virtual float convert(float input, const string &inputUnit, const string &outputUnit) const =0
Convert a given value in a given unit to a desired unit.
virtual int getUnitAsInteger(const string &) const
Given a unit name return a value that it can map to as an integer Returns -1 value if not found.
Definition: Unit.h:54
virtual Vector4 convert(const Vector4 &input, const string &inputUnit, const string &outputUnit) const =0
Convert a given value in a given unit to a desired unit.
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.
virtual void write(DocumentPtr doc) const =0
Create unit definitions in a document based on the converter.
A registry for unit converters.
Definition: Unit.h:169
bool convertToUnit(DocumentPtr doc, const string &unitType, const string &targetUnit)
Convert input values which have a source unit to a given target unit.
int getUnitAsInteger(const string &unitName) const
Given a unit name return a value that it can map to as an integer Returns -1 value if not found.
UnitConverterPtr getUnitConverter(UnitTypeDefPtr def)
Get a unit converter for a given UnitDef Returns any empty pointer if a converter does not exist for ...
void write(DocumentPtr doc) const
Create unit definitions in a document based on registered converters.
static UnitConverterRegistryPtr create()
Creator.
bool removeUnitConverter(UnitTypeDefPtr def)
Remove a unit converter for a given UnitDef.
void clearUnitConverters()
Clear all unit converters from the registry.
bool addUnitConverter(UnitTypeDefPtr def, UnitConverterPtr converter)
Add a unit converter for a given UnitDef.
A unit definition element within a Document.
Definition: Definition.h:457
A vector of two floating-point values.
Definition: Types.h:286
A vector of three floating-point values.
Definition: Types.h:306
A vector of four floating-point values.
Definition: Types.h:328