Skip to content

Commit d7bd658

Browse files
committed
Exposed matrix3x4_t as Matrix3x4
1 parent 4da0b42 commit d7bd658

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

addons/source-python/packages/source-python/mathlib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
from _mathlib import Quaternion
1313
from _mathlib import Plane
1414
from _mathlib import RadianEuler
15+
from _mathlib import Matrix3x4
1516

1617

1718
# =============================================================================
1819
# >> ALL DECLARATION
1920
# =============================================================================
20-
__all__ = ('NULL_QANGLE',
21+
__all__ = ('NULL_MATRIX',
22+
'NULL_QANGLE',
2123
'NULL_QUATERNION',
2224
'NULL_RADIANEULER',
2325
'NULL_VECTOR',
26+
'Matrix3x4',
2427
'Plane',
2528
'QAngle',
2629
'Quaternion',
@@ -37,3 +40,4 @@
3740
NULL_QUATERNION = Quaternion() #:
3841
NULL_RADIANEULER = RadianEuler() #:
3942
NULL_VECTOR = Vector() #:
43+
NULL_MATRIX = Matrix3x4(NULL_VECTOR, NULL_VECTOR, NULL_VECTOR, NULL_VECTOR) #:

src/core/modules/mathlib/mathlib.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,52 @@ class RadianEulerExt
129129
{ return str("RadianEuler" + str(tuple(pRadianEuler))); }
130130
};
131131

132+
133+
//-----------------------------------------------------------------------------
134+
// matrix3x4_t extension class.
135+
//-----------------------------------------------------------------------------
136+
class Matrix3x4Row
137+
{
138+
public:
139+
Matrix3x4Row(float* row)
140+
{
141+
m_row = row;
142+
}
143+
144+
float& operator[](unsigned int index)
145+
{
146+
return m_row[index];
147+
}
148+
149+
str __repr__()
150+
{
151+
return str(tuple(*this));
152+
}
153+
154+
public:
155+
float* m_row;
156+
};
157+
158+
159+
class matrix3x4_tExt
160+
{
161+
public:
162+
static Matrix3x4Row* __getitem__(matrix3x4_t& matrix, unsigned int index)
163+
{
164+
if (index >= 3) {
165+
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range (%d)", index)
166+
}
167+
168+
return new Matrix3x4Row(matrix[index]);
169+
}
170+
171+
static str __repr__(matrix3x4_t& matrix)
172+
{
173+
return str("["
174+
+ str(__getitem__(matrix, 0)) + ",\n"
175+
+ str(__getitem__(matrix, 1)) + ",\n"
176+
+ str(__getitem__(matrix, 2)) + "]");
177+
}
178+
};
179+
132180
#endif // _MATHLIB_H

src/core/modules/mathlib/mathlib_wrap.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void export_qangle(scope);
4343
void export_quaternion(scope);
4444
void export_cplane_t(scope);
4545
void export_radian_euler(scope);
46+
void export_matrix3x4_t(scope);
4647

4748

4849
//-----------------------------------------------------------------------------
@@ -55,6 +56,7 @@ DECLARE_SP_MODULE(_mathlib)
5556
export_quaternion(_mathlib);
5657
export_cplane_t(_mathlib);
5758
export_radian_euler(_mathlib);
59+
export_matrix3x4_t(_mathlib);
5860
}
5961

6062

@@ -504,3 +506,65 @@ void export_radian_euler(scope _mathlib)
504506
ADD_MEM_TOOLS(RadianEuler)
505507
;
506508
}
509+
510+
511+
//-----------------------------------------------------------------------------
512+
// Exports matrix3x4_t.
513+
//-----------------------------------------------------------------------------
514+
void export_matrix3x4_t(scope _mathlib)
515+
{
516+
// TODO:
517+
// - Add support for e.g. matrix[0] = (1, 2, 3, 4)
518+
class_<matrix3x4_t>("Matrix3x4")
519+
.def(init<
520+
float, float, float, float,
521+
float, float, float, float,
522+
float, float, float, float>(
523+
"Create a new 3x4 matrix.\n\n"
524+
".. note:: The matrix is not initialized with ``0.0``."))
525+
526+
.def(init<matrix3x4_t&>(
527+
"Copy an existing matrix."))
528+
529+
.def(init<const Vector&, const Vector&, const Vector&, const Vector&>())
530+
.def("__repr__", &matrix3x4_tExt::__repr__)
531+
532+
.def(
533+
"invalidate",
534+
&matrix3x4_t::Invalidate,
535+
"Invalidate the the matrix (set all values to ``nan``).")
536+
537+
.def(
538+
"__getitem__",
539+
&matrix3x4_tExt::__getitem__,
540+
manage_new_object_policy(),
541+
"Return a single row of the matrix (0 - 2).\n\n"
542+
":rtype: tuple")
543+
544+
ADD_MEM_TOOLS(matrix3x4_t)
545+
;
546+
547+
class_<Matrix3x4Row>("Matrix3x4Row", no_init)
548+
.def("__repr__", &Matrix3x4Row::__repr__)
549+
550+
.def(
551+
"__getitem__",
552+
&GetItemIndexer<Matrix3x4Row, float, 0, 3>,
553+
(arg("index")),
554+
"Return the value of a column (0 - 3).\n"
555+
"\n"
556+
":param int index: An index between 0 and 3 that identifies the column.\n"
557+
":rtype: float"
558+
)
559+
560+
.def(
561+
"__setitem__",
562+
&SetItemIndexer<Matrix3x4Row, float, 0, 3>,
563+
(arg("index"), arg("value")),
564+
"Set the value of a column.\n"
565+
"\n"
566+
":param int index: An index between 0 and 3 that identifies the column.\n"
567+
":param float value: The value to set."
568+
)
569+
;
570+
}

0 commit comments

Comments
 (0)