TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Map2D< Storage, Compute > Class Template Reference

#include <Map2D.h>

Public Types

typedef Storage StorageType
 
typedef Compute ComputeType
 
typedef Map2D< Storage, Compute > Type
 
typedef shared_ptr< Map2DRef
 

Public Member Functions

const Storage & fastGet (int x, int y) const
 
void fastSet (int x, int y, const Storage &v)
 
void resize (uint32 newW, uint32 newH)
 
bool changed ()
 
void setChanged (bool c)
 
Storage * getCArray ()
 
const Storage * getCArray () const
 
Array< Storage > & getArray ()
 
const Array< Storage > & getArray () const
 
bool inBounds (int x, int y) const
 
bool inBounds (const Vector2int16 &v) const
 
const Storage & get (int x, int y, WrapMode wrap) const
 
const Storage & get (int x, int y) const
 
const Storage & get (const Vector2int16 &p) const
 
const Storage & get (const Vector2int16 &p, WrapMode wrap) const
 
Storage & get (int x, int y, WrapMode wrap)
 
Storage & get (int x, int y)
 
Storage & get (const Vector2int16 &p)
 
void set (const Vector2int16 &p, const Storage &v)
 
void set (int x, int y, const Storage &v, WrapMode wrap)
 
void set (int x, int y, const Storage &v)
 
void setAll (const Storage &v)
 
template<class T >
void set (const shared_ptr< Map2D< Storage, T > > &src)
 
void maybeFlipVertical (bool flip)
 
virtual void flipVertical ()
 
virtual void flipHorizontal ()
 
virtual void crop (int newX, int newY, int newW, int newH)
 
virtual void crop (const Rect2D &rect)
 
Compute nearest (float x, float y, WrapMode wrap) const
 
Compute nearest (float x, float y) const
 
Compute nearest (const Vector2 &p) const
 
Compute average () const
 
Compute bilinear (float x, float y, WrapMode wrap) const
 
Compute bilinear (float x, float y) const
 
Compute bilinear (const Vector2 &p) const
 
Compute bilinear (const Vector2 &p, WrapMode wrap) const
 
Compute bicubic (float x, float y, WrapMode wrap) const
 
Compute bicubic (float x, float y) const
 
Compute bicubic (const Vector2 &p, WrapMode wrap) const
 
Compute bicubic (const Vector2 &p) const
 
int32 width () const
 
int32 height () const
 
Vector2int16 size () const
 
Rect2D rect2DBounds () const
 
size_t sizeInMemory () const
 
WrapMode wrapMode () const
 
void setWrapMode (WrapMode m)
 
- Public Member Functions inherited from G3D::ReferenceCountedObject
virtual ~ReferenceCountedObject ()
 

Static Public Member Functions

static Ref create (int w=0, int h=0, WrapMode wrap=WrapMode::ERROR)
 

Public Attributes

GMutex mutex
 

Protected Member Functions

const Storage & slowGet (int x, int y, WrapMode wrap)
 
Compute bicubic (const Compute *ctrl, double s) const
 
 Map2D (int w, int h, WrapMode wrap)
 

Protected Attributes

Storage ZERO
 
uint32 w
 
uint32 h
 
WrapMode _wrapMode
 
AtomicInt32 m_changed
 
Array< Storage > data
 

Detailed Description

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
class G3D::Map2D< Storage, Compute >

Map of values across a discrete 2D plane. Can be thought of as a generic class for 2D images, allowing flexibility as to pixel format and convenient methods. In fact, the "pixels" can be any values on a grid that can be sensibly interpolated–RGB colors, scalars, 4D vectors, and so on.

Other "image" classes in G3D:

G3D::GImage - Supports file formats, fast, Color3uint8 and Color4uint8 formats. No interpolation.

G3D::shared_ptr<Texture> - Represents image on the graphics card (not directly readable on the CPU). Supports 2D, 3D, and a variety of interpolation methods, loads file formats.

