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