Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_vertices 5 #define tools_sg_vertices 6 7 #include "node" 8 #include "gstos" 9 10 #include "sf" 11 #include "mf" 12 #include "render_action" 13 #include "pick_action" 14 #include "bbox_action" 15 #include "visible_action" 16 17 #include "../vmanip" 18 19 namespace tools { 20 namespace sg { 21 22 class vertices : public node, public gstos { 23 TOOLS_NODE(vertices,tools::sg::vertices,node 24 typedef gstos parent_gstos; 25 public: 26 sf<gl::mode_t> mode; 27 mf<float> xyzs; 28 public: 29 virtual const desc_fields& node_desc_fields( 30 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::ver 31 static const desc_fields s_v(parent::node_ 32 TOOLS_ARG_FIELD_DESC(mode), 33 TOOLS_ARG_FIELD_DESC(xyzs) 34 ); 35 return s_v; 36 } 37 private: 38 void add_fields(){ 39 add_field(&mode); 40 add_field(&xyzs); 41 } 42 protected: //gstos 43 virtual unsigned int create_gsto(std::ostrea 44 //unsigned int npt = xyzs.values().size()/ 45 //::printf("debug : vertices : %lu : creat 46 return a_mgr.create_gsto_from_data(xyzs.va 47 } 48 49 public: 50 virtual void render(render_action& a_action) 51 if(touched()) {clean_gstos();reset_touched 52 if(xyzs.empty()) return; 53 54 const state& state = a_action.state(); 55 56 if(state.m_use_gsto) { 57 unsigned int _id = get_gsto_id(a_action. 58 if(_id) { 59 #ifdef __APPLE__ 60 bool restore_blend = check_set_blend(a 61 #endif 62 a_action.begin_gsto(_id); 63 size_t npt = xyzs.values().size()/3; 64 bufpos pos = 0; 65 if(gl::is_line(mode.value())) { 66 //Same logic as Inventor SoLightMode 67 a_action.set_lighting(false); 68 a_action.draw_gsto_v(mode.value(),np 69 a_action.set_lighting(state.m_GL_LIG 70 } else { 71 a_action.draw_gsto_v(mode.value(),np 72 } 73 a_action.end_gsto(); 74 #ifdef __APPLE__ 75 if(restore_blend) a_action.set_blend(t 76 #endif 77 return; 78 } else { //!_id 79 // use immediate rendering. 80 } 81 82 } else { 83 clean_gstos(&a_action.render_manager()); 84 } 85 86 #ifdef __APPLE__ 87 bool restore_blend = check_set_blend(a_act 88 #endif 89 90 // immediate rendering : 91 if(gl::is_line(mode.value())) { 92 //Same logic as Inventor SoLightModel.mo 93 a_action.set_lighting(false); 94 a_action.draw_vertex_array(mode.value(), 95 a_action.set_lighting(state.m_GL_LIGHTIN 96 } else { 97 a_action.draw_vertex_array(mode.value(), 98 } 99 100 #ifdef __APPLE__ 101 if(restore_blend) a_action.set_blend(true) 102 #endif 103 } 104 virtual void pick(pick_action& a_action) { 105 if(touched()) {clean_gstos();reset_touched 106 a_action.add__primitive(*this,mode.value() 107 } 108 109 virtual void bbox(bbox_action& a_action) { 110 if(touched()) {clean_gstos();reset_touched 111 a_action.add_points(xyzs.values()); 112 } 113 virtual void is_visible(visible_action& a_ac 114 if(touched()) {clean_gstos();reset_touched 115 if(_is_visible(a_action)) a_action.increme 116 } 117 118 public: 119 vertices() 120 :parent() 121 ,mode(gl::line_strip()){ 122 #ifdef TOOLS_MEM 123 mem::increment(s_class().c_str()); 124 #endif 125 add_fields(); 126 } 127 virtual ~vertices(){ 128 #ifdef TOOLS_MEM 129 mem::decrement(s_class().c_str()); 130 #endif 131 } 132 public: 133 vertices(const vertices& a_from) 134 :parent(a_from) 135 ,parent_gstos(a_from) 136 ,mode(a_from.mode) 137 ,xyzs(a_from.xyzs) 138 { 139 #ifdef TOOLS_MEM 140 mem::increment(s_class().c_str()); 141 #endif 142 add_fields(); 143 } 144 vertices& operator=(const vertices& a_from){ 145 parent::operator=(a_from); 146 parent_gstos::operator=(a_from); 147 148 mode = a_from.mode; 149 xyzs = a_from.xyzs; 150 151 return *this; 152 } 153 public: 154 template <class VEC> 155 void add(const VEC& a_v) { 156 xyzs.add(a_v.x()); 157 xyzs.add(a_v.y()); 158 xyzs.add(a_v.z()); 159 } 160 void add(float a_x,float a_y,float a_z) { 161 xyzs.add(a_x); 162 xyzs.add(a_y); 163 xyzs.add(a_z); 164 } 165 void add_allocated(size_t& a_pos,float a_x,f 166 std::vector<float>& v = xyzs.values(); 167 v[a_pos] = a_x;a_pos++; 168 v[a_pos] = a_y;a_pos++; 169 v[a_pos] = a_z;a_pos++; 170 xyzs.touch(); 171 } 172 bool add(const std::vector<float>& a_v) { 173 std::vector<float>::size_type _number = a_ 174 if(3*_number!=a_v.size()) return false; 175 std::vector<float>::const_iterator it; 176 for(it=a_v.begin();it!=a_v.end();it+=3) { 177 xyzs.add(*(it+0)); 178 xyzs.add(*(it+1)); 179 xyzs.add(*(it+2)); 180 } 181 return true; 182 } 183 184 size_t number() const {return xyzs.size()/3; 185 void clear() {xyzs.clear();} 186 187 bool add_dashed_line(float a_bx,float a_by,f 188 float a_ex,float a_ey,f 189 unsigned int a_num_dash 190 //optimized version. 191 if(!a_num_dash) return false; 192 // there is a dash at beg and end of line. 193 194 float fac = 1.0f/float(2*a_num_dash-1); 195 float sx = (a_ex-a_bx)*fac; 196 float sy = (a_ey-a_by)*fac; 197 float sz = (a_ez-a_bz)*fac; 198 199 float two_sx = sx*2.0f; 200 float two_sy = sy*2.0f; 201 float two_sz = sz*2.0f; 202 203 float px = a_bx; 204 float py = a_by; 205 float pz = a_bz; 206 207 for(unsigned int idash=0;idash<a_num_dash; 208 add(px,py,pz); 209 add(px+sx,py+sy,pz+sz); 210 px += two_sx; 211 py += two_sy; 212 pz += two_sz; 213 } 214 return true; 215 } 216 bool center() { 217 std::vector<float>& v = xyzs.values(); 218 std::vector<float>::size_type _number = v. 219 if(!_number) return true; 220 if(3*_number!=v.size()) return false; 221 float x_mean = 0; 222 float y_mean = 0; 223 float z_mean = 0; 224 {for(std::vector<float>::const_iterator it= 225 x_mean += *(it+0); 226 y_mean += *(it+1); 227 z_mean += *(it+2); 228 }} 229 x_mean /= float(_number); 230 y_mean /= float(_number); 231 z_mean /= float(_number); 232 {for(std::vector<float>::iterator it=v.begi 233 *(it+0) -= x_mean; 234 *(it+1) -= y_mean; 235 *(it+2) -= z_mean; 236 }} 237 return true; 238 } 239 protected: 240 bool _is_visible(const matrix_action& a_acti 241 if(xyzs.empty()) return false; 242 const state& _state = a_action.state(); 243 pick_action action(a_action.out(),_state.m 244 action.set_win_size(_state.m_ww,_state.m_w 245 action.set_area(0,float(_state.m_ww),0,flo 246 action.set_stop_at_first(true); 247 action.matrix_action::operator=(a_action); 248 int old_cur = action.cur(); //not 0. 249 action.add__primitive(*this,mode.value(),x 250 if(action.cur()!=old_cur) return false; 251 if(!action.node()) return false; 252 return true; 253 } 254 #ifdef __APPLE__ 255 protected: 256 // macOS/Mojave : on this version, points ar 257 bool check_set_blend(render_action& a_action 258 bool restore_blend = false; 259 const state& state = a_action.state(); 260 if(state.m_GL_BLEND) { 261 if(state.m_color.a()==1) { 262 a_action.set_blend(false); 263 restore_blend = true; 264 } 265 } 266 return restore_blend; 267 } 268 #endif //__APPLE__ 269 }; 270 271 }} 272 273 #endif