Warning, /include/Geant4/tools/sg/vertices 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_vertices
0005 #define tools_sg_vertices
0006
0007 #include "node"
0008 #include "gstos"
0009
0010 #include "sf"
0011 #include "mf"
0012 #include "render_action"
0013 #include "pick_action"
0014 #include "bbox_action"
0015 #include "visible_action"
0016
0017 #include "../vmanip"
0018
0019 namespace tools {
0020 namespace sg {
0021
0022 class vertices : public node, public gstos {
0023 TOOLS_NODE(vertices,tools::sg::vertices,node)
0024 typedef gstos parent_gstos;
0025 public:
0026 sf<gl::mode_t> mode;
0027 mf<float> xyzs;
0028 public:
0029 virtual const desc_fields& node_desc_fields() const {
0030 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::vertices)
0031 static const desc_fields s_v(parent::node_desc_fields(),2, //WARNING : take care of count.
0032 TOOLS_ARG_FIELD_DESC(mode),
0033 TOOLS_ARG_FIELD_DESC(xyzs)
0034 );
0035 return s_v;
0036 }
0037 private:
0038 void add_fields(){
0039 add_field(&mode);
0040 add_field(&xyzs);
0041 }
0042 protected: //gstos
0043 virtual unsigned int create_gsto(std::ostream&,sg::render_manager& a_mgr) {
0044 //unsigned int npt = xyzs.values().size()/3;
0045 //::printf("debug : vertices : %lu : create_gsto : %u\n",this,npt);
0046 return a_mgr.create_gsto_from_data(xyzs.values());
0047 }
0048
0049 public:
0050 virtual void render(render_action& a_action) {
0051 if(touched()) {clean_gstos();reset_touched();}
0052 if(xyzs.empty()) return;
0053
0054 const state& state = a_action.state();
0055
0056 if(state.m_use_gsto) {
0057 unsigned int _id = get_gsto_id(a_action.out(),a_action.render_manager());
0058 if(_id) {
0059 #ifdef __APPLE__
0060 bool restore_blend = check_set_blend(a_action);
0061 #endif
0062 a_action.begin_gsto(_id);
0063 size_t npt = xyzs.values().size()/3;
0064 bufpos pos = 0;
0065 if(gl::is_line(mode.value())) {
0066 //Same logic as Inventor SoLightModel.model = BASE_COLOR.
0067 a_action.set_lighting(false);
0068 a_action.draw_gsto_v(mode.value(),npt,pos);
0069 a_action.set_lighting(state.m_GL_LIGHTING);
0070 } else {
0071 a_action.draw_gsto_v(mode.value(),npt,pos);
0072 }
0073 a_action.end_gsto();
0074 #ifdef __APPLE__
0075 if(restore_blend) a_action.set_blend(true);
0076 #endif
0077 return;
0078 } else { //!_id
0079 // use immediate rendering.
0080 }
0081
0082 } else {
0083 clean_gstos(&a_action.render_manager());
0084 }
0085
0086 #ifdef __APPLE__
0087 bool restore_blend = check_set_blend(a_action);
0088 #endif
0089
0090 // immediate rendering :
0091 if(gl::is_line(mode.value())) {
0092 //Same logic as Inventor SoLightModel.model = BASE_COLOR.
0093 a_action.set_lighting(false);
0094 a_action.draw_vertex_array(mode.value(),xyzs.values());
0095 a_action.set_lighting(state.m_GL_LIGHTING);
0096 } else {
0097 a_action.draw_vertex_array(mode.value(),xyzs.values());
0098 }
0099
0100 #ifdef __APPLE__
0101 if(restore_blend) a_action.set_blend(true);
0102 #endif
0103 }
0104 virtual void pick(pick_action& a_action) {
0105 if(touched()) {clean_gstos();reset_touched();}
0106 a_action.add__primitive(*this,mode.value(),xyzs.values(),true);
0107 }
0108
0109 virtual void bbox(bbox_action& a_action) {
0110 if(touched()) {clean_gstos();reset_touched();}
0111 a_action.add_points(xyzs.values());
0112 }
0113 virtual void is_visible(visible_action& a_action) {
0114 if(touched()) {clean_gstos();reset_touched();}
0115 if(_is_visible(a_action)) a_action.increment();
0116 }
0117
0118 public:
0119 vertices()
0120 :parent()
0121 ,mode(gl::line_strip()){
0122 #ifdef TOOLS_MEM
0123 mem::increment(s_class().c_str());
0124 #endif
0125 add_fields();
0126 }
0127 virtual ~vertices(){
0128 #ifdef TOOLS_MEM
0129 mem::decrement(s_class().c_str());
0130 #endif
0131 }
0132 public:
0133 vertices(const vertices& a_from)
0134 :parent(a_from)
0135 ,parent_gstos(a_from)
0136 ,mode(a_from.mode)
0137 ,xyzs(a_from.xyzs)
0138 {
0139 #ifdef TOOLS_MEM
0140 mem::increment(s_class().c_str());
0141 #endif
0142 add_fields();
0143 }
0144 vertices& operator=(const vertices& a_from){
0145 parent::operator=(a_from);
0146 parent_gstos::operator=(a_from);
0147
0148 mode = a_from.mode;
0149 xyzs = a_from.xyzs;
0150
0151 return *this;
0152 }
0153 public:
0154 template <class VEC>
0155 void add(const VEC& a_v) {
0156 xyzs.add(a_v.x());
0157 xyzs.add(a_v.y());
0158 xyzs.add(a_v.z());
0159 }
0160 void add(float a_x,float a_y,float a_z) {
0161 xyzs.add(a_x);
0162 xyzs.add(a_y);
0163 xyzs.add(a_z);
0164 }
0165 void add_allocated(size_t& a_pos,float a_x,float a_y,float a_z) {
0166 std::vector<float>& v = xyzs.values();
0167 v[a_pos] = a_x;a_pos++;
0168 v[a_pos] = a_y;a_pos++;
0169 v[a_pos] = a_z;a_pos++;
0170 xyzs.touch();
0171 }
0172 bool add(const std::vector<float>& a_v) {
0173 std::vector<float>::size_type _number = a_v.size()/3;
0174 if(3*_number!=a_v.size()) return false;
0175 std::vector<float>::const_iterator it;
0176 for(it=a_v.begin();it!=a_v.end();it+=3) {
0177 xyzs.add(*(it+0));
0178 xyzs.add(*(it+1));
0179 xyzs.add(*(it+2));
0180 }
0181 return true;
0182 }
0183
0184 size_t number() const {return xyzs.size()/3;}
0185 void clear() {xyzs.clear();}
0186
0187 bool add_dashed_line(float a_bx,float a_by,float a_bz,
0188 float a_ex,float a_ey,float a_ez,
0189 unsigned int a_num_dash) {
0190 //optimized version.
0191 if(!a_num_dash) return false;
0192 // there is a dash at beg and end of line.
0193
0194 float fac = 1.0f/float(2*a_num_dash-1);
0195 float sx = (a_ex-a_bx)*fac;
0196 float sy = (a_ey-a_by)*fac;
0197 float sz = (a_ez-a_bz)*fac;
0198
0199 float two_sx = sx*2.0f;
0200 float two_sy = sy*2.0f;
0201 float two_sz = sz*2.0f;
0202
0203 float px = a_bx;
0204 float py = a_by;
0205 float pz = a_bz;
0206
0207 for(unsigned int idash=0;idash<a_num_dash;idash++) {
0208 add(px,py,pz);
0209 add(px+sx,py+sy,pz+sz);
0210 px += two_sx;
0211 py += two_sy;
0212 pz += two_sz;
0213 }
0214 return true;
0215 }
0216 bool center() {
0217 std::vector<float>& v = xyzs.values();
0218 std::vector<float>::size_type _number = v.size()/3;
0219 if(!_number) return true;
0220 if(3*_number!=v.size()) return false;
0221 float x_mean = 0;
0222 float y_mean = 0;
0223 float z_mean = 0;
0224 {for(std::vector<float>::const_iterator it=v.begin();it!=v.end();it+=3) {
0225 x_mean += *(it+0);
0226 y_mean += *(it+1);
0227 z_mean += *(it+2);
0228 }}
0229 x_mean /= float(_number);
0230 y_mean /= float(_number);
0231 z_mean /= float(_number);
0232 {for(std::vector<float>::iterator it=v.begin();it!=v.end();it+=3) {
0233 *(it+0) -= x_mean;
0234 *(it+1) -= y_mean;
0235 *(it+2) -= z_mean;
0236 }}
0237 return true;
0238 }
0239 protected:
0240 bool _is_visible(const matrix_action& a_action) {
0241 if(xyzs.empty()) return false;
0242 const state& _state = a_action.state();
0243 pick_action action(a_action.out(),_state.m_ww,_state.m_wh,0,float(_state.m_ww),0,float(_state.m_wh));
0244 action.set_win_size(_state.m_ww,_state.m_wh);
0245 action.set_area(0,float(_state.m_ww),0,float(_state.m_wh));
0246 action.set_stop_at_first(true);
0247 action.matrix_action::operator=(a_action); //IMPORTANT.
0248 int old_cur = action.cur(); //not 0.
0249 action.add__primitive(*this,mode.value(),xyzs.values(),true);
0250 if(action.cur()!=old_cur) return false;
0251 if(!action.node()) return false;
0252 return true;
0253 }
0254 #ifdef __APPLE__
0255 protected:
0256 // macOS/Mojave : on this version, points are blended even if alpha is one !
0257 bool check_set_blend(render_action& a_action) {
0258 bool restore_blend = false;
0259 const state& state = a_action.state();
0260 if(state.m_GL_BLEND) {
0261 if(state.m_color.a()==1) {
0262 a_action.set_blend(false);
0263 restore_blend = true;
0264 }
0265 }
0266 return restore_blend;
0267 }
0268 #endif //__APPLE__
0269 };
0270
0271 }}
0272
0273 #endif