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_gstos 4 #ifndef tools_sg_gstos 5 #define tools_sg_gstos 5 #define tools_sg_gstos 6 6 7 #include "render_manager" 7 #include "render_manager" 8 #include <vector> 8 #include <vector> 9 #include <ostream> 9 #include <ostream> 10 10 11 //#define TOOLS_SG_GSTOS_DEBUG 11 //#define TOOLS_SG_GSTOS_DEBUG 12 12 13 namespace tools { 13 namespace tools { 14 namespace sg { 14 namespace sg { 15 15 16 class gstos { 16 class gstos { 17 protected: 17 protected: 18 gstos() {} 18 gstos() {} 19 virtual ~gstos() {clean_gstos();} 19 virtual ~gstos() {clean_gstos();} 20 protected: 20 protected: 21 gstos(const gstos&) {} 21 gstos(const gstos&) {} 22 gstos& operator=(const gstos& a_from) { 22 gstos& operator=(const gstos& a_from) { 23 if(&a_from==this) return *this; 23 if(&a_from==this) return *this; 24 clean_gstos(); 24 clean_gstos(); 25 return *this; 25 return *this; 26 } 26 } 27 public: 27 public: 28 size_t num_gstos() const {return m_gstos.siz 28 size_t num_gstos() const {return m_gstos.size();} 29 protected: 29 protected: 30 unsigned int get_tex_id(std::ostream& a_out, 30 unsigned int get_tex_id(std::ostream& a_out,render_manager& a_mgr,const img_byte& a_img,bool a_NEAREST) { 31 unsigned int _id = _find(&a_mgr); 31 unsigned int _id = _find(&a_mgr); 32 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 32 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 33 #ifdef TOOLS_SG_GSTOS_DEBUG 33 #ifdef TOOLS_SG_GSTOS_DEBUG 34 a_out << "debug : tools::sg::gstos::get_ 34 a_out << "debug : tools::sg::gstos::get_tex_id : " << _id << " not valid." << std::endl; 35 #endif 35 #endif 36 clean_gstos(&a_mgr); 36 clean_gstos(&a_mgr); 37 _id = 0; 37 _id = 0; 38 } 38 } 39 if(!_id) { 39 if(!_id) { 40 #ifdef TOOLS_SG_GSTOS_DEBUG 40 #ifdef TOOLS_SG_GSTOS_DEBUG 41 a_out << "debug : tools::sg::gstos::get_ 41 a_out << "debug : tools::sg::gstos::get_tex_id : create_texture ... " << std::endl; 42 #endif 42 #endif 43 _id = a_mgr.create_texture(a_img,a_NEARE 43 _id = a_mgr.create_texture(a_img,a_NEAREST); 44 #ifdef TOOLS_SG_GSTOS_DEBUG 44 #ifdef TOOLS_SG_GSTOS_DEBUG 45 a_out << "debug : tools::sg::gstos::get_ 45 a_out << "debug : tools::sg::gstos::get_tex_id : create_texture : _id " << _id << "." << std::endl; 46 #endif 46 #endif 47 if(!_id) { 47 if(!_id) { 48 a_out << "tools::sg::gstos::get_tex_id 48 a_out << "tools::sg::gstos::get_tex_id :" 49 << " render_manager.create_textu 49 << " render_manager.create_texture() failed." 50 << std::endl; 50 << std::endl; 51 } else { 51 } else { 52 m_gstos.push_back(std::pair<unsigned i 52 m_gstos.push_back(std::pair<unsigned int,render_manager*>(_id,&a_mgr)); 53 } 53 } 54 } 54 } 55 return _id; 55 return _id; 56 } 56 } 57 protected: 57 protected: 58 virtual unsigned int create_gsto(std::ostrea 58 virtual unsigned int create_gsto(std::ostream&,render_manager&) {return 0;} 59 59 60 unsigned int get_gsto_id(std::ostream& a_out 60 unsigned int get_gsto_id(std::ostream& a_out,render_manager& a_mgr){ 61 unsigned int _id = _find(&a_mgr); 61 unsigned int _id = _find(&a_mgr); 62 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 62 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 63 #ifdef TOOLS_SG_GSTOS_DEBUG 63 #ifdef TOOLS_SG_GSTOS_DEBUG 64 a_out << "debug : tools::sg::gstos::get_ 64 a_out << "debug : tools::sg::gstos::get_gsto_id : " << _id << " not valid." << std::endl; 65 #endif 65 #endif 66 clean_gstos(&a_mgr); 66 clean_gstos(&a_mgr); 67 _id = 0; 67 _id = 0; 68 } 68 } 69 if(!_id) { 69 if(!_id) { 70 #ifdef TOOLS_SG_GSTOS_DEBUG 70 #ifdef TOOLS_SG_GSTOS_DEBUG 71 a_out << "debug : tools::sg::gstos::get_ 71 a_out << "debug : tools::sg::gstos::get_gsto_id : create_gsto ..." << std::endl; 72 #endif 72 #endif 73 _id = create_gsto(a_out,a_mgr); 73 _id = create_gsto(a_out,a_mgr); 74 #ifdef TOOLS_SG_GSTOS_DEBUG 74 #ifdef TOOLS_SG_GSTOS_DEBUG 75 a_out << "debug : tools::sg::gstos::get_ 75 a_out << "debug : tools::sg::gstos::get_gsto_id : create_gsto : _id " << _id << "." << std::endl; 76 #endif 76 #endif 77 if(!_id) { 77 if(!_id) { 78 // could be ok if there is no graphica 78 // could be ok if there is no graphical data to load. 79 //a_out << "tools::sg::gstos::get_gsto 79 //a_out << "tools::sg::gstos::get_gsto_id :" 80 // << " create_gsto() failed." 80 // << " create_gsto() failed." 81 // << std::endl; 81 // << std::endl; 82 } else { 82 } else { 83 m_gstos.push_back(std::pair<unsigned i 83 m_gstos.push_back(std::pair<unsigned int,render_manager*>(_id,&a_mgr)); 84 } 84 } 85 } 85 } 86 return _id; 86 return _id; 87 } 87 } 88 protected: 88 protected: 89 void clean_gstos() { 89 void clean_gstos() { 90 std::vector< std::pair<unsigned int,render 90 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 91 for(it=m_gstos.begin();it!=m_gstos.end();) 91 for(it=m_gstos.begin();it!=m_gstos.end();){ 92 (*it).second->delete_gsto((*it).first); 92 (*it).second->delete_gsto((*it).first); 93 it = m_gstos.erase(it); 93 it = m_gstos.erase(it); 94 } 94 } 95 } 95 } 96 void clean_gstos(render_manager* a_mgr) { 96 void clean_gstos(render_manager* a_mgr) { 97 std::vector< std::pair<unsigned int,render 97 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 98 for(it=m_gstos.begin();it!=m_gstos.end();) 98 for(it=m_gstos.begin();it!=m_gstos.end();){ 99 if((*it).second==a_mgr) { 99 if((*it).second==a_mgr) { 100 (*it).second->delete_gsto((*it).first) 100 (*it).second->delete_gsto((*it).first); 101 it = m_gstos.erase(it); 101 it = m_gstos.erase(it); 102 } else { 102 } else { 103 it++; 103 it++; 104 } 104 } 105 } 105 } 106 } 106 } 107 protected: 107 protected: 108 unsigned int _find(render_manager* a_mgr) { 108 unsigned int _find(render_manager* a_mgr) { 109 std::vector< std::pair<unsigned int,render 109 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 110 for(it=m_gstos.begin();it!=m_gstos.end();+ 110 for(it=m_gstos.begin();it!=m_gstos.end();++it){ 111 if((*it).second==a_mgr) return (*it).fir 111 if((*it).second==a_mgr) return (*it).first; 112 } 112 } 113 return 0; 113 return 0; 114 } 114 } 115 115 116 protected: 116 protected: 117 std::vector< std::pair<unsigned int,render_m 117 std::vector< std::pair<unsigned int,render_manager*> > m_gstos; 118 }; 118 }; 119 119 120 }} 120 }} 121 121 122 #endif 122 #endif