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_mnmx 4 #ifndef tools_sg_mnmx 5 #define tools_sg_mnmx 5 #define tools_sg_mnmx 6 6 7 #include "node" 7 #include "node" 8 #include "bbox_action" 8 #include "bbox_action" 9 9 10 namespace tools { 10 namespace tools { 11 11 12 inline bool mnmx(std::ostream& a_out,sg::node& 12 inline bool mnmx(std::ostream& a_out,sg::node& a_node,vec3f& a_mn,vec3f& a_mx){ 13 sg::bbox_action action(a_out); 13 sg::bbox_action action(a_out); 14 a_node.bbox(action); 14 a_node.bbox(action); 15 if(!action.end() || action.box().is_empty()) 15 if(!action.end() || action.box().is_empty()) { 16 a_out << "tools::mnmx :" 16 a_out << "tools::mnmx :" 17 << " bbox problem." 17 << " bbox problem." 18 << std::endl; 18 << std::endl; 19 a_mn.set_value(0,0,0); 19 a_mn.set_value(0,0,0); 20 a_mx.set_value(0,0,0); 20 a_mx.set_value(0,0,0); 21 return false; 21 return false; 22 } 22 } 23 a_mn = action.box().mn(); 23 a_mn = action.box().mn(); 24 a_mx = action.box().mx(); 24 a_mx = action.box().mx(); 25 return true; 25 return true; 26 } 26 } 27 27 28 } 28 } 29 29 30 #include "matrix" 30 #include "matrix" 31 31 32 namespace tools { 32 namespace tools { 33 33 34 inline bool center_adjust(std::ostream& a_out, 34 inline bool center_adjust(std::ostream& a_out, 35 sg::node& a_node,sg: 35 sg::node& a_node,sg::matrix& a_tsf, 36 unsigned int a_ww,un 36 unsigned int a_ww,unsigned int a_wh, 37 float a_height, 37 float a_height, 38 float& a_dx,float& a 38 float& a_dx,float& a_dy,float& a_dz, 39 bool a_verbose = tru 39 bool a_verbose = true) { 40 //NOTE : we assume an ortho camera. 40 //NOTE : we assume an ortho camera. 41 if(!a_ww||!a_wh) { 41 if(!a_ww||!a_wh) { 42 if(a_verbose) { 42 if(a_verbose) { 43 a_out << "tools::center_adjust :" 43 a_out << "tools::center_adjust :" 44 << " null viewer width or height." 44 << " null viewer width or height." 45 << std::endl; 45 << std::endl; 46 } 46 } 47 a_dx = 0;a_dy = 0;a_dz = 0; 47 a_dx = 0;a_dy = 0;a_dz = 0; 48 return false; 48 return false; 49 } 49 } 50 sg::bbox_action _action(a_out); 50 sg::bbox_action _action(a_out); 51 a_node.bbox(_action); 51 a_node.bbox(_action); 52 if(!_action.box().get_size(a_dx,a_dy,a_dz)) 52 if(!_action.box().get_size(a_dx,a_dy,a_dz)) { 53 if(a_verbose) { 53 if(a_verbose) { 54 a_out << "tools::center_adjust :" 54 a_out << "tools::center_adjust :" 55 << " empty box." 55 << " empty box." 56 << std::endl; 56 << std::endl; 57 } 57 } 58 a_dx = 0;a_dy = 0;a_dz = 0; 58 a_dx = 0;a_dy = 0;a_dz = 0; 59 return false; 59 return false; 60 } 60 } 61 if(!a_dx||!a_dy) { 61 if(!a_dx||!a_dy) { 62 if(a_verbose) { 62 if(a_verbose) { 63 a_out << "tools::center_adjust :" 63 a_out << "tools::center_adjust :" 64 << " dx or dy null." 64 << " dx or dy null." 65 << std::endl; 65 << std::endl; 66 } 66 } 67 a_dx = 0;a_dy = 0;a_dz = 0; 67 a_dx = 0;a_dy = 0;a_dz = 0; 68 return false; 68 return false; 69 } 69 } 70 vec3f c; 70 vec3f c; 71 if(!_action.box().center(c)) { 71 if(!_action.box().center(c)) { 72 if(a_verbose) { 72 if(a_verbose) { 73 a_out << "tools::center_adjust :" 73 a_out << "tools::center_adjust :" 74 << " can't get box center." 74 << " can't get box center." 75 << std::endl; 75 << std::endl; 76 } 76 } 77 a_dx = 0;a_dy = 0;a_dz = 0; 77 a_dx = 0;a_dy = 0;a_dz = 0; 78 return false; 78 return false; 79 } 79 } 80 float vp_aspect = float(a_ww)/float(a_wh); 80 float vp_aspect = float(a_ww)/float(a_wh); 81 float scene_aspect = float(a_dx)/float(a_dy) 81 float scene_aspect = float(a_dx)/float(a_dy); 82 //::printf("debug : set_tsf : %d %d : %g %g 82 //::printf("debug : set_tsf : %d %d : %g %g %g : %g %g\n", 83 // a_ww,a_wh,a_dx,a_dy,a_dz,vp_aspect,sce 83 // a_ww,a_wh,a_dx,a_dy,a_dz,vp_aspect,scene_aspect); 84 float scale; 84 float scale; 85 if(vp_aspect>=scene_aspect) { 85 if(vp_aspect>=scene_aspect) { 86 scale = a_height/a_dy; 86 scale = a_height/a_dy; 87 } else { 87 } else { 88 scale = (vp_aspect*a_height)/a_dx; 88 scale = (vp_aspect*a_height)/a_dx; 89 } 89 } 90 a_tsf.set_scale(scale,scale,scale); 90 a_tsf.set_scale(scale,scale,scale); 91 a_tsf.mul_translate(-c.x(),-c.y(),0); 91 a_tsf.mul_translate(-c.x(),-c.y(),0); 92 return true; 92 return true; 93 } 93 } 94 94 95 inline bool center_adjust(std::ostream& a_out, 95 inline bool center_adjust(std::ostream& a_out, 96 sg::node& a_node,sg: 96 sg::node& a_node,sg::matrix& a_tsf, 97 unsigned int a_ww,un 97 unsigned int a_ww,unsigned int a_wh, 98 float a_height,bool 98 float a_height,bool a_verbose = true) { 99 float dx,dy,dz; 99 float dx,dy,dz; 100 return center_adjust(a_out,a_node,a_tsf,a_ww 100 return center_adjust(a_out,a_node,a_tsf,a_ww,a_wh,a_height, 101 dx,dy,dz,a_verbose); 101 dx,dy,dz,a_verbose); 102 } 102 } 103 103 104 } 104 } 105 105 106 #endif 106 #endif