Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_sg_group 4 #ifndef tools_sg_group 5 #define tools_sg_group 5 #define tools_sg_group 6 6 7 //#define TOOLS_SG_GROUP_DEBUG 7 //#define TOOLS_SG_GROUP_DEBUG 8 8 9 #include "node" 9 #include "node" 10 10 11 #include "pick_action" 11 #include "pick_action" 12 #include "event_action" 12 #include "event_action" 13 13 14 #ifdef TOOLS_SG_GROUP_DEBUG 14 #ifdef TOOLS_SG_GROUP_DEBUG 15 #include "render_action" 15 #include "render_action" 16 #endif 16 #endif 17 17 18 namespace tools { 18 namespace tools { 19 namespace sg { 19 namespace sg { 20 20 21 class group : public node { 21 class group : public node { 22 TOOLS_NODE(group,tools::sg::group,node) 22 TOOLS_NODE(group,tools::sg::group,node) 23 public: 23 public: 24 virtual void render(render_action& a_action) 24 virtual void render(render_action& a_action) { 25 #ifdef TOOLS_SG_GROUP_DEBUG 25 #ifdef TOOLS_SG_GROUP_DEBUG 26 std::ostream& out = a_action.out(); 26 std::ostream& out = a_action.out(); 27 tools_vforcit(node*,m_children,it) { 27 tools_vforcit(node*,m_children,it) { 28 out << "debug : tools::sg::group::render 28 out << "debug : tools::sg::group::render : children : " << (*it)->s_cls() << " begin : " << std::endl; 29 (*it)->render(a_action); 29 (*it)->render(a_action); 30 out << "debug : tools::sg::group::render 30 out << "debug : tools::sg::group::render : children : " << (*it)->s_cls() << " end," << std::endl; 31 } 31 } 32 #else 32 #else 33 tools_vforcit(node*,m_children,it) (*it)-> 33 tools_vforcit(node*,m_children,it) (*it)->render(a_action); 34 #endif 34 #endif 35 } 35 } 36 virtual void pick(pick_action& a_action) { 36 virtual void pick(pick_action& a_action) { 37 tools_vforcit(node*,m_children,it) { 37 tools_vforcit(node*,m_children,it) { 38 (*it)->pick(a_action); 38 (*it)->pick(a_action); 39 if(a_action.done()) break; 39 if(a_action.done()) break; 40 } 40 } 41 } 41 } 42 virtual void bbox(bbox_action& a_action) { 42 virtual void bbox(bbox_action& a_action) { 43 tools_vforcit(node*,m_children,it) (*it)-> 43 tools_vforcit(node*,m_children,it) (*it)->bbox(a_action); 44 } 44 } 45 virtual void event(event_action& a_action) { 45 virtual void event(event_action& a_action) { 46 tools_vforcit(node*,m_children,it) { 46 tools_vforcit(node*,m_children,it) { 47 (*it)->event(a_action); 47 (*it)->event(a_action); 48 if(a_action.done()) break; 48 if(a_action.done()) break; 49 } 49 } 50 } 50 } 51 virtual void search(search_action& a_action) 51 virtual void search(search_action& a_action) { 52 parent::search(a_action); 52 parent::search(a_action); 53 if(a_action.done()) return; 53 if(a_action.done()) return; 54 if(a_action.do_path()) a_action.path_push( 54 if(a_action.do_path()) a_action.path_push(this); 55 tools_vforcit(node*,m_children,it) { 55 tools_vforcit(node*,m_children,it) { 56 (*it)->search(a_action); 56 (*it)->search(a_action); 57 if(a_action.done()) return; 57 if(a_action.done()) return; 58 } 58 } 59 if(a_action.do_path()) a_action.path_pop() 59 if(a_action.do_path()) a_action.path_pop(); 60 } 60 } 61 virtual void get_matrix(get_matrix_action& a 61 virtual void get_matrix(get_matrix_action& a_action) { 62 tools_vforcit(node*,m_children,it) { 62 tools_vforcit(node*,m_children,it) { 63 (*it)->get_matrix(a_action); 63 (*it)->get_matrix(a_action); 64 if(a_action.done()) return; 64 if(a_action.done()) return; 65 } 65 } 66 } 66 } 67 virtual bool write(write_action& a_action) { 67 virtual bool write(write_action& a_action) { 68 if(!a_action.beg_node(*this)) return false 68 if(!a_action.beg_node(*this)) return false; 69 if(!write_fields(a_action)) return false; 69 if(!write_fields(a_action)) return false; 70 if(!write_children(a_action)) return false 70 if(!write_children(a_action)) return false; 71 if(!a_action.end_node(*this)) return false 71 if(!a_action.end_node(*this)) return false; 72 return true; 72 return true; 73 } 73 } 74 virtual void is_visible(visible_action& a_ac 74 virtual void is_visible(visible_action& a_action) { 75 tools_vforcit(node*,m_children,it) { 75 tools_vforcit(node*,m_children,it) { 76 (*it)->is_visible(a_action); 76 (*it)->is_visible(a_action); 77 } 77 } 78 } 78 } 79 public: 79 public: 80 group():node(){} 80 group():node(){} 81 virtual ~group(){clear();} 81 virtual ~group(){clear();} 82 public: 82 public: 83 group(const group& a_from) 83 group(const group& a_from) 84 :node(a_from) 84 :node(a_from) 85 { 85 { 86 tools_vforcit(node*,a_from.m_children,it) 86 tools_vforcit(node*,a_from.m_children,it) m_children.push_back((*it)->copy()); 87 } 87 } 88 group& operator=(const group& a_from){ 88 group& operator=(const group& a_from){ 89 node::operator=(a_from); 89 node::operator=(a_from); 90 if(&a_from==this) return *this; 90 if(&a_from==this) return *this; 91 clear(); 91 clear(); 92 tools_vforcit(node*,a_from.m_children,it) 92 tools_vforcit(node*,a_from.m_children,it) m_children.push_back((*it)->copy()); 93 return *this; 93 return *this; 94 } 94 } 95 public: 95 public: 96 void add(node* a_node) { 96 void add(node* a_node) { 97 //WARNING : take ownership of a_node. 97 //WARNING : take ownership of a_node. 98 m_children.push_back(a_node); 98 m_children.push_back(a_node); 99 } 99 } 100 void add_front(node* a_node) { 100 void add_front(node* a_node) { 101 //WARNING : take ownership of a_node. 101 //WARNING : take ownership of a_node. 102 m_children.insert(m_children.begin(),a_nod 102 m_children.insert(m_children.begin(),a_node); 103 } 103 } 104 void set(unsigned int a_index,node* a_node) 104 void set(unsigned int a_index,node* a_node) { 105 //WARNING : take ownership of a_node. 105 //WARNING : take ownership of a_node. 106 //WARNING : no check is done on a_index. 106 //WARNING : no check is done on a_index. 107 m_children[a_index] = a_node; 107 m_children[a_index] = a_node; 108 } 108 } 109 109 110 bool replace(const node* a_from,node* a_to,b 110 bool replace(const node* a_from,node* a_to,bool a_del){ 111 tools_vforit(node*,m_children,it) { 111 tools_vforit(node*,m_children,it) { 112 if((*it)==a_from) { 112 if((*it)==a_from) { 113 node* old = *it; 113 node* old = *it; 114 (*it) = a_to; 114 (*it) = a_to; 115 if(a_del) delete old; 115 if(a_del) delete old; 116 return true; 116 return true; 117 } 117 } 118 } 118 } 119 return false; 119 return false; 120 } 120 } 121 121 122 void swap(unsigned int a_1,unsigned int a_2) 122 void swap(unsigned int a_1,unsigned int a_2){ 123 // WARNING : no check is done on a_1,a_2. 123 // WARNING : no check is done on a_1,a_2. 124 node* tmp = m_children[a_1]; 124 node* tmp = m_children[a_1]; 125 m_children[a_1] = m_children[a_2]; 125 m_children[a_1] = m_children[a_2]; 126 m_children[a_2] = tmp; 126 m_children[a_2] = tmp; 127 } 127 } 128 128 129 template <class T> 129 template <class T> 130 T* search() const { 130 T* search() const { 131 tools_vforcit(node*,m_children,it) { 131 tools_vforcit(node*,m_children,it) { 132 T* o = safe_cast<node,T>(*(*it)); 132 T* o = safe_cast<node,T>(*(*it)); 133 if(o) return o; 133 if(o) return o; 134 } 134 } 135 return 0; 135 return 0; 136 } 136 } 137 137 138 /* 138 /* 139 template <class T> 139 template <class T> 140 T* rsearch_from(const node* a_node) const { 140 T* rsearch_from(const node* a_node) const { 141 bool found = false; 141 bool found = false; 142 tools_vforcrit(node*,m_children,it) { 142 tools_vforcrit(node*,m_children,it) { 143 // the below logic permits to test a_nod 143 // the below logic permits to test a_node. 144 if(!found) {if(*it==a_node) found = true 144 if(!found) {if(*it==a_node) found = true;} 145 if(found) { 145 if(found) { 146 T* o = safe_cast<node,T>(*(*it)); 146 T* o = safe_cast<node,T>(*(*it)); 147 if(o) return o; 147 if(o) return o; 148 } 148 } 149 } 149 } 150 return 0; 150 return 0; 151 } 151 } 152 */ 152 */ 153 153 154 void* rsearch_from(const node* a_node, 154 void* rsearch_from(const node* a_node, 155 const std::string& a_clas 155 const std::string& a_class, 156 bool a_inc_a_node = true) 156 bool a_inc_a_node = true) const { 157 bool found = false; 157 bool found = false; 158 tools_vforcrit(node*,m_children,it) { 158 tools_vforcrit(node*,m_children,it) { 159 // the below logic permits to test a_nod 159 // the below logic permits to test a_node. 160 if(!found) { 160 if(!found) { 161 if(*it==a_node) { 161 if(*it==a_node) { 162 found = true; 162 found = true; 163 if(!a_inc_a_node) continue; //skip a 163 if(!a_inc_a_node) continue; //skip a_node 164 } 164 } 165 } 165 } 166 if(found) { 166 if(found) { 167 void* p = (*it)->cast(a_class); 167 void* p = (*it)->cast(a_class); 168 if(p) return p; 168 if(p) return p; 169 } 169 } 170 } 170 } 171 return 0; 171 return 0; 172 } 172 } 173 173 174 bool remove(const node* a_node){ 174 bool remove(const node* a_node){ 175 //NOTE : no delete on a_node is performed. 175 //NOTE : no delete on a_node is performed. 176 tools_vforit(node*,m_children,it) { 176 tools_vforit(node*,m_children,it) { 177 if(a_node==(*it)) { 177 if(a_node==(*it)) { 178 m_children.erase(it); 178 m_children.erase(it); 179 return true; 179 return true; 180 } 180 } 181 } 181 } 182 return false; 182 return false; 183 } 183 } 184 184 185 bool remove_index(unsigned int a_index){ 185 bool remove_index(unsigned int a_index){ 186 //NOTE : no delete on node at a_index is p 186 //NOTE : no delete on node at a_index is performed. 187 std::vector<node*>::iterator it = m_childr 187 std::vector<node*>::iterator it = m_children.begin(); 188 it += a_index; 188 it += a_index; 189 if(it>=m_children.end()) return false; 189 if(it>=m_children.end()) return false; 190 m_children.erase(it); 190 m_children.erase(it); 191 return true; 191 return true; 192 } 192 } 193 193 194 bool delete_from(const node* a_node,bool a_i 194 bool delete_from(const node* a_node,bool a_inc_a_node = true){ 195 bool found = false; 195 bool found = false; 196 std::vector<node*>::iterator it; 196 std::vector<node*>::iterator it; 197 for(it=m_children.begin();it!=m_children.e 197 for(it=m_children.begin();it!=m_children.end();) { 198 if(!found) { 198 if(!found) { 199 if(*it==a_node) { 199 if(*it==a_node) { 200 found = true; 200 found = true; 201 if(!a_inc_a_node) {it++;continue;} / 201 if(!a_inc_a_node) {it++;continue;} //skip a_node 202 } 202 } 203 } 203 } 204 if(found) { 204 if(found) { 205 node* old = *it; 205 node* old = *it; 206 it = m_children.erase(it); 206 it = m_children.erase(it); 207 delete old; 207 delete old; 208 } else { 208 } else { 209 it++; 209 it++; 210 } 210 } 211 } 211 } 212 return found; 212 return found; 213 } 213 } 214 214 215 void transfer(group& a_from) { 215 void transfer(group& a_from) { 216 if(&a_from==this) return; 216 if(&a_from==this) return; 217 clear(); 217 clear(); 218 m_children.resize(a_from.size()); 218 m_children.resize(a_from.size()); 219 std::vector<node*>::iterator it = m_child 219 std::vector<node*>::iterator it = m_children.begin(); 220 std::vector<node*>::iterator fit = a_from. 220 std::vector<node*>::iterator fit = a_from.m_children.begin(); 221 for(;fit!=a_from.m_children.end();++it,++f 221 for(;fit!=a_from.m_children.end();++it,++fit) { 222 *it = *fit; 222 *it = *fit; 223 *fit = 0; 223 *fit = 0; 224 } 224 } 225 a_from.m_children.clear(); 225 a_from.m_children.clear(); 226 } 226 } 227 227 228 void transfer(std::vector<node*>& a_to) { 228 void transfer(std::vector<node*>& a_to) { 229 a_to = m_children; 229 a_to = m_children; 230 m_children.clear(); 230 m_children.clear(); 231 //touch(); 231 //touch(); 232 } 232 } 233 233 234 //void clear() {safe_clear<node>(m_children);} 234 //void clear() {safe_clear<node>(m_children);} 235 void clear() {safe_reverse_clear<node>(m_chi 235 void clear() {safe_reverse_clear<node>(m_children);} 236 236 237 void raw_clear() { //used for sg coming from 237 void raw_clear() { //used for sg coming from exlib/rroot/vis_volume. 238 tools::raw_clear<node>(m_children); //tool << 238 tools::raw_clear<node>(m_children); //inlib:: is needed. 239 } 239 } 240 240 241 //void children_clear() {m_children.clear();} 241 //void children_clear() {m_children.clear();} //warning : nodes are not deleted. Used in [hep_]cone_anim. 242 242 243 size_t size() const {return m_children.size( 243 size_t size() const {return m_children.size();} 244 bool empty() const {return m_children.size() 244 bool empty() const {return m_children.size()?false:true;} 245 node* operator[](size_t a_index) const{ 245 node* operator[](size_t a_index) const{ 246 //WARNING : no check is done on a_index. 246 //WARNING : no check is done on a_index. 247 return m_children[a_index]; 247 return m_children[a_index]; 248 } 248 } 249 const std::vector<node*>& children() const { 249 const std::vector<node*>& children() const {return m_children;} 250 std::vector<node*>& children() {return m_chi 250 std::vector<node*>& children() {return m_children;} 251 251 252 bool insert(unsigned int a_index,node* a_new 252 bool insert(unsigned int a_index,node* a_new){ //iv2sg 253 if(a_index==m_children.size()) { 253 if(a_index==m_children.size()) { 254 m_children.push_back(a_new); 254 m_children.push_back(a_new); 255 return true; 255 return true; 256 } 256 } 257 unsigned int index = 0; 257 unsigned int index = 0; 258 std::vector<node*>::iterator it; 258 std::vector<node*>::iterator it; 259 for(it=m_children.begin();it!=m_children.e 259 for(it=m_children.begin();it!=m_children.end();++it,index++) { 260 if(index==a_index) { 260 if(index==a_index) { 261 m_children.insert(it,a_new); 261 m_children.insert(it,a_new); 262 return true; 262 return true; 263 } 263 } 264 } 264 } 265 return false; 265 return false; 266 } 266 } 267 /* 267 /* 268 bool insert(const node* a_from,node* a_new){ 268 bool insert(const node* a_from,node* a_new){ 269 tools_vforit(node*,m_children,it) { 269 tools_vforit(node*,m_children,it) { 270 if((*it)==a_from) { 270 if((*it)==a_from) { 271 m_children.insert(it,a_new); 271 m_children.insert(it,a_new); 272 return true; 272 return true; 273 } 273 } 274 } 274 } 275 return false; 275 return false; 276 } 276 } 277 */ 277 */ 278 template <class T> 278 template <class T> 279 T* rsearch() const { //used in agora. 279 T* rsearch() const { //used in agora. 280 tools_vforcrit(node*,m_children,it) { 280 tools_vforcrit(node*,m_children,it) { 281 T* o = safe_cast<node,T>(*(*it)); 281 T* o = safe_cast<node,T>(*(*it)); 282 if(o) return o; 282 if(o) return o; 283 } 283 } 284 return 0; 284 return 0; 285 } 285 } 286 286 287 bool position(const node* a_node,unsigned in 287 bool position(const node* a_node,unsigned int& a_index) const { 288 a_index = 0; 288 a_index = 0; 289 tools_vforcit(node*,m_children,it) { 289 tools_vforcit(node*,m_children,it) { 290 if(a_node==(*it)) return true; 290 if(a_node==(*it)) return true; 291 a_index++; 291 a_index++; 292 } 292 } 293 return false; 293 return false; 294 } 294 } 295 295 296 node* node_at(unsigned int a_index) const { 296 node* node_at(unsigned int a_index) const { 297 if(a_index>=m_children.size()) return 0; 297 if(a_index>=m_children.size()) return 0; 298 return m_children[a_index]; 298 return m_children[a_index]; 299 } 299 } 300 300 301 template <class T> 301 template <class T> 302 T* child(unsigned int a_index) const { 302 T* child(unsigned int a_index) const { 303 if(a_index>=m_children.size()) return 0; 303 if(a_index>=m_children.size()) return 0; 304 node* _node = m_children[a_index]; 304 node* _node = m_children[a_index]; 305 if(!_node) return 0; 305 if(!_node) return 0; 306 return safe_cast<node,T>(*_node); 306 return safe_cast<node,T>(*_node); 307 } 307 } 308 308 309 309 310 protected: 310 protected: 311 bool write_children(write_action& a_action) 311 bool write_children(write_action& a_action) { 312 tools_vforcit(node*,m_children,it) { 312 tools_vforcit(node*,m_children,it) { 313 if(!(*it)->write(a_action)) return false 313 if(!(*it)->write(a_action)) return false; 314 } 314 } 315 return true; 315 return true; 316 } 316 } 317 protected: 317 protected: 318 std::vector<node*> m_children; 318 std::vector<node*> m_children; 319 }; 319 }; 320 320 321 }} 321 }} 322 322 323 #endif 323 #endif