g4tools  5.4.0
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
tools::sg::polyhedron Class Reference
Inheritance diagram for tools::sg::polyhedron:
Inheritance graph
[legend]
Collaboration diagram for tools::sg::polyhedron:
Collaboration graph
[legend]

Public Types

enum  update_what { faces = 0, lines = 1, faces_and_lines }
 
typedef tessellate::prims_t prims_t
 

Public Member Functions

virtual const desc_fieldsnode_desc_fields () const
 
virtual void render (render_action &a_action)
 
virtual void bbox (bbox_action &a_action)
 
virtual void pick (pick_action &a_action)
 
 polyhedron ()
 
virtual ~polyhedron ()
 
 polyhedron (const polyhedron &a_from)
 
polyhedronoperator= (const polyhedron &a_from)
 
void update_sg (std::ostream &a_out, const update_what &a_what)
 
const std::vector< float > & xyzs () const
 
- Public Member Functions inherited from tools::sg::node
virtual void * cast (const std::string &a_class) const
 
virtual const std::string & s_cls () const =0
 
virtual nodecopy () const =0
 
virtual unsigned int cls_version () const
 
virtual void search (search_action &a_action)
 
virtual void get_matrix (get_matrix_action &a_action)
 
virtual bool write (write_action &a_action)
 
virtual void event (event_action &)
 
virtual bool read (read_action &a_action)
 
virtual void is_visible (visible_action &)
 
virtual void protocol_one_fields (std::vector< field * > &a_fields) const
 
virtual bool draw_in_frame_buffer () const
 
virtual bool touched ()
 
virtual void reset_touched ()
 
 node ()
 
virtual ~node ()
 
void touch ()
 
fieldfield_from_desc (const field_desc &a_desc) const
 
void dump_field_descs (std::ostream &a_out) const
 
fieldfind_field_by_name (const std::string &a_name) const
 
- Public Member Functions inherited from tools::sg::gstos
size_t num_gstos () const
 

Public Attributes

hep::sf_polyhedron ph
 
sf< bool > solid
 
sf< bool > reduced_wire_frame
 
sf< bool > enforce_edges
 

Protected Member Functions

virtual unsigned int create_gsto (std::ostream &a_out, render_manager &a_mgr)
 
void _clear ()
 
- Protected Member Functions inherited from tools::sg::node
 node (const node &)
 
nodeoperator= (const node &)
 
void add_field (field *a_field)
 
bool write_fields (write_action &a_action)
 
bool read_fields (read_action &a_action)
 
field_desc::offset_t field_offset (const field *a_field) const
 
fieldfind_field (const field_desc &a_rdesc) const
 
void check_fields (std::ostream &a_out) const
 
- Protected Member Functions inherited from tools::sg::gstos
 gstos ()
 
virtual ~gstos ()
 
 gstos (const gstos &)
 
gstosoperator= (const gstos &a_from)
 
unsigned int get_tex_id (std::ostream &a_out, render_manager &a_mgr, const img_byte &a_img, bool a_NEAREST)
 
unsigned int get_gsto_id (std::ostream &a_out, render_manager &a_mgr)
 
void clean_gstos ()
 
void clean_gstos (render_manager *a_mgr)
 
unsigned int _find (render_manager *a_mgr)
 

Protected Attributes

std::vector< float > m_xyzs
 
tessellatem_tess
 
std::vector< prims_tm_primss
 
size_t m_size
 
bool m_draw_edges
 
- Protected Attributes inherited from tools::sg::gstos
std::vector< std::pair< unsigned int, render_manager * > > m_gstos
 

Detailed Description

Definition at line 23 of file polyhedron.

Member Typedef Documentation

◆ prims_t

Definition at line 50 of file polyhedron.

Member Enumeration Documentation

◆ update_what

Enumerator
faces 
lines 
faces_and_lines 

Definition at line 336 of file polyhedron.

336  {
337  faces = 0,
338  lines = 1,
340  };

