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