G3D::Image3 - A subclass of Map2D<Color3> that supports image loading and saving and conversion to Texture.

G3D::Image4 - A subclass of Map2D<Color4> that supports image loading and saving and conversion to Texture.

G3D::Image3uint8 - A subclass of Map2D<Color3uint8> that supports image loading and saving and conversion to Texture.

G3D::Image4uint8 - A subclass of Map2D<Color4uint8> that supports image loading and saving and conversion to Texture.

There are two type parameters– the first (@ Storage) is the type used to store the "pixel" values efficiently and the second (Compute) is the type operated on by computation. The Compute::Compute(Storage&) constructor is used to convert between storage and computation types. Storage is often an integer version of Compute, for example Map2D<double, uint8>. By default, the computation type is:

   Storage       Computation
   uint8          float32
   uint16         float32
   uint32         float64
   uint64         float64
   int8           float32
   int16          float32
   int32          float64
   int64          float64
   float32        float64
   float64        float64
   Vector2        Vector2
   Vector2int16   Vector2
   Vector3        Vector3
   Vector3int16   Vector3
   Vector4        Vector4
   Color3         Color3
   Color3uint8    Color3
   Color4         Color4
   Color4uint8    Color4
  

Any other storage type defaults to itself as the computation type.

The computation type can be any that supports lerp, +, -, *, /, and an empty constructor.

Assign value:

im->set(x, y, 7); or im->get(x, y) = 7;

Read value:

int c = im(x, y);

Can also sample with nearest neighbor, bilinear, and bicubic interpolation.

Sampling follows OpenGL conventions, where pixel values represent grid points and (0.5, 0.5) is half-way between two vertical and two horizontal grid points. To draw an image of dimensions w x h with nearest neighbor sampling, render pixels from [0, 0] to [w - 1, h - 1].

Under the WrapMode::CLAMP wrap mode, the value of bilinear interpolation becomes constant outside [1, w - 2] horizontally. Nearest neighbor interpolation is constant outside [0, w - 1] and bicubic outside [3, w - 4]. The class does not offer quadratic interpolation because the interpolation filter could not center over a pixel.

Author
Morgan McGuire, http://graphics.cs.williams.edu

Member Typedef Documentation

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
typedef Compute G3D::Map2D< Storage, Compute >::ComputeType
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
typedef shared_ptr<Map2D> G3D::Map2D< Storage, Compute >::Ref
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
typedef Storage G3D::Map2D< Storage, Compute >::StorageType
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
typedef Map2D<Storage, Compute> G3D::Map2D< Storage, Compute >::Type

Constructor & Destructor Documentation

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
G3D::Map2D< Storage, Compute >::Map2D ( int  w,
int  h,
WrapMode  wrap 
)
inlineprotected
284  : w(0), h(0), _wrapMode(wrap), m_changed(1) {
285  ZERO = Storage(Compute(Storage()) * 0);
286  resize(w, h);
287  }
Storage ZERO
Definition: Map2D.h:188
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
WrapMode _wrapMode
Definition: Map2D.h:196
AtomicInt32 m_changed
Definition: Map2D.h:200
void resize(uint32 newW, uint32 newH)
Definition: Map2D.h:304

+ Here is the caller graph for this function:

Member Function Documentation

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::average ( ) const
inline

Returns the average value of all elements of the map

539  {
540  if ((w == 0) || (h == 0)) {
541  return ZERO;
542  }
543 
544  // To avoid overflows, compute the average of row averages
545 
546  Compute rowSum = ZERO;
547  for (unsigned int y = 0; y < h; ++y) {
548  Compute sum = ZERO;
549  int offset = y * w;
550  for (unsigned int x = 0; x < w; ++x) {
551  sum += Compute(data[offset + x]);
552  }
553  rowSum += sum * (1.0f / w);
554  }
555 
556  return rowSum * (1.0f / h);
557  }
Storage ZERO
Definition: Map2D.h:188
Array< Storage > data
Definition: Map2D.h:202
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bicubic ( const Compute *  ctrl,
double  s 
) const
inlineprotected