Constructor & Destructor Documentation

◆ polyhedron() [1/2]

tools::sg::polyhedron::polyhedron ( )
inline

Definition at line 293 of file polyhedron.

294  :parent()
295  ,ph(hep::polyhedron())
296  ,solid(true)
297  ,reduced_wire_frame(true)
298  ,enforce_edges(false)
299 
300  ,m_tess(0)
301  {
302  add_fields();
303  }

◆ ~polyhedron()

virtual tools::sg::polyhedron::~polyhedron ( )
inlinevirtual

Definition at line 304 of file polyhedron.

304  {
305  _clear();
306  delete m_tess;
307  }

◆ polyhedron() [2/2]

tools::sg::polyhedron::polyhedron ( const polyhedron a_from)
inline

Definition at line 309 of file polyhedron.

310  :parent(a_from)
311  ,gstos(a_from)
312  ,ph(a_from.ph)
313  ,solid(a_from.solid)
314  ,reduced_wire_frame(a_from.reduced_wire_frame)
315  ,enforce_edges(a_from.enforce_edges)
316 
317  ,m_xyzs(a_from.m_xyzs)
318  ,m_tess(0)
319  {
320  add_fields();
321  }

Member Function Documentation

◆ _clear()

void tools::sg::polyhedron::_clear ( )
inlineprotected

Definition at line 480 of file polyhedron.

480  {
482  m_primss.clear();
483  m_xyzs.clear();
484  }

◆ bbox()

virtual void tools::sg::polyhedron::bbox ( bbox_action a_action)
inlinevirtual

Reimplemented from tools::sg::node.

Definition at line 254 of file polyhedron.

254  {
255  if(touched()) {
256  update_sg(a_action.out(),lines);
257  reset_touched();
258  }
259  if(solid.value()) {
261  const prims_t& prims = *itt;
262  tools_vforcit(tess_prim*,prims,it) {
263  const tess_prim& item = *(*it);
264  const std::vector<float>& coords = item.m_coords;
265  a_action.add_points(coords);
266  }
267  }
268  } else {
269  a_action.add_points(m_xyzs);
270  }
271  }

◆ create_gsto()

virtual unsigned int tools::sg::polyhedron::create_gsto ( std::ostream &  a_out,
render_manager a_mgr 
)
inlineprotectedvirtual

Reimplemented from tools::sg::gstos.

Definition at line 53 of file polyhedron.

53  {
54  m_size = 0;
55  if(solid.value()) {
56 
57  size_t sz = 0;
58 
59  if(m_draw_edges) sz += m_xyzs.size();
60 
62  const prims_t& prims = *itt;
63  tools_vforcit(tess_prim*,prims,it) {
64  const tess_prim& item = *(*it);
65  const std::vector<float>& coords = item.m_coords;
66  const std::vector<float>& norms = item.m_norms;
67  if(coords.empty()) continue;
68  if(norms.size()!=coords.size()) {
69  a_out << "tools::sg::polyhedron::create_gsto :"
70  << " bad number of normals."
71  << std::endl;
72  continue;
73  }
74  sz += coords.size()+norms.size();
75  }
76  }}
77 
78  if(sz) {
79 
80  float* buffer = new float[sz];
81  char* pos = (char*)buffer;
82  size_t psz = 0;
83 
84  if(m_draw_edges) {
85  if(m_xyzs.size()) {
86  size_t _sz = m_xyzs.size()*sizeof(float);
87  ::memcpy(pos,vec_data<float>(m_xyzs),_sz);
88  pos += _sz;
89  psz += _sz;
90  m_size = m_xyzs.size();
91  //m_xyzs.clear(); //we need them for pick,write,...
92  }
93  }
94 
96  const prims_t& prims = *itt;
97  tools_vforcit(tess_prim*,prims,it) {
98  tess_prim& item = *(*it);
99  const std::vector<float>& coords = item.m_coords;
100  const std::vector<float>& norms = item.m_norms;
101  if(coords.empty()) continue;
102  if(norms.size()!=coords.size()) continue;
103 
104  item.m_size = coords.size();
105 
106  {item.m_pos_coords = psz;
107  size_t _sz = coords.size()*sizeof(float);
108  ::memcpy(pos,vec_data<float>(coords),_sz);
109  pos += _sz;
110  psz += _sz;}
111 
112  {item.m_pos_norms = psz;
113  size_t _sz = norms.size()*sizeof(float);
114  ::memcpy(pos,vec_data<float>(norms),_sz);
115  pos += _sz;
116  psz += _sz;}
117 
118  // we don't need coords, norms for rendering
119  // but needed for pick,write...
120  //coords.clear();
121  //norms.clear();
122 
123  //we still need m_prims.
124 
125  }
126  }}
127 
128  unsigned int id = a_mgr.create_gsto_from_data(sz,buffer);
129  delete [] buffer;
130  return id;
131  }
132 
133  } else {
134  m_size = m_xyzs.size();
135  return a_mgr.create_gsto_from_data(m_xyzs);
136  }
137  return 0;
138  }

◆ node_desc_fields()

virtual const desc_fields& tools::sg::polyhedron::node_desc_fields ( ) const
inlinevirtual

Reimplemented from tools::sg::node.

Definition at line 31 of file polyhedron.

31  {
33  static const desc_fields s_v(parent::node_desc_fields(),4, //WARNING : take care of count.
38  );
39  return s_v;
40  }

◆ operator=()

polyhedron& tools::sg::polyhedron::operator= ( const polyhedron a_from)
inline

Definition at line 322 of file polyhedron.

322  {
323  parent::operator=(a_from);
324  gstos::operator=(a_from);
325  ph = a_from.ph;
326  solid = a_from.solid;
327  reduced_wire_frame = a_from.reduced_wire_frame;
328  enforce_edges = a_from.enforce_edges;
329 
330  m_xyzs = a_from.m_xyzs;
331 
332  return *this;
333  }

◆ pick()

virtual void tools::sg::polyhedron::pick ( pick_action a_action)
inlinevirtual

Reimplemented from tools::sg::node.

Definition at line 273 of file polyhedron.

273  {
274  if(touched()) {
276  update_sg(a_action.out(),what);
277  reset_touched();
278  }
279  if(solid.value()) {
281  const prims_t& prims = *itt;
282  tools_vforcit(tess_prim*,prims,it) {
283  const tess_prim& item = *(*it);
284  const std::vector<float>& coords = item.m_coords;
285  if(a_action.add__primitive(*this,item.m_mode,coords,true)) return;
286  }
287  }
288  } else {
289  a_action.add__lines(*this,m_xyzs,true);
290  }
291  }

◆ render()

virtual void tools::sg::polyhedron::render ( render_action a_action)
inlinevirtual

Reimplemented from tools::sg::node.

Definition at line 141 of file polyhedron.

