Initial revision
[lsystem3d.git] / lsystem3d / src / vector.cpp
diff --git a/lsystem3d/src/vector.cpp b/lsystem3d/src/vector.cpp
new file mode 100644 (file)
index 0000000..7b51842
--- /dev/null
@@ -0,0 +1,145 @@
+// Copyright (C) 2006 Erik Dahlberg
+// 
+// This file is part of LSystem3d.
+// 
+// LSystem3D is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+// 
+// LSystem3D 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 General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+
+#include <cmath>
+
+#include "vector.h"
+
+
+
+/**
+ * Constructor
+ * @param x the x-coordinate
+ * @param y the y-coordinate
+ * @param z the z-coordinate
+ */
+Vector::Vector(double x, double y, double z)
+{
+    setXYZ(x, y, z);
+}
+
+
+
+/**
+ * Destructor
+ */
+Vector::~Vector()
+{
+}
+
+
+
+/**
+ * Rotate around another vector
+ * @param angle rotation angle
+ * @param vector arbitrary vector to rotate around
+ */
+void Vector::rotate(double angle, Vector vector)
+{
+    double c = cos(angle);
+    double s = sin(angle);
+    
+    double x =
+    _x * (vector._x * vector._x * (1.0 - c) + c) +
+    _y * (vector._x * vector._y * (1.0 - c) + vector._z * s) +
+    _z * (vector._x * vector._z * (1.0 - c) - vector._y * s);
+    
+    double y =
+    _x * (vector._y * vector._x * (1.0 - c) - vector._z * s) +
+    _y * (vector._y * vector._y * (1.0 - c) + c) +
+    _z * (vector._y * vector._z * (1.0 - c) + vector._x * s);
+    
+    double z =
+    _x * (vector._z * vector._x * (1.0 - c) + vector._y * s) +
+    _y * (vector._z * vector._y * (1.0 - c) - vector._x * s) +
+    _z * (vector._z * vector._z * (1.0 - c) + c);
+    
+    _x = x;
+    _y = y;
+    _z = z;
+    
+    // TODO: call it from outside?
+    normalize();
+}
+
+
+
+/**
+ * Normalize vector
+ */
+void Vector::normalize()
+{
+    double length = sqrt(_x * _x + _y * _y + _z * _z);
+    
+    if (length != 0)
+    {
+        _x /= length;
+        _y /= length;
+        _z /= length;
+    }
+}
+
+
+
+/**
+ * Get scalar product of the vectors
+ * @param vector arbitrary vector
+ * @return the scalar product
+ */
+double Vector::getScalarProduct(Vector vector)
+{
+    double scalarProduct = getX() * vector.getX() +
+                           getY() * vector.getY() +
+                           getZ() * vector.getZ();
+    
+    return scalarProduct;
+}
+
+
+
+/**
+ * Get cross product of the vectors
+ * @param vector arbitrary vector
+ * @return the cross product
+ */
+Vector Vector::getCrossProduct(Vector vector)
+{
+    Vector crossProduct(getY() * vector.getZ() - getZ() * vector.getY(),
+                        getZ() * vector.getX() - getX() * vector.getZ(),
+                        getX() * vector.getY() - getY() * vector.getX());
+
+    return crossProduct;
+}
+
+
+
+/**
+ * Get angle between the vectors
+ * @param vector the second (normalized) vector
+ * @return the angle, in degrees
+ */
+double Vector::getAngle(Vector vector)
+{
+    double scalarProduct = getScalarProduct(vector);
+    double angle = (acos(scalarProduct) / (M_PIl * 2.0)) * 360.0;
+    
+    return angle;
+}