Given four control points and a value on the range [0, 1) evaluates the Catmull-rom spline between the times of the middle two control points

251  {
252 
253  // f = B * S * ctrl'
254 
255  // B matrix: Catmull-Rom spline basis
256  static const double B[4][4] = {
257  { 0.0, -0.5, 1.0, -0.5},
258  { 1.0, 0.0, -2.5, 1.5},
259  { 0.0, 0.5, 2.0, -1.5},
260  { 0.0, 0.0, -0.5, 0.5}};
261 
262  // S: Powers of the fraction
263  double S[4];
264  double s2 = s * s;
265  S[0] = 1.0;
266  S[1] = s;
267  S[2] = s2;
268  S[3] = s2 * s;
269 
270  Compute sum(ZERO);
271 
272  for (int c = 0; c < 4; ++c) {
273  double coeff = 0.0;
274  for (int power = 0; power < 4; ++power) {
275  coeff += B[c][power] * S[power];
276  }
277  sum += ctrl[c] * coeff;
278  }
279 
280  return sum;
281  }
Storage ZERO
Definition: Map2D.h:188
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bicubic ( float  x,
float  y,
WrapMode  wrap 
) const
inline

Uses Catmull-Rom splines to interpolate between grid values. Guaranteed to match nearest(x, y) at integers.

604  {
605  int i = iFloor(x);
606  int j = iFloor(y);
607  float fX = x - i;
608  float fY = y - j;
609 
610  Compute vsample[4];
611  for (int v = 0; v < 4; ++v) {
612 
613  // Horizontal interpolation
614  Compute hsample[4];
615  for (int u = 0; u < 4; ++u) {
616  hsample[u] = Compute(get(i + u - 1, j + v - 1, wrap));
617  }
618 
619  vsample[v] = bicubic(hsample, fX);
620  }
621 
622  // Vertical interpolation
623  return bicubic(vsample, fY);
624  }
int iFloor(double fValue)
Definition: g3dmath.h:603
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
G3D::int16 y
Definition: Vector2int16.h:38
Compute bicubic(const Compute *ctrl, double s) const
Definition: Map2D.h:251
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bicubic ( float  x,
float  y 
) const
inline
626  {
627  return bicubic(x, y, _wrapMode);
628  }
WrapMode _wrapMode
Definition: Map2D.h:196
G3D::int16 y
Definition: Vector2int16.h:38
Compute bicubic(const Compute *ctrl, double s) const
Definition: Map2D.h:251
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bicubic ( const Vector2 p,
WrapMode  wrap 
) const
inline
630  {
631  return bicubic(p.x, p.y, wrap);
632  }
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
Compute bicubic(const Compute *ctrl, double s) const
Definition: Map2D.h:251
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bicubic ( const Vector2 p) const
inline
634  {
635  return bicubic(p.x, p.y, _wrapMode);
636  }
WrapMode _wrapMode
Definition: Map2D.h:196
Compute bicubic(const Compute *ctrl, double s) const
Definition: Map2D.h:251
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bilinear ( float  x,
float  y,
WrapMode  wrap 
) const
inline

Needs to access elements from (floor(x), floor(y)) to (floor(x) + 1, floor(y) + 1) and will use the wrap mode appropriately (possibly generating out of bounds errors).

Guaranteed to match nearest(x, y) at integers.

566  {
567  const int i = iFloor(x);
568  const int j = iFloor(y);
569 
570  const float fX = x - i;
571  const float fY = y - j;
572 
573  // Horizontal interpolation, first row
574  const Compute& t0 = get(i, j, wrap);
575  const Compute& t1 = get(i + 1, j, wrap);
576 
577  // Horizontal interpolation, second row
578  const Compute& t2 = get(i, j + 1, wrap);
579  const Compute& t3 = get(i + 1, j + 1, wrap);
580 
581  const Compute& A = lerp(t0, t1, fX);
582  const Compute& B = lerp(t2, t3, fX);
583 
584  // Vertical interpolation
585  return lerp(A, B, fY);
586  }
int iFloor(double fValue)
Definition: g3dmath.h:603
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
G3D::int16 y
Definition: Vector2int16.h:38
double lerp(double a, double b, double f)
Definition: g3dmath.h:189
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bilinear ( float  x,
float  y 
) const
inline
588  {
589  return bilinear(x, y, _wrapMode);
590  }
WrapMode _wrapMode
Definition: Map2D.h:196
G3D::int16 y
Definition: Vector2int16.h:38
Compute bilinear(float x, float y, WrapMode wrap) const
Definition: Map2D.h:566
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bilinear ( const Vector2 p) const
inline
592  {
593  return bilinear(p.x, p.y, _wrapMode);
594  }
WrapMode _wrapMode
Definition: Map2D.h:196
Compute bilinear(float x, float y, WrapMode wrap) const
Definition: Map2D.h:566
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::bilinear ( const Vector2 p,
WrapMode  wrap 
) const
inline
596  {
597  return bilinear(p.x, p.y, wrap);
598  }
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
Compute bilinear(float x, float y, WrapMode wrap) const
Definition: Map2D.h:566
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
bool G3D::Map2D< Storage, Compute >::changed ( )
inline

Returns true if this map has been written to since the last call to setChanged(false). This is useful if you are caching a texture map other value that must be recomputed whenever this changes.

318  {
319  return m_changed.value() != 0;
320  }
int32 value() const
Definition: AtomicInt32.h:67
AtomicInt32 m_changed
Definition: Map2D.h:200
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
static Ref G3D::Map2D< Storage, Compute >::create ( int  w = 0,
int  h = 0,
WrapMode  wrap = WrapMode::ERROR 
)
inlinestatic
298  {
299  return Ref(new Map2D(w, h, wrap));
300  }
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
shared_ptr< Map2D > Ref
Definition: Map2D.h:184
uint32 w
Definition: Map2D.h:191
Map2D(int w, int h, WrapMode wrap)
Definition: Map2D.h:284
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
virtual void G3D::Map2D< Storage, Compute >::crop ( int  newX,
int  newY,
int  newW,
int  newH 
)
inlinevirtual

Crops this map so that it only contains pixels between (x, y) and (x + w - 1, y + h - 1) inclusive.

499  {
500  alwaysAssertM(newX + newW <= (int)w, "Cannot grow when cropping");
501  alwaysAssertM(newY + newH <= (int)h, "Cannot grow when cropping");
502  alwaysAssertM(newX >= 0 && newY >= 0, "Origin out of bounds.");
503 
504  // Always safe to copy towards the upper left, provided
505  // that we're iterating towards the lower right. This lets us avoid
506  // reallocating the underlying array.
507  for (int y = 0; y < newH; ++y) {
508  for (int x = 0; x < newW; ++x) {
509  data[x + y * newW] = data[(x + newX) + (y + newY) * w];
510  }
511  }
512 
513  resize(newW, newH);
514  }
Array< Storage > data
Definition: Map2D.h:202
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
void resize(uint32 newW, uint32 newH)
Definition: Map2D.h:304

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
virtual void G3D::Map2D< Storage, Compute >::crop ( const Rect2D rect)
inlinevirtual

iRounds to the nearest x0 and y0.

517  {
518  crop(iRound(rect.x0()), iRound(rect.y0()), iRound(rect.x1()) - iRound(rect.x0()), iRound(rect.y1()) - iRound(rect.y0()));
519  }
int iRound(double fValue)
Definition: g3dmath.h:226
virtual void crop(int newX, int newY, int newW, int newH)
Definition: Map2D.h:499
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::fastGet ( int  x,
int  y 
) const
inline

Unsafe access to the underlying data structure with no wrapping support; requires that (x, y) is in bounds.

235  {
236  debugAssert(((uint32)x < w) && ((uint32)y < h));
237  return data[x + y * w];
238  }
Array< Storage > data
Definition: Map2D.h:202
uint32 h
Definition: Map2D.h:194
#define debugAssert(exp)
Definition: debugAssert.h:160
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::fastSet ( int  x,
int  y,
const Storage &  v 
)
inline

Unsafe access to the underlying data structure with no wrapping support; requires that (x, y) is in bounds.

241  {
242  debugAssert(((uint32)x < w) && ((uint32)y < h));
243  data[x + y * w] = v;
244  }
Array< Storage > data
Definition: Map2D.h:202
uint32 h
Definition: Map2D.h:194
#define debugAssert(exp)
Definition: debugAssert.h:160
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
virtual void G3D::Map2D< Storage, Compute >::flipHorizontal ( )
inlinevirtual
481  {
482  int halfWidth = w / 2;
483  Storage* d = data.getCArray();
484  for (int x = 0; x < halfWidth; ++x) {
485  for (int y = 0; y < (int)h; ++y) {
486  int i1 = y * w + x;
487  int i2 = y * w + (w - x - 1);
488  Storage temp = d[i1];
489  d[i1] = d[i2];
490  d[i2] = temp;
491  }
492  }
493  setChanged(true);
494  }
Array< Storage > data
Definition: Map2D.h:202
T * getCArray()
Definition: Array.h:256
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
G3D::int16 y
Definition: Vector2int16.h:38
void setChanged(bool c)
Definition: Map2D.h:323
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
virtual void G3D::Map2D< Storage, Compute >::flipVertical ( )
inlinevirtual
464  {
465  int halfHeight = h/2;
466  Storage* d = data.getCArray();
467  for (int y = 0; y < halfHeight; ++y) {
468  int o1 = y * w;
469  int o2 = (h - y - 1) * w;
470  for (int x = 0; x < (int)w; ++x) {
471  int i1 = o1 + x;
472  int i2 = o2 + x;
473  Storage temp = d[i1];
474  d[i1] = d[i2];
475  d[i2] = temp;
476  }
477  }
478  setChanged(true);
479  }
Array< Storage > data
Definition: Map2D.h:202
T * getCArray()
Definition: Array.h:256
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
G3D::int16 y
Definition: Vector2int16.h:38
void setChanged(bool c)
Definition: Map2D.h:323
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::get ( int  x,
int  y,
WrapMode  wrap 
) const
inline

Get the value at (x, y).

Note that the type of image->get(x, y) is the storage type, not the computation type. If the constructor promoting Storage to Compute rescales values (as, for example Color3(Color3uint8&) does), this will not match the value returned by Map2D::nearest.

