[Scummvm-git-logs] scummvm master -> 650e26b6ab3f6ca0ae685c53fd186ae87b2f84a0

sev- sev at scummvm.org
Wed Jul 25 10:18:36 CEST 2018


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
650e26b6ab COMMON: Add at() index function to cosine/sine table


Commit: 650e26b6ab3f6ca0ae685c53fd186ae87b2f84a0
    https://github.com/scummvm/scummvm/commit/650e26b6ab3f6ca0ae685c53fd186ae87b2f84a0
Author: David Fioramonti (dafioram at gmail.com)
Date: 2018-07-25T10:18:32+02:00

Commit Message:
COMMON: Add at() index function to cosine/sine table

The cos/sin table class now has an at() function for indexing
safely into its internal array.

This allows the checking and computing of the correct
indexes to be done internally.

The indexing in allows cos/sine of 0 to 2pi to be
obtained.

The values returned by getTable are the same as before.

Comments that describe the values that the table
contains has been modified to line up with what
the code is doing.

Changed paths:
    common/cosinetables.cpp
    common/cosinetables.h
    common/sinetables.cpp
    common/sinetables.h


diff --git a/common/cosinetables.cpp b/common/cosinetables.cpp
index 4bee1f2..42c6e4e 100644
--- a/common/cosinetables.cpp
+++ b/common/cosinetables.cpp
@@ -32,17 +32,35 @@ CosineTable::CosineTable(int bitPrecision) {
 
 	_bitPrecision = bitPrecision;
 
-	int m = 1 << _bitPrecision;
-	double freq = 2 * M_PI / m;
-	_table = new float[m / 2];
+	_nPoints = 1 << _bitPrecision;
+	_radResolution = 2.0 * M_PI / _nPoints;
+	_refSize = _nPoints / 4;
+	_table = new float[_nPoints / 2];
 
-	// Table contains cos(2*pi*i/m) for 0<=i<m/4,
-	// followed by 3m/4<=i<m
-	for (int i = 0; i <= m / 4; i++)
-		_table[i] = cos(i * freq);
+	// Table contains cos(2*pi*i/_nPoints) for 0<=i<=_nPoints/4,
+	// followed by 3_nPoints/4<=i<_nPoints
+	for (int i = 0; i <= _nPoints / 4; i++)
+		_table[i] = cos(i * _radResolution);
 
-	for (int i = 1; i < m / 4; i++)
-		_table[m / 2 - i] = _table[i];
+	for (int i = 1; i < _nPoints / 4; i++)
+		_table[_nPoints / 2 - i] = _table[i];
+}
+
+float CosineTable::at(int index) const {
+	assert((index >= 0) && (index < _nPoints));
+	if (index < _refSize)
+		// [0,pi/2)
+		return _table[index];
+	if ((index > _refSize) && (index < 2 * _refSize))
+		// (pi/2,pi)
+		return -_table[2 * _refSize - index];
+	if ((index >= 2 * _refSize) && (index < 3 * _refSize))
+		// [pi,3/2pi)
+		return -_table[index - 2 * _refSize];
+	if ((index > 3 * _refSize) && (index < _nPoints))
+		// (3/2pi,2pi)
+		return _table[_nPoints - index];
+	return 0.0f; // cos(pi/2) and cos(3pi/2) = 0
 }
 
 CosineTable::~CosineTable() {
diff --git a/common/cosinetables.h b/common/cosinetables.h
index 0ff01e4..c08bb4c 100644
--- a/common/cosinetables.h
+++ b/common/cosinetables.h
@@ -39,22 +39,32 @@ public:
 	 * Get pointer to table.
 	 *
 	 * This table contains 2^bitPrecision/2 entries.
+	 * Prefer to use at()
 	 * The layout of this table is as follows:
-	 * - Entries 0 up to (excluding) 2^bitPrecision/4:
-	 *           cos(0) till (excluding) cos(1/2*pi)
-	 * - Entries 2^bitPrecision/4 up to (excluding) 2^bitPrecision/2:
-	 *           cos(3/2*pi) till (excluding) cos(2*pi)
+	 * - Entries 0 up to (including) 2^bitPrecision/4:
+	 *           cos(0) till (including) cos(1/2*pi)
+	 * - Entries (excluding) 2^bitPrecision/4 up to 2^bitPrecision/2:
+	 *           (excluding) cos(3/2*pi) till (excluding) cos(2*pi)
 	 */
 	const float *getTable() { return _table; }
 
 	/**
-	 * Get pointer to table
+	 * Returns cos(2*pi * index / 2^bitPrecision )
+	 * Index must be in range [0,2^bitPrecision-1]
 	 */
-	int getPrecision() { return _bitPrecision; }
+	float at(int index) const;
+
+	/**
+	 * Get bit precision
+	 */
+	int getBitPrecision() { return _bitPrecision; }
 
 private:
 	float *_table;
 	int _bitPrecision;
+	double _radResolution; // Smallest radian increment
+	int _refSize; // _nPoints / 4
+	int _nPoints; // range of operator[]
 };
 
 } // End of namespace Common
diff --git a/common/sinetables.cpp b/common/sinetables.cpp
index c01705e..c85a7bf 100644
--- a/common/sinetables.cpp
+++ b/common/sinetables.cpp
@@ -32,17 +32,39 @@ SineTable::SineTable(int bitPrecision) {
 
 	_bitPrecision = bitPrecision;
 
-	int m = 1 << _bitPrecision;
-	double freq = 2 * M_PI / m;
-	_table = new float[m / 2];
+	_nPoints = 1 << _bitPrecision;
+	_radResolution = 2.0 * M_PI / _nPoints;
+	_refSize = _nPoints / 4;
+	_table = new float[_nPoints / 2];
 
-	// Table contains sin(2*pi*i/m) for 0<=i<m/4,
-	// followed by m/2<=i<3m/4
-	for (int i = 0; i < m / 4; i++)
-		_table[i] = sin(i * freq);
+	// Table contains sin(2*pi*i/_nPoints) for 0<=i<_nPoints/4,
+	// followed by _nPoints/2<=i<3_nPoints/4
+	for (int i = 0; i < _nPoints / 4; i++)
+		_table[i] = sin(i * _radResolution);
 
-	for (int i = 0; i < m / 4; i++)
-		_table[m / 4 + i] = -_table[i];
+	for (int i = 0; i < _nPoints / 4; i++)
+		_table[_nPoints / 4 + i] = -_table[i];
+
+}
+
+float SineTable::at(int index) const { 
+	assert((index >= 0) && (index < _nPoints));
+	if (index < _refSize)
+		// [0,pi/2)
+		return _table[index];
+	if (index == _refSize)
+		// pi/2
+		return 1.0f; // sin(pi/2) = 1.0	
+	if ((index > _refSize) && (index < 2 * _refSize))
+		// (pi/2,pi)
+		return _table[2 * _refSize - index];
+	if ((index >= 2 * _refSize) && (index < 3 * _refSize))
+		// [pi,3/2pi)
+		return -_table[index - 2 * _refSize];
+	if ((index > 3 * _refSize) && (index < _nPoints))
+		// (3/2pi,2pi)
+		return -_table[_nPoints - index];
+	return -1.0f; // sin(3pi/2) = -1.0
 }
 
 SineTable::~SineTable() {
diff --git a/common/sinetables.h b/common/sinetables.h
index 67225c0..f91592c 100644
--- a/common/sinetables.h
+++ b/common/sinetables.h
@@ -39,22 +39,32 @@ public:
 	 * Get pointer to table
 	 *
 	 * This table contains 2^bitPrecision/2 entries.
+	 * Prefer to use at()
 	 * The layout of this table is as follows:
 	 * - Entries 0 up to (excluding) 2^bitPrecision/4:
 	 *           sin(0) till (excluding) sin(1/2*pi)
-	 * - Entries 2^bitPrecision/4 up to (excluding) 2^bitPrecision/2:
+	 * - Entries 2^bitPrecision/4 up to 2^bitPrecision/2:
 	 *           sin(pi) till (excluding) sin(3/2*pi)
 	 */
 	const float *getTable() { return _table; }
 
 	/**
-	 * Get pointer to table
+	 * Returns sin(2*pi * index / 2^bitPrecision )
+	 * Index must be in range [0,2^bitPrecision-1]
+	 */
+	float at(int index) const;
+
+	/**
+	 * Get bit precision
 	 */
-	int getPrecision() { return _bitPrecision; }
+	int getBitPrecision() { return _bitPrecision; }
 
 private:
 	float *_table;
 	int _bitPrecision;
+	double _radResolution; // Smallest radian increment
+	int _refSize; // _nPoints / 4
+	int _nPoints; // range of operator[]
 };
 
 } // End of namespace Common





More information about the Scummvm-git-logs mailing list