CPointArray.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 2015 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CPointArrayH
46 #define CPointArrayH
47 //------------------------------------------------------------------------------
48 #include "graphics/CGenericArray.h"
49 //------------------------------------------------------------------------------
50 
51 //------------------------------------------------------------------------------
52 namespace chai3d {
53 //------------------------------------------------------------------------------
54 
55 //------------------------------------------------------------------------------
56 class cGenericObject;
57 class cCollisionRecorder;
58 struct cCollisionSettings;
59 //------------------------------------------------------------------------------
60 
61 //==============================================================================
68 //==============================================================================
69 
70 //------------------------------------------------------------------------------
72 typedef std::shared_ptr<cPointArray> cPointArrayPtr;
73 //------------------------------------------------------------------------------
74 
75 //==============================================================================
104 //==============================================================================
106 {
107  //--------------------------------------------------------------------------
108  // CONSTRUCTOR & DESTRUCTOR:
109  //--------------------------------------------------------------------------
110 
111 public:
112 
113  //--------------------------------------------------------------------------
119  //--------------------------------------------------------------------------
120  cPointArray(cVertexArrayPtr a_vertexArray) : cGenericArray(a_vertexArray)
121  {
122  // clear all points
123  clear();
124 
125  // store pointer to vertex array
126  m_vertices = a_vertexArray;
127 
128  // initialize OpenGL buffer ID
129  m_elementBuffer = 0;
130  }
131 
132 
133  //--------------------------------------------------------------------------
137  //--------------------------------------------------------------------------
139 
140 
142  void clear()
143  {
144  m_allocated.clear();
145  m_indices.clear();
146  m_freeElements.clear();
147  m_flagMarkForResize = true;
148  }
149 
150 
152  static cPointArrayPtr create(cVertexArrayPtr a_vertexArray) { return (std::make_shared<cPointArray>(a_vertexArray)); }
153 
154 
155  //--------------------------------------------------------------------------
156  // PUBLIC METHODS:
157  //--------------------------------------------------------------------------
158 
159 public:
160 
162  virtual unsigned int getNumVerticesPerElement() { return (1); }
163 
165  cPointArrayPtr copy();
166 
168  void compress();
169 
170 
171  //--------------------------------------------------------------------------
179  //--------------------------------------------------------------------------
180  int newPoint(const unsigned int a_vertexIndex0)
181  {
182  int index;
183 
184  // check if there is an available slot on the free point list
185  if (m_freeElements.size() > 0)
186  {
187  index = m_freeElements.front();
188  m_allocated[index] = true;
189  m_freeElements.pop_front();
190  setVertex(index, a_vertexIndex0);
191  }
192  else
193  {
194  // store new indices
195  m_indices.push_back(a_vertexIndex0);
196  m_allocated.push_back(true);
197 
198  m_flagMarkForResize = true;
199 
200  // store index of new point
201  index = (int)(m_allocated.size())-1;
202  }
203 
204  // mark for update
205  m_flagMarkForUpdate = true;
206 
207  // return index number to new point
208  return (index);
209  }
210 
211 
212  //--------------------------------------------------------------------------
220  //--------------------------------------------------------------------------
221  void removePoint(const unsigned int a_pointIndex)
222  {
223  // sanity check
224  if (m_allocated[a_pointIndex])
225  {
226  // disable point
227  m_allocated[a_pointIndex] = false;
228 
229  // reset index to vertex
230  setVertex(a_pointIndex, 0);
231 
232  // add point to free list
233  m_freeElements.push_back(a_pointIndex);
234 
235  // mark for update
236  m_flagMarkForUpdate = true;
237  }
238  }
239 
240 
241  //--------------------------------------------------------------------------
249  //--------------------------------------------------------------------------
250  inline void setVertex(const unsigned int a_pointIndex,
251  const unsigned int a_vertexIndex)
252  {
253  m_indices[a_pointIndex] = a_vertexIndex;
254 
255  // mark for update
256  m_flagMarkForUpdate = true;
257  }
258 
259 
260  //--------------------------------------------------------------------------
268  //--------------------------------------------------------------------------
269  inline unsigned int getVertexIndex0(const unsigned int a_pointIndex) const
270  {
271  return (m_indices[a_pointIndex]);
272  };
273 
274 
275  //--------------------------------------------------------------------------
286  //--------------------------------------------------------------------------
287  virtual unsigned int getVertexIndex(const unsigned int a_elementIndex,
288  const unsigned int a_vertexNumber) const
289  {
290  switch (a_vertexNumber)
291  {
292  case 0 : return (m_indices[a_elementIndex]);
293  default: return (0);
294  }
295  }
296 
297 
298  //--------------------------------------------------------------------------
309  //--------------------------------------------------------------------------
310  cVector3d getTexCoordAtPosition(const unsigned int a_pointIndex,
311  const cVector3d& a_localPos)
312  {
313  cVector3d texCoord;
314 
315  if (m_vertices->getUseTexCoordData())
316  {
317  texCoord = m_vertices->getTexCoord(getVertexIndex0(a_pointIndex));
318  }
319  else
320  {
321  texCoord.set(0.0, 0.0, 0.0);
322  }
323 
324  // return result
325  return (texCoord);
326  }
327 
328 
329  //--------------------------------------------------------------------------
333  //--------------------------------------------------------------------------
334  inline void render()
335  {
336 #ifdef C_USE_OPENGL
337  unsigned int numPoints = getNumElements();
338 
339  // allocate object and buffers if needed
340  if (m_elementBuffer == 0)
341  {
342  glGenBuffers(1, &m_elementBuffer);
343  }
344 
345  // bind buffer
346  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
347 
348  // update buffer size if needed
350  {
351  glBufferData(GL_ELEMENT_ARRAY_BUFFER, numPoints * sizeof(unsigned int), &(m_indices[0]), GL_STATIC_DRAW);
352  m_flagMarkForResize = true;
353  }
354 
355  // update data if needed
357  {
358  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
359  glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, numPoints * sizeof(unsigned int), &(m_indices[0]));
360 
361  m_flagMarkForUpdate = false;
362  }
363 
364  // render object
365  m_vertices->renderInitialize();
366 
367  glEnableClientState(GL_VERTEX_ARRAY);
368  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
369  glDrawElements(GL_POINTS, numPoints, GL_UNSIGNED_INT, (void*)0);
370  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
371 
372  m_vertices->renderFinalize();
373 #endif
374  }
375 
377  virtual bool computeCollision(const unsigned int a_elementIndex,
378  cGenericObject* a_object,
379  cVector3d& a_segmentPointA,
380  cVector3d& a_segmentPointB,
381  cCollisionRecorder& a_recorder,
382  cCollisionSettings& a_settings) const;
383 };
384 
385 //------------------------------------------------------------------------------
386 } // namespace chai3d
387 //------------------------------------------------------------------------------
388 
389 //------------------------------------------------------------------------------
390 #endif
391 //------------------------------------------------------------------------------
392 
393 
This class implements a 3D vector.
Definition: CVector3d.h:88
unsigned int getVertexIndex0(const unsigned int a_pointIndex) const
Definition: CPointArray.h:269
std::shared_ptr< cPointArray > cPointArrayPtr
Definition: CPointArray.h:71
virtual unsigned int getNumVerticesPerElement()
This method returns the number of vertices that compose a point.
Definition: CPointArray.h:162
Implements an abstract class for describing elements composed of vertices.
This class implements an array of 3D points.
Definition: CPointArray.h:105
std::vector< unsigned int > m_indices
Element indices to vertices.
Definition: CGenericArray.h:230
std::vector< bool > m_allocated
Element allocation flags.
Definition: CGenericArray.h:233
cPointArray(cVertexArrayPtr a_vertexArray)
Definition: CPointArray.h:120
std::list< unsigned int > m_freeElements
List of free elements.
Definition: CGenericArray.h:259
cPointArrayPtr copy()
This method copies point data and return new point array.
Definition: CPointArray.cpp:212
void clear()
This method clears all points from the array.
Definition: CPointArray.h:142
void set(const double &a_x, const double &a_y, const double &a_z)
This method initializes this vector with components x, y, and z passed as arguments.
Definition: CVector3d.h:298
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition: CGenericObject.h:112
This class implements a collision detection recorder that stores all collision events that are report...
Definition: CCollisionBasics.h:185
bool m_flagMarkForUpdate
If true then element data has been modified.
Definition: CGenericArray.h:236
void setVertex(const unsigned int a_pointIndex, const unsigned int a_vertexIndex)
Definition: CPointArray.h:250
This structure stores the collision settings that are passed to a collision detector when querying fo...
Definition: CCollisionBasics.h:242
virtual unsigned int getNumElements()
This method returns the number of allocated elements.
Definition: CGenericArray.h:145
GLuint m_elementBuffer
OpenGL Buffer for storing elements.
Definition: CGenericArray.h:249
cVector3d getTexCoordAtPosition(const unsigned int a_pointIndex, const cVector3d &a_localPos)
Definition: CPointArray.h:310
std::shared_ptr< cVertexArray > cVertexArrayPtr
Definition: CVertexArray.h:107
void compress()
This method compresses the array by removing non used points.
Definition: CPointArray.cpp:248
void render()
Definition: CPointArray.h:334
~cPointArray()
Definition: CPointArray.h:138
int newPoint(const unsigned int a_vertexIndex0)
Definition: CPointArray.h:180
static cPointArrayPtr create(cVertexArrayPtr a_vertexArray)
Shared cPointArray allocator.
Definition: CPointArray.h:152
Definition: CAudioBuffer.cpp:56
virtual bool computeCollision(const unsigned int a_elementIndex, cGenericObject *a_object, cVector3d &a_segmentPointA, cVector3d &a_segmentPointB, cCollisionRecorder &a_recorder, cCollisionSettings &a_settings) const
This method checks if a given line segment intersects a selected point from this array.
Definition: CPointArray.cpp:73
This class implements an abstract class for describing geometrical elements composed of vertices...
Definition: CGenericArray.h:90
virtual unsigned int getVertexIndex(const unsigned int a_elementIndex, const unsigned int a_vertexNumber) const
Definition: CPointArray.h:287
bool m_flagMarkForResize
If true then element array size has changed.
Definition: CGenericArray.h:239
void removePoint(const unsigned int a_pointIndex)
Definition: CPointArray.h:221
cVertexArrayPtr m_vertices
Vertex array that contains all vertices used to describe the elements of this array.
Definition: CGenericArray.h:227