Commit 6f7993e6 authored by Jan Möbius's avatar Jan Möbius

Added Vector4 Scripting datatype

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@16492 383ad7c9-94d9-4d36-a494-682f7c89f535
parent eb357b4f
......@@ -459,8 +459,20 @@ Core::init() {
scriptEngine_.newQObject(&vec3dPrototype_));
// set a constructor to allow creation via Vector(x,y,z)
QScriptValue ctor = scriptEngine_.newFunction(createVector);
scriptEngine_.globalObject().setProperty("Vector", ctor);
QScriptValue ctorVec3 = scriptEngine_.newFunction(createVector);
scriptEngine_.globalObject().setProperty("Vector", ctorVec3);
//==========================================================================
// Register the 4d Vector Type to the core ( is Vec4d )
//==========================================================================
qScriptRegisterMetaType(&scriptEngine_,
toScriptValueVector,
fromScriptValueVector,
scriptEngine_.newQObject(&vec4dPrototype_));
// set a constructor to allow creation via Vector(x,y,z)
QScriptValue ctorVec4 = scriptEngine_.newFunction(createVector4);
scriptEngine_.globalObject().setProperty("Vector4", ctorVec4);
//==========================================================================
// Register the DataType Class to the core
......
......@@ -83,9 +83,11 @@
// Prototypes for scripting
#include "OpenFlipper/Scripting/scriptPrototypes/prototypeVec3d.hh"
#include "OpenFlipper/Scripting/scriptPrototypes/prototypeVec4d.hh"
#include "OpenFlipper/Scripting/scriptPrototypes/prototypeMatrix4x4.hh"
#include "OpenFlipper/Scripting/scriptPrototypes/prototypeDataType.hh"
#include <OpenFlipper/Scripting/scriptWrappers/vec3dWrapper.hh>
#include <OpenFlipper/Scripting/scriptWrappers/vec4dWrapper.hh>
#include <OpenFlipper/Scripting/scriptWrappers/matrix4x4Wrapper.hh>
#include <OpenFlipper/Scripting/scriptWrappers/DataTypeWrapper.hh>
// #include <OpenFlipper/Scripting/scriptWrappers/ObjectIdWrapper.hh>
......@@ -1235,6 +1237,9 @@ private slots:
/// Prototype for the Vector type
prototypeVec3d vec3dPrototype_;
/// Prototype for the Vector type
prototypeVec4d vec4dPrototype_;
/// Prototype for the DataType
prototypeDataType DataTypePrototype_;
......
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 14819 $ *
* $LastChangedBy: moebius $ *
* $Date: 2012-06-29 13:42:49 +0200 (Fr, 29 Jun 2012) $ *
* *
\*===========================================================================*/
//
// C++ Interface: prototypeVec4d
//
// Description:
//
//
// Author: Jan Möbius <moebius@cs.rwth-aachen.de>, (C) 2007
//
#define PROTOTYPEVEC4D_C
#include "prototypeVec4d.hh"
#include <iostream>
prototypeVec4d::prototypeVec4d(QObject *parent ) :
QObject(parent)
{
}
QString prototypeVec4d::toString() const {
return "Vector : ( " +
thisObject().property("x").toString() + "," +
thisObject().property("y").toString() + "," +
thisObject().property("z").toString() + "," +
thisObject().property("w").toString() + " )";
}
void prototypeVec4d::multiply(QScriptValue _scalar) {
thisObject().setProperty("x", QScriptValue(thisObject().property("x").toNumber() * _scalar.toNumber()));
thisObject().setProperty("y", QScriptValue(thisObject().property("y").toNumber() * _scalar.toNumber()));
thisObject().setProperty("z", QScriptValue(thisObject().property("z").toNumber() * _scalar.toNumber()));
thisObject().setProperty("w", QScriptValue(thisObject().property("w").toNumber() * _scalar.toNumber()));
}
void prototypeVec4d::add(QScriptValue _vector) {
thisObject().setProperty("x", QScriptValue(_vector.property("x").toNumber() + thisObject().property("x").toNumber()));
thisObject().setProperty("y", QScriptValue(_vector.property("y").toNumber() + thisObject().property("y").toNumber()));
thisObject().setProperty("z", QScriptValue(_vector.property("z").toNumber() + thisObject().property("z").toNumber()));
thisObject().setProperty("w", QScriptValue(_vector.property("w").toNumber() + thisObject().property("w").toNumber()));
}
void prototypeVec4d::sub(QScriptValue _vector) {
thisObject().setProperty("x", QScriptValue(thisObject().property("x").toNumber() - _vector.property("x").toNumber()));
thisObject().setProperty("y", QScriptValue(thisObject().property("y").toNumber() - _vector.property("y").toNumber()));
thisObject().setProperty("z", QScriptValue(thisObject().property("z").toNumber() - _vector.property("z").toNumber()));
thisObject().setProperty("w", QScriptValue(thisObject().property("w").toNumber() - _vector.property("w").toNumber()));
}
void prototypeVec4d::zero() {
thisObject().setProperty("x", QScriptValue(0.0));
thisObject().setProperty("y", QScriptValue(0.0));
thisObject().setProperty("z", QScriptValue(0.0));
thisObject().setProperty("w", QScriptValue(0.0));
}
QScriptValue prototypeVec4d::sprod(QScriptValue _vector) {
return QScriptValue( thisObject().property("x").toNumber() * _vector.property("x").toNumber() +
thisObject().property("y").toNumber() * _vector.property("y").toNumber() +
thisObject().property("z").toNumber() * _vector.property("z").toNumber() +
thisObject().property("w").toNumber() * _vector.property("w").toNumber() );
}
QScriptValue prototypeVec4d::norm() {
return QScriptValue( sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() +
thisObject().property("w").toNumber() * thisObject().property("w").toNumber() ) );
}
QScriptValue prototypeVec4d::sqrnorm() {
return QScriptValue( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() +
thisObject().property("w").toNumber() * thisObject().property("w").toNumber() );
}
void prototypeVec4d::normalize() {
double length = sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() +
thisObject().property("w").toNumber() * thisObject().property("w").toNumber() );
if ( length != 0.0 ) {
thisObject().setProperty("x", ( thisObject().property("x").toNumber() / length ) );
thisObject().setProperty("y", ( thisObject().property("y").toNumber() / length ) );
thisObject().setProperty("z", ( thisObject().property("z").toNumber() / length ) );
thisObject().setProperty("w", ( thisObject().property("w").toNumber() / length ) );
}
}
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 14819 $ *
* $LastChangedBy: moebius $ *
* $Date: 2012-06-29 13:42:49 +0200 (Fr, 29 Jun 2012) $ *
* *
\*===========================================================================*/
//
// C++ Interface: prototypeVec4d
//
// Description:
//
//
// Author: Jan Möbius <moebius@cs.rwth-aachen.de>, (C) 2013
//
/** \page scripting_vector_type_4d Vector4d data type for scripting
*
* The vector data type is corresponding to the Vec4d type in C++. The implementation of the
* scripting type is done in prototypeVec3d.
*
* You can use the following functions:
* \code
* var vec = Vector(1,2,3,4)
* var vec2 = Vector(10,20,30,4)
*
* vec.multiply(0.5) // Multiply vec with 0.5
* vec.add(vec2) // Add vec2 to vec
* vec.sub(vec2) // Subtract vec2 from vec
*
* var norm = vec.norm() // Calculate the length of the vector
* var sqrnorm = vec.sqrnorm() // Calculate the squared length of the vector
*
* var sprod = vec.sprod(vec) //Compute scalar product with the vector and itself
*
* vec.normalize() // Normalize vector
*
* vec.zero() // Set all components of the vector to zero
*
* print(vec) // Output vector to the console
*
* \endcode
*
*/
#ifndef PROTOTYPEVEC4D_HH
#define PROTOTYPEVEC4D_HH
#include <QtCore/QObject>
#include <QtScript/QtScript>
/** \class prototypeVec4d
*
* This class wraps the Vec4d to the scripting language
*
* \ref scripting_vector_type
*
*/
class prototypeVec4d : public QObject , public QScriptable
{
Q_OBJECT
public:
prototypeVec4d(QObject *parent = 0);
public slots:
/** \brief Multiplies the given vector with the scalar
*
* @param _scalar Scalar value that gets multiplied
*/
void multiply(QScriptValue _scalar);
/** \brief Adds another vector to this vector
*
* @param _vector Vector that should be added
*/
void add(QScriptValue _vector);
/** \brief Subtracts another vector from this vector
*
* @param _vector Vector that should be subtracted
*/
void sub(QScriptValue _vector);
/** \brief Resets all components of the vector to zero
*
*/
void zero();
/** \brief Calculate scalar product
*
* @param _vector Second vector for scalar product (Can be the vector itself)
* @return Scalar product value
*/
QScriptValue sprod(QScriptValue _vector);
/** \brief Calculate the Euclidean norm of the vector
*
* @return Norm of the vector
*/
QScriptValue norm();
/** \brief Calculate the squared Euclidean norm of the vector
*
* @return Squared norm of the vector
*/
QScriptValue sqrnorm();
/** \brief Normalize the vector
*/
void normalize();
public Q_SLOTS:
QString toString() const;
};
#endif // PROTOTYPEVEC4D_HH
......@@ -63,10 +63,10 @@
* @{ */
//===========================================================================
/// Convert Vector to scriptvalue
/// Convert Vector to script value
QScriptValue toScriptValueVector(QScriptEngine *engine, const Vector &s);
/// Convert scriptvalue to Vector
/// Convert script value to Vector
void fromScriptValueVector(const QScriptValue &obj, Vector &s);
/// Create an Vector in Scripting environment
......
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 10745 $ *
* $LastChangedBy: moebius $ *
* $Date: 2011-01-26 10:23:50 +0100 (Mi, 26. Jan 2011) $ *
* *
\*===========================================================================*/
//=============================================================================
//
// Wrapper for Vector ( Vec3d ) - IMPLEMENTATION
//
//=============================================================================
//== INCLUDES =================================================================
#include "vec4dWrapper.hh"
//== IMPLEMENTATION ==========================================================
//=== Vector ( = Vec3d) =======================================================
QScriptValue toScriptValueVector4(QScriptEngine *engine, const Vector4 &s)
{
QScriptValue obj = engine->newObject();
obj.setProperty("x", QScriptValue(engine, s[0]));
obj.setProperty("y", QScriptValue(engine, s[1]));
obj.setProperty("z", QScriptValue(engine, s[2]));
obj.setProperty("w", QScriptValue(engine, s[3]));
return obj;
}
void fromScriptValueVector4(const QScriptValue &obj, Vector4 &s)
{
if (obj.isObject()) {
s[0] = obj.property("x").toNumber();
s[1] = obj.property("y").toNumber();
s[2] = obj.property("z").toNumber();
s[3] = obj.property("w").toNumber();
return;
}
QString _from = obj.toString();
if (_from.startsWith ("Vector4 ("))
_from.remove (0, 8);
else if (_from.startsWith ("Vector4 : ( "))
_from.remove (0, 11);
if (_from.endsWith (")"))
_from.remove (_from.length () - 1, 1);
QStringList sl = _from.split (',');
float v[4];
bool ok = true;
if (sl.length () == 4) {
for (int i = 0; i < 4 && ok; i++)
v[i] = sl[i].toFloat (&ok);
if (ok)
for (int i = 0; i < 4; i++)
s[i] = v[i];
else {
for (int i = 0; i < 4; i++)
s[i] = 0;
std::cerr << "String to Vec4D conversion failed!" << std::endl;
}
}
}
QScriptValue createVector4(QScriptContext *context, QScriptEngine *engine)
{
Vector s;
QScriptValue callee = context->callee();
// If arguments are given, use them for initialization otherwise
// initialize with 0
if (context->argumentCount() == 4) {
s[0] = context->argument(0).toNumber();
s[1] = context->argument(1).toNumber();
s[2] = context->argument(2).toNumber();
s[3] = context->argument(3).toNumber();
} else {
s[0] = 0.0;
s[1] = 0.0;
s[2] = 0.0;
s[3] = 0.0;
}
return engine->toScriptValue(s);
}
QScriptValue Vector4ToString(QScriptContext *context, QScriptEngine *engine)
{
QString result = "Vector : ( " +
context->thisObject().property("x").toString() + "," +
context->thisObject().property("y").toString() + "," +
context->thisObject().property("z").toString() + ","+
context->thisObject().property("w").toString() + " )";
return QScriptValue(engine, result);
}
//=============================================================================
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 14817 $ *
* $LastChangedBy: moebius $ *
* $Date: 2012-06-28 15:29:23 +0200 (Do, 28 Jun 2012) $ *
* *
\*===========================================================================*/
//
// C++ Interface: vec4dWrapper.hh
//
// Description:
//
//
// Author: Jan Möbius <moebius@cs.rwth-aachen.de>, (C) 2013
//
#ifndef WRAPPERVEC4D_HH
#define WRAPPERVEC4D_HH
#include "OpenFlipper/common/Types.hh"
#include <QtScript/QtScript>
//===========================================================================
/** @name Script Wrappers for Vector class ( ACG::Vec4d )
* @{ */
//===========================================================================
/// Convert Vector to script value
QScriptValue toScriptValueVector4(QScriptEngine *engine, const Vector4 &s);
/// Convert script value to Vector
void fromScriptValueVector4(const QScriptValue &obj, Vector4 &s);
/// Create an Vector in Scripting environment
QScriptValue createVector4(QScriptContext *, QScriptEngine *engine);
QScriptValue Vector4ToString(QScriptContext *context, QScriptEngine *engine);
/** @} */
#endif // WRAPPERVEC4D_HH
......@@ -177,6 +177,9 @@ std::ostream &operator<<(std::ostream &stream, DataType type);
/// Standard Type for 3d Vector used for scripting
typedef ACG::Vec3d Vector;
/// Standard Type for 4d Vector used for scripting
typedef ACG::Vec4d Vector4;
/// Standard Type for id Lists used for scripting
typedef std::vector< int > IdList;
/// Standard Type for a 4x4 Matrix used for scripting
......@@ -186,6 +189,7 @@ Q_DECLARE_METATYPE(IdList)
Q_DECLARE_METATYPE(DataType)
Q_DECLARE_METATYPE(QVector< int >)
Q_DECLARE_METATYPE(Vector)
Q_DECLARE_METATYPE(Vector4)
Q_DECLARE_METATYPE(Matrix4x4)
//================================================================================================
......
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