| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include "BaseDotVisitor.h" |
|---|
| 14 | |
|---|
| 15 | #include <fstream> |
|---|
| 16 | #include <cassert> |
|---|
| 17 | |
|---|
| 18 | #include <osg/Node> |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/Group> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | |
|---|
| 23 | using namespace osg; |
|---|
| 24 | |
|---|
| 25 | namespace osgDot { |
|---|
| 26 | |
|---|
| 27 | BaseDotVisitor::BaseDotVisitor() |
|---|
| 28 | { |
|---|
| 29 | _rankdir = "rankdir = LR;"; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | _nodes.imbue(std::locale("C")); |
|---|
| 35 | _edges.imbue(std::locale("C")); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | BaseDotVisitor::~BaseDotVisitor() { |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void BaseDotVisitor::setOptions(const osgDB::Options* options) |
|---|
| 42 | { |
|---|
| 43 | _options = const_cast<osgDB::Options*>(options); |
|---|
| 44 | OSG_INFO<<"BaseDotVisitor::setOptions("<<options<<")"<<std::endl; |
|---|
| 45 | if (_options.valid() && !(_options->getOptionString().empty())) |
|---|
| 46 | { |
|---|
| 47 | |
|---|
| 48 | std::string optionString = _options->getOptionString(); |
|---|
| 49 | |
|---|
| 50 | OSG_INFO<<" BaseDotVisitor::optionString ("<<optionString<<")"<<std::endl; |
|---|
| 51 | |
|---|
| 52 | std::string::size_type pos = optionString.find("rankdir"); |
|---|
| 53 | if (pos!=std::string::npos) |
|---|
| 54 | { |
|---|
| 55 | std::string::size_type semi_pos = optionString.find(";",pos); |
|---|
| 56 | if (semi_pos!=std::string::npos) |
|---|
| 57 | { |
|---|
| 58 | _rankdir = optionString.substr(pos, semi_pos-pos); |
|---|
| 59 | OSG_INFO<<" BaseDotVisitor::Set _rankdir to "<<_rankdir<<std::endl; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | bool BaseDotVisitor::run( osg::Node& root, std::ostream* fout ) { |
|---|
| 67 | setTraversalMode( TRAVERSE_ALL_CHILDREN ); |
|---|
| 68 | if ( fout && *fout ) { |
|---|
| 69 | root.accept( *this ); |
|---|
| 70 | |
|---|
| 71 | *fout << "digraph osg_scenegraph { "<<_rankdir<< std::endl; |
|---|
| 72 | |
|---|
| 73 | *fout << _nodes.str() << _edges.str(); |
|---|
| 74 | |
|---|
| 75 | *fout << "}" << std::endl; |
|---|
| 76 | |
|---|
| 77 | _nodes.clear(); |
|---|
| 78 | _edges.clear(); |
|---|
| 79 | _objectMap.clear(); |
|---|
| 80 | |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void BaseDotVisitor::apply(Node& node) { |
|---|
| 88 | int id; |
|---|
| 89 | if ( getOrCreateId( &node, id ) ) { |
|---|
| 90 | handle( node, id ); |
|---|
| 91 | handleNodeAndTraverse( node, id ); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void BaseDotVisitor::apply(Geode& node) { |
|---|
| 96 | int id; |
|---|
| 97 | if ( getOrCreateId( &node, id ) ) { |
|---|
| 98 | handle( node, id ); |
|---|
| 99 | handleNodeAndTraverse( node, id ); |
|---|
| 100 | |
|---|
| 101 | unsigned int i; |
|---|
| 102 | for ( i = 0; i < node.getNumDrawables(); i++ ) { |
|---|
| 103 | osg::Drawable* drawable = node.getDrawable( i ); |
|---|
| 104 | int id2; |
|---|
| 105 | if ( getOrCreateId( drawable, id2 ) ) { |
|---|
| 106 | handle( *drawable, id2 ); |
|---|
| 107 | osg::StateSet* s = drawable->getStateSet(); |
|---|
| 108 | if ( s ) { |
|---|
| 109 | int id3; |
|---|
| 110 | if ( getOrCreateId( s, id3 ) ) { |
|---|
| 111 | handle( *s, id3 ); |
|---|
| 112 | } |
|---|
| 113 | handle( *drawable, *s, id2, id3 ); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | handle( node, *drawable, id, id2 ); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void BaseDotVisitor::apply(Group& node) { |
|---|
| 124 | int id; |
|---|
| 125 | |
|---|
| 126 | if ( getOrCreateId( &node, id ) ) { |
|---|
| 127 | handle( node, id ); |
|---|
| 128 | handleNodeAndTraverse( node, id ); |
|---|
| 129 | |
|---|
| 130 | unsigned int i; |
|---|
| 131 | for ( i = 0; i < node.getNumChildren(); i++ ) { |
|---|
| 132 | osg::Node* child = node.getChild( i ); |
|---|
| 133 | |
|---|
| 134 | int id2; |
|---|
| 135 | getOrCreateId( child, id2 ); |
|---|
| 136 | handle( node, *child, id, id2 ); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void BaseDotVisitor::handle(osg::Node& node, int id) { |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void BaseDotVisitor::handle(osg::Geode& node, int id) { |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void BaseDotVisitor::handle(osg::Group& node, int id) { |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void BaseDotVisitor::handle(osg::Group& parent, osg::Node& child, int parentID, int childID ) { |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void BaseDotVisitor::handleNodeAndTraverse(osg::Node& node, int id) { |
|---|
| 156 | osg::StateSet* ss = node.getStateSet(); |
|---|
| 157 | if ( ss ) { |
|---|
| 158 | int id2; |
|---|
| 159 | if ( getOrCreateId( ss, id2 ) ) { |
|---|
| 160 | handle( *ss, id2 ); |
|---|
| 161 | } |
|---|
| 162 | handle( node, *ss, id, id2 ); |
|---|
| 163 | } |
|---|
| 164 | traverse(node); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void BaseDotVisitor::handle(osg::StateSet& stateset, int id) { |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void BaseDotVisitor::handle(osg::Node& node, osg::StateSet& stateset, int parentID, int childID) { |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void BaseDotVisitor::handle(osg::Drawable& drawable, int id) { |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | void BaseDotVisitor::handle(osg::Drawable& drawable, osg::StateSet& stateset, int parentID, int childID ) { |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | void BaseDotVisitor::handle(osg::Geode& geode, osg::Drawable& drawable, int parentID, int childID ) { |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | bool BaseDotVisitor::getOrCreateId( osg::Object* object, int &id ) { |
|---|
| 183 | assert( object ); |
|---|
| 184 | ObjectMap::iterator it = _objectMap.find( object ); |
|---|
| 185 | if ( it != _objectMap.end() ) { |
|---|
| 186 | id = it->second; |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | id = _objectMap.size(); |
|---|
| 191 | _objectMap[ object ] = id; |
|---|
| 192 | return true; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | } |
|---|