141  {
142  const state& state = a_action.state();
143 
144  bool draw_edges = false;
145  if(solid.value()) {
146  draw_edges = state.m_GL_LIGHTING?false:true;
147  if(enforce_edges.value()) draw_edges = true;
148  }
149 
150  if( touched() ||
151  (draw_edges && m_xyzs.empty()) ||
152  (solid.value() && m_primss.empty()) ){
154  if(solid.value()) what = draw_edges?faces_and_lines:faces;
155  update_sg(a_action.out(),what);
156  reset_touched();
157  }
158 
159  unsigned int _id = 0;
160  if(state.m_use_gsto) {
161  m_draw_edges = draw_edges;
162  _id = get_gsto_id(a_action.out(),a_action.render_manager());
163  } else {
164  clean_gstos(&a_action.render_manager());
165  }
166 
167  //a_action.out() << "debug : tools::sg::polyhedron::render_action :"
168  // << " m_xyzs.size() " << m_xyzs.size()
169  // << " m_primss.size() " << m_primss.size()
170  // << std::endl;
171 
172  if(solid.value()) {
173 
174  if(_id) {
175 
176  a_action.begin_gsto(_id);
177  if(draw_edges && m_size) {
178  //Same logic as Inventor SoLightModel.model = BASE_COLOR.
179  a_action.set_lighting(false);
180  a_action.color4f(0,0,0,1);
181 
182  a_action.draw_gsto_v(gl::lines(),m_size/3,0);
183 
184  //pushes back the filled polygons to avoid z-fighting with lines
185  a_action.set_polygon_offset(true);
186 
187  a_action.color4f(state.m_color);
188  a_action.set_lighting(state.m_GL_LIGHTING);
189  }
190 
192  const prims_t& prims = *itt;
193  tools_vforcit(tess_prim*,prims,it) {
194  const tess_prim& item = *(*it);
195  a_action.draw_gsto_vn(item.m_mode,item.m_size/3,item.m_pos_coords,item.m_pos_norms);
196  }
197  }}
198 
199  a_action.set_polygon_offset(state.m_GL_POLYGON_OFFSET_FILL);
200  a_action.end_gsto();
201  } else {
202 
203  if(draw_edges){
204  if(m_xyzs.size()) {
205  //Same logic as Inventor SoLightModel.model = BASE_COLOR.
206  a_action.set_lighting(false);
207  a_action.color4f(0,0,0,1);
208 
209  a_action.draw_vertex_array(gl::lines(),m_xyzs);
210 
211  //pushes back the filled polygons to avoid z-fighting with lines
212  a_action.set_polygon_offset(true);
213 
214  a_action.color4f(state.m_color);
215  a_action.set_lighting(state.m_GL_LIGHTING);
216  }
217  }
218 
220  const prims_t& prims = *itt;
221  tools_vforcit(tess_prim*,prims,it) {
222  const tess_prim& item = *(*it);
223  const std::vector<float>& coords = item.m_coords;
224  if(coords.empty()) continue;
225  const std::vector<float>& norms = item.m_norms;
226  if(norms.size()!=coords.size()) {
227  a_action.out() << "tools::sg::polyhedron::render :"
228  << " bad number of normals."
229  << std::endl;
230  continue;
231  }
232  a_action.draw_vertex_normal_array(item.m_mode,coords,norms);
233  }
234  }}
235  a_action.set_polygon_offset(state.m_GL_POLYGON_OFFSET_FILL);
236  }
237 
238  } else { //not solid
239  //Same logic as Inventor SoLightModel.model = BASE_COLOR.
240  a_action.set_lighting(false); // do we want that ?
241 
242  if(_id) {
243  a_action.begin_gsto(_id);
244  a_action.draw_gsto_v(gl::lines(),m_size/3,0);
245  a_action.end_gsto();
246  } else {
247  a_action.draw_vertex_array(gl::lines(),m_xyzs);
248  }
249 
250  a_action.set_lighting(state.m_GL_LIGHTING);
251  }
252  }

◆ update_sg()

void tools::sg::polyhedron::update_sg ( std::ostream &  a_out,
const update_what a_what 
)
inline

Definition at line 342 of file polyhedron.

342  {
343  //a_out << "debug : tools::sg::polyhedron::update_sg" << std::endl;
344 
345  clean_gstos(); //must reset for all render_manager.
346 
347  _clear();
348 
349  const hep::polyhedron& sbPolyhedron = ph.value();
350  if(sbPolyhedron.GetNoFacets()<=0) {
351  //a_out << "tools::sg::polyhedron::update_sg : abnormal polyhedron." << std::endl;
352  return; // Abnormal polyhedron.
353  }
354 
355  if((a_what==faces)||(a_what==faces_and_lines)) {
356 
357  std::vector<double> doubles;
358 
359  vec3d unitNormal;
360  vec3d* pV = sbPolyhedron.GetPV();
361 
362  // Assume all facets are convex quadrilaterals :
363  bool notLastFace;
364  do {
365  notLastFace = sbPolyhedron.GetNextUnitNormal(unitNormal);
366 
367  doubles.clear(); //begin POLYGON
368 
369  bool notLastEdge;
370  int edgeFlag = 1;
371  do {
372  // optimize to avoid copying vec3f.
373  //notLastEdge = sbPolyhedron.GetNextVertex(vertex,edgeFlag);
374  int index;
375  notLastEdge = sbPolyhedron.GetNextVertexIndex(index,edgeFlag);
376  vec3d* vertex = pV+index; //WARNING : must not be modified !
377 
378  doubles.push_back((*vertex)[0]);
379  doubles.push_back((*vertex)[1]);
380  doubles.push_back((*vertex)[2]);
381 
382  } while (notLastEdge);
383 
384  if(!m_tess) m_tess = new tessellate(a_out);
385 
386  m_primss.push_back(prims_t());
387  prims_t& prims = m_primss.back();
388 
389  m_tess->do_it(doubles.size()/3,vec_data(doubles),prims);
390 
391  m_tess->clear();
392 
393  } while (notLastFace);
394  }
395 
396  if((a_what==lines)||(a_what==faces_and_lines)) {
397  float pvbx = 0;
398  float pvby = 0;
399  float pvbz = 0;
400 
401  float pvex,pvey,pvez;
402 
403  vec3d unitNormal;
404  vec3d* pV = sbPolyhedron.GetPV();
405 
406  bool notLastFace;
407  do {
408  notLastFace = sbPolyhedron.GetNextUnitNormal(unitNormal);
409 
410  // Treat edges :
411  int edgeFlag = 1;
412  int prevEdgeFlag = edgeFlag;
413  bool notLastEdge;
414  bool firstEdge = true;
415  do {
416  int index;
417  notLastEdge = sbPolyhedron.GetNextVertexIndex(index,edgeFlag);
418  vec3d* vertex = pV+index; //WARNING : must not be modified !
419 
420  if(!reduced_wire_frame) edgeFlag = 1;
421  if(firstEdge) {
422  if(edgeFlag) {
423  pvbx = (float)(*vertex)[0];
424  pvby = (float)(*vertex)[1];
425  pvbz = (float)(*vertex)[2];
426  } else {
427  }
428  firstEdge = false;
429  prevEdgeFlag = edgeFlag;
430  } else {
431  if(edgeFlag!=prevEdgeFlag) {
432  if(edgeFlag) { // Pass to a visible edge :
433  pvbx = (float)(*vertex)[0];
434  pvby = (float)(*vertex)[1];
435  pvbz = (float)(*vertex)[2];
436  } else { // Pass to an invisible edge :
437  pvex = (float)(*vertex)[0];
438  pvey = (float)(*vertex)[1];
439  pvez = (float)(*vertex)[2];
440 
441  m_xyzs.push_back(pvbx);
442  m_xyzs.push_back(pvby);
443  m_xyzs.push_back(pvbz);
444 
445  m_xyzs.push_back(pvex);
446  m_xyzs.push_back(pvey);
447  m_xyzs.push_back(pvez);
448  }
449  prevEdgeFlag = edgeFlag;
450  } else {
451  if(edgeFlag) {
452  pvex = (float)(*vertex)[0];
453  pvey = (float)(*vertex)[1];
454  pvez = (float)(*vertex)[2];
455 
456  m_xyzs.push_back(pvbx);
457  m_xyzs.push_back(pvby);
458  m_xyzs.push_back(pvbz);
459 
460  m_xyzs.push_back(pvex);
461  m_xyzs.push_back(pvey);
462  m_xyzs.push_back(pvez);
463 
464  pvbx = pvex;
465  pvby = pvey;
466  pvbz = pvez;
467  } else {
468  }
469  }
470  }
471  } while (notLastEdge);
472  } while (notLastFace);
473 
474  }
475  }

◆ xyzs()

const std::vector<float>& tools::sg::polyhedron::xyzs ( ) const
inline

