|
| 1 | +#include <vsg/all.h> |
| 2 | + |
| 3 | +#ifdef vsgXchange_FOUND |
| 4 | +# include <vsgXchange/all.h> |
| 5 | +#endif |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <chrono> |
| 9 | +#include <iostream> |
| 10 | +#include <thread> |
| 11 | + |
| 12 | +size_t traverseChildren(const vsg::Group* group) |
| 13 | +{ |
| 14 | + size_t count = group->children.size(); |
| 15 | + for(auto& child : group->children) |
| 16 | + { |
| 17 | + if (auto child_group = child.cast<vsg::Group>()) count += traverseChildren(child_group); |
| 18 | + } |
| 19 | + return count; |
| 20 | +} |
| 21 | + |
| 22 | +int main(int argc, char** argv) |
| 23 | +{ |
| 24 | + // set up defaults and read command line arguments to override them |
| 25 | + vsg::CommandLine arguments(&argc, argv); |
| 26 | + |
| 27 | + if (arguments.read("--args")) std::cout << arguments << std::endl; |
| 28 | + |
| 29 | + // if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout() |
| 30 | + if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std(); |
| 31 | + |
| 32 | + // set up vsg::Options to pass in filepaths, ReaderWriters and other IO related options to use when reading and writing files. |
| 33 | + auto options = vsg::Options::create(); |
| 34 | + options->sharedObjects = vsg::SharedObjects::create(); |
| 35 | + options->fileCache = vsg::getEnv("VSG_FILE_CACHE"); |
| 36 | + options->paths = vsg::getEnvPaths("VSG_FILE_PATH"); |
| 37 | + |
| 38 | +#ifdef vsgXchange_all |
| 39 | + // add vsgXchange's support for reading and writing 3rd party file formats |
| 40 | + options->add(vsgXchange::all::create()); |
| 41 | +#endif |
| 42 | + |
| 43 | + options->readOptions(arguments); |
| 44 | + |
| 45 | + size_t iterationCount = arguments.value<size_t>(100000, "-i"); |
| 46 | + |
| 47 | + auto root = vsg::Group::create(); |
| 48 | + |
| 49 | + vsg::Path path; |
| 50 | + |
| 51 | + // read any vsg files |
| 52 | + for (int i = 1; i < argc; ++i) |
| 53 | + { |
| 54 | + vsg::Path filename = arguments[i]; |
| 55 | + path = vsg::filePath(filename); |
| 56 | + |
| 57 | + auto object = vsg::read(filename, options); |
| 58 | + if (auto node = object.cast<vsg::Node>()) |
| 59 | + { |
| 60 | + root->addChild(node); |
| 61 | + } |
| 62 | + else if (object) |
| 63 | + { |
| 64 | + std::cout << "Unable to view object of type " << object->className() << std::endl; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + std::cout << "Unable to load file " << filename << std::endl; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (root->children.empty()) |
| 73 | + { |
| 74 | + for(size_t i = 0; i<1000; ++i) |
| 75 | + { |
| 76 | + if (i%3==0) root->addChild(vsg::Node::create()); |
| 77 | + else if (i%3==1) root->addChild(vsg::VertexDraw::create()); |
| 78 | + else if (i%3==2) root->addChild(vsg::Geometry::create()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (VSG_USE_dynamic_cast) |
| 83 | + { |
| 84 | + std::cout<<"Using dynamic_cast<> based RTTI vsg::Object::cast<> implementation."<<std::endl; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + std::cout<<"Using typeid() based RTTI vsg::Object::cast<> implementation - faster than using dynamic_cast<>."<<std::endl; |
| 89 | + } |
| 90 | + |
| 91 | + auto startTime = vsg::clock::now(); |
| 92 | + |
| 93 | + size_t count = 0; |
| 94 | + |
| 95 | + for(size_t i=0; i<iterationCount; ++i) |
| 96 | + { |
| 97 | + count += (1 + traverseChildren(root)); |
| 98 | + } |
| 99 | + |
| 100 | + auto time = std::chrono::duration<float, std::chrono::seconds::period>(vsg::clock::now() - startTime).count(); |
| 101 | + std::cout << "Time " << time*1000.0 << "ms" << " count = " << count << std::endl; |
| 102 | + std::cout << "Cast and traverse per second " << static_cast<size_t>((static_cast<double>(count)/time))<< std::endl; |
| 103 | + |
| 104 | + // clean up done automatically thanks to ref_ptr<> |
| 105 | + return 0; |
| 106 | +} |
0 commit comments