/* * Copyright (C) 2009 * Robert Bosch LLC * Research and Technology Center North America * Palo Alto, California * * All rights reserved. * *------------------------------------------------------------------------------ * project ....: Autonomous Technologies * file .......: rtcArray2.h * authors ....: Benjamin Pitzer * organization: Robert Bosch LLC * creation ...: 08/16/2006 * modified ...: $Date: 2009-01-21 18:19:16 -0800 (Wed, 21 Jan 2009) $ * changed by .: $Author: benjaminpitzer $ * revision ...: $Revision: 14 $ */ #ifndef RTC_ARRAY2_H #define RTC_ARRAY2_H //== INCLUDES ================================================================== #include "rtcMath.h" #include "rtcVec2.h" #include "rtcArray.h" //== NAMESPACES ================================================================ namespace rtc { // Forward declarations template class Array; // K-dimensional array template class Array2; // 2-dimensional array template class Vec2; // 2d Vector /** * A 2-Dimensional Array */ template class Array2: public Array { public: // Constructors/Destructor Array2(); Array2(int rows, int columns); Array2(int rows, int columns, const T* d); Array2(int rows, int columns, const T a); Array2(const Array& a); // Mutators void setSize(int rows, int cols); // Accessors int rows() const; int columns() const; T& at(int rows, int columns); const T& at(int rows, int columns) const; T& operator () (int rows, int columns); T operator () (int rows, int columns) const; // Helper functions int indexOf(int rows, int columns) const; // inherit member data and functions of parent using Array::x; using Array::reset; using Array::operator (); using Array::size; using Array::setSize; using Array::at; protected: // inherit member data and functions of parent using Array::dim; using Array::mul; using Array::len; }; // Declare a few common typdefs typedef Array2 Array2b; typedef Array2 Array2c; typedef Array2 Array2uc; typedef Array2 Array2i; typedef Array2 Array2f; typedef Array2 Array2d; //============================================================================== // Array2 //============================================================================== // Constructors/Destructor /** Ctor that does no initalization. */ template inline Array2::Array2() : Array() {} /** Ctor that starts with given dimensions * @param rows is the number of rows of a two-dimensional array * @param columns is the number of columns of a two-dimensional array */ template inline Array2::Array2(int rows, int columns) : Array() { setSize(rows,columns); } /** Ctor that initializes elements from an array. * @param rows is the number of rows of a two-dimensional array * @param columns is the number of columns of a two-dimensional array * @param d pointer to the initalization array */ template inline Array2::Array2(int rows, int columns, const T* d) { setSize(rows,columns); set(d); } /** Ctor that initializes all elements from a scalar. * @param rows is the number of rows of a two-dimensional array * @param columns is the number of columns of a two-dimensional array * @param a the value to assign to all elements */ template inline Array2::Array2(int rows, int columns, const T a) { setSize(rows,columns); set(a); } /** Ctor that initializes an Array2 with a Array. * @param a is the array to duplicate */ template inline Array2::Array2(const Array& a) : Array(a) { } // Mutators /** Set the size of the array * @param rows is the number of rows of a two-dimensional array * @param columns is the number of columns of a two-dimensional array */ template inline void Array2::setSize(int rows, int columns) { Array::setSize(Vec2i(rows,columns)); } /** Returns mutable reference to array element * @param row is the row index * @param column is the column index * @return a reference to the array element */ template inline T& Array2::at(int row, int column) { return x[indexOf(row,column)]; } /** Returns mutable reference to array element * @param row is the row index * @param column is the column index * @return a reference to the array element */ template inline const T& Array2::at(int row, int column) const { return x[indexOf(row,column)]; } /** Returns mutable reference to array element * @param row is the row index * @param column is the column index * @return a reference to the array element */ template inline T& Array2::operator () (int row, int column) { return x[indexOf(row,column)]; } /** Returns array element * @param row is the row index * @param column is the column index * @return array element */ template inline T Array2::operator () (int row, int column) const { return x[indexOf(row,column)]; } /** Returns the number of rows * @return the number of rows */ template inline int Array2::rows() const { return Array::dim[0]; } /** Returns the number of columns * @return the number of columns */ template inline int Array2::columns() const { return Array::dim[1]; } // Helper functions (used internally only) /** Returns linear index of given array indices * @param row is the row index * @param column is the column index * @return the linear index in the data array x */ template inline int Array2::indexOf(int row, int column) const { #if AR_CHECK_BOUNDS if (row<0 || row>=dim(0) || column<0 || column>=dim(1)) { std::stringstream ss; ss << "Array2: Error. Indices (" << row << ", " << column; ss << ") exceed bounds [0, "; ss << dim(0) << "] or [0, " << dim(1) << "]."; ss << std::endl << std::flush; throw Exception(ss.str()); } #endif return row*mul(0)+column; } //============================================================================== } // namespace rtc //============================================================================== #endif // RTC_ARRAY2_H defined //==============================================================================