Warning, /include/Geant4/tools/sg/search is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 //
0005 // Helper functions to search node(s) in a scene graph.
0006 //
0007
0008 #ifndef tools_sg_search
0009 #define tools_sg_search
0010
0011 #include "search_action"
0012 #include "node"
0013
0014 namespace tools {
0015 namespace sg {
0016
0017 template <class NODE>
0018 inline NODE* find_first_node_of_class(std::ostream& a_out,node& a_from) {
0019 search_action action(a_out);
0020 action.reset();
0021 action.set_what(search_action::search_node_of_class);
0022 action.set_stop_at_first(true);
0023 action.set_class(NODE::s_class());
0024 a_from.search(action);
0025 const std::vector<void*>& _objs = action.objs();
0026 if(_objs.empty()) return 0;
0027 return (NODE*)_objs[0];
0028 }
0029
0030 inline node* find_first_node_with_class(std::ostream& a_out,node& a_from,const std::string& a_class) {
0031 search_action action(a_out);
0032 action.reset();
0033 action.set_what(search_action::search_node_of_class);
0034 action.set_stop_at_first(true);
0035 action.set_class(a_class);
0036 a_from.search(action);
0037 const std::vector<void*>& _objs = action.objs();
0038 if(_objs.empty()) return 0;
0039 return (node*)_objs[0];
0040 }
0041
0042 }}
0043
0044 #include "path"
0045
0046 namespace tools {
0047 namespace sg {
0048
0049 inline const path_t& find_path(search_action& a_action,node& a_from,node& a_node,bool a_verbose) {
0050 a_action.reset();
0051 a_action.set_what(search_action::search_path_to_node);
0052 a_action.set_node(&a_node);
0053 a_from.search(a_action);
0054 if(!a_action.done()) {
0055 if(a_verbose) {
0056 a_action.out() << "tools::sg::find_path :"
0057 << " not found node of class " << a_node.s_cls()
0058 << " from head node of class " << a_from.s_cls()
0059 << std::endl;
0060 }
0061 a_action.clear_path();
0062 return a_action.path();
0063 }
0064 if(a_verbose) {
0065 a_action.out() << "tools::sg::find_path :"
0066 << " found node of class " << a_node.s_cls()
0067 << " from head node of class " << a_from.s_cls()
0068 << std::endl;
0069 }
0070 const path_t& path = a_action.path();
0071 if(path.empty()) {
0072 if(a_verbose) {
0073 a_action.out() << "tools::sg::find_path :"
0074 << " node has no path !"
0075 << std::endl;
0076 }
0077 return path;
0078 }
0079 if(&a_node!=path[path.size()-1]) {
0080 a_action.out() << "tools::sg::find_path :"
0081 << " node / path tail mismatch !"
0082 << " node " << a_node.s_cls()
0083 << " tail " << path[path.size()-1]->s_cls()
0084 << std::endl;
0085 a_action.clear_path();
0086 return a_action.path();
0087 }
0088 return path;
0089 }
0090
0091 inline const search_action::paths_t& find_paths(search_action& a_action,node& a_from,const std::string& a_class){
0092 a_action.reset();
0093 a_action.set_what(search_action::search_path_to_node_of_class);
0094 a_action.set_class(a_class);
0095 a_from.search(a_action);
0096 return a_action.paths();
0097 }
0098
0099 template <class NODE>
0100 inline const search_action::paths_t& find_paths(search_action& a_action,node& a_from){
0101 a_action.reset();
0102 a_action.set_what(search_action::search_path_to_node_of_class);
0103 a_action.set_class(NODE::s_class());
0104 a_from.search(a_action);
0105 return a_action.paths();
0106 }
0107
0108 template <class NODE>
0109 inline NODE* find_ancestor(std::ostream& a_out,node& a_from,node& a_node,bool a_verbose) {
0110 search_action action(a_out);
0111 return rfind<NODE>(find_path(action,a_from,a_node,a_verbose));
0112 }
0113
0114 template <class CONTAINER>
0115 inline CONTAINER* find_container(std::ostream& a_out,node& a_from,node& a_node,bool a_verbose) {
0116 search_action action(a_out);
0117 return container<CONTAINER>(find_path(action,a_from,a_node,a_verbose));
0118 }
0119
0120 template <class NODE>
0121 inline NODE* search_node(std::ostream& a_out,node& a_from) {
0122 search_action sa(a_out);
0123 const paths_t& paths = find_paths<NODE>(sa,a_from);
0124 tools_vforcit(path_t,paths,it) {
0125 NODE* _node = tail<NODE>(*it);
0126 if(_node) return _node;
0127 }
0128 return 0;
0129 }
0130
0131 template <class SELECTABLE>
0132 inline SELECTABLE* search_selectable(std::ostream& a_out,node& a_from) {
0133 search_action sa(a_out);
0134 const paths_t& paths = find_paths<SELECTABLE>(sa,a_from);
0135 tools_vforcit(path_t,paths,it) {
0136 SELECTABLE* _node = tail<SELECTABLE>(*it);
0137 if(_node && _node->border_visible.value()) return _node;
0138 }
0139 /*
0140 tools_vforcit(sg::path_t,paths,it) {
0141 SELECTABLE* _node = sg::tail<SELECTABLE>(*it);
0142 if(_node) return _node;
0143 }
0144 */
0145 return 0;
0146 }
0147
0148 }}
0149
0150 #endif