HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Unit classes
11 
12 #include <MaterialXCore/Export.h>
13 
14 #include <MaterialXCore/Document.h>
15 
17 
18 class UnitConverter;
21 
22 /// A shared pointer to a UnitConverter
23 using UnitConverterPtr = shared_ptr<UnitConverter>;
24 /// A shared pointer to a const UnitConverter
25 using ConstUnitConverterPtr = shared_ptr<const UnitConverter>;
26 
27 /// A shared pointer to a LinearUnitConverter
28 using LinearUnitConverterPtr = shared_ptr<LinearUnitConverter>;
29 /// A shared pointer to a const LinearUnitConverter
30 using ConstLinearUnitConverterPtr = shared_ptr<const LinearUnitConverter>;
31 
32 /// A shared pointer to a UnitConverterRegistry
33 using UnitConverterRegistryPtr = shared_ptr<UnitConverterRegistry>;
34 /// A shared pointer to a const UnitConverterRegistry
35 using ConstUnitConverterRegistryPtr = shared_ptr<const UnitConverterRegistry>;
36 
37 /// @class UnitConverter
38 /// An abstract base class for unit converters.
39 /// Each unit converter instance is responsible for a single unit type.
41 {
42  public:
44  virtual ~UnitConverter() { }
45 
46  /// Convert a given value in a given unit to a desired unit
47  /// @param input Input value to convert
48  /// @param inputUnit Unit of input value
49  /// @param outputUnit Unit for output value
50  virtual float convert(float input, const string& inputUnit, const string& outputUnit) const = 0;
51 
52  /// Given a unit name return a value that it can map to as an integer
53  /// Returns -1 value if not found
54  virtual int getUnitAsInteger(const string&) const { return -1; }
55 
56  /// Given an integer index return the unit name in the map used by the converter
57  /// Returns Empty string if not found
58  virtual string getUnitFromInteger(int) const { return EMPTY_STRING; }
59 
60  /// Convert a given value in a given unit to a desired unit
61  /// @param input Input value to convert
62  /// @param inputUnit Unit of input value
63  /// @param outputUnit Unit for output value
64  virtual Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const = 0;
65 
66  /// Convert a given value in a given unit to a desired unit
67  /// @param input Input value to convert
68  /// @param inputUnit Unit of input value
69  /// @param outputUnit Unit for output value
70  virtual Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const = 0;
71 
72  /// Convert a given value in a given unit to a desired unit
73  /// @param input Input value to convert
74  /// @param inputUnit Unit of input value
75  /// @param outputUnit Unit for output value
76  virtual Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const = 0;
77 
78  /// Create unit definitions in a document based on the converter
79  virtual void write(DocumentPtr doc) const = 0;
80 };
81 
82 /// @class LinearUnitConverter
83 /// A converter class for linear units that require only a scalar multiplication.
85 {
86  public:
87  virtual ~LinearUnitConverter() { }
88 
89  /// Creator
91 
92  /// Return the unit type string
93  const string& getUnitType() const
94  {
95  return _unitType;
96  }
97 
98  /// Create unit definitions in a document based on the converter
99  void write(DocumentPtr doc) const override;
100 
101  /// @name Conversion
102  /// @{
103 
104  /// Return the mappings from unit names to the scale value
105  /// defined by a linear converter.
106  const std::unordered_map<string, float>& getUnitScale() const
107  {
108  return _unitScale;
109  }
110 
111  /// Ratio between the given unit to a desired unit
112  /// @param inputUnit Unit of input value
113  /// @param outputUnit Unit for output value
114  float conversionRatio(const string& inputUnit, const string& outputUnit) const;
115 
116  /// Convert a given value in a given unit to a desired unit
117  /// @param input Input value to convert
118  /// @param inputUnit Unit of input value
119  /// @param outputUnit Unit for output value
120  float convert(float input, const string& inputUnit, const string& outputUnit) const override;
121 
122  /// Convert a given value in a given unit to a desired unit
123  /// @param input Input value to convert
124  /// @param inputUnit Unit of input value
125  /// @param outputUnit Unit for output value
126  Vector2 convert(const Vector2& input, const string& inputUnit, const string& outputUnit) const override;
127 
128  /// Convert a given value in a given unit to a desired unit
129  /// @param input Input value to convert
130  /// @param inputUnit Unit of input value
131  /// @param outputUnit Unit for output value
132  Vector3 convert(const Vector3& input, const string& inputUnit, const string& outputUnit) const override;
133 
134  /// Convert a given value in a given unit to a desired unit
135  /// @param input Input value to convert
136  /// @param inputUnit Unit of input value
137  /// @param outputUnit Unit for output value
138  Vector4 convert(const Vector4& input, const string& inputUnit, const string& outputUnit) const override;
139 
140  /// @}
141  /// @name Shader Mapping
142  /// @{
143 
144  /// Given a unit name return a value that it can map to as an integer.
145  /// Returns -1 value if not found
146  int getUnitAsInteger(const string& unitName) const override;
147 
148  /// Given an integer index return the unit name in the map used by the converter.
149  /// Returns Empty string if not found
150  virtual string getUnitFromInteger(int index) const override;
151 
152  /// @}
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 
163 /// Map of unit converters
164 using UnitConverterPtrMap = std::unordered_map<string, UnitConverterPtr>;
165 
166 /// @class UnitConverterRegistry
167 /// A registry for unit converters.
169 {
170  public:
172 
173  /// Creator
174  static UnitConverterRegistryPtr create();
175 
176  /// Add a unit converter for a given UnitDef.
177  /// Returns false if a converter has already been registered for the given UnitDef
178  bool addUnitConverter(UnitTypeDefPtr def, UnitConverterPtr converter);
179 
180  /// Remove a unit converter for a given UnitDef.
181  /// Returns false if a converter does not exist for the given UnitDef
182  bool removeUnitConverter(UnitTypeDefPtr def);
183 
184  /// Get a unit converter for a given UnitDef
185  /// Returns any empty pointer if a converter does not exist for the given UnitDef
186  UnitConverterPtr getUnitConverter(UnitTypeDefPtr def);
187 
188  /// Clear all unit converters from the registry.
189  void clearUnitConverters();
190 
191  /// Given a unit name return a value that it can map to as an integer
192  /// Returns -1 value if not found
193  int getUnitAsInteger(const string& unitName) const;
194 
195  /// Create unit definitions in a document based on registered converters
196  void write(DocumentPtr doc) const;
197 
198  /// Convert input values which have a source unit to a given target unit.
199  /// Returns if any unit conversion occured.
200  bool convertToUnit(DocumentPtr doc, const string& unitType, const string& targetUnit);
201 
202  private:
205 
207 
208  private:
209  UnitConverterPtrMap _unitConverters;
210 };
211 
213 
214 #endif
virtual void write(DocumentPtr doc) const =0
Create unit definitions in a document based on the converter.
std::unordered_map< string, UnitConverterPtr > UnitConverterPtrMap
Map of unit converters.
Definition: Unit.h:164
shared_ptr< UnitConverter > UnitConverterPtr
A shared pointer to a UnitConverter.
Definition: Unit.h:23
virtual string getUnitFromInteger(int) const
Definition: Unit.h:58
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
UnitConverter()
Definition: Unit.h:43
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
Tto convert(const Tfrom &source)
shared_ptr< const LinearUnitConverter > ConstLinearUnitConverterPtr
A shared pointer to a const LinearUnitConverter.
Definition: Unit.h:30
#define MX_CORE_API
Definition: Export.h:18
shared_ptr< UnitTypeDef > UnitTypeDefPtr
A shared pointer to a UnitTypeDef.
Definition: Definition.h:67
const std::unordered_map< string, float > & getUnitScale() const
Definition: Unit.h:106
virtual ~UnitConverter()
Definition: Unit.h:44
virtual ~UnitConverterRegistry()
Definition: Unit.h:171
Definition: Types.h:327
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
Definition: Types.h:285
const string & getUnitType() const
Return the unit type string.
Definition: Unit.h:93
shared_ptr< const UnitConverterRegistry > ConstUnitConverterRegistryPtr
A shared pointer to a const UnitConverterRegistry.
Definition: Unit.h:35
shared_ptr< UnitConverterRegistry > UnitConverterRegistryPtr
A shared pointer to a UnitConverterRegistry.
Definition: Unit.h:33
shared_ptr< const UnitConverter > ConstUnitConverterPtr
A shared pointer to a const UnitConverter.
Definition: Unit.h:25
virtual float convert(float input, const string &inputUnit, const string &outputUnit) const =0
Definition: Types.h:305
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
virtual ~LinearUnitConverter()
Definition: Unit.h:87
shared_ptr< LinearUnitConverter > LinearUnitConverterPtr
A shared pointer to a LinearUnitConverter.
Definition: Unit.h:28
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
virtual int getUnitAsInteger(const string &) const
Definition: Unit.h:54