Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
TriangleStripGenerator.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include "../external/tristripper/tri_stripper.cpp"
33 #include "../external/tristripper/policy.cpp"
34 #include "../external/tristripper/connectivity_graph.cpp"
35 
36 using namespace triangle_stripper;
37 
39 #include <vlGraphics/Geometry.hpp>
41 #include <vlCore/Log.hpp>
42 #include <vlCore/Say.hpp>
43 
44 using namespace vl;
45 
46 namespace
47 {
48  void fillIndices(std::vector<unsigned int>& indices, const DrawCall* dc, bool substitute_quads)
49  {
50  indices.clear();
51 
52  if ( dc->primitiveType() == PT_QUADS && !substitute_quads )
53  return;
54 
55  indices.reserve( 1000 );
56 
57  for(TriangleIterator trit = dc->triangleIterator(); trit.hasNext(); trit.next())
58  {
59  int a = trit.a();
60  int b = trit.b();
61  int c = trit.c();
62  // skip degenerate triangles
63  if (a != b && b != c)
64  {
65  indices.push_back(a);
66  indices.push_back(b);
67  indices.push_back(c);
68  }
69  }
70  }
71 }
72 
73 void TriangleStripGenerator::stripfy(Geometry* geom, int cache_size, bool merge_strips, bool remove_doubles, bool substitute_quads)
74 {
75  if (remove_doubles)
76  {
78  dvr.removeDoubles(geom);
79  }
80 
81  for( int idraw=geom->drawCalls().size(); idraw--; )
82  {
83  DrawCall* dc = geom->drawCalls().at(idraw);
84 
85  triangle_stripper::indices indices;
86 
87  fillIndices(indices, dc, substitute_quads);
88 
89  std::vector<unsigned int> algo2_strip;
90  std::vector<unsigned int> algo2_tris;
91  // stripfyAlgo2(algo2_strip, algo2_tris, indices, cache_size);
92 
93  tri_stripper striper(indices);
94  striper.SetCacheSize(cache_size);
95  striper.SetMinStripSize(4);
96  primitive_vector out;
97  striper.Strip(&out);
98 
99  // install new strip
100  if (out.size())
101  {
102  geom->drawCalls().erase(idraw,1);
103  algo2_strip.reserve(indices.size());
104  for(unsigned s=0; s<out.size(); ++s)
105  {
106  if (out[s].Type == TRIANGLE_STRIP)
107  {
108  algo2_strip.clear();
109  for(unsigned p=0; p<out[s].Indices.size(); ++p)
110  algo2_strip.push_back(out[s].Indices[p]);
111 
113  draw_elems->indexBuffer()->resize(algo2_strip.size());
114  memcpy(draw_elems->indexBuffer()->ptr(), &algo2_strip[0], sizeof(unsigned int)*algo2_strip.size());
115  geom->drawCalls().push_back(draw_elems.get());
116  }
117  else // TRIANGLES
118  {
119  algo2_tris.clear();
120  for(unsigned p=0; p<out[s].Indices.size(); ++p)
121  algo2_tris.push_back(out[s].Indices[p]);
122 
124  draw_elems->indexBuffer()->resize(algo2_tris.size());
125  memcpy(draw_elems->indexBuffer()->ptr(), &algo2_tris[0], sizeof(unsigned int)*algo2_tris.size());
126  geom->drawCalls().push_back(draw_elems.get());
127  }
128  }
129  }
130  }
131 
132  if (merge_strips)
133  geom->mergeTriangleStrips();
134 }
135 
bool hasNext()
Returns false if the iterator has reached the end of the triangle list.
const T * get() const
Definition: Object.hpp:128
Removes from a Geometry the vertices with the same attributes.
void removeDoubles(Geometry *geom)
void resize(size_t dim)
Definition: Array.hpp:233
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
Visualization Library main namespace.
const unsigned char * ptr() const
Returns the pointer to the first element of the local buffer. Equivalent to bufferObject()->ptr() ...
Definition: Array.hpp:103
See DrawElements.
virtual TriangleIterator triangleIterator() const =0
Returns a TriangleIterator used to iterate through the triangles of a DrawCall.
Iterator used to extract the indices of every single triangle of a DrawCall regardless of the primiti...
DrawCall * mergeTriangleStrips()
Merges all the PT_TRIANGLE_STRIP DrawElementsUInt objects into one single PT_TRIANGLE_STRIP DrawEleme...
Definition: Geometry.cpp:430
arr_type * indexBuffer()
The BufferObject containing the indices used to render.
The base class of DrawArrays, DrawElements, MultiDrawElements and DrawRangeElements.
Definition: DrawCall.hpp:90
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
EPrimitiveType primitiveType() const
Returns the draw call&#39;s primitive type.
Definition: DrawCall.hpp:110
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:102