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 return a_mgr.create_gsto_from_data(xyzs.values());
0045 }
0046
0047 public:
0048 virtual void render(render_action& a_action) {
0049 if(touched()) {clean_gstos();reset_touched();}
0050 if(xyzs.empty()) return;
0051
0052 const state& state = a_action.state();
0053
0054 if(state.m_use_gsto) {
0055 unsigned int _id = get_gsto_id(a_action.out(),a_action.render_manager());
0056 if(_id) {
0057 a_action.begin_gsto(_id);
0058 size_t npt = xyzs.values().size()/3;
0059 bufpos pos = 0;
0060 if(gl::is_line(mode.value())) {
0061 //Same logic as Inventor SoLightModel.model = BASE_COLOR.
0062 a_action.set_lighting(false);
0063 a_action.draw_gsto_v(mode.value(),npt,pos);
0064 a_action.set_lighting(state.m_GL_LIGHTING);
0065 } else {
0066 a_action.draw_gsto_v(mode.value(),npt,pos);
0067 }
0068 a_action.end_gsto();
0069 return;
0070 } else { //!_id
0071 // use immediate rendering.
0072 }
0073
0074 } else {
0075 clean_gstos(&a_action.render_manager());
0076 }
0077
0078
0079 // immediate rendering :
0080 if(gl::is_line(mode.value())) {
0081 //Same logic as Inventor SoLightModel.model = BASE_COLOR.
0082 a_action.set_lighting(false);
0083 a_action.draw_vertex_array(mode.value(),xyzs.values());
0084 a_action.set_lighting(state.m_GL_LIGHTING);
0085 } else {
0086 a_action.draw_vertex_array(mode.value(),xyzs.values());
0087 }
0088
0089 }
0090 virtual void pick(pick_action& a_action) {
0091 if(touched()) {clean_gstos();reset_touched();}
0092 a_action.add__primitive(*this,mode.value(),xyzs.values(),true);
0093 }
0094
0095 virtual void bbox(bbox_action& a_action) {
0096 if(touched()) {clean_gstos();reset_touched();}
0097 a_action.add_points(xyzs.values());
0098 }
0099 virtual void is_visible(visible_action& a_action) {
0100 if(touched()) {clean_gstos();reset_touched();}
0101 if(_is_visible(a_action)) a_action.increment();
0102 }
0103
0104 public:
0105 vertices()
0106 :parent()
0107 ,mode(gl::line_strip()){
0108 add_fields();
0109 }
0110 virtual ~vertices(){
0111 }
0112 public:
0113 vertices(const vertices& a_from)
0114 :parent(a_from)
0115 ,parent_gstos(a_from)
0116 ,mode(a_from.mode)
0117 ,xyzs(a_from.xyzs)
0118 {
0119 add_fields();
0120 }
0121 vertices& operator=(const vertices& a_from){
0122 parent::operator=(a_from);
0123 parent_gstos::operator=(a_from);
0124
0125 mode = a_from.mode;
0126 xyzs = a_from.xyzs;
0127
0128 return *this;
0129 }
0130 public:
0131 template <class VEC>
0132 void add(const VEC& a_v) {
0133 xyzs.add(a_v.x());
0134 xyzs.add(a_v.y());
0135 xyzs.add(a_v.z());
0136 }
0137 void add(float a_x,float a_y,float a_z) {
0138 xyzs.add(a_x);
0139 xyzs.add(a_y);
0140 xyzs.add(a_z);
0141 }
0142 void add_allocated(size_t& a_pos,float a_x,float a_y,float a_z) {
0143 std::vector<float>& v = xyzs.values();
0144 v[a_pos] = a_x;a_pos++;
0145 v[a_pos] = a_y;a_pos++;
0146 v[a_pos] = a_z;a_pos++;
0147 xyzs.touch();
0148 }
0149 bool add(const std::vector<float>& a_v) {
0150 std::vector<float>::size_type _number = a_v.size()/3;
0151 if(3*_number!=a_v.size()) return false;
0152 std::vector<float>::const_iterator it;
0153 for(it=a_v.begin();it!=a_v.end();it+=3) {
0154 xyzs.add(*(it+0));
0155 xyzs.add(*(it+1));
0156 xyzs.add(*(it+2));
0157 }
0158 return true;
0159 }
0160
0161 size_t number() const {return xyzs.size()/3;}
0162 void clear() {xyzs.clear();}
0163
0164 bool add_dashed_line(float a_bx,float a_by,float a_bz,
0165 float a_ex,float a_ey,float a_ez,
0166 unsigned int a_num_dash) {
0167 //optimized version.
0168 if(!a_num_dash) return false;
0169 // there is a dash at beg and end of line.
0170
0171 float fac = 1.0f/float(2*a_num_dash-1);
0172 float sx = (a_ex-a_bx)*fac;
0173 float sy = (a_ey-a_by)*fac;
0174 float sz = (a_ez-a_bz)*fac;
0175
0176 float two_sx = sx*2.0f;
0177 float two_sy = sy*2.0f;
0178 float two_sz = sz*2.0f;
0179
0180 float px = a_bx;
0181 float py = a_by;
0182 float pz = a_bz;
0183
0184 for(unsigned int idash=0;idash<a_num_dash;idash++) {
0185 add(px,py,pz);
0186 add(px+sx,py+sy,pz+sz);
0187 px += two_sx;
0188 py += two_sy;
0189 pz += two_sz;
0190 }
0191 return true;
0192 }
0193 bool center() {
0194 std::vector<float>& v = xyzs.values();
0195 std::vector<float>::size_type _number = v.size()/3;
0196 if(!_number) return true;
0197 if(3*_number!=v.size()) return false;
0198 float x_mean = 0;
0199 float y_mean = 0;
0200 float z_mean = 0;
0201 {for(std::vector<float>::const_iterator it=v.begin();it!=v.end();it+=3) {
0202 x_mean += *(it+0);
0203 y_mean += *(it+1);
0204 z_mean += *(it+2);
0205 }}
0206 x_mean /= float(_number);
0207 y_mean /= float(_number);
0208 z_mean /= float(_number);
0209 {for(std::vector<float>::iterator it=v.begin();it!=v.end();it+=3) {
0210 *(it+0) -= x_mean;
0211 *(it+1) -= y_mean;
0212 *(it+2) -= z_mean;
0213 }}
0214 return true;
0215 }
0216 protected:
0217 bool _is_visible(const matrix_action& a_action) {
0218 if(xyzs.empty()) return false;
0219 const state& _state = a_action.state();
0220 pick_action action(a_action.out(),_state.m_ww,_state.m_wh,0,float(_state.m_ww),0,float(_state.m_wh));
0221 action.set_win_size(_state.m_ww,_state.m_wh);
0222 action.set_area(0,float(_state.m_ww),0,float(_state.m_wh));
0223 action.set_stop_at_first(true);
0224 action.matrix_action::operator=(a_action); //IMPORTANT.
0225 int old_cur = action.cur(); //not 0.
0226 action.add__primitive(*this,mode.value(),xyzs.values(),true);
0227 if(action.cur()!=old_cur) return false;
0228 if(!action.node()) return false;
0229 return true;
0230 }
0231 };
0232
0233 }}
0234
0235 #endif