367  {
368  if (((uint32)x < w) && ((uint32)y < h)) {
369  return data[x + y * w];
370  } else {
371  // Remove the const to allow a slowGet on this object
372  // (we're returning a const reference so this is ok)
373  return const_cast<Type*>(this)->slowGet(x, y, wrap);
374  }
375 # ifndef G3D_WINDOWS
376  // gcc gives a useless warning that the above code might reach the end of the function;
377  // we use this line to supress the warning.
378  return ZERO;
379 # endif
380  }
Storage ZERO
Definition: Map2D.h:188
Array< Storage > data
Definition: Map2D.h:202
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
const Storage & slowGet(int x, int y, WrapMode wrap)
Definition: Map2D.h:205
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
Type
Type of JSON value.
Definition: rapidjson.h:642
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::get ( int  x,
int  y 
) const
inline
382  {
383  return get(x, y, _wrapMode);
384  }
WrapMode _wrapMode
Definition: Map2D.h:196
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::get ( const Vector2int16 p) const
inline
386  {
387  return get(p.x, p.y, _wrapMode);
388  }
WrapMode _wrapMode
Definition: Map2D.h:196
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::get ( const Vector2int16 p,
WrapMode  wrap 
) const
inline
390  {
391  return get(p.x, p.y, wrap);
392  }
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Storage& G3D::Map2D< Storage, Compute >::get ( int  x,
int  y,
WrapMode  wrap 
)
inline
394  {
395  return const_cast<Storage&>(const_cast<const Type*>(this)->get(x, y, wrap));
396 # ifndef G3D_WINDOWS
397  // gcc gives a useless warning that the above code might reach the end of the function;
398  // we use this line to supress the warning.
399  return ZERO;
400 # endif
401  }
Storage ZERO
Definition: Map2D.h:188
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
Type
Type of JSON value.
Definition: rapidjson.h:642
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Storage& G3D::Map2D< Storage, Compute >::get ( int  x,
int  y 
)
inline
403  {
404  return const_cast<Storage&>(const_cast<const Type*>(this)->get(x, y));
405 # ifndef G3D_WINDOWS
406  // gcc gives a useless warning that the above code might reach the end of the function;
407  // we use this line to supress the warning.
408  return ZERO;
409 # endif
410  }
Storage ZERO
Definition: Map2D.h:188
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
Type
Type of JSON value.
Definition: rapidjson.h:642
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Storage& G3D::Map2D< Storage, Compute >::get ( const Vector2int16 p)
inline
412  {
413  return get(p.x, p.y);
414  }
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Array<Storage>& G3D::Map2D< Storage, Compute >::getArray ( )
inline

Row-major array. You should call setChanged(true) if you mutate the array.

340  {
341  return data;
342  }
Array< Storage > data
Definition: Map2D.h:202
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Array<Storage>& G3D::Map2D< Storage, Compute >::getArray ( ) const
inline
345  {
346  return data;
347  }
Array< Storage > data
Definition: Map2D.h:202
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Storage* G3D::Map2D< Storage, Compute >::getCArray ( )
inline

Returns a pointer to the underlying row-major data. There is no padding at the end of the row. Be careful–this will be reallocated during a resize. You should call setChanged(true) if you mutate the array.

329  {
330  return data.getCArray();
331  }
Array< Storage > data
Definition: Map2D.h:202
T * getCArray()
Definition: Array.h:256
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage* G3D::Map2D< Storage, Compute >::getCArray ( ) const
inline
334  {
335  return data.getCArray();
336  }
Array< Storage > data
Definition: Map2D.h:202
T * getCArray()
Definition: Array.h:256
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
int32 G3D::Map2D< Storage, Compute >::height ( ) const
inline

Pixel height

645  {
646  return (int32)h;
647  }
uint32 h
Definition: Map2D.h:194
int32_t int32
Definition: Define.h:146

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
bool G3D::Map2D< Storage, Compute >::inBounds ( int  x,
int  y 
) const
inline

is (x, y) strictly within the image bounds, or will it trigger some kind of wrap mode

350  {
351  return (((uint32)x < w) && ((uint32)y < h));
352  }
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
bool G3D::Map2D< Storage, Compute >::inBounds ( const Vector2int16 v) const
inline

is (x, y) strictly within the image bounds, or will it trigger some kind of wrap mode

355  {
356  return inBounds(v.x, v.y);
357  }
bool inBounds(int x, int y) const
Definition: Map2D.h:350
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::maybeFlipVertical ( bool  flip)
inline

flips if flip is true

458  {
459  if (flip) {
460  flipVertical();
461  }
462  }
virtual void flipVertical()
Definition: Map2D.h:464
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::nearest ( float  x,
float  y,
WrapMode  wrap 
) const
inline

Returns the nearest neighbor. Pixel values are considered to be at the upper left corner, so image->nearest(x, y) == image(x, y)

