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 
62 
63 /// Format for a TfToken
64 #define FORMAT_VAL(TYPE, GET_VAL)\
65 static SYS_FORCE_INLINE size_t \
66 format(char *buffer, size_t bufsize, const TYPE &val) \
67 { \
68  UT::Format::Writer writer(buffer, bufsize); \
69  UT::Format::Formatter f; \
70  return f.format(writer, "{}", {val.GET_VAL()}); \
71 }; \
72 /* end of macro */
73 
74 FORMAT_VAL(TfToken, GetString);
75 FORMAT_VAL(SdfAssetPath, GetAssetPath);
76 FORMAT_VAL(SdfTimeCode, GetValue);
77 
78 #undef FORMAT_VAL
79 
80 static SYS_FORCE_INLINE size_t
81 format(char *buffer, size_t bufsize, const SdfPath &val)
82 {
83  UT::Format::Writer writer(buffer, bufsize);
85  // Avoid calling SdfPath::GetString() as will cache the std::string forever
86  return f.format(writer, "{}", {HUSD_Path(val).pathStr()});
87 }
88 
89 static SYS_FORCE_INLINE size_t
90 format(char *buffer, size_t bufsize, HdFormat val)
91 {
92  UT::Format::Writer writer(buffer, bufsize);
94  const char *tname = nullptr;
95  size_t size = HdGetComponentCount(val);
96  switch (HdGetComponentFormat(val))
97  {
98  case HdFormatUNorm8: tname = "uint8"; break;
99  case HdFormatSNorm8: tname = "int8"; break;
100  case HdFormatFloat16: tname = "fpreal16"; break;
101  case HdFormatFloat32: tname = "fpreal32"; break;
102  case HdFormatInt32: tname = "int32"; break;
103  default: tname = "<undefined_type>"; break;
104  }
105  return f.format(writer, "{}[{}]", {tname, size});
106 }
107 
108 namespace
109 {
110  template <typename T>
111  static SYS_FORCE_INLINE size_t
112  formatVector(char *buffer, size_t bufsize, const T *data, size_t size)
113  {
114  UT_WorkBuffer tmp;
115  if (size)
116  {
117  tmp.format("{}", data[0]);
118  for (size_t i = 1; i < size; ++i)
119  tmp.appendFormat(", {}", data[i]);
120  }
121  UT::Format::Writer writer(buffer, bufsize);
123  return f.format(writer, "[{}]", {tmp});
124  }
125 
126  template <> size_t
127  formatVector<GfHalf>(char *buffer, size_t bufsize,
128  const GfHalf *data, size_t size)
129  {
130  UT_WorkBuffer tmp;
131  if (size)
132  {
133  tmp.format("{}", float(data[0]));
134  for (size_t i = 1; i < size; ++i)
135  tmp.appendFormat(", {}", float(data[i]));
136  }
137  UT::Format::Writer writer(buffer, bufsize);
139  return f.format(writer, "[{}]", {tmp});
140  }
141 
142  template <typename T>
143  static SYS_FORCE_INLINE size_t
144  formatIterator(char *buffer, size_t bufsize, T begin, const T &end)
145  {
146  UT_WorkBuffer tmp;
147  if (begin != end)
148  {
149  tmp.format("{}", *begin);
150  for (++begin; begin != end; ++begin)
151  tmp.appendFormat(", {}", *begin);
152  }
153  UT::Format::Writer writer(buffer, bufsize);
155  return f.format(writer, "[{}]", {tmp});
156  }
157 }
158 
159 
160 static SYS_FORCE_INLINE size_t
161 format(char *buffer, size_t bufsize, const TfTokenVector &vtok)
162 {
163  return formatVector(buffer, bufsize, &vtok[0], vtok.size());
164 }
165 
166 #define FORMAT_TYPE(TYPE, METHOD, SIZE) \
167  static SYS_FORCE_INLINE size_t \
168  format(char *buffer, size_t bufsize, const TYPE &val) \
169  { return formatVector(buffer, bufsize, val.METHOD(), SIZE); }
170 
171 FORMAT_TYPE(GfVec2h, data, 2)
172 FORMAT_TYPE(GfVec2i, data, 2)
173 FORMAT_TYPE(GfVec2f, data, 2)
174 FORMAT_TYPE(GfVec2d, data, 2)
175 FORMAT_TYPE(GfVec3h, data, 3)
176 FORMAT_TYPE(GfVec3i, data, 3)
177 FORMAT_TYPE(GfVec3f, data, 3)
178 FORMAT_TYPE(GfVec3d, data, 3)
179 FORMAT_TYPE(GfVec4h, data, 4)
180 FORMAT_TYPE(GfVec4i, data, 4)
181 FORMAT_TYPE(GfVec4f, data, 4)
182 FORMAT_TYPE(GfVec4d, data, 4)
183 
184 FORMAT_TYPE(GfMatrix2f, GetArray, 4)
185 FORMAT_TYPE(GfMatrix2d, GetArray, 4)
186 FORMAT_TYPE(GfMatrix3f, GetArray, 9)
187 FORMAT_TYPE(GfMatrix3d, GetArray, 9)
188 FORMAT_TYPE(GfMatrix4f, GetArray, 16)
189 FORMAT_TYPE(GfMatrix4d, GetArray, 16)
190 
191 #undef FORMAT_TYPE
192 
193 template <typename T>
194 static SYS_FORCE_INLINE size_t
195 format(char *buffer, size_t bufsize, const VtArray<T> &arr)
196 {
197  return formatIterator(buffer, bufsize, arr.begin(), arr.end());
198 }
199 
200 template <typename T>
201 static SYS_FORCE_INLINE size_t
202 format(char *buffer, size_t bufsize, const VtValue &val)
203 {
204  UT::Format::Writer writer(buffer, bufsize);
206  return f.format(writer, "{}", val.Get<T>());
207 }
208 
209 static SYS_FORCE_INLINE size_t
210 format(char *buffer, size_t bufsize, const VtValue &val)
211 {
212  UT::Format::Writer writer(buffer, bufsize);
214  UT_OStringStream os;
215  os << val << std::ends;
216  return f.format(writer, "{}", {os.str()});
217 }
218 
219 static SYS_FORCE_INLINE size_t
220 format(char *buffer, size_t bufsize, const GfRange1f &r)
221 {
222  UT::Format::Writer writer(buffer, bufsize);
224  return f.format(writer, "[{0}, {1}]", {r.GetMin(), r.GetMax()});
225 }
226 
227 static SYS_FORCE_INLINE size_t
228 format(char *buffer, size_t bufsize, const GfRange1d &r)
229 {
230  UT::Format::Writer writer(buffer, bufsize);
232  return f.format(writer, "[{0}, {1}]", {r.GetMin(), r.GetMax()});
233 }
234 
235 namespace
236 {
237  template <typename T>
238  static SYS_FORCE_INLINE size_t
239  formatQuat(char *buffer, size_t bufsize, const T &q)
240  {
241  const auto &ii = q.GetImaginary();
242  UT::Format::Writer writer(buffer, bufsize);
244  return f.format(writer, "{0}+({1},{2},{3})i",
245  {q.GetReal(), ii[0], ii[1], ii[2]});
246  }
247 }
248 
249 static SYS_FORCE_INLINE size_t
250 format(char *buffer, size_t bufsize, const GfQuatd &q)
251 {
252  return formatQuat(buffer, bufsize, q);
253 }
254 static SYS_FORCE_INLINE size_t
255 format(char *buffer, size_t bufsize, const GfQuatf &q)
256 {
257  return formatQuat(buffer, bufsize, q);
258 }
259 static SYS_FORCE_INLINE size_t
260 format(char *buffer, size_t bufsize, const GfQuath &q)
261 {
262  // Specialization for GfHalf
263  return formatQuat(buffer, bufsize, GfQuatf(q.GetReal(), q.GetImaginary()));
264 }
265 
266 
268 
269 #endif
Definition: quath.h:60
Definition: vec4i.h:60
double GetMin() const
Returns the minimum value of the range.
Definition: range1d.h:95
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
HdFormat
Definition: types.h:423
Definition: vec2i.h:60
GLboolean * data
Definition: glcorearb.h:131
Definition: vec3f.h:62
Definition: vec4d.h:62
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:59
Definition: vec2d.h:62
GfHalf GetReal() const
Return the real coefficient.
Definition: quath.h:106
Definition: vec4h.h:63
T const & Get() const &
Definition: value.h:1137
size_t appendFormat(const char *fmt, const Args &...args)
GLfloat f
Definition: glcorearb.h:1926
Definition: token.h:87
double GetMax() const
Returns the maximum value of the range.
Definition: range1d.h:98
Definition: core.h:760
#define FORMAT_TYPE(TYPE, METHOD, SIZE)
Definition: XUSD_Format.h:166
Definition: vec2h.h:63
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:112
void pathStr(UT_WorkBuffer &outpath) const
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
Definition: vec3i.h:60
Definition: types.h:170
Definition: path.h:290
float GetMin() const
Returns the minimum value of the range.
Definition: range1f.h:95
Definition: vec4f.h:62
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:1432
HD_API size_t HdGetComponentCount(HdType t)
float GetMax() const
Returns the maximum value of the range.
Definition: range1f.h:98
Definition: vec2f.h:62
Definition: vec3d.h:62
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Type-safe formatting, modeled on the Python str.format function.
GLboolean r
Definition: glcorearb.h:1222
Definition: quatd.h:59
size_t format(W &writer, const char *format, std::initializer_list< ArgValue > args)
Definition: vec3h.h:63
Definition: value.h:164
Definition: format.h:895
#define FORMAT_VAL(TYPE, GET_VAL)
Format for a TfToken.
Definition: XUSD_Format.h:64
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558