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]
IndexIterator.hpp
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 #ifndef IndexIterator_INCLUDE_ONCE
33 #define IndexIterator_INCLUDE_ONCE
34 
35 #include <vlCore/Object.hpp>
36 
37 namespace vl
38 {
39 //-----------------------------------------------------------------------------
40 // IndexIteratorAbstract
41 //-----------------------------------------------------------------------------
44  {
46 
47  public:
49  {
50  VL_DEBUG_SET_OBJECT_NAME()
51  }
52  int index() const { return mIndex; }
53  virtual bool hasNext() const = 0;
54  virtual bool next() = 0;
55 
56  protected:
57  int mIndex;
58  };
59 //-----------------------------------------------------------------------------
60 // IndexIterator
61 //-----------------------------------------------------------------------------
63  class IndexIterator: public Object
64  {
66 
67  public:
69  {
70  VL_DEBUG_SET_OBJECT_NAME()
71  }
72  void initialize(IndexIteratorAbstract* iterator) { mIterator = iterator; }
73  int index() { return mIterator->index(); }
74  bool hasNext() { return mIterator->hasNext(); }
75  bool next() { return mIterator->next(); }
76  bool operator++() { return next(); }
77 
78  protected:
80  };
81 //-----------------------------------------------------------------------------
82 // IndexIteratorDrawArrays
83 //-----------------------------------------------------------------------------
86  {
88 
89  public:
91  {
92  VL_DEBUG_SET_OBJECT_NAME()
93  initialize(0,0);
94  }
95 
96  void initialize(int start, int count)
97  {
98  mStart = start;
99  mCount = count;
100  mCurPos = start;
101  mIndex = start;
102  }
103 
104  virtual bool hasNext() const
105  {
106  return mCurPos != mStart + mCount;
107  }
108 
109  virtual bool next()
110  {
111  ++mCurPos;
112  mIndex = mCurPos;
113  return true;
114  }
115 
116  protected:
117  int mStart;
118  int mCount;
119  int mCurPos;
120  };
121 //-----------------------------------------------------------------------------
122 // IndexIteratorElements
123 //-----------------------------------------------------------------------------
125  template<class TArray>
127  {
129 
130  public:
132  {
133  VL_DEBUG_SET_OBJECT_NAME()
134  initialize( NULL, NULL, NULL, 0, false, 0 );
135  }
136 
137  void initialize( const TArray* idx_array, const std::vector<GLint>* p_base_vertices, const std::vector<GLsizei>* p_vert_counts,
138  int base_vert, bool prim_restart_on, unsigned int prim_restart_idx )
139  {
140  mArray = idx_array;
141  mBaseVert = base_vert;
142  mpBaseVertices = p_base_vertices;
143  mpVertCounts = p_vert_counts;
144  mBaseCount = 0;
145  mBaseIdx = 0;
146 
147  mPrimRestartEnabled = prim_restart_on;
148  mPrimRestartIdx = prim_restart_idx;
149  mCurPos = 0;
150  if (mArray && mArray->size())
151  {
152  mIndex = mArray->at(0) + mBaseVert;
153  }
154 
155  if (p_vert_counts)
156  {
157  VL_CHECK(p_base_vertices)
158  VL_CHECK( p_base_vertices->size() == p_vert_counts->size() )
159 
160  mBaseCount = (*p_vert_counts)[mBaseIdx];
161 
162  mIndex = (*mpBaseVertices)[mBaseIdx];
163  }
164  }
165 
166  virtual bool hasNext() const
167  {
168  return mCurPos != (int)mArray->size();
169  }
170 
171  virtual bool next()
172  {
173  ++mCurPos;
174  while( mCurPos < (int)mArray->size() && mArray->at(mCurPos) == mPrimRestartIdx && mPrimRestartEnabled )
175  ++mCurPos;
176  if ( mCurPos < (int)mArray->size() )
177  {
178  mIndex = mArray->at(mCurPos) + mBaseVert;
179  if (mpVertCounts)
180  {
181  VL_CHECK(mpBaseVertices)
182  mBaseCount--;
183  if (!mBaseCount)
184  {
185  mBaseIdx++;
186  mBaseCount = (*mpVertCounts)[mBaseIdx];
187  }
188  mIndex += (*mpBaseVertices)[mBaseIdx];
189  }
190 
191  return true;
192  }
193  else
194  {
195  mIndex = -1;
196  mCurPos = (int)mArray->size();
197  return false;
198  }
199  }
200 
201  protected:
202  const TArray* mArray;
204  int mCurPos;
206  unsigned int mPrimRestartIdx;
207  const std::vector<GLint>* mpBaseVertices;
208  const std::vector<GLsizei>* mpVertCounts;
210  int mBaseIdx;
211  };
212 //-----------------------------------------------------------------------------
213 }
214 
215 #endif
Index iterator operating over DrawElements, DrawRangeElements and MultiDrawElements.
const std::vector< GLint > * mpBaseVertices
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
ref< IndexIteratorAbstract > mIterator
void initialize(int start, int count)
Visualization Library main namespace.
void initialize(IndexIteratorAbstract *iterator)
virtual bool next()=0
Abstract class used as base for all the index iterators specializations.
void initialize(const TArray *idx_array, const std::vector< GLint > *p_base_vertices, const std::vector< GLsizei > *p_vert_counts, int base_vert, bool prim_restart_on, unsigned int prim_restart_idx)
virtual bool hasNext() const
The base class for all the reference counted objects.
Definition: Object.hpp:158
Index iterator operating used by DrawArrays.
const std::vector< GLsizei > * mpVertCounts
#define NULL
Definition: OpenGLDefs.hpp:81
virtual bool hasNext() const
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
virtual bool hasNext() const =0
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
Wraps a IndexIteratorAbstract to iterate over the indices of a DrawCall.
#define VL_CHECK(expr)
Definition: checks.hpp:73