Definition at line 477 of file polyhedron.

477 {return m_xyzs;}

Member Data Documentation

◆ enforce_edges

sf<bool> tools::sg::polyhedron::enforce_edges

Definition at line 29 of file polyhedron.

◆ m_draw_edges

bool tools::sg::polyhedron::m_draw_edges
protected

Definition at line 494 of file polyhedron.

◆ m_primss

std::vector<prims_t> tools::sg::polyhedron::m_primss
protected

Definition at line 491 of file polyhedron.

◆ m_size

size_t tools::sg::polyhedron::m_size
protected

Definition at line 493 of file polyhedron.

◆ m_tess

tessellate* tools::sg::polyhedron::m_tess
protected

Definition at line 490 of file polyhedron.

◆ m_xyzs

std::vector<float> tools::sg::polyhedron::m_xyzs
protected

Definition at line 487 of file polyhedron.

◆ ph

hep::sf_polyhedron tools::sg::polyhedron::ph

Definition at line 26 of file polyhedron.

◆ reduced_wire_frame

sf<bool> tools::sg::polyhedron::reduced_wire_frame

Definition at line 28 of file polyhedron.

◆ solid

sf<bool> tools::sg::polyhedron::solid

Definition at line 27 of file polyhedron.


The documentation for this class was generated from the following file:
tools::sg::polyhedron::update_what
update_what
Definition: polyhedron:336
tools::sg::polyhedron::reduced_wire_frame
sf< bool > reduced_wire_frame
Definition: polyhedron:28
tools::sg::gstos::gstos
gstos()
Definition: gstos:18
tools::vec_data
const T * vec_data(const std::vector< T > &a_vec)
Definition: vdata:18
tools::sg::polyhedron::m_tess
tessellate * m_tess
Definition: polyhedron:490
tools::sg::gstos::clean_gstos
void clean_gstos()
Definition: gstos:89
tools::gl::lines
mode_t lines()
Definition: glprims:17
tools::raw_clear
void raw_clear(std::map< K, V * > &a_m)
Definition: mapmanip:56
tools::sg::gstos::operator=
gstos & operator=(const gstos &a_from)
Definition: gstos:22
tools::sg::polyhedron::m_primss
std::vector< prims_t > m_primss
Definition: polyhedron:491
tools::sg::polyhedron::faces
@ faces
Definition: polyhedron:337
tools::sg::polyhedron::m_draw_edges
bool m_draw_edges
Definition: polyhedron:494
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::sg::gstos::get_gsto_id
unsigned int get_gsto_id(std::ostream &a_out, render_manager &a_mgr)
Definition: gstos:60
tools::sg::polyhedron::_clear
void _clear()
Definition: polyhedron:480
tools::sg::tessellate::do_it
void do_it(size_t a_npt, const double *a_contour, prims_t &a_prims)
Definition: tessellate:143
tools::sg::tessellate::clear
void clear()
Definition: tessellate:157
tools::sg::polyhedron::solid
sf< bool > solid
Definition: polyhedron:27
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::sg::polyhedron::ph
hep::sf_polyhedron ph
Definition: polyhedron:26
tools::sg::polyhedron::enforce_edges
sf< bool > enforce_edges
Definition: polyhedron:29
tools::sg::polyhedron
Definition: polyhedron:23
tools::sg::polyhedron::m_xyzs
std::vector< float > m_xyzs
Definition: polyhedron:487
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::polyhedron::m_size
size_t m_size
Definition: polyhedron:493
tools::what
what
Definition: strip:12
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::sg::polyhedron::prims_t
tessellate::prims_t prims_t
Definition: polyhedron:50
tools::sg::polyhedron::update_sg
void update_sg(std::ostream &a_out, const update_what &a_what)
Definition: polyhedron:342
tools::sg::polyhedron::lines
@ lines
Definition: polyhedron:338
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::polyhedron::faces_and_lines
@ faces_and_lines
Definition: polyhedron:339
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