Menu

[025cc2]: / DirectPython11 / DPMatrix.cpp  Maximize  Restore  History

Download this file

254 lines (213 with data), 7.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright (C) 2010, Heikki Salo
All rights reserved.
Distributed under the BSD license:
http://www.opensource.org/licenses/bsd-license.php
*/
#include "stdafx.h"
#include "DPMatrix.hpp"
#include "DPUtils.hpp"
DPMatrix::DPMatrix(python::object& source)
{
python::extract<DPMatrix&> isMatrix(source);
if (isMatrix.check()) {
*this = isMatrix;
}
else {
fromSequence(source);
}
}
void DPMatrix::fromSequence(python::object& source)
{
const Py_ssize_t size = python::len(source);
if (size == 4) {
//Assume nested sequences
for (UINT row=0; row < 4; ++row) {
for (UINT col=0; col < 4; ++col) {
matrix(row, col) = python::extract<float>(source[row][col]);
}
}
}
else if (size == 16) {
//A sequence
for (UINT i=0; i < 16; ++i) {
UINT row = i / 4;
UINT col = i % 4;
matrix(row, col) = python::extract<float>(source[i]);
}
}
else {
DPThrow("Unknown sequence argument");
}
}
void DPMatrix::identity()
{
matrix = XMMatrixIdentity();
}
void DPMatrix::scale(python::object& s)
{
XMVECTOR v = PyToXMV(s);
matrix = XMMatrixScaling(v.x, v.y, v.z);
}
void DPMatrix::rotate(python::object& r)
{
XMVECTOR v = PyToXMV(r);
matrix = XMMatrixRotationRollPitchYaw(v.x, v.y, v.z);
}
void DPMatrix::translate(python::object& t)
{
XMVECTOR v = PyToXMV(t);
matrix = XMMatrixTranslation(v.x, v.y, v.z);
}
void DPMatrix::transpose()
{
matrix = XMMatrixTranspose(matrix);
}
void DPMatrix::transformation(python::object& tr, python::object& rot, python::object& scale,
python::object& rorigin, python::object& sorigin, python::object& sorientation)
{
XMVECTOR trans = PyToXMV(tr);
XMVECTOR rotQuat= XMQuaternionRotationRollPitchYawFromVector(PyToXMV(rot));
XMVECTOR scaling = PyToXMV(scale);
XMVECTOR rotOrigin = PyToXMV(rorigin);
XMVECTOR scalingOrigin = PyToXMV(sorigin);
XMVECTOR scalingOrientationQuat = XMQuaternionRotationRollPitchYawFromVector(PyToXMV(sorientation));
matrix = XMMatrixTransformation(scalingOrigin, scalingOrientationQuat,
scaling, rotOrigin, rotQuat, trans);
}
float DPMatrix::inverse()
{
XMVECTOR determinant;
XMMATRIX inversed = XMMatrixInverse(&determinant, matrix);
if (XMMatrixIsInfinite(inversed))
DPThrow("Inversion failed");
matrix = inversed;
return determinant.x;
}
DPVector DPMatrix::transformVector(python::object& pyvector)
{
XMVECTOR v = PyToXMV(pyvector);
return XMVector3TransformCoord(v, matrix);
}
DPVector DPMatrix::transformNormal(python::object& pyvector)
{
XMVECTOR v = PyToXMV(pyvector);
return XMVector3TransformNormal(v, matrix);
}
void DPMatrix::lookAtLH(python::object& pEye, python::object& pLookat, python::object& pUp)
{
XMVECTOR eye = PyToXMV(pEye);
XMVECTOR lookat = PyToXMV(pLookat);
XMVECTOR up = PyToXMV(pUp);
matrix = XMMatrixLookAtLH(eye, lookat, up);
}
void DPMatrix::perspectiveFovLH(float fovy, float aspect, float zmin, float zmax)
{
matrix = XMMatrixPerspectiveFovLH(fovy, aspect, zmin, zmax);
}
void DPMatrix::orthoLH(float w, float h, float zmin, float zmax)
{
matrix = XMMatrixOrthographicLH(w, h, zmin, zmax);
}
DPMatrixRow DPMatrix::getitem(UINT row)
{
if (row >= 4)
throw std::out_of_range("Invalid row index");
//Works when using "plain" matrices. The row
//will know what data to modify and 'with_custodian_and_ward_postcall'
//is used to make sure that the matrix does not go away while any rows
//are still alive.
DPMatrixRow matrixRow;
matrixRow.data = ((float*)&matrix._11) + (row * 4);
return matrixRow;
}
float DPMatrix::getitem_pair(python::tuple& coords)
{
UINT row = python::extract<UINT>(coords[0]);
UINT col = python::extract<UINT>(coords[1]);
if (row >= 4 || col >= 4)
throw std::out_of_range("Invalid index");
return matrix(row, col);
}
void DPMatrix::setitem_pair(python::object& coords, float value)
{
UINT row = python::extract<UINT>(coords[0]);
UINT col = python::extract<UINT>(coords[1]);
if (row >= 4 || col >= 4)
throw std::out_of_range("Invalid index");
matrix(row, col) = value;
}
python::str DPMatrix::str()
{
std::ostringstream ss;
ss << std::fixed;
for (UINT row=0; row < 4; ++row) {
ss << '[';
for (UINT col=0; col < 4; ++col) {
ss << matrix(row, col);
if (col < 3)
ss << ", ";
}
ss << "]\n";
}
return python::str(ss.str().c_str());
}
python::object DPMatrix::toBytes() const
{
PyObject* value = PyBytes_FromStringAndSize((char*)&matrix, sizeof(matrix));
if (value == 0)
python::throw_error_already_set();
return python::object(python::handle<>(value));
}
python::object DPMatrix::toList() const
{
python::list result;
for (UINT row=0; row < 4; ++row) {
for (UINT col=0; col < 4; ++col) {
result.append(matrix(row, col));
}
}
return result;
}
struct DPMatrixPickleSuite : python::pickle_suite {
static python::tuple getinitargs(DPMatrix const& m)
{
return python::make_tuple(m.toList());
}
};
void ExportDPMatrix()
{
using namespace boost::python;
python::object zeroTuple = make_tuple(0, 0, 0);
python::object oneTuple = make_tuple(1, 1, 1);
class_<DPMatrix>("Matrix")
.def(init<python::object&>())
.def("toBytes", &DPMatrix::toBytes) //Undocumented.
.def("toList", &DPMatrix::toList)
.def("identity", &DPMatrix::identity)
.def("invert", &DPMatrix::inverse)
.def("scale", &DPMatrix::scale)
.def("rotate", &DPMatrix::rotate)
.def("translate", &DPMatrix::translate)
.def("transpose", &DPMatrix::transpose)
.def("transformation", &DPMatrix::transformation, (arg("translation") = zeroTuple,
arg("rotation") = zeroTuple, arg("scaling") = oneTuple, arg("rotorigin") = zeroTuple,
arg("scaleorigin") = zeroTuple, arg("scaleorientation") = zeroTuple ))
.def("transformVector", &DPMatrix::transformVector)
.def("transformNormal", &DPMatrix::transformNormal)
.def("lookAt", &DPMatrix::lookAtLH)
.def("perspectiveFov", &DPMatrix::perspectiveFovLH)
.def("orthographic", &DPMatrix::orthoLH)
.def("__str__", &DPMatrix::str)
.def("__getitem__", &DPMatrix::getitem, with_custodian_and_ward_postcall<0, 1>())
.def("__getitem__", &DPMatrix::getitem_pair)
.def("__setitem__", &DPMatrix::setitem_pair)
.def_pickle(DPMatrixPickleSuite())
.def(self * self)
.def(self *= self)
;
class_<DPMatrixRow>("MatrixRow")
.def("__getitem__", &DPMatrixRow::getitem)
.def("__setitem__", &DPMatrixRow::setitem)
;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.