Cleaned up the code, all test cases I have sent at it work.

check current compatibility at:
http://www.happypedestrian.com/lwjgl/matrixop.html
This commit is contained in:
Tristan Campbell 2002-09-08 23:57:33 +00:00
parent a27f7dc6d0
commit 0b758f82ef
16 changed files with 187 additions and 173 deletions

View File

@ -70,7 +70,7 @@ void Matrix::transposeMatrix(float * mat, int src_width, int src_height)
}
SrcMatrix::SrcMatrix ( jint addr, jint s,
MatrixSrc::MatrixSrc ( jint addr, jint s,
jint w, jint h,
jint e, jboolean t):
Matrix(addr, s, e),
@ -102,16 +102,16 @@ SrcMatrix::SrcMatrix ( jint addr, jint s,
if (elements == 1)
{
// fool the nextRecord function into returning a value
// fool the nextMatrix function into returning a value
elements = 2;
nextRecord();
nextMatrix();
elements = 1;
}
}
SrcMatrix::~SrcMatrix()
MatrixSrc::~MatrixSrc()
{
//cout << "SrcMatrix destructor \n";
//cout << "MatrixSrc destructor \n";
delete [] record;
@ -119,7 +119,7 @@ SrcMatrix::~SrcMatrix()
delete [] transpose_record;
}
float * SrcMatrix::nextRecord()
float * MatrixSrc::nextMatrix()
{
if (elements > 1)
{
@ -159,7 +159,7 @@ float * SrcMatrix::nextRecord()
return current_record_ptr;
}
DstMatrix::DstMatrix (jint addr, jint s, jint w, jint h, jint e, jboolean t):
MatrixDst::MatrixDst (jint addr, jint s, jint w, jint h, jint e, jboolean t):
Matrix(addr, s, e)
{
width = w;
@ -181,9 +181,9 @@ DstMatrix::DstMatrix (jint addr, jint s, jint w, jint h, jint e, jboolean t):
record_offset = address - stride;
}
DstMatrix::~DstMatrix()
MatrixDst::~MatrixDst()
{
//cout << "DstMatrix destructor \n";
//cout << "MatrixDst destructor \n";
delete [] record;
if (transpose_record != 0)
@ -206,7 +206,7 @@ DstMatrix::~DstMatrix()
}
}
void DstMatrix::configureBuffer(SrcMatrix & a, SrcMatrix & b)
void MatrixDst::configureBuffer(MatrixSrc & a, MatrixSrc & b)
{
@ -226,7 +226,7 @@ void DstMatrix::configureBuffer(SrcMatrix & a, SrcMatrix & b)
createBuffer();
}
void DstMatrix::configureBuffer(SrcMatrix & a)
void MatrixDst::configureBuffer(MatrixSrc & a)
{
if (identicalDataSpaces(a))
record_buffered = JNI_TRUE;
@ -234,14 +234,14 @@ void DstMatrix::configureBuffer(SrcMatrix & a)
createBuffer();
}
void DstMatrix::createBuffer()
void MatrixDst::createBuffer()
{
data_buffered = JNI_TRUE;
buffer = new char[ elements * stride ];
record_offset = buffer - stride;
}
float * DstMatrix::nextRecord()
float * MatrixDst::nextMatrix()
{
record_offset = &record_offset[stride];
@ -258,7 +258,7 @@ float * DstMatrix::nextRecord()
}
void DstMatrix::writeRecord()
void MatrixDst::writeComplete()
{
if (last_record_in_temp)
{

View File

@ -43,7 +43,7 @@ class Matrix
// Src Matrix
//////////////////////////////////////////////////////////////////////////////////////
class SrcMatrix: public Matrix
class MatrixSrc: public Matrix
{
private:
char * record_offset; // the offset of this record in memory
@ -51,41 +51,46 @@ class SrcMatrix: public Matrix
float * record; // temporary storage to store a fully aligned and transposed
// copy of the record, if the one in memory is not so
float * current_record_ptr; // the address of the memory containing the record last
// returned by the nextRecord() function
// returned by the nextMatrix() function
jint record_size; // the total floats in each record
public:
SrcMatrix ( jint address, jint stride, jint width, jint height, jint elements, jboolean transpose);
MatrixSrc ( jint address, jint stride, jint width, jint height, jint elements, jboolean transpose);
~MatrixSrc();
void rewind() { record_offset = address; }
float * nextRecord();
~SrcMatrix();
float * nextMatrix();
};
///////////////////////////////////////////////////////////////////////////////////////
// Dst Matrix
//////////////////////////////////////////////////////////////////////////////////////
class DstMatrix: public Matrix
class MatrixDst: public Matrix
{
char * record_offset; // the offset of the record in memory
jboolean data_buffered; // if all of the data has to be buffered
char * buffer; // a buffer used when data_buffered
private:
char * record_offset; // the offset of the record in memory
jboolean last_record_in_temp;
jboolean record_buffered; // if only a single record is buffered
float * record; // to store data if source is unaligned
jint record_size;
jboolean data_buffered; // if all of the data has to be buffered
char * buffer; // a buffer used when data_buffered
jboolean last_record_in_temp;
jboolean record_buffered; // if only a single record is buffered
float * record; // to store data if source is unaligned
jint record_size;
void createBuffer();
public:
DstMatrix (jint address, jint stride, jint width, jint height, jint elements, jboolean transpose);
void configureBuffer(SrcMatrix & a, SrcMatrix & b);
void configureBuffer(SrcMatrix & a);
void createBuffer();
float * nextRecord();
void writeRecord();
~DstMatrix();
MatrixDst (jint address, jint stride, jint width, jint height, jint elements, jboolean transpose);
~MatrixDst();
void configureBuffer(MatrixSrc & a, MatrixSrc & b);
void configureBuffer(MatrixSrc & a);
float * nextMatrix();
void writeComplete();
};

View File

@ -68,32 +68,33 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpAdd_00024MatrixOpDirect_
jboolean transposeDest
)
{
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
left.width, left.height, left.elements * right.elements, transposeDest);
dest.configureBuffer(left, right);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
left.rewind();
for (int i = 0; i < left.elements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < right.elements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
for (int k = (dest.width * dest.height) - 1; k >= 0; k--)
destRecord[k] = leftRecord[k] + rightRecord[k];
int k = dest.width * dest.height;
while (k--)
destMatrix[k] = leftMatrix[k] + rightMatrix[k];
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -68,33 +68,33 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpAdd_00024MatrixOpSafe_ex
jboolean transposeDest
)
{
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
left.width, left.height, left.elements * right.elements, transposeDest);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
left.rewind();
for (int i = 0; i < leftElements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < rightElements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
for (int k = (leftSourceWidth * rightSourceWidth) - 1; k >= 0; k--)
destRecord[k] = leftRecord[k] + rightRecord[k];
int k = dest.width * dest.height;
while (k--)
destMatrix[k] = leftMatrix[k] + rightMatrix[k];
dest.writeRecord();
dest.writeComplete();
}
}
}

View File

@ -68,21 +68,22 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpCopy_00024MatrixOpDirect
transposeDest = false;
}
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
dest.configureBuffer(source);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
int matrixByteCount = source.width*source.height*sizeof(jfloat);
for (int i = 0; i < source.elements; i++)
{
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
// just do a straight memory copy
memcpy(destRecord, sourceRecord, source.width*source.height*sizeof(jfloat));
dest.writeRecord();
memcpy(destMatrix, srcMatrix, matrixByteCount);
dest.writeComplete();
}
}

View File

@ -69,18 +69,19 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpCopy_00024MatrixOpSafe_e
transposeDest = false;
}
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
int matrixByteCount = source.width*source.height*sizeof(jfloat);
for (int i = 0; i < source.elements; i++)
{
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
// just do a straight memory copy
memcpy(destRecord, sourceRecord, source.width * source.height * sizeof(jfloat));
dest.writeRecord();
memcpy(destMatrix, srcMatrix, matrixByteCount);
dest.writeComplete();
}
}

View File

@ -65,25 +65,25 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpDire
// We are under the assumption that sourceWidth == sourceHeight and the matrix
// defined within is invertable
SrcMatrix source (sourceAddress, sourceStride,
MatrixSrc source (sourceAddress, sourceStride,
sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
source.width, source.height, source.elements, transposeDest);
dest.configureBuffer(source);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
int temp_side = source.width-1;
float temp_matrix [temp_side*temp_side];
for (int i = 0; i < source.elements; i++)
{
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
// calculate the determinant
float det = determinant(sourceRecord, source.width);
float det = determinant(srcMatrix, source.width);
#ifdef _DEBUG
printf("Determinant: %f\n", det);
@ -108,10 +108,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpDire
for (int row = 0; row < source.height; row++)
{
// get the sub matrix
subMatrix(sourceRecord, source.width, temp_matrix, col, row);
subMatrix(srcMatrix, source.width, temp_matrix, col, row);
// transpose the result
destRecord[col + row * source.height]
destMatrix[col + row * source.height]
= (sign / det) * determinant(temp_matrix, temp_side);
// swap signs
@ -119,6 +119,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpDire
}
}
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -71,23 +71,23 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpSafe
// We are under the assumption that sourceWidth == sourceHeight and the matrix
// defined within is invertable
SrcMatrix source (sourceAddress, sourceStride,
MatrixSrc source (sourceAddress, sourceStride,
sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
source.width, source.height, source.elements, transposeDest);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
int temp_side = source.width-1;
float temp_matrix [temp_side*temp_side];
for (int i = 0; i < source.elements; i++)
{
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
// calculate the determinant
float det = determinant(sourceRecord, source.width);
float det = determinant(srcMatrix, source.width);
#ifdef _DEBUG
printf("Determinant: %f\n", det);
@ -112,10 +112,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpSafe
for (int row = 0; row < source.height; row++)
{
// get the sub matrix
subMatrix(sourceRecord, source.width, temp_matrix, col, row);
subMatrix(srcMatrix, source.width, temp_matrix, col, row);
// transpose the result
destRecord[col + row * source.height]
destMatrix[col + row * source.height]
= (sign / det) * determinant(temp_matrix, temp_side);
// swap signs
@ -123,6 +123,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpInvert_00024MatrixOpSafe
}
}
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -77,16 +77,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpDi
}
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
right.width, left.height, left.elements * right.elements, transposeDest);
dest.configureBuffer(left, right);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
// check out discussions envolving ordering
@ -94,17 +94,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpDi
left.rewind();
for (int i = 0; i < left.elements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < right.elements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
// zero the elements of the destination matrix
for (int d = 0; d < dest.width * dest.height; d++)
destRecord[d] = 0;
destMatrix[d] = 0;
// loop through each column of the right matrix
int rightCell = 0;
@ -119,16 +119,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpDi
{
for (int leftRow = 0; leftRow < left.height; leftRow++)
{
destRecord[leftRow] += rightRecord[rightCell] * leftRecord[leftCell++];
destMatrix[leftRow] += rightMatrix[rightCell] * leftMatrix[leftCell++];
}
rightCell ++ ;
}
//rightRecord = &rightRecord[right.height];
destRecord = &destRecord[dest.height];
//rightMatrix = &rightMatrix[right.height];
destMatrix = &destMatrix[dest.height];
}
dest.writeRecord();
dest.writeComplete();
}
}
}

View File

@ -79,14 +79,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpSa
}
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
right.width, left.height, left.elements * right.elements, transposeDest);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
// check out discussions envolving ordering
@ -94,17 +94,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpSa
left.rewind();
for (int i = 0; i < left.elements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < right.elements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
// zero the elements of the destination matrix
for (int d = 0; d < dest.width * dest.height; d++)
destRecord[d] = 0;
destMatrix[d] = 0;
// loop through each column of the right matrix
int rightCell = 0;
@ -119,16 +119,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpMultiply_00024MatrixOpSa
{
for (int leftRow = 0; leftRow < left.height; leftRow++)
{
destRecord[leftRow] += rightRecord[rightCell] * leftRecord[leftCell++];
destMatrix[leftRow] += rightMatrix[rightCell] * leftMatrix[leftCell++];
}
rightCell ++ ;
}
//rightRecord = &rightRecord[right.height];
destRecord = &destRecord[dest.height];
//rightMatrix = &rightMatrix[right.height];
destMatrix = &destMatrix[dest.height];
}
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -62,25 +62,26 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpNegate_00024MatrixOpDire
jboolean transposeDest
)
{
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, numElements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, numElements, transposeDest);
dest.configureBuffer(source);
int * sourceRecord, * destRecord;
int * srcMatrix, * destMatrix;
for (int i = 0; i < source.elements; i++)
{
sourceRecord = (int *) source.nextRecord();
destRecord = (int *) dest.nextRecord();
srcMatrix = (int *) source.nextMatrix();
destMatrix = (int *) dest.nextMatrix();
// we can cheat and use the less expensive xor
// to switch the sign bit of the float
// single precision format 1 - sign 8 - exponent (excess 127) 23 - mantisa
for (int j = 0; j < sourceWidth*sourceHeight; j++)
destRecord[j] = sourceRecord[j] ^ 0x80000000;
int j = source.width*source.height;
while (j--)
destMatrix[j] = srcMatrix[j] ^ 0x80000000;
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -62,23 +62,24 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpNegate_00024MatrixOpSafe
jboolean transposeDest
)
{
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, numElements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, numElements, transposeDest);
int * sourceRecord, * destRecord;
int * srcMatrix, * destMatrix;
for (int i = 0; i < source.elements; i++)
{
sourceRecord = (int *) source.nextRecord();
destRecord = (int *) dest.nextRecord();
srcMatrix = (int *) source.nextMatrix();
destMatrix = (int *) dest.nextMatrix();
// we can cheat and use the less expensive xor
// to switch the sign bit of the float
// single precision format 1 - sign 8 - exponent (excess 127) 23 - mantisa
for (int j = 0; j < sourceWidth*sourceHeight; j++)
destRecord[j] = sourceRecord[j] ^ 0x80000000;
int j = source.width*source.height;
while (j--)
destMatrix[j] = srcMatrix[j] ^ 0x80000000;
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -66,31 +66,32 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpNormalise_00024MatrixOpD
jboolean transposeDest
)
{
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
dest.configureBuffer(source);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
float magnitude, magnitude_squared;
int i, j;
int i, j, matrixFloatCount = source.width * source.height;
for (i = 0; i < source.elements; i++)
{
magnitude_squared = 0;
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
for (j = 0 ; j < sourceWidth*sourceHeight; j++)
magnitude_squared += sourceRecord[j] * sourceRecord[j];
j = matrixFloatCount;
while (j--)
magnitude_squared += srcMatrix[j] * srcMatrix[j];
magnitude = (float) sqrt((double) magnitude_squared);
for (j = 0; j < sourceWidth*sourceHeight; j++)
destRecord[j] = sourceRecord[j] / magnitude;
j = matrixFloatCount;
while (j--)
destMatrix[j] = srcMatrix[j] / magnitude;
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -66,29 +66,30 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpNormalise_00024MatrixOpS
jboolean transposeDest
)
{
SrcMatrix source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
DstMatrix dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
MatrixSrc source (sourceAddress, sourceStride, sourceWidth, sourceHeight, numElements, transposeSource);
MatrixDst dest (destAddress, destStride, source.width, source.height, source.elements, transposeDest);
float * sourceRecord, * destRecord;
float * srcMatrix, * destMatrix;
float magnitude, magnitude_squared;
int i, j;
int i, j, matrixFloatCount = source.width * source.height;
for (i = 0; i < source.elements; i++)
{
magnitude_squared = 0;
sourceRecord = source.nextRecord();
destRecord = dest.nextRecord();
srcMatrix = source.nextMatrix();
destMatrix = dest.nextMatrix();
for (j = 0 ; j < sourceWidth*sourceHeight; j++)
magnitude_squared += sourceRecord[j] * sourceRecord[j];
j = matrixFloatCount;
while (j--)
magnitude_squared += srcMatrix[j] * srcMatrix[j];
magnitude = (float) sqrt((double) magnitude_squared);
for (j = 0; j < sourceWidth*sourceHeight; j++)
destRecord[j] = sourceRecord[j] / magnitude;
j = matrixFloatCount;
while (j--)
destMatrix[j] = srcMatrix[j] / magnitude;
dest.writeRecord();
dest.writeComplete();
}
}

View File

@ -75,32 +75,33 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpSubtract_00024MatrixOpDi
transposeDest = !transposeDest;
}
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
left.width, left.height, left.elements * right.elements, transposeDest);
dest.configureBuffer(left, right);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
left.rewind();
for (int i = 0; i < left.elements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < right.elements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
for (int k = (left.width * left.height) - 1; k >= 0; k--)
destRecord[k] = leftRecord[k] - rightRecord[k];
int k = dest.width * dest.height;
while (k--)
destMatrix[k] = leftMatrix[k] - rightMatrix[k];
dest.writeRecord();
dest.writeComplete();
}
}
}

View File

@ -76,30 +76,31 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Math_00024MatrixOpSubtract_00024MatrixOpSa
transposeDest = !transposeDest;
}
SrcMatrix left (leftSourceAddress, leftSourceStride,
MatrixSrc left (leftSourceAddress, leftSourceStride,
leftSourceWidth, leftSourceHeight, leftElements, transposeLeftSource);
SrcMatrix right (rightSourceAddress, leftSourceStride,
MatrixSrc right (rightSourceAddress, leftSourceStride,
rightSourceWidth, rightSourceHeight, rightElements, transposeRightSource);
DstMatrix dest (destAddress, destStride,
MatrixDst dest (destAddress, destStride,
left.width, left.height, left.elements * right.elements, transposeDest);
float * leftRecord, * rightRecord, * destRecord;
float * leftMatrix, * rightMatrix, * destMatrix;
left.rewind();
for (int i = 0; i < left.elements; i++)
{
leftRecord = left.nextRecord();
leftMatrix = left.nextMatrix();
right.rewind();
for (int j = 0; j < right.elements; j++)
{
rightRecord = right.nextRecord();
destRecord = dest.nextRecord();
rightMatrix = right.nextMatrix();
destMatrix = dest.nextMatrix();
for (int k = (left.width * left.height) - 1; k >= 0; k--)
destRecord[k] = leftRecord[k] - rightRecord[k];
int k = dest.width * dest.height;
while (k--)
destMatrix[k] = leftMatrix[k] - rightMatrix[k];
dest.writeRecord();
dest.writeComplete();
}
}
}