Stellarium 0.12.4
TriangleIterator.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2012 Ferdinand Majerech
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _TRIANGLEITERATOR_HPP_
21 #define _TRIANGLEITERATOR_HPP_
22 
23 #include <QVector>
24 
25 // For PrimitiveType
26 #include "renderer/StelVertexBuffer.hpp"
27 
28 
36 template<class V>
38 {
39 public:
49  TriangleIterator(const QVector<V>& vertices, const PrimitiveType primitiveType)
50  : vertices(vertices)
51  , primitiveType(primitiveType)
52  , currentVertex(0)
53  {
54  Q_ASSERT_X(vertices.size() != 1 && vertices.size() != 2, Q_FUNC_INFO,
55  "A vertex array containing triangles of any primitive type must not "
56  "have 1 or 2 vertices");
57  switch(primitiveType)
58  {
59  case PrimitiveType_Triangles:
60  Q_ASSERT_X(vertices.size() % 3 == 0, Q_FUNC_INFO,
61  "Size of a vertex array of triangles must be divisible by 3");
62  currentVertex = 0;
63  break;
64  case PrimitiveType_TriangleStrip:
65  currentVertex = 2;
66  break;
67  case PrimitiveType_TriangleFan:
68  currentVertex = 1;
69  break;
70  default:
71  Q_ASSERT_X(false, Q_FUNC_INFO, "Not a triangle primitive type");
72  }
73  }
74 
83  bool next(V& a, V& b, V& c)
84  {
85  Q_ASSERT_X(currentVertex > 0, Q_FUNC_INFO, "Current vertex is negative");
86  Q_ASSERT_X(currentVertex <= vertices.size(), Q_FUNC_INFO,
87  "Iterating outside the vertex array");
88  if(currentVertex == vertices.size()){return false;}
89 
90  switch(primitiveType)
91  {
92  case PrimitiveType_Triangles:
93  a = vertices[currentVertex++];
94  b = vertices[currentVertex++];
95  c = vertices[currentVertex++];
96  break;
97  case PrimitiveType_TriangleStrip:
98  if(currentVertex % 2 == 0)
99  {
100  a = vertices[currentVertex - 2];
101  b = vertices[currentVertex - 1];
102  }
103  else
104  {
105  a = vertices[currentVertex - 1];
106  b = vertices[currentVertex - 2];
107  }
108  c = vertices[currentVertex];
109  ++currentVertex;
110  break;
111  case PrimitiveType_TriangleFan:
112  a = vertices[0];
113  b = vertices[currentVertex];
114  c = vertices[currentVertex + 1];
115  ++currentVertex;
116  break;
117  default:
118  Q_ASSERT_X(false, Q_FUNC_INFO, "Not a triangle primitive type");
119  }
120 
121  return true;
122  }
123 
124 private:
126  const QVector<V>& vertices;
127 
129  const PrimitiveType primitiveType;
130 
132  int currentVertex;
133 };
134 
135 #endif // _TRIANGLEITERATOR_HPP_