Commit d00a66d1 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added vector3d.h.

parent 9f925679
......@@ -215,6 +215,7 @@ libcdo_la_SOURCES = \
util_wildcards.cc \
util_wildcards.h \
varlist.h \
vector3d.h \
verifygrid.h \
zaxis_print.cc \
cdo_zaxis.cc \
......
......@@ -8,97 +8,7 @@
#include <array>
#include <map>
class Vector3d
{
private:
double X, Y, Z;
public:
// Sets all members to zero
Vector3d() : X(0), Y(0), Z(0) {};
Vector3d(const double &x, const double &y, const double &z) : X(x), Y(y), Z(z) {}
Vector3d(const double coords[3]) : X(coords[0]), Y(coords[1]), Z(coords[2]) {}
double operator[] (size_t index) const
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
}
return double();
}
Vector3d operator+ (const Vector3d &other) const
{
return Vector3d(X + other.X, Y + other.Y, Z + other.Z);
}
Vector3d operator- (const Vector3d &other) const
{
return Vector3d(X - other.X, Y - other.Y, Z - other.Z);
}
Vector3d operator- (void) const
{
return Vector3d(-X, -Y, -Z);
}
// Calculate the cross/outer/vector product
Vector3d operator% (const Vector3d &other) const
{
return Vector3d(Y*other.Z - Z*other.Y,
Z*other.X - X*other.Z,
X*other.Y - Y*other.X);
}
// Division by scalars
Vector3d operator/ (const double scalar) const
{
return Vector3d(X / scalar, Y / scalar, Z / scalar);
}
Vector3d operator/= (const double scalar)
{
return *this = *this / scalar;
}
// Calculate the dot/inner/scalar product
double operator* (const Vector3d &other) const
{
return (X * other.X) + (Y* other.Y) + (Z * other.Z);
}
double magnitude() const
{
return sqrt((X * X) + (Y * Y) + (Z * Z));
}
Vector3d normalised() const
{
return Vector3d(*this) / magnitude();
}
/*
Vector3d d_normalize()
{
const double dnorm = sqrt(*this * *this);
return Vector3d(*this) / dnorm;
}
*/
double longitude() const
{
return atan2(Y, X);
}
double latitude() const
{
return M_PI_2 - acos(Z);
}
};
#include "vector3d.h"
using Index = size_t;
using Vertex = Vector3d;
......@@ -113,6 +23,7 @@ namespace icosahedron
// Southern hemisphere are the other 6 elements of vertices[6:11]
// 12 vertices
static VertexList vertices(12);
void
init(void)
{
......
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2019 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
See COPYING file for copying and redistribution conditions.
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; version 2 of the License.
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.
*/
#ifndef VECTOR3D_H
#define VECTOR3D_H
class Vector3d
{
private:
double X, Y, Z;
public:
// Sets all members to zero
Vector3d() : X(0), Y(0), Z(0) {};
Vector3d(const double &x, const double &y, const double &z) : X(x), Y(y), Z(z) {}
Vector3d(const double coords[3]) : X(coords[0]), Y(coords[1]), Z(coords[2]) {}
double operator[] (size_t index) const
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
}
return double();
}
Vector3d operator+ (const Vector3d &other) const
{
return Vector3d(X + other.X, Y + other.Y, Z + other.Z);
}
Vector3d operator- (const Vector3d &other) const
{
return Vector3d(X - other.X, Y - other.Y, Z - other.Z);
}
Vector3d operator- (void) const
{
return Vector3d(-X, -Y, -Z);
}
// Calculate the cross/outer/vector product
Vector3d operator% (const Vector3d &other) const
{
return Vector3d(Y*other.Z - Z*other.Y,
Z*other.X - X*other.Z,
X*other.Y - Y*other.X);
}
// Division by scalars
Vector3d operator/ (const double scalar) const
{
return Vector3d(X / scalar, Y / scalar, Z / scalar);
}
Vector3d operator/= (const double scalar)
{
return *this = *this / scalar;
}
// Calculate the dot/inner/scalar product
double operator* (const Vector3d &other) const
{
return (X * other.X) + (Y* other.Y) + (Z * other.Z);
}
double magnitude() const
{
return sqrt((X * X) + (Y * Y) + (Z * Z));
}
Vector3d normalised() const
{
return Vector3d(*this) / magnitude();
}
/*
Vector3d d_normalize()
{
const double dnorm = sqrt(*this * *this);
return Vector3d(*this) / dnorm;
}
*/
double longitude() const
{
return atan2(Y, X);
}
double latitude() const
{
return M_PI_2 - acos(Z);
}
};
#endif
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