Materialx Protobuf API 1.39.5
Serialization API to convert between MaterialX and Google Protobuf formats.
Loading...
Searching...
No Matches
Util Class Reference

Utility class for inspecting and debugging Protobuf MaterialXDocument messages. More...

#include <materialx_serializer.h>

Static Public Member Functions

static MaterialXDocument fromString (const std::string &data)
 Parse binary data to create Protobuf document.
 
static std::string toString (const MaterialXDocument &pb_doc)
 Serialize Protobuf document to binary string.
 
static std::string toJson (const MaterialXDocument &pb_doc, int indent=2)
 Convert Protobuf document to JSON string.
 
static void debugInspect (const MaterialXDocument &pb_doc, int max_depth=10)
 Print detailed document structure with attributes and children.
 
static void debugInspectCompact (const MaterialXDocument &pb_doc, int max_depth=10)
 Print compact document structure.
 
static void debugInspectSimple (const MaterialXDocument &pb_doc)
 Print simple tree structure of document.
 
static std::string generateMermaidDiagram (const MaterialXDocument &pb_doc)
 Generate a Mermaid diagram from protobuf document hierarchy.
 

Static Private Member Functions

static void printElement (const MaterialXElement &element, int depth, int max_depth)
 
static void printElementCompact (const MaterialXElement &element, int depth, int max_depth)
 
static void printTree (const MaterialXElement &element, const std::string &prefix, bool is_last)
 
static void addMermaidElements (const MaterialXElement &element, std::vector< std::string > &lines)
 

Detailed Description

Utility class for inspecting and debugging Protobuf MaterialXDocument messages.

  • Print the protobuf document structure in various formats.
  • Generate Mermaid diagrams for visualization of the document hierarchy.

Member Function Documentation

◆ addMermaidElements()

void Util::addMermaidElements ( const MaterialXElement & element,
std::vector< std::string > & lines )
staticprivate
306 {
307 std::string node_id = element.name();
308 std::replace(node_id.begin(), node_id.end(), ' ', '_');
309
310 std::ostringstream node_line;
311 node_line << " " << node_id << "[" << element.name() << " : " << element.type() << "]";
312 lines.push_back(node_line.str());
313
314 for (const MaterialXElement& child : element.children()) {
315 std::string child_id = child.name();
316 std::replace(child_id.begin(), child_id.end(), ' ', '_');
317
318 addMermaidElements(child, lines);
319
320 std::ostringstream edge_line;
321 edge_line << " " << node_id << " --> " << child_id;
322 lines.push_back(edge_line.str());
323 }
324}
static void addMermaidElements(const MaterialXElement &element, std::vector< std::string > &lines)
Definition materialx_serializer.cpp:306

◆ debugInspect()

void Util::debugInspect ( const MaterialXDocument & pb_doc,
int max_depth = 10 )
static

Print detailed document structure with attributes and children.

Parameters
pb_docProtobuf document to inspect.
max_depthMaximum depth to print (default: 10).
166 {
167 const Version& pb_version = pb_doc.schema_version();
168 std::cout << "Schema Version: " << pb_version.major() << "."
169 << pb_version.minor() << "." << pb_version.patch() << std::endl;
170
171 // Print document attributes
172 std::ostringstream attrs;
173 for (const Attribute& attr : pb_doc.attributes()) {
174 if (!attrs.str().empty()) attrs << ", ";
175 attrs << attr.key() << "=" << attr.value();
176 }
177 std::cout << "Document:" << (attrs.str().empty() ? "" : " [" + attrs.str() + "]") << std::endl;
178
179 // Print elements
180 for (const MaterialXElement& element : pb_doc.elements()) {
181 printElement(element, 0, max_depth);
182 }
183}
static void printElement(const MaterialXElement &element, int depth, int max_depth)
Definition materialx_serializer.cpp:185

◆ debugInspectCompact()

void Util::debugInspectCompact ( const MaterialXDocument & pb_doc,
int max_depth = 10 )
static

Print compact document structure.

Parameters
pb_docProtobuf document to inspect.
max_depthMaximum depth to print (default: 10).
205 {
206 const Version& pb_version = pb_doc.schema_version();
207 std::cout << "Schema Version: " << pb_version.major() << "."
208 << pb_version.minor() << "." << pb_version.patch() << std::endl;
209
210 // Print document attributes
211 std::ostringstream attrs;
212 for (const Attribute& attr : pb_doc.attributes()) {
213 if (!attrs.str().empty()) attrs << ", ";
214 attrs << attr.key() << "=" << attr.value();
215 }
216 std::cout << "Document:" << (attrs.str().empty() ? "" : " [" + attrs.str() + "]") << std::endl;
217
218 // Print elements
219 for (const MaterialXElement& element : pb_doc.elements()) {
220 printElementCompact(element, 0, max_depth);
221 }
222}
static void printElementCompact(const MaterialXElement &element, int depth, int max_depth)
Definition materialx_serializer.cpp:224

◆ debugInspectSimple()

void Util::debugInspectSimple ( const MaterialXDocument & pb_doc)
static

Print simple tree structure of document.

Parameters
pb_docProtobuf document to inspect.
256 {
257 const Version& pb_version = pb_doc.schema_version();
258 std::cout << "Schema Version: " << pb_version.major() << "."
259 << pb_version.minor() << "." << pb_version.patch() << std::endl;
260
261 // Print document attributes
262 std::ostringstream attrs;
263 for (const Attribute& attr : pb_doc.attributes()) {
264 if (!attrs.str().empty()) attrs << ", ";
265 attrs << attr.key() << "=" << attr.value();
266 }
267 std::cout << "Document:" << (attrs.str().empty() ? "" : " [" + attrs.str() + "]") << std::endl;
268
269 // Print elements
270 int total = pb_doc.elements_size();
271 for (int i = 0; i < total; ++i) {
272 printTree(pb_doc.elements(i), "", i == total - 1);
273 }
274}
static void printTree(const MaterialXElement &element, const std::string &prefix, bool is_last)
Definition materialx_serializer.cpp:276

◆ fromString()

MaterialXDocument Util::fromString ( const std::string & data)
static

Parse binary data to create Protobuf document.

Parameters
dataBinary string data.
Returns
Protobuf document.
140 {
141 MaterialXDocument pb_doc;
142 if (!pb_doc.ParseFromString(data)) {
143 throw std::runtime_error("Failed to parse protobuf data");
144 }
145 return pb_doc;
146}

◆ generateMermaidDiagram()

std::string Util::generateMermaidDiagram ( const MaterialXDocument & pb_doc)
static

Generate a Mermaid diagram from protobuf document hierarchy.

Parameters
pb_docProtobuf document to visualize.
Returns
Mermaid diagram source code.
290 {
291 std::vector<std::string> lines;
292 lines.push_back("graph LR");
293
294 for (const MaterialXElement& element : pb_doc.elements()) {
295 addMermaidElements(element, lines);
296 }
297
298 std::ostringstream result;
299 for (const std::string& line : lines) {
300 result << line << "\n";
301 }
302
303 return result.str();
304}

◆ printElement()

void Util::printElement ( const MaterialXElement & element,
int depth,
int max_depth )
staticprivate
185 {
186 if (depth > max_depth) return;
187
188 std::string indent(depth * 2, ' ');
189 std::cout << indent << element.name() << " (" << element.type() << ")" << std::endl;
190
191 // Print attributes
192 for (const Attribute& attr : element.attributes()) {
193 std::cout << indent << " │ " << attr.key() << ": " << attr.value() << std::endl;
194 }
195
196 // Print children recursively
197 if (element.children_size() > 0) {
198 std::cout << indent << " └─ Children (" << element.children_size() << "):" << std::endl;
199 for (const MaterialXElement& child : element.children()) {
200 printElement(child, depth + 2, max_depth);
201 }
202 }
203}

◆ printElementCompact()

void Util::printElementCompact ( const MaterialXElement & element,
int depth,
int max_depth )
staticprivate
224 {
225 if (depth > max_depth) return;
226
227 std::string indent(depth * 2, ' ');
228
229 // Print compact info
230 std::ostringstream line;
231 line << indent << element.name();
232 if (!element.type().empty()) {
233 line << " (" << element.type() << ")";
234 }
235
236 // Print first 2 attributes
237 if (element.attributes_size() > 0) {
238 line << " [";
239 int count = 0;
240 for (const Attribute& attr : element.attributes()) {
241 if (count > 0) line << ", ";
242 line << attr.key() << "=" << attr.value();
243 if (++count >= 2) break;
244 }
245 line << "]";
246 }
247
248 std::cout << line.str() << std::endl;
249
250 // Recursively print children
251 for (const MaterialXElement& child : element.children()) {
252 printElementCompact(child, depth + 1, max_depth);
253 }
254}

◆ printTree()

void Util::printTree ( const MaterialXElement & element,
const std::string & prefix,
bool is_last )
staticprivate
276 {
277 std::string connector = is_last ? "└── " : "├── ";
278 std::cout << prefix << connector << element.name() << " (" << element.type() << ")" << std::endl;
279
280 // Update prefix for children
281 std::string new_prefix = prefix + (is_last ? " " : "│ ");
282
283 // Print children
284 int total = element.children_size();
285 for (int i = 0; i < total; ++i) {
286 printTree(element.children(i), new_prefix, i == total - 1);
287 }
288}

◆ toJson()

std::string Util::toJson ( const MaterialXDocument & pb_doc,
int indent = 2 )
static

Convert Protobuf document to JSON string.

Parameters
pb_docProtobuf document to convert.
indentNumber of spaces for indentation (default: 2).
Returns
JSON string representation.
156 {
157 google::protobuf::util::JsonPrintOptions options;
158 options.add_whitespace = true;
159 options.preserve_proto_field_names = true;
160
161 std::string json_output;
162 google::protobuf::util::MessageToJsonString(pb_doc, &json_output, options);
163 return json_output;
164}

◆ toString()

std::string Util::toString ( const MaterialXDocument & pb_doc)
static

Serialize Protobuf document to binary string.

Parameters
pb_docProtobuf document to serialize.
Returns
Binary string representation.
148 {
149 std::string result;
150 if (!pb_doc.SerializeToString(&result)) {
151 throw std::runtime_error("Failed to serialize protobuf document");
152 }
153 return result;
154}

The documentation for this class was generated from the following files: