summaryrefslogtreecommitdiff
path: root/3d/vector.cc
diff options
context:
space:
mode:
authorhugues <hugues@0f7e0d06-a6f9-0310-a55f-d5f984f55e4c>2006-03-25 15:07:51 +0000
committerHugues Hiegel <hugues@hiegel.fr>2008-03-18 10:06:55 +0100
commit56cc59cf44ec64440ba4d1c0d005196195c758e6 (patch)
tree0e4bc431438a05c2e32b8703a8c79dcbf26a7cbf /3d/vector.cc
parentd49be924baa2759aefa5b5311a35adf50db48e12 (diff)
Nettoyage du repository glagenHEADmaster
git-svn-id: file:///usr/local/opt/svn/repos/glagen@12 0f7e0d06-a6f9-0310-a55f-d5f984f55e4c
Diffstat (limited to '3d/vector.cc')
-rw-r--r--3d/vector.cc252
1 files changed, 252 insertions, 0 deletions
diff --git a/3d/vector.cc b/3d/vector.cc
new file mode 100644
index 0000000..06e3085
--- /dev/null
+++ b/3d/vector.cc
@@ -0,0 +1,252 @@
+//=============================================================================
+//
+// Glagen : a planet sized landscape generator
+// Copyright (C) 2002 Julien Guertault, Hugues Hiegel, Meng-Tih Lam
+//
+// This program 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.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+//=============================================================================
+//
+// Glagen : GPL LAndscape GENerator
+//
+// vector.cc for Glagen : made by Zavie (Julien Guertault)
+//
+// www.glagen.org
+//
+//=============================================================================
+
+#include <cmath>
+#include "data_glagen.hh"
+#include "misc.hh"
+#include "vector.hh"
+
+// ================================================ Constructors and destructor
+
+Vector :: Vector ()
+{
+ vect[0] = 0;
+ vect[1] = 0;
+ vect[2] = 0;
+}
+
+Vector :: Vector (const Vector &source)
+{
+ vect[0] = source.X ();
+ vect[1] = source.Y ();
+ vect[2] = source.Z ();
+}
+
+Vector :: Vector (const float x, const float y, const float z)
+{
+ vect[0] = x;
+ vect[1] = y;
+ vect[2] = z;
+}
+
+Vector :: ~Vector () {}
+
+
+// ======================================================================= Read
+
+float Vector :: X () const { return vect[0]; }
+float Vector :: Y () const { return vect[1]; }
+float Vector :: Z () const { return vect[2]; }
+
+float Vector :: Norm () const
+{
+ return (sqrtf(vect[0] * vect[0] +
+ vect[1] * vect[1] +
+ vect[2] * vect[2]));
+}
+
+// Fast norm approximation computing
+float Vector :: Approx_norm () const
+{
+ return (Approx_dist(vect[0], vect[1], vect[2]));
+}
+
+
+// ====================================================================== Write
+
+void Vector :: Set (const float x, const float y, const float z)
+{
+ vect[0] = x;
+ vect[1] = y;
+ vect[2] = z;
+}
+
+void Vector :: Set_X (const float x){ vect[0] = x;}
+void Vector :: Set_Y (const float y){ vect[1] = y;}
+void Vector :: Set_Z (const float z){ vect[2] = z;}
+
+void Vector :: Normalize ()
+{
+ float norm = Norm ();
+ vect[0] = vect[0] / norm;
+ vect[1] = vect[1] / norm;
+ vect[2] = vect[2] / norm;
+}
+
+void Vector :: Approx_normalize ()
+{
+ float norm = Approx_norm ();
+ vect[0] = vect[0] / norm;
+ vect[1] = vect[1] / norm;
+ vect[2] = vect[2] / norm;
+}
+
+void Vector :: Rotate (const Vector &axis, const float angle)
+{
+ float cosinus = cosf (angle);
+ float sinus = sinf (angle);
+ float cos_bis = (1 - cosinus) * axis.X ();
+ float cos_third = (1 - cosinus) * axis.Y ();
+
+ float x = (cosinus + cos_bis * axis.X ()) * vect[0];
+ x = x + (cos_bis * axis.Y () - axis.Z () * sinus) * vect[1];
+ x = x + (cos_bis * axis.Z () + axis.Y () * sinus) * vect[2];
+
+ float y = (cos_bis * axis.Y () + axis.Z () * sinus) * vect[0];
+ y = y + (cosinus + cos_third * axis.Y ()) * vect[1];
+ y = y + (cos_third * axis.Z () - axis.X () * sinus) * vect[2];
+
+ float z = (cos_bis * axis.Z () - axis.Y () * sinus) * vect[0];
+ z = z + (cos_third * axis.Z () + axis.X () * sinus) * vect[1];
+ z = z + (cosinus + (1 - cosinus) * axis.Z () * axis.Z ()) * vect[2];
+
+ vect[0] = x;
+ vect[1] = y;
+ vect[2] = z;
+}
+
+
+// ================================================================== Operators
+
+// @
+// @
+// @@@@@
+// @
+// @
+Vector Vector :: operator + (const class Vector &v) const
+{
+ return Vector(vect[0] + v.X (),
+ vect[1] + v.Y (),
+ vect[2] + v.Z ());
+}
+
+//
+//
+// @@@@@
+//
+//
+Vector Vector :: operator - (const class Vector &v) const
+{
+ return Vector(vect[0] - v.X (),
+ vect[1] - v.Y (),
+ vect[2] - v.Z ());
+}
+
+//
+// @
+// @@@
+// @
+//
+float Vector :: operator * (const class Vector &v) const
+{
+ return (vect[0] * v.X () +
+ vect[1] * v.Y () +
+ vect[2] * v.Z ());
+}
+
+// @
+// @ @
+// @ @
+//
+//
+Vector Vector :: operator ^ (const class Vector &v) const
+{
+ return Vector((vect[1] * v.Z ()) - (vect[2] * v.Y ()),
+ (vect[2] * v.X ()) - (vect[0] * v.Z ()),
+ (vect[0] * v.Y ()) - (vect[1] * v.X ()));
+}
+
+// @
+// @ @@@@@
+// @@@@@
+// @ @@@@@
+// @
+Vector Vector :: operator += (const class Vector &v)
+{
+ *this = *this + v;
+ return *this;
+}
+
+//
+// @@@@@
+// @@@@@
+// @@@@@
+//
+Vector Vector :: operator -= (const class Vector &v)
+{
+ *this = *this - v;
+ return *this;
+}
+
+
+// @@@
+// @ @
+// @@@ @@@@
+// @ @ @
+// @@@ @
+Vector Vector :: operator * (const float &a) const
+{
+ return Vector(X () * a,
+ Y () * a,
+ Z () * a);
+}
+
+// @ @@@
+// @ @
+// @ @@@@
+// @ @ @
+// @ @@@ @
+Vector Vector :: operator / (const float &a) const
+{
+ return Vector(X () / a,
+ Y () / a,
+ Z () / a);
+}
+
+// @@@
+// @ @@@@@ @
+// @@@ @@@@
+// @ @@@@@ @ @
+// @@@ @
+Vector Vector :: operator *= (const float &a)
+{
+ *this = *this * a;
+ return *this;
+}
+
+// @ @@@
+// @ @@@@@ @
+// @ @@@@
+// @ @@@@@ @ @
+// @ @@@ @
+Vector Vector :: operator /= (const float &a)
+{
+ *this = *this / a;
+ return *this;
+}