]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/VectorArrayAdapter.hxx
Random trees from Stuart Buchanan
[simgear.git] / simgear / scene / util / VectorArrayAdapter.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2007 Tim Moore
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 as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef VECTORARRAYADAPTERHXX
23 #define VECTORARRAYADAPTERHXX 1
24
25 namespace simgear
26 {
27 template <typename Vector>
28 class VectorArrayAdapter {
29 public:
30     /*
31      * Adapter that provides 2D array access to the elements of a
32      * std::vector in row major order.
33      * @param v the vector
34      * @param rowStride distance from an element to the corresponding
35      *  element in the next row.
36      * @param baseOffset offset of the first element of the array from the
37      *  beginning of the vector.
38      * @param rowOffset offset of the first element in a row from the
39      *  actual beginning of the row.
40      */
41     VectorArrayAdapter(Vector& v, int rowStride, int baseOffset = 0,
42         int rowOffset = 0):
43         _v(v),  _rowStride(rowStride), _baseOffset(baseOffset),
44         _rowOffset(rowOffset)
45     {
46     }
47     
48     typename Vector::value_type& operator() (int i, int j)
49     {
50         return _v[_baseOffset + i * _rowStride + _rowOffset + j];
51     }
52     const typename Vector::value_type& operator() (int i, int j) const
53     {
54         return _v[_baseOffset + i * _rowStride + _rowOffset + j];
55     }
56 private:
57     Vector& _v;
58     const int _rowStride;
59     const int _baseOffset;
60     const int _rowOffset;
61 };
62 }
63 #endif