524  {
525  int ix = iRound(x);
526  int iy = iRound(y);
527  return Compute(get(ix, iy, wrap));
528  }
int iRound(double fValue)
Definition: g3dmath.h:226
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::nearest ( float  x,
float  y 
) const
inline
530  {
531  return nearest(x, y, _wrapMode);
532  }
Compute nearest(float x, float y, WrapMode wrap) const
Definition: Map2D.h:524
WrapMode _wrapMode
Definition: Map2D.h:196
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Compute G3D::Map2D< Storage, Compute >::nearest ( const Vector2 p) const
inline
534  {
535  return nearest(p.x, p.y);
536  }
Compute nearest(float x, float y, WrapMode wrap) const
Definition: Map2D.h:524
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Rect2D G3D::Map2D< Storage, Compute >::rect2DBounds ( ) const
inline

Rectangle from (0, 0) to (w, h)

656  {
657  return Rect2D::xywh(0, 0, w, h);
658  }
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
static Rect2D xywh(float x, float y, float w, float h)
Definition: Rect2D.h:260
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::resize ( uint32  newW,
uint32  newH 
)
inline

Resizes without clearing, leaving garbage.

304  {
305  if ((newW != w) || (newH != h)) {
306  w = newW;
307  h = newH;
308  data.resize(w * h);
309  setChanged(true);
310  }
311  }
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
Array< Storage > data
Definition: Map2D.h:202
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
void setChanged(bool c)
Definition: Map2D.h:323

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::set ( const Vector2int16 p,
const Storage &  v 
)
inline

Sets the changed flag to true

417  {
418  set(p.x, p.y, v);
419  }
void set(const Vector2int16 &p, const Storage &v)
Definition: Map2D.h:417

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::set ( int  x,
int  y,
const Storage &  v,
WrapMode  wrap 
)
inline

Sets the changed flag to true

422  {
423  setChanged(true);
424  if (((uint32)x < w) && ((uint32)y < h)) {
425  // In bounds, wrapping isn't an issue.
426  data[x + y * w] = v;
427  } else {
428  const_cast<Storage&>(slowGet(x, y, wrap)) = v;
429  }
430  }
Array< Storage > data
Definition: Map2D.h:202
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
const Storage & slowGet(int x, int y, WrapMode wrap)
Definition: Map2D.h:205
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
void setChanged(bool c)
Definition: Map2D.h:323
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::set ( int  x,
int  y,
const Storage &  v 
)
inline
432  {
433  set(x, y, v, _wrapMode);
434  }
WrapMode _wrapMode
Definition: Map2D.h:196
G3D::int16 y
Definition: Vector2int16.h:38
void set(const Vector2int16 &p, const Storage &v)
Definition: Map2D.h:417
G3D::int16 x
Definition: Vector2int16.h:37
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
template<class T >
void G3D::Map2D< Storage, Compute >::set ( const shared_ptr< Map2D< Storage, T > > &  src)
inline

Copy values from src, which must have the same size

446  {
447  debugAssert(src->width() == width());
448  debugAssert(src->height() == height());
449  const Array<Storage>& s = src->data;
450  int N = w * h;
451  for (int i = 0; i < N; ++i) {
452  data[i] = s[i];
453  }
454  setChanged(true);
455  }
int32 height() const
Definition: Map2D.h:645
Array< Storage > data
Definition: Map2D.h:202
uint32 h
Definition: Map2D.h:194
int32 width() const
Definition: Map2D.h:639
#define debugAssert(exp)
Definition: debugAssert.h:160
uint32 w
Definition: Map2D.h:191
void setChanged(bool c)
Definition: Map2D.h:323
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::setAll ( const Storage &  v)
inline
437  {
438  for(int i = 0; i < data.size(); ++i) {
439  data[i] = v;
440  }
441  setChanged(true);
442  }
Array< Storage > data
Definition: Map2D.h:202
int size() const
Definition: Array.h:430
void setChanged(bool c)
Definition: Map2D.h:323
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::setChanged ( bool  c)
inline

Set/unset the changed flag.

323  {
324  m_changed = c ? 1 : 0;
325  }
AtomicInt32 m_changed
Definition: Map2D.h:200

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
void G3D::Map2D< Storage, Compute >::setWrapMode ( WrapMode  m)
inline
671  {
672  _wrapMode = m;
673  }
WrapMode _wrapMode
Definition: Map2D.h:196
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Vector2int16 G3D::Map2D< Storage, Compute >::size ( ) const
inline

Dimensions in pixels

651  {
652  return Vector2int16(w, h);
653  }
Vector2int16
Definition: Vector2int16.h:28
uint32 h
Definition: Map2D.h:194
uint32 w
Definition: Map2D.h:191
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
size_t G3D::Map2D< Storage, Compute >::sizeInMemory ( ) const
inline

Number of bytes occupied by the image data and this structure

661  {
662  return data.size() * sizeof(Storage) + sizeof(*this);
663  }
Array< Storage > data
Definition: Map2D.h:202
int size() const
Definition: Array.h:430
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
const Storage& G3D::Map2D< Storage, Compute >::slowGet ( int  x,
int  y,
WrapMode  wrap 
)
inlineprotected

Handles the exceptional cases from get

205  {
206  switch (wrap) {
207  case WrapMode::CLAMP:
208  return fastGet(iClamp(x, 0, w - 1), iClamp(y, 0, h - 1));
209 
210  case WrapMode::TILE:
211  return fastGet(iWrap(x, w), iWrap(y, h));
212 
213  case WrapMode::ZERO:
214  return ZERO;
215 
216  case WrapMode::ERROR:
217  alwaysAssertM(((uint32)x < w) && ((uint32)y < h),
218  format("Index out of bounds: (%d, %d), w = %d, h = %d",
219  x, y, w, h));
220 
221  // intentionally fall through
222  case WrapMode::IGNORE:
223  // intentionally fall through
224  default:
225  {
226  static Storage temp;
227  return temp;
228  }
229  }
230  }
Storage ZERO
Definition: Map2D.h:188
Definition: WrapMode.h:60
Definition: WrapMode.h:61
Definition: WrapMode.h:63
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
const Storage & fastGet(int x, int y) const
Definition: Map2D.h:235
Definition: WrapMode.h:59
Definition: WrapMode.h:62
uint32 w
Definition: Map2D.h:191
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
int iWrap(int val, int hi)
Definition: g3dmath.h:594
G3D::int16 x
Definition: Vector2int16.h:37
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
int iClamp(int val, int low, int hi)
Definition: g3dmath.h:545

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
int32 G3D::Map2D< Storage, Compute >::width ( ) const
inline

Pixel width

639  {
640  return (int32)w;
641  }
uint32 w
Definition: Map2D.h:191
int32_t int32
Definition: Define.h:146

+ Here is the caller graph for this function:

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
WrapMode G3D::Map2D< Storage, Compute >::wrapMode ( ) const
inline
666  {
667  return _wrapMode;
668  }
WrapMode _wrapMode
Definition: Map2D.h:196

Member Data Documentation

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
WrapMode G3D::Map2D< Storage, Compute >::_wrapMode
protected
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Array<Storage> G3D::Map2D< Storage, Compute >::data
protected
template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
uint32 G3D::Map2D< Storage, Compute >::h
protected

Height, in pixels.

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
AtomicInt32 G3D::Map2D< Storage, Compute >::m_changed
protected

0 if no mutating method has been invoked since the last call to setChanged();

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
GMutex G3D::Map2D< Storage, Compute >::mutex

Although Map2D is not threadsafe (except for the setChanged() method), you can use this mutex to create your own threadsafe access to a Map2D. Not used by the default implementation.

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
uint32 G3D::Map2D< Storage, Compute >::w
protected

Width, in pixels.

template<typename Storage, typename Compute = typename G3D::_internal::_GetComputeType<Storage>::Type>
Storage G3D::Map2D< Storage, Compute >::ZERO
protected

The documentation for this class was generated from the following file: