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