Initial revision
[lsystem3d.git] / src / vector.cpp
1 // Copyright (C) 2006 Erik Dahlberg
2 //
3 // This file is part of LSystem3d.
4 //
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
22 #include <cmath>
23
24 #include "vector.h"
25
26
27
28 /**
29 * Constructor
30 * @param x the x-coordinate
31 * @param y the y-coordinate
32 * @param z the z-coordinate
33 */
34 Vector::Vector(double x, double y, double z)
35 {
36 setXYZ(x, y, z);
37 }
38
39
40
41 /**
42 * Destructor
43 */
44 Vector::~Vector()
45 {
46 }
47
48
49
50 /**
51 * Rotate around another vector
52 * @param angle rotation angle
53 * @param vector arbitrary vector to rotate around
54 */
55 void Vector::rotate(double angle, Vector vector)
56 {
57 double c = cos(angle);
58 double s = sin(angle);
59
60 double x =
61 _x * (vector._x * vector._x * (1.0 - c) + c) +
62 _y * (vector._x * vector._y * (1.0 - c) + vector._z * s) +
63 _z * (vector._x * vector._z * (1.0 - c) - vector._y * s);
64
65 double y =
66 _x * (vector._y * vector._x * (1.0 - c) - vector._z * s) +
67 _y * (vector._y * vector._y * (1.0 - c) + c) +
68 _z * (vector._y * vector._z * (1.0 - c) + vector._x * s);
69
70 double z =
71 _x * (vector._z * vector._x * (1.0 - c) + vector._y * s) +
72 _y * (vector._z * vector._y * (1.0 - c) - vector._x * s) +
73 _z * (vector._z * vector._z * (1.0 - c) + c);
74
75 _x = x;
76 _y = y;
77 _z = z;
78
79 // TODO: call it from outside?
80 normalize();
81 }
82
83
84
85 /**
86 * Normalize vector
87 */
88 void Vector::normalize()
89 {
90 double length = sqrt(_x * _x + _y * _y + _z * _z);
91
92 if (length != 0)
93 {
94 _x /= length;
95 _y /= length;
96 _z /= length;
97 }
98 }
99
100
101
102 /**
103 * Get scalar product of the vectors
104 * @param vector arbitrary vector
105 * @return the scalar product
106 */
107 double Vector::getScalarProduct(Vector vector)
108 {
109 double scalarProduct = getX() * vector.getX() +
110 getY() * vector.getY() +
111 getZ() * vector.getZ();
112
113 return scalarProduct;
114 }
115
116
117
118 /**
119 * Get cross product of the vectors
120 * @param vector arbitrary vector
121 * @return the cross product
122 */
123 Vector Vector::getCrossProduct(Vector vector)
124 {
125 Vector crossProduct(getY() * vector.getZ() - getZ() * vector.getY(),
126 getZ() * vector.getX() - getX() * vector.getZ(),
127 getX() * vector.getY() - getY() * vector.getX());
128
129 return crossProduct;
130 }
131
132
133
134 /**
135 * Get angle between the vectors
136 * @param vector the second (normalized) vector
137 * @return the angle, in degrees
138 */
139 double Vector::getAngle(Vector vector)
140 {
141 double scalarProduct = getScalarProduct(vector);
142 double angle = (acos(scalarProduct) / (M_PIl * 2.0)) * 360.0;
143
144 return angle;
145 }