Browse Source

Fix for ptMatrix44 getData and setData

Bartek Bok 13 years ago
parent
commit
eab59fca2d
  1. 70
      Sources/Plasma/FeatureLib/pfPython/pyMatrix44.cpp
  2. 4
      Sources/Plasma/FeatureLib/pfPython/pyMatrix44.h
  3. 65
      Sources/Plasma/FeatureLib/pfPython/pyMatrix44Glue.cpp

70
Sources/Plasma/FeatureLib/pfPython/pyMatrix44.cpp

@ -67,50 +67,36 @@ PyObject* pyMatrix44::GetTranslate(PyObject* pt)
return pt;
}
std::vector< std::vector<hsScalar> > pyMatrix44::GetData()
hsScalar* pyMatrix44::GetData()
{
std::vector<hsScalar> row0, row1, row2, row3;
row0.push_back(fMatrix.fMap[0][0]); row0.push_back(fMatrix.fMap[0][1]); row0.push_back(fMatrix.fMap[0][2]); row0.push_back(fMatrix.fMap[0][3]);
row1.push_back(fMatrix.fMap[1][0]); row1.push_back(fMatrix.fMap[1][1]); row1.push_back(fMatrix.fMap[1][2]); row1.push_back(fMatrix.fMap[1][3]);
row2.push_back(fMatrix.fMap[2][0]); row2.push_back(fMatrix.fMap[2][1]); row2.push_back(fMatrix.fMap[2][2]); row2.push_back(fMatrix.fMap[2][3]);
row3.push_back(fMatrix.fMap[3][0]); row3.push_back(fMatrix.fMap[3][1]); row3.push_back(fMatrix.fMap[3][2]); row3.push_back(fMatrix.fMap[3][3]);
std::vector< std::vector<hsScalar> > pyMat;
pyMat.push_back(row0);
pyMat.push_back(row1);
pyMat.push_back(row2);
pyMat.push_back(row3);
return pyMat;
hsScalar *res = new hsScalar[4*4];
res[0] = fMatrix.fMap[0][0]; res[1] = fMatrix.fMap[0][1]; res[2] = fMatrix.fMap[0][2]; res[3] = fMatrix.fMap[0][3];
res[4] = fMatrix.fMap[1][0]; res[5] = fMatrix.fMap[1][1]; res[6] = fMatrix.fMap[1][2]; res[7] = fMatrix.fMap[1][3];
res[8] = fMatrix.fMap[2][0]; res[9] = fMatrix.fMap[2][1]; res[10] = fMatrix.fMap[2][2]; res[11] = fMatrix.fMap[2][3];
res[12] = fMatrix.fMap[3][0]; res[13] = fMatrix.fMap[3][1]; res[14] = fMatrix.fMap[3][2]; res[15] = fMatrix.fMap[3][3];
return res;
}
void pyMatrix44::SetData(const std::vector< std::vector<hsScalar> > & mat)
void pyMatrix44::SetData(const hsScalar mat[])
{
// make sure they are passing us the correct size
if ( mat.size() == 4 )
{
int i,j;
for ( i=0;i<3;i++)
{
std::vector<hsScalar> pyrow = mat[i];
if ( pyrow.size() == 4 )
{
for ( j=0;j<3;j++)
fMatrix.fMap[i][j] = pyrow[j];
}
else // not enough ... throw error
{
char errmsg[256];
sprintf(errmsg, "Wrong number of elements in row of matrix");
PyErr_SetString(PyExc_TypeError, errmsg);
return;
}
}
}
else
{
char errmsg[256];
sprintf(errmsg, "Wrong number of rows in the matrix");
PyErr_SetString(PyExc_TypeError, errmsg);
}
fMatrix.fMap[0][0] = mat[0];
fMatrix.fMap[0][1] = mat[1];
fMatrix.fMap[0][2] = mat[2];
fMatrix.fMap[0][3] = mat[3];
fMatrix.fMap[1][0] = mat[4];
fMatrix.fMap[1][1] = mat[5];
fMatrix.fMap[1][2] = mat[6];
fMatrix.fMap[1][3] = mat[7];
fMatrix.fMap[2][0] = mat[8];
fMatrix.fMap[2][1] = mat[9];
fMatrix.fMap[2][2] = mat[10];
fMatrix.fMap[2][3] = mat[11];
fMatrix.fMap[3][0] = mat[12];
fMatrix.fMap[3][1] = mat[13];
fMatrix.fMap[3][2] = mat[14];
fMatrix.fMap[3][3] = mat[15];
}

4
Sources/Plasma/FeatureLib/pfPython/pyMatrix44.h

@ -79,8 +79,8 @@ public:
PyObject* GetUpAxis() { return pyVector3::New(fMatrix.GetAxis(hsMatrix44::kUp)); } // returns pyVector3
PyObject* GetRightAxis() { return pyVector3::New(fMatrix.GetAxis(hsMatrix44::kRight)); } // returns pyVector3
std::vector< std::vector<hsScalar> > GetData();
void SetData(const std::vector< std::vector<hsScalar> > & mat);
hsScalar* GetData();
void SetData(const hsScalar mat[]);
};

65
Sources/Plasma/FeatureLib/pfPython/pyMatrix44Glue.cpp

@ -284,61 +284,40 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptMatrix44, right)
PYTHON_METHOD_DEFINITION_NOARGS(ptMatrix44, getData)
{
std::vector< std::vector<hsScalar> > mat = self->fThis->GetData();
PyObject *retVal = PyTuple_New(4);
for (int curRow = 0; curRow < mat.size(); curRow++)
hsScalar *mat = self->fThis->GetData();
PyObject *retVal = Py_BuildValue("(ffff)(ffff)(ffff)(ffff)",
mat[0], mat[1], mat[2], mat[3],
mat[4], mat[5], mat[6], mat[7],
mat[8], mat[9], mat[10], mat[11],
mat[12], mat[13], mat[14], mat[15]);
if (retVal == NULL)
{
PyObject *row = PyTuple_New(4);
for (int curElement = 0; curElement < mat[curRow].size(); curElement++)
{
PyObject *item = PyInt_FromLong((long)mat[curRow][curElement]);
PyTuple_SetItem(row, curElement, item); // steals the ref
}
PyTuple_SetItem(retVal, curRow, row); // steals the ref
PyErr_SetString(PyExc_TypeError, "setData expects a 4x4 tuple of floats");
PYTHON_RETURN_ERROR;
}
delete mat;
return retVal;
}
PYTHON_METHOD_DEFINITION(ptMatrix44, setData, args)
{
PyObject *matTuple = NULL;
if (!PyArg_ParseTuple(args, "O", &matTuple))
{
PyErr_SetString(PyExc_TypeError, "setData expects a 4x4 tuple of floats");
PYTHON_RETURN_ERROR;
}
if (!PyTuple_Check(matTuple))
hsScalar mat[4*4];
if (!PyArg_ParseTuple(args, "((ffff)(ffff)(ffff)(ffff))",
&mat[0], &mat[1], &mat[2], &mat[3],
&mat[4], &mat[5], &mat[6], &mat[7],
&mat[8], &mat[9], &mat[10], &mat[11],
&mat[12], &mat[13], &mat[14], &mat[15]))
{
PyErr_SetString(PyExc_TypeError, "setData expects a 4x4 tuple of floats");
PYTHON_RETURN_ERROR;
}
std::vector< std::vector<hsScalar> > mat;
int numRows = PyTuple_Size(matTuple);
for (int curRow = 0; curRow < numRows; curRow++)
{
std::vector<hsScalar> vecRow;
PyObject *row = PyTuple_GetItem(matTuple, curRow); // borrowed ref
if (!PyTuple_Check(row))
{
PyErr_SetString(PyExc_TypeError, "setData expects a 4x4 tuple of floats");
PYTHON_RETURN_ERROR;
}
int numElements = PyTuple_Size(row);
for (int curElement = 0; curElement < numElements; curElement++)
{
PyObject *item = PyTuple_GetItem(row, curElement); // borrowed ref
if (!PyFloat_Check(item))
{
PyErr_SetString(PyExc_TypeError, "setData expects a 4x4 tuple of floats");
PYTHON_RETURN_ERROR;
}
vecRow.push_back((float)PyFloat_AsDouble(item));
}
mat.push_back(vecRow);
}
self->fThis->SetData(mat);
if (PyErr_Occurred())
PYTHON_RETURN_ERROR;
PYTHON_RETURN_NONE;

Loading…
Cancel
Save