Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef ThreeDPoint_h_ |
michael@0 | 8 | #define ThreeDPoint_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include <cmath> |
michael@0 | 11 | #include <algorithm> |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | |
michael@0 | 15 | namespace dom { |
michael@0 | 16 | |
michael@0 | 17 | struct ThreeDPoint { |
michael@0 | 18 | ThreeDPoint() |
michael@0 | 19 | : x(0.) |
michael@0 | 20 | , y(0.) |
michael@0 | 21 | , z(0.) |
michael@0 | 22 | { |
michael@0 | 23 | } |
michael@0 | 24 | ThreeDPoint(double aX, double aY, double aZ) |
michael@0 | 25 | : x(aX) |
michael@0 | 26 | , y(aY) |
michael@0 | 27 | , z(aZ) |
michael@0 | 28 | { |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | double Magnitude() const |
michael@0 | 32 | { |
michael@0 | 33 | return sqrt(x * x + y * y + z * z); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void Normalize() |
michael@0 | 37 | { |
michael@0 | 38 | // Normalize with the maximum norm first to avoid overflow and underflow. |
michael@0 | 39 | double invMax = 1 / MaxNorm(); |
michael@0 | 40 | x *= invMax; |
michael@0 | 41 | y *= invMax; |
michael@0 | 42 | z *= invMax; |
michael@0 | 43 | |
michael@0 | 44 | double invDistance = 1 / Magnitude(); |
michael@0 | 45 | x *= invDistance; |
michael@0 | 46 | y *= invDistance; |
michael@0 | 47 | z *= invDistance; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const |
michael@0 | 51 | { |
michael@0 | 52 | return ThreeDPoint(y * rhs.z - z * rhs.y, |
michael@0 | 53 | z * rhs.x - x * rhs.z, |
michael@0 | 54 | x * rhs.y - y * rhs.x); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | double DotProduct(const ThreeDPoint& rhs) |
michael@0 | 58 | { |
michael@0 | 59 | return x * rhs.x + y * rhs.y + z * rhs.z; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | bool IsZero() const |
michael@0 | 63 | { |
michael@0 | 64 | return x == 0 && y == 0 && z == 0; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // For comparing two vectors of close to unit magnitude. |
michael@0 | 68 | bool FuzzyEqual(const ThreeDPoint& other); |
michael@0 | 69 | |
michael@0 | 70 | double x, y, z; |
michael@0 | 71 | |
michael@0 | 72 | private: |
michael@0 | 73 | double MaxNorm() const |
michael@0 | 74 | { |
michael@0 | 75 | return std::max(fabs(x), std::max(fabs(y), fabs(z))); |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
michael@0 | 80 | ThreeDPoint operator*(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
michael@0 | 81 | ThreeDPoint operator*(const ThreeDPoint& lhs, const double rhs); |
michael@0 | 82 | bool operator==(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
michael@0 | 83 | |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | #endif |
michael@0 | 88 |