Commit ae5048b2 authored by Jan Möbius's avatar Jan Möbius

Added Matrix vector Multiplication for scripting

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@14822 383ad7c9-94d9-4d36-a494-682f7c89f535
parent f95207bf
......@@ -42,6 +42,11 @@ TODO: OpenFlipper Scripting and examples.
The Vec3d used in the C++ code is mapped to the scripting language. Details
can be found here:
\subpage scripting_vector_type
\section scripting_matrixtype Matrix data type
The Matrix4x4T type used in the C++ code is mapped to the scripting language. Details
can be found here:
\subpage scripting_matrix_type
\section scripting_examples Scripting Examples
\subsection scripting_examples_qstringlist Iterating over a QStringList
......
......@@ -55,7 +55,9 @@
#define PROTOTYPEMATRIX4X4_C
#include "prototypeMatrix4x4.hh"
#include <OpenFlipper/Scripting/scriptWrappers/vec3dWrapper.hh>
#include <iostream>
prototypeMatrix4x4::prototypeMatrix4x4(QObject *parent ) :
QObject(parent)
{
......@@ -71,11 +73,51 @@ QString prototypeMatrix4x4::toString() const {
result = result + thisObject().property(QString::number(i) + QString::number(j)).toString() + " ";
}
//Dont add a newline at the end of the Matrix
// Don't add a newline at the end of the Matrix
if ( i != 3 )
result += "\n";
}
return result;
}
QScriptValue prototypeMatrix4x4::transform_vector(QScriptValue _vector )
{
// Convert from this objects properties to the matrix representation
Matrix4x4 matrix;
for ( uint i = 0 ; i < 4 ; ++i )
for ( uint j = 0 ; j < 4 ; ++j )
matrix(i,j) = thisObject().property(QString::number(i) + QString::number(j)).toNumber();
// Convert the vector from scripting representation to C++
ACG::Vec3d vector(_vector.property("x").toNumber(),_vector.property("y").toNumber(),_vector.property("z").toNumber());
// Calculate result vector
ACG::Vec3d result = matrix.transform_vector(vector);
// Transform back to scriptable vector
return QScriptValue( engine()->toScriptValue(result) );
}
QScriptValue prototypeMatrix4x4::transform_point(QScriptValue _vector )
{
// Convert from this objects properties to the matrix representation
Matrix4x4 matrix;
for ( uint i = 0 ; i < 4 ; ++i )
for ( uint j = 0 ; j < 4 ; ++j )
matrix(i,j) = thisObject().property(QString::number(i) + QString::number(j)).toNumber();
// Convert the vector from scripting representation to C++
ACG::Vec3d vector(_vector.property("x").toNumber(),_vector.property("y").toNumber(),_vector.property("z").toNumber());
// Calculate result vector
const ACG::Vec3d result = matrix.transform_point(vector);
// Transform back to scriptable vector
return QScriptValue( engine()->toScriptValue(result) );
}
......@@ -55,19 +55,67 @@
#ifndef PROTOTYPEVMATRIX4X4_HH
#define PROTOTYPEVMATRIX4X4_HH
/** \page scripting_matrix_type Matrix data type used for scripting
*
* The matrix type Matrix4x4 is used in the scripting language to handle matrix operations.
*
* You can use the following functions:
* \code
* var matrix = Matrix4x4(1,0,0,1,
* 0,1,0,2,
* 0,0,1,3,
* 0,0,0,1 ) // Create a matrix that contains only a translation (1,2,3).
*
* var vec = Vector(1,2,3)
*
* matrix.transform_point(vec);
* matrix.transform_vector(vec);
*
* \endcode
*
*/
/*
var matrix = Matrix4x4(1,0,0,1,
0,1,0,2,
0,0,1,3,
0,0,0,1 )
var vec = Vector(1,2,3)
print(matrix.multPoint(vec))
print(matrix.multVector(vec))
*/
#include <QtCore/QObject>
#include <QtScript/QtScript>
#include "OpenFlipper/common/Types.hh"
class prototypeMatrix4x4 : public QObject , public QScriptable
{
Q_OBJECT
public:
prototypeMatrix4x4(QObject *parent = 0);
public Q_SLOTS:
QString toString() const;
Q_OBJECT
public:
prototypeMatrix4x4(QObject *parent = 0);
public slots:
/** \brief Matrix vector product
*
* Transforms a vector by the given matrix (translation is omitted)
*/
QScriptValue transform_vector(QScriptValue _vector);
/** \brief Matrix vector product
*
* Transforms a point by the given matrix (translation is included)
*/
QScriptValue transform_point(QScriptValue _vector);
public Q_SLOTS:
QString toString() const;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment