Browse Source

Fix for ptMatrix44 getData and setData

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

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

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

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

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

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

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

Loading…
Cancel
Save