HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XUSD_Format.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * NAME: XUSD_Format.h (HUSD Library, C++)
17  *
18  * COMMENTS:
19  */
20 
21 #ifndef __XUSD_Format__
22 #define __XUSD_Format__
23 
24 /// @file Implementations for UTformat() style printing
25 
26 #include "HUSD_Path.h"
27 #include <pxr/base/tf/token.h>
28 #include <pxr/base/gf/matrix2f.h>
29 #include <pxr/base/gf/matrix2d.h>
30 #include <pxr/base/gf/matrix3f.h>
31 #include <pxr/base/gf/matrix3d.h>
32 #include <pxr/base/gf/matrix4f.h>
33 #include <pxr/base/gf/matrix4d.h>
34 #include <pxr/base/gf/vec2i.h>
35 #include <pxr/base/gf/vec2h.h>
36 #include <pxr/base/gf/vec2f.h>
37 #include <pxr/base/gf/vec2d.h>
38 #include <pxr/base/gf/vec3i.h>
39 #include <pxr/base/gf/vec3h.h>
40 #include <pxr/base/gf/vec3f.h>
41 #include <pxr/base/gf/vec3d.h>
42 #include <pxr/base/gf/vec4i.h>
43 #include <pxr/base/gf/vec4h.h>
44 #include <pxr/base/gf/vec4f.h>
45 #include <pxr/base/gf/vec4d.h>
46 #include <pxr/base/gf/quath.h>
47 #include <pxr/base/gf/quatf.h>
48 #include <pxr/base/gf/quatd.h>
49 #include <pxr/base/gf/range1f.h>
50 #include <pxr/base/gf/range1d.h>
51 #include <pxr/base/vt/array.h>
52 #include <pxr/base/vt/value.h>
53 #include <pxr/usd/sdf/path.h>
54 #include <pxr/usd/sdf/assetPath.h>
55 #include <pxr/usd/sdf/timeCode.h>
56 #include <pxr/imaging/hd/types.h>
57 #include <UT/UT_Format.h>
58 #include <UT/UT_WorkBuffer.h>
59 #include <UT/UT_StringStream.h>
60 #include "XUSD_HydraUtils.h"
61 
63 
64 /// Format for a TfToken
65 #define FORMAT_VAL(TYPE, GET_VAL)\
66 static SYS_FORCE_INLINE size_t \
67 format(char *buffer, size_t bufsize, const TYPE &val) \
68 { \
69  UT::Format::Writer writer(buffer, bufsize); \
70  UT::Format::Formatter f; \
71  return f.format(writer, "{}", {val.GET_VAL()}); \
72 }; \
73 /* end of macro */
74 
75 FORMAT_VAL(TfToken, GetString);
76 FORMAT_VAL(SdfAssetPath, GetAssetPath);
77 FORMAT_VAL(SdfTimeCode, GetValue);
78 
79 #undef FORMAT_VAL
80 
81 static SYS_FORCE_INLINE size_t
82 format(char *buffer, size_t bufsize, const SdfPath &val)
83 {
84  UT::Format::Writer writer(buffer, bufsize);
86  // Avoid calling SdfPath::GetString() as will cache the std::string forever
87  return f.format(writer, "{}", {HUSD_Path(val).pathStr()});
88 }
89 
90 static SYS_FORCE_INLINE size_t
91 format(char *buffer, size_t bufsize, HdFormat val)
92 {
93  UT::Format::Writer writer(buffer, bufsize);
95  const char *tname = nullptr;
96  size_t size = HdGetComponentCount(val);
97  switch (HdGetComponentFormat(val))
98  {
99  case HdFormatUNorm8: tname = "uint8"; break;
100  case HdFormatSNorm8: tname = "int8"; break;
101  case HdFormatUInt16: tname = "uint16"; break;
102  case HdFormatInt16: tname = "int16"; break;
103  case HdFormatFloat16: tname = "fpreal16"; break;
104  case HdFormatFloat32: tname = "fpreal32"; break;
105  case HdFormatInt32: tname = "int32"; break;
106  default: tname = "<undefined_type>"; break;
107  }
108  return f.format(writer, "{}[{}]", {tname, size});
109 }
110 
111 namespace
112 {
113  template <typename T>
114  static SYS_FORCE_INLINE size_t
115  formatVector(char *buffer, size_t bufsize, const T *data, size_t size)
116  {
117  UT_WorkBuffer tmp;
118  if (size)
119  {
120  tmp.format("{}", data[0]);
121  for (size_t i = 1; i < size; ++i)
122  tmp.appendFormat(", {}", data[i]);
123  }
124  UT::Format::Writer writer(buffer, bufsize);
126  return f.format(writer, "[{}]", {tmp});
127  }
128 
129  template <> size_t
130  formatVector<GfHalf>(char *buffer, size_t bufsize,
131  const GfHalf *data, size_t size)
132  {
133  UT_WorkBuffer tmp;
134  if (size)
135  {
136  tmp.format("{}", float(data[0]));
137  for (size_t i = 1; i < size; ++i)
138  tmp.appendFormat(", {}", float(data[i]));
139  }
140  UT::Format::Writer writer(buffer, bufsize);
142  return f.format(writer, "[{}]", {tmp});
143  }
144 
145  template <typename T>
146  static SYS_FORCE_INLINE size_t
147  formatIterator(char *buffer, size_t bufsize, T begin, const T &end)
148  {
149  UT_WorkBuffer tmp;
150  if (begin != end)
151  {
152  tmp.format("{}", *begin);
153  for (++begin; begin != end; ++begin)
154  tmp.appendFormat(", {}", *begin);
155  }
156  UT::Format::Writer writer(buffer, bufsize);
158  return f.format(writer, "[{}]", {tmp});
159  }
160 }
161 
162 
163 static SYS_FORCE_INLINE size_t
164 format(char *buffer, size_t bufsize, const TfTokenVector &vtok)
165 {
166  return formatVector(buffer, bufsize, &vtok[0], vtok.size());
167 }
168 
169 #define FORMAT_TYPE(TYPE, METHOD, SIZE) \
170  static SYS_FORCE_INLINE size_t \
171  format(char *buffer, size_t bufsize, const TYPE &val) \
172  { return formatVector(buffer, bufsize, val.METHOD(), SIZE); }
173 
174 FORMAT_TYPE(GfVec2h, data, 2)
175 FORMAT_TYPE(GfVec2i, data, 2)
176 FORMAT_TYPE(GfVec2f, data, 2)
177 FORMAT_TYPE(GfVec2d, data, 2)
178 FORMAT_TYPE(GfVec3h, data, 3)
179 FORMAT_TYPE(GfVec3i, data, 3)
180 FORMAT_TYPE(GfVec3f, data, 3)
181 FORMAT_TYPE(GfVec3d, data, 3)
182 FORMAT_TYPE(GfVec4h, data, 4)
183 FORMAT_TYPE(GfVec4i, data, 4)
184 FORMAT_TYPE(GfVec4f, data, 4)
185 FORMAT_TYPE(GfVec4d, data, 4)
186 
187 FORMAT_TYPE(GfMatrix2f, GetArray, 4)
188 FORMAT_TYPE(GfMatrix2d, GetArray, 4)
189 FORMAT_TYPE(GfMatrix3f, GetArray, 9)
190 FORMAT_TYPE(GfMatrix3d, GetArray, 9)
191 FORMAT_TYPE(GfMatrix4f, GetArray, 16)
192 FORMAT_TYPE(GfMatrix4d, GetArray, 16)
193 
194 #undef FORMAT_TYPE
195 
196 template <typename T>
197 static SYS_FORCE_INLINE size_t
198 format(char *buffer, size_t bufsize, const VtArray<T> &arr)
199 {
200  return formatIterator(buffer, bufsize, arr.begin(), arr.end());
201 }
202 
203 template <typename T>
204 static SYS_FORCE_INLINE size_t
205 format(char *buffer, size_t bufsize, const VtValue &val)
206 {
207  UT::Format::Writer writer(buffer, bufsize);
209  return f.format(writer, "{}", val.Get<T>());
210 }
211 
212 static SYS_FORCE_INLINE size_t
213 format(char *buffer, size_t bufsize, const VtValue &val)
214 {
215  UT::Format::Writer writer(buffer, bufsize);
217  UT_OStringStream os;
218  os << val << std::ends;
219  return f.format(writer, "{}", {os.str()});
220 }
221 
222 static SYS_FORCE_INLINE size_t
223 format(char *buffer, size_t bufsize, const GfRange1f &r)
224 {
225  UT::Format::Writer writer(buffer, bufsize);
227  return f.format(writer, "[{0}, {1}]", {r.GetMin(), r.GetMax()});
228 }
229 
230 static SYS_FORCE_INLINE size_t
231 format(char *buffer, size_t bufsize, const GfRange1d &r)
232 {
233  UT::Format::Writer writer(buffer, bufsize);
235  return f.format(writer, "[{0}, {1}]", {r.GetMin(), r.GetMax()});
236 }
237 
238 namespace
239 {
240  template <typename T>
241  static SYS_FORCE_INLINE size_t
242  formatQuat(char *buffer, size_t bufsize, const T &q)
243  {
244  const auto &ii = q.GetImaginary();
245  UT::Format::Writer writer(buffer, bufsize);
247  return f.format(writer, "{0}+({1},{2},{3})i",
248  {q.GetReal(), ii[0], ii[1], ii[2]});
249  }
250 }
251 
252 static SYS_FORCE_INLINE size_t
253 format(char *buffer, size_t bufsize, const GfQuatd &q)
254 {
255  return formatQuat(buffer, bufsize, q);
256 }
257 static SYS_FORCE_INLINE size_t
258 format(char *buffer, size_t bufsize, const GfQuatf &q)
259 {
260  return formatQuat(buffer, bufsize, q);
261 }
262 static SYS_FORCE_INLINE size_t
263 format(char *buffer, size_t bufsize, const GfQuath &q)
264 {
265  // Specialization for GfHalf
266  return formatQuat(buffer, bufsize, GfQuatf(q.GetReal(), q.GetImaginary()));
267 }
268 
269 
271 
272 #endif
Definition: quath.h:43
Definition: vec4i.h:43
double GetMin() const
Returns the minimum value of the range.
Definition: range1d.h:78
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
HdFormat
Definition: types.h:408
Definition: vec2i.h:43
GLboolean * data
Definition: glcorearb.h:131
Definition: vec3f.h:45
Definition: vec4d.h:45
An output stream object that owns its own string buffer storage.
GLdouble GLdouble GLdouble q
Definition: glad.h:2445
const UT_WorkBuffer & str()
Returns a read-only reference to the underlying UT_WorkBuffer.
GLuint buffer
Definition: glcorearb.h:660
Definition: quatf.h:42
Definition: vec2d.h:45
GfHalf GetReal() const
Return the real coefficient.
Definition: quath.h:89
Definition: vec4h.h:46
T const & Get() const &
Definition: value.h:1121
size_t appendFormat(const char *fmt, const Args &...args)
GLfloat f
Definition: glcorearb.h:1926
Definition: token.h:70
double GetMax() const
Returns the maximum value of the range.
Definition: range1d.h:81
#define FORMAT_TYPE(TYPE, METHOD, SIZE)
Definition: XUSD_Format.h:169
Definition: vec2h.h:46
GLuint GLuint end
Definition: glcorearb.h:475
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
const GfVec3h & GetImaginary() const
Return the imaginary coefficient.
Definition: quath.h:95
void pathStr(UT_WorkBuffer &outpath) const
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
Definition: vec3i.h:43
Definition: types.h:153
Definition: path.h:273
float GetMin() const
Returns the minimum value of the range.
Definition: range1f.h:78
Definition: vec4f.h:45
GLsizeiptr size
Definition: glcorearb.h:664
size_t format(const char *fmt, const Args &...args)
HD_API HdFormat HdGetComponentFormat(HdFormat f)
Return the single-channel version of a given format.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_API size_t HdGetComponentCount(HdType t)
float GetMax() const
Returns the maximum value of the range.
Definition: range1f.h:81
Definition: vec2f.h:45
Definition: vec3d.h:45
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Type-safe formatting, modeled on the Python str.format function.
GLboolean r
Definition: glcorearb.h:1222
Definition: quatd.h:42
size_t format(W &writer, const char *format, std::initializer_list< ArgValue > args)
Definition: vec3h.h:46
Definition: value.h:146
Definition: format.h:1821
#define FORMAT_VAL(TYPE, GET_VAL)
Format for a TfToken.
Definition: XUSD_Format.h:65
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566