mercator  0.4.0
A terrain generation library for the Worldforge system.
Buffer.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2003 Alistair Riddoch
4 
5 #ifndef MERCATOR_BUFFER_H
6 #define MERCATOR_BUFFER_H
7 
8 #include <vector>
9 
10 namespace Mercator {
11 
13 template<typename DataType>
14 class Buffer {
15  protected:
17  const unsigned int m_channels;
19  const unsigned int m_size;
21  std::vector<DataType> m_data;
22 
23  public:
28  explicit Buffer(unsigned int size, unsigned int channels);
29  virtual ~Buffer();
30 
36  DataType & operator()(unsigned int x,unsigned int y,unsigned int channel) {
37  return m_data[(y * m_size + x) * m_channels + channel];
38  }
39 
45  const DataType & operator()(unsigned int x,
46  unsigned int y,
47  unsigned int channel) const {
48  return m_data[(y * m_size + x) * m_channels + channel];
49  }
50 
51 
53  unsigned int getSize() const {
54  return m_size;
55  }
56 
58  unsigned int getChannels() const {
59  return m_channels;
60  }
61 
63  DataType * getData() {
64  return m_data.data();
65  }
66 
68  const DataType * getData() const {
69  return m_data.data();
70  }
71 
76  void allocate() {
77  m_data.resize(m_size * m_size * m_channels);
78  }
79 
83  bool isValid() const {
84  return (!m_data.empty());
85  }
86 
90  void invalidate() {
91  m_data.resize(0);
92  }
93 
94 };
95 
96 } // namespace Mercator
97 
98 #endif // MERCATOR_BUFFER_H
Mercator::Buffer::m_data
std::vector< DataType > m_data
Pointer to buffer containing data values.
Definition: Buffer.h:21
Mercator::Buffer::getChannels
unsigned int getChannels() const
Accessor for the number of data values per height point.
Definition: Buffer.h:58
Mercator::Buffer::allocate
void allocate()
Allocate the storage required by the buffer.
Definition: Buffer.h:76
Mercator::Buffer::invalidate
void invalidate()
De-allocate the storage for this buffer.
Definition: Buffer.h:90
Mercator::Buffer::operator()
const DataType & operator()(unsigned int x, unsigned int y, unsigned int channel) const
Retrieve the data value at a given point.
Definition: Buffer.h:45
Mercator::Buffer::isValid
bool isValid() const
Determine if this buffer has valid allocated storage.
Definition: Buffer.h:83
Mercator::Buffer
Template for managing buffers of data for a segment.
Definition: Buffer.h:14
Mercator::Buffer::m_channels
const unsigned int m_channels
The number of data values per height point.
Definition: Buffer.h:17
Mercator::Buffer::getData
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:63
Mercator::Buffer::operator()
DataType & operator()(unsigned int x, unsigned int y, unsigned int channel)
Retrieve the data value at a given point.
Definition: Buffer.h:36
Mercator::Buffer::Buffer
Buffer(unsigned int size, unsigned int channels)
Constructor.
Definition: Buffer_impl.h:10
Mercator::Buffer::getData
const DataType * getData() const
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:68
Mercator::Buffer::m_size
const unsigned int m_size
The size of segment, m_res + 1.
Definition: Buffer.h:19
Mercator::Buffer::getSize
unsigned int getSize() const
Accessor for the size of segment, m_res + 1.
Definition: Buffer.h:53