Warning, /include/Geant4/tools/sg/group 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 #ifndef tools_sg_group
0005 #define tools_sg_group
0006
0007 #include "node"
0008
0009 #include "pick_action"
0010 #include "event_action"
0011
0012 namespace tools {
0013 namespace sg {
0014
0015 class group : public node {
0016 TOOLS_NODE(group,tools::sg::group,node)
0017 public:
0018 virtual void render(render_action& a_action) {
0019 tools_vforcit(node*,m_children,it) (*it)->render(a_action);
0020 }
0021 virtual void pick(pick_action& a_action) {
0022 tools_vforcit(node*,m_children,it) {
0023 (*it)->pick(a_action);
0024 if(a_action.done()) break;
0025 }
0026 }
0027 virtual void bbox(bbox_action& a_action) {
0028 tools_vforcit(node*,m_children,it) (*it)->bbox(a_action);
0029 }
0030 virtual void event(event_action& a_action) {
0031 tools_vforcit(node*,m_children,it) {
0032 (*it)->event(a_action);
0033 if(a_action.done()) break;
0034 }
0035 }
0036 virtual void search(search_action& a_action) {
0037 parent::search(a_action);
0038 if(a_action.done()) return;
0039 if(a_action.do_path()) a_action.path_push(this);
0040 tools_vforcit(node*,m_children,it) {
0041 (*it)->search(a_action);
0042 if(a_action.done()) return;
0043 }
0044 if(a_action.do_path()) a_action.path_pop();
0045 }
0046 virtual void get_matrix(get_matrix_action& a_action) {
0047 tools_vforcit(node*,m_children,it) {
0048 (*it)->get_matrix(a_action);
0049 if(a_action.done()) return;
0050 }
0051 }
0052 virtual bool write(write_action& a_action) {
0053 if(!a_action.beg_node(*this)) return false;
0054 if(!write_fields(a_action)) return false;
0055 if(!write_children(a_action)) return false;
0056 if(!a_action.end_node(*this)) return false;
0057 return true;
0058 }
0059 virtual void is_visible(visible_action& a_action) {
0060 tools_vforcit(node*,m_children,it) {
0061 (*it)->is_visible(a_action);
0062 }
0063 }
0064 public:
0065 group():node(){}
0066 virtual ~group(){clear();}
0067 public:
0068 group(const group& a_from)
0069 :node(a_from)
0070 {
0071 tools_vforcit(node*,a_from.m_children,it) m_children.push_back((*it)->copy());
0072 }
0073 group& operator=(const group& a_from){
0074 node::operator=(a_from);
0075 if(&a_from==this) return *this;
0076 clear();
0077 tools_vforcit(node*,a_from.m_children,it) m_children.push_back((*it)->copy());
0078 return *this;
0079 }
0080 public:
0081 void add(node* a_node) {
0082 //WARNING : take ownership of a_node.
0083 m_children.push_back(a_node);
0084 }
0085 void add_front(node* a_node) {
0086 //WARNING : take ownership of a_node.
0087 m_children.insert(m_children.begin(),a_node);
0088 }
0089 void set(unsigned int a_index,node* a_node) {
0090 //WARNING : take ownership of a_node.
0091 //WARNING : no check is done on a_index.
0092 m_children[a_index] = a_node;
0093 }
0094
0095 bool replace(const node* a_from,node* a_to,bool a_del){
0096 tools_vforit(node*,m_children,it) {
0097 if((*it)==a_from) {
0098 node* old = *it;
0099 (*it) = a_to;
0100 if(a_del) delete old;
0101 return true;
0102 }
0103 }
0104 return false;
0105 }
0106
0107 void swap(unsigned int a_1,unsigned int a_2){
0108 // WARNING : no check is done on a_1,a_2.
0109 node* tmp = m_children[a_1];
0110 m_children[a_1] = m_children[a_2];
0111 m_children[a_2] = tmp;
0112 }
0113
0114 template <class T>
0115 T* search() const {
0116 tools_vforcit(node*,m_children,it) {
0117 T* o = safe_cast<node,T>(*(*it));
0118 if(o) return o;
0119 }
0120 return 0;
0121 }
0122
0123 void* rsearch_from(const node* a_node,
0124 const std::string& a_class,
0125 bool a_inc_a_node = true) const {
0126 bool found = false;
0127 for(auto it = m_children.crbegin(); it != m_children.crend(); ++it) {
0128 // the below logic permits to test a_node.
0129 if(!found) {
0130 if(*it==a_node) {
0131 found = true;
0132 if(!a_inc_a_node) continue; //skip a_node
0133 }
0134 }
0135 if(found) {
0136 void* p = (*it)->cast(a_class);
0137 if(p) return p;
0138 }
0139 }
0140 return 0;
0141 }
0142
0143 bool remove(const node* a_node){
0144 //NOTE : no delete on a_node is performed.
0145 tools_vforit(node*,m_children,it) {
0146 if(a_node==(*it)) {
0147 m_children.erase(it);
0148 return true;
0149 }
0150 }
0151 return false;
0152 }
0153
0154 bool remove_index(unsigned int a_index){
0155 //NOTE : no delete on node at a_index is performed.
0156 std::vector<node*>::iterator it = m_children.begin();
0157 it += a_index;
0158 if(it>=m_children.end()) return false;
0159 m_children.erase(it);
0160 return true;
0161 }
0162
0163 bool delete_from(const node* a_node,bool a_inc_a_node = true){
0164 bool found = false;
0165 std::vector<node*>::iterator it;
0166 for(it=m_children.begin();it!=m_children.end();) {
0167 if(!found) {
0168 if(*it==a_node) {
0169 found = true;
0170 if(!a_inc_a_node) {it++;continue;} //skip a_node
0171 }
0172 }
0173 if(found) {
0174 node* old = *it;
0175 it = m_children.erase(it);
0176 delete old;
0177 } else {
0178 it++;
0179 }
0180 }
0181 return found;
0182 }
0183
0184 void transfer(group& a_from) {
0185 if(&a_from==this) return;
0186 clear();
0187 m_children.resize(a_from.size());
0188 std::vector<node*>::iterator it = m_children.begin();
0189 std::vector<node*>::iterator fit = a_from.m_children.begin();
0190 for(;fit!=a_from.m_children.end();++it,++fit) {
0191 *it = *fit;
0192 *fit = 0;
0193 }
0194 a_from.m_children.clear();
0195 }
0196
0197 void transfer(std::vector<node*>& a_to) {
0198 a_to = m_children;
0199 m_children.clear();
0200 //touch();
0201 }
0202
0203 void clear() {safe_reverse_clear<node>(m_children);}
0204
0205 void raw_clear() { //used for sg coming from exlib/rroot/vis_volume.
0206 tools::raw_clear<node>(m_children); //tools:: is needed.
0207 }
0208
0209 size_t size() const {return m_children.size();}
0210 bool empty() const {return m_children.size()?false:true;}
0211 node* operator[](size_t a_index) const{
0212 //WARNING : no check is done on a_index.
0213 return m_children[a_index];
0214 }
0215 const std::vector<node*>& children() const {return m_children;}
0216 std::vector<node*>& children() {return m_children;}
0217
0218 bool insert(unsigned int a_index,node* a_new){ //iv2sg
0219 if(a_index==m_children.size()) {
0220 m_children.push_back(a_new);
0221 return true;
0222 }
0223 unsigned int index = 0;
0224 std::vector<node*>::iterator it;
0225 for(it=m_children.begin();it!=m_children.end();++it,index++) {
0226 if(index==a_index) {
0227 m_children.insert(it,a_new);
0228 return true;
0229 }
0230 }
0231 return false;
0232 }
0233
0234 template <class T>
0235 T* rsearch() const { //used in agora.
0236 for(auto it = m_children.crbegin(); it != m_children.crend(); ++it) {
0237 T* o = safe_cast<node,T>(*(*it));
0238 if(o) return o;
0239 }
0240 return 0;
0241 }
0242
0243 bool position(const node* a_node,unsigned int& a_index) const {
0244 a_index = 0;
0245 tools_vforcit(node*,m_children,it) {
0246 if(a_node==(*it)) return true;
0247 a_index++;
0248 }
0249 return false;
0250 }
0251
0252 node* node_at(unsigned int a_index) const {
0253 if(a_index>=m_children.size()) return 0;
0254 return m_children[a_index];
0255 }
0256
0257 template <class T>
0258 T* child(unsigned int a_index) const {
0259 if(a_index>=m_children.size()) return 0;
0260 node* _node = m_children[a_index];
0261 if(!_node) return 0;
0262 return safe_cast<node,T>(*_node);
0263 }
0264
0265
0266 protected:
0267 bool write_children(write_action& a_action) {
0268 tools_vforcit(node*,m_children,it) {
0269 if(!(*it)->write(a_action)) return false;
0270 }
0271 return true;
0272 }
0273 protected:
0274 std::vector<node*> m_children;
0275 };
0276
0277 }}
0278
0279 #endif