1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/ThreeDPoint.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef ThreeDPoint_h_ 1.11 +#define ThreeDPoint_h_ 1.12 + 1.13 +#include <cmath> 1.14 +#include <algorithm> 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +namespace dom { 1.19 + 1.20 +struct ThreeDPoint { 1.21 + ThreeDPoint() 1.22 + : x(0.) 1.23 + , y(0.) 1.24 + , z(0.) 1.25 + { 1.26 + } 1.27 + ThreeDPoint(double aX, double aY, double aZ) 1.28 + : x(aX) 1.29 + , y(aY) 1.30 + , z(aZ) 1.31 + { 1.32 + } 1.33 + 1.34 + double Magnitude() const 1.35 + { 1.36 + return sqrt(x * x + y * y + z * z); 1.37 + } 1.38 + 1.39 + void Normalize() 1.40 + { 1.41 + // Normalize with the maximum norm first to avoid overflow and underflow. 1.42 + double invMax = 1 / MaxNorm(); 1.43 + x *= invMax; 1.44 + y *= invMax; 1.45 + z *= invMax; 1.46 + 1.47 + double invDistance = 1 / Magnitude(); 1.48 + x *= invDistance; 1.49 + y *= invDistance; 1.50 + z *= invDistance; 1.51 + } 1.52 + 1.53 + ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const 1.54 + { 1.55 + return ThreeDPoint(y * rhs.z - z * rhs.y, 1.56 + z * rhs.x - x * rhs.z, 1.57 + x * rhs.y - y * rhs.x); 1.58 + } 1.59 + 1.60 + double DotProduct(const ThreeDPoint& rhs) 1.61 + { 1.62 + return x * rhs.x + y * rhs.y + z * rhs.z; 1.63 + } 1.64 + 1.65 + bool IsZero() const 1.66 + { 1.67 + return x == 0 && y == 0 && z == 0; 1.68 + } 1.69 + 1.70 + // For comparing two vectors of close to unit magnitude. 1.71 + bool FuzzyEqual(const ThreeDPoint& other); 1.72 + 1.73 + double x, y, z; 1.74 + 1.75 +private: 1.76 + double MaxNorm() const 1.77 + { 1.78 + return std::max(fabs(x), std::max(fabs(y), fabs(z))); 1.79 + } 1.80 +}; 1.81 + 1.82 +ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs); 1.83 +ThreeDPoint operator*(const ThreeDPoint& lhs, const ThreeDPoint& rhs); 1.84 +ThreeDPoint operator*(const ThreeDPoint& lhs, const double rhs); 1.85 +bool operator==(const ThreeDPoint& lhs, const ThreeDPoint& rhs); 1.86 + 1.87 +} 1.88 +} 1.89 + 1.90 +#endif 1.91 +