michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef ThreeDPoint_h_ michael@0: #define ThreeDPoint_h_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: namespace dom { michael@0: michael@0: struct ThreeDPoint { michael@0: ThreeDPoint() michael@0: : x(0.) michael@0: , y(0.) michael@0: , z(0.) michael@0: { michael@0: } michael@0: ThreeDPoint(double aX, double aY, double aZ) michael@0: : x(aX) michael@0: , y(aY) michael@0: , z(aZ) michael@0: { michael@0: } michael@0: michael@0: double Magnitude() const michael@0: { michael@0: return sqrt(x * x + y * y + z * z); michael@0: } michael@0: michael@0: void Normalize() michael@0: { michael@0: // Normalize with the maximum norm first to avoid overflow and underflow. michael@0: double invMax = 1 / MaxNorm(); michael@0: x *= invMax; michael@0: y *= invMax; michael@0: z *= invMax; michael@0: michael@0: double invDistance = 1 / Magnitude(); michael@0: x *= invDistance; michael@0: y *= invDistance; michael@0: z *= invDistance; michael@0: } michael@0: michael@0: ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const michael@0: { michael@0: return ThreeDPoint(y * rhs.z - z * rhs.y, michael@0: z * rhs.x - x * rhs.z, michael@0: x * rhs.y - y * rhs.x); michael@0: } michael@0: michael@0: double DotProduct(const ThreeDPoint& rhs) michael@0: { michael@0: return x * rhs.x + y * rhs.y + z * rhs.z; michael@0: } michael@0: michael@0: bool IsZero() const michael@0: { michael@0: return x == 0 && y == 0 && z == 0; michael@0: } michael@0: michael@0: // For comparing two vectors of close to unit magnitude. michael@0: bool FuzzyEqual(const ThreeDPoint& other); michael@0: michael@0: double x, y, z; michael@0: michael@0: private: michael@0: double MaxNorm() const michael@0: { michael@0: return std::max(fabs(x), std::max(fabs(y), fabs(z))); michael@0: } michael@0: }; michael@0: michael@0: ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs); michael@0: ThreeDPoint operator*(const ThreeDPoint& lhs, const ThreeDPoint& rhs); michael@0: ThreeDPoint operator*(const ThreeDPoint& lhs, const double rhs); michael@0: bool operator==(const ThreeDPoint& lhs, const ThreeDPoint& rhs); michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: