Developer Documentation
prototypeVec3d.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 #define PROTOTYPEVEC3D_C
44 
45 #include "prototypeVec3d.hh"
46 
47 prototypeVec3d::prototypeVec3d(QObject *parent ) :
48  QObject(parent)
49 {
50 
51 }
52 
53 QString prototypeVec3d::toString() const {
54  return "Vector : ( " +
55  thisObject().property("x").toString() + "," +
56  thisObject().property("y").toString() + "," +
57  thisObject().property("z").toString() + " )";
58 }
59 
60 void prototypeVec3d::multiply(QScriptValue _scalar) {
61  thisObject().setProperty("x", QScriptValue(thisObject().property("x").toNumber() * _scalar.toNumber()));
62  thisObject().setProperty("y", QScriptValue(thisObject().property("y").toNumber() * _scalar.toNumber()));
63  thisObject().setProperty("z", QScriptValue(thisObject().property("z").toNumber() * _scalar.toNumber()));
64 }
65 
66 void prototypeVec3d::add(QScriptValue _vector) {
67  thisObject().setProperty("x", QScriptValue(_vector.property("x").toNumber() + thisObject().property("x").toNumber()));
68  thisObject().setProperty("y", QScriptValue(_vector.property("y").toNumber() + thisObject().property("y").toNumber()));
69  thisObject().setProperty("z", QScriptValue(_vector.property("z").toNumber() + thisObject().property("z").toNumber()));
70 }
71 
72 
73 void prototypeVec3d::sub(QScriptValue _vector) {
74  thisObject().setProperty("x", QScriptValue(thisObject().property("x").toNumber() - _vector.property("x").toNumber()));
75  thisObject().setProperty("y", QScriptValue(thisObject().property("y").toNumber() - _vector.property("y").toNumber()));
76  thisObject().setProperty("z", QScriptValue(thisObject().property("z").toNumber() - _vector.property("z").toNumber()));
77 }
78 
80  thisObject().setProperty("x", QScriptValue(0.0));
81  thisObject().setProperty("y", QScriptValue(0.0));
82  thisObject().setProperty("z", QScriptValue(0.0));
83 }
84 
85 QScriptValue prototypeVec3d::sprod(QScriptValue _vector) {
86  return QScriptValue( thisObject().property("x").toNumber() * _vector.property("x").toNumber() +
87  thisObject().property("y").toNumber() * _vector.property("y").toNumber() +
88  thisObject().property("z").toNumber() * _vector.property("z").toNumber() );
89 
90 }
91 
92 QScriptValue prototypeVec3d::norm() {
93  return QScriptValue( sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
94  thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
95  thisObject().property("z").toNumber() * thisObject().property("z").toNumber() ) );
96 }
97 
98 QScriptValue prototypeVec3d::sqrnorm() {
99  return QScriptValue( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
100  thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
101  thisObject().property("z").toNumber() * thisObject().property("z").toNumber() );
102 }
103 
105  double length = sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
106  thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
107  thisObject().property("z").toNumber() * thisObject().property("z").toNumber() );
108 
109  if ( length != 0.0 ) {
110  thisObject().setProperty("x", ( thisObject().property("x").toNumber() / length ) );
111  thisObject().setProperty("y", ( thisObject().property("y").toNumber() / length ) );
112  thisObject().setProperty("z", ( thisObject().property("z").toNumber() / length ) );
113  }
114 
115 
116 }
117 
void multiply(QScriptValue _scalar)
Multiplies the given vector with the scalar.
QScriptValue sqrnorm()
Calculate the squared Euclidean norm of the vector.
void normalize()
Normalize the vector.
void add(QScriptValue _vector)
Adds another vector to this vector.
void zero()
Resets all components of the vector to zero.
QScriptValue sprod(QScriptValue _vector)
Calculate scalar product.
void sub(QScriptValue _vector)
Subtracts another vector from this vector.
QScriptValue norm()
Calculate the Euclidean norm of the vector.