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