g4tools  5.4.0
vertices
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
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 {
24  typedef gstos parent_gstos;
25 public:
28 public:
29  virtual const desc_fields& node_desc_fields() const {
31  static const desc_fields s_v(parent::node_desc_fields(),2, //WARNING : take care of count.
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::ostream&,sg::render_manager& a_mgr) {
44  //unsigned int npt = xyzs.values().size()/3;
45  //::printf("debug : vertices : %lu : create_gsto : %u\n",this,npt);
46  return a_mgr.create_gsto_from_data(xyzs.values());
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.out(),a_action.render_manager());
58  if(_id) {
59 #ifdef __APPLE__
60  bool restore_blend = check_set_blend(a_action);
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 SoLightModel.model = BASE_COLOR.
67  a_action.set_lighting(false);
68  a_action.draw_gsto_v(mode.value(),npt,pos);
70  } else {
71  a_action.draw_gsto_v(mode.value(),npt,pos);
72  }
73  a_action.end_gsto();
74 #ifdef __APPLE__
75  if(restore_blend) a_action.set_blend(true);
76 #endif
77  return;
78  } else {
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_action);
88 #endif
89 
90  // immediate rendering :
91  if(gl::is_line(mode.value())) {
92  //Same logic as Inventor SoLightModel.model = BASE_COLOR.
93  a_action.set_lighting(false);
94  a_action.draw_vertex_array(mode.value(),xyzs.values());
96  } else {
97  a_action.draw_vertex_array(mode.value(),xyzs.values());
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(),xyzs.values(),true);
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_action) {
114  if(touched()) {clean_gstos();reset_touched();}
115  if(_is_visible(a_action)) a_action.increment();
116  }
117 
118 public:
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,float a_y,float a_z) {
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_v.size()/3;
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,float a_bz,
188  float a_ex,float a_ey,float a_ez,
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;idash++) {
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.size()/3;
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=v.begin();it!=v.end();it+=3) {
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.begin();it!=v.end();it+=3) {
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_action) {
241  if(xyzs.empty()) return false;
242  const state& _state = a_action.state();
243  pick_action action(a_action.out(),_state.m_ww,_state.m_wh,0,float(_state.m_ww),0,float(_state.m_wh));
244  action.set_win_size(_state.m_ww,_state.m_wh);
245  action.set_area(0,float(_state.m_ww),0,float(_state.m_wh));
246  action.set_stop_at_first(true);
247  action.matrix_action::operator=(a_action); //IMPORTANT.
248  int old_cur = action.cur(); //not 0.
249  action.add__primitive(*this,mode.value(),xyzs.values(),true);
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 are blended even if alpha is one !
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
tools::sg::vertices::render
virtual void render(render_action &a_action)
Definition: vertices:50
tools::sg::field::touch
void touch()
Definition: field:59
tools::sg::gstos
Definition: gstos:16
node
tools::sg::vertices::~vertices
virtual ~vertices()
Definition: vertices:127
tools::sg::vertices::add
void add(float a_x, float a_y, float a_z)
Definition: vertices:160
tools::sg::render_action::draw_gsto_v
virtual void draw_gsto_v(gl::mode_t, size_t, bufpos)=0
tools::sg::render_action::draw_vertex_array
virtual void draw_vertex_array(gl::mode_t, size_t, const float *)=0
tools::sg::state
Definition: state:25
tools::sg::render_action::set_lighting
virtual void set_lighting(bool)=0
tools::sg::bmf::empty
bool empty() const
Definition: bmf:70
tools::gl::is_line
bool is_line(mode_t a_mode)
Definition: glprims:31
tools::sg::state::m_GL_LIGHTING
bool m_GL_LIGHTING
Definition: state:248
tools::sg::vertices::operator=
vertices & operator=(const vertices &a_from)
Definition: vertices:144
tools::sg::vertices::mode
sf< gl::mode_t > mode
Definition: vertices:26
tools::sg::action
Definition: action:19
tools::sg::bmf::add
void add(const T &a_value)
Definition: bmf:73
pick_action
tools::sg::vertices::clear
void clear()
Definition: vertices:185
render_action
tools::sg::vertices::vertices
vertices(const vertices &a_from)
Definition: vertices:133
tools::sg::gstos::clean_gstos
void clean_gstos()
Definition: gstos:89
tools::sg::node
Definition: node:28
tools::sg::bbox_action
Definition: bbox_action:15
TOOLS_NODE
#define TOOLS_NODE(a__class, a__sclass, a__parent)
Definition: node:324
mf
tools::sg::vertices::vertices
vertices()
Definition: vertices:119
tools::sg::matrix_action
Definition: matrix_action:15
tools::gl::line_strip
mode_t line_strip()
Definition: glprims:19
tools::sg::desc_fields
Definition: field_desc:148
tools::sg::gstos::operator=
gstos & operator=(const gstos &a_from)
Definition: gstos:22
tools::sg::bmf::values
const std::vector< T > & values() const
Definition: bmf:71
tools::sg::vertices::create_gsto
virtual unsigned int create_gsto(std::ostream &, sg::render_manager &a_mgr)
Definition: vertices:43
tools::sg::render_action::set_blend
virtual void set_blend(bool)=0
tools::sg::pick_action
Definition: pick_action:59
tools::sg::pick_action::add__primitive
bool add__primitive(sg::node &a_node, gl::mode_t a_mode, size_t a_floatn, const float *a_xyzs, bool a_stop=false)
Definition: pick_action:431
tools::sg::render_manager::create_gsto_from_data
virtual unsigned int create_gsto_from_data(size_t, const float *)=0
tools::sg::vertices::is_visible
virtual void is_visible(visible_action &a_action)
Definition: vertices:113
tools::sg::state::m_wh
unsigned int m_wh
Definition: state:242
tools::sg::mf< float >
tools::sg::vertices::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: vertices:29
tools::sg::vertices::add_allocated
void add_allocated(size_t &a_pos, float a_x, float a_y, float a_z)
Definition: vertices:165
tools::sg::state::m_use_gsto
bool m_use_gsto
Definition: state:256
tools::sg::render_manager
Definition: render_manager:16
tools::sg::vertices::center
bool center()
Definition: vertices:216
tools::sg::vertices::bbox
virtual void bbox(bbox_action &a_action)
Definition: vertices:109
tools::sg::vertices::_is_visible
bool _is_visible(const matrix_action &a_action)
Definition: vertices:240
tools::sg::render_action::render_manager
virtual sg::render_manager & render_manager()=0
sf
tools::sg::primitive_visitor::add_points
bool add_points(size_t a_floatn, const float *a_xyzs, bool a_stop=false)
Definition: primitive_visitor:1405
tools::colorf::a
float a() const
Definition: colorf:35
gstos
tools::sg::render_action
Definition: render_action:24
tools::sg::gstos::get_gsto_id
unsigned int get_gsto_id(std::ostream &a_out, render_manager &a_mgr)
Definition: gstos:60
tools::sg::render_action::begin_gsto
virtual void begin_gsto(gstoid)=0
VBO /////////////////////////////////////////////////////////.
visible_action
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::bmf::clear
void clear()
Definition: bmf:108
tools::sg::node::add_field
void add_field(field *a_field)
Definition: node:128
tools::sg::render_action::end_gsto
virtual void end_gsto()=0
tools::sg::vertices::add_dashed_line
bool add_dashed_line(float a_bx, float a_by, float a_bz, float a_ex, float a_ey, float a_ez, unsigned int a_num_dash)
Definition: vertices:187
tools::sg::vertices::add
bool add(const std::vector< float > &a_v)
Definition: vertices:172
tools::sg::state::m_ww
unsigned int m_ww
Definition: state:241
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::sg::vertices::xyzs
mf< float > xyzs
Definition: vertices:27
tools::sg::visible_action::increment
void increment()
Definition: visible_action:33
tools::sg::vertices::pick
virtual void pick(pick_action &a_action)
Definition: vertices:104
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::action::out
std::ostream & out() const
Definition: action:51
tools::sg::bufpos
size_t bufpos
Definition: render_action:22
tools::sg::bmf::size
size_t size() const
Definition: bmf:69
tools::sg::state::m_color
colorf m_color
Definition: state:259
tools::sg::state::m_GL_BLEND
bool m_GL_BLEND
Definition: state:254
tools::sg::states::state
const sg::state & state() const
Definition: states:76
tools::sg::visible_action
Definition: visible_action:12
tools::sg::vertices::number
size_t number() const
Definition: vertices:184
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::vertices::add
void add(const VEC &a_v)
Definition: vertices:155
tools::sg::sf< gl::mode_t >
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71
tools::sg::node::reset_touched
virtual void reset_touched()
Definition: node:102
bbox_action
tools::sg::vertices
Definition: vertices:22