Line data Source code
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 0 : ThreeDPoint()
20 0 : : x(0.)
21 : , y(0.)
22 0 : , z(0.)
23 : {
24 0 : }
25 0 : ThreeDPoint(double aX, double aY, double aZ)
26 0 : : x(aX)
27 : , y(aY)
28 0 : , z(aZ)
29 : {
30 0 : }
31 :
32 0 : double Magnitude() const
33 : {
34 0 : return sqrt(x * x + y * y + z * z);
35 : }
36 :
37 0 : void Normalize()
38 : {
39 : // Zero vectors cannot be normalized. For our purpose, normalizing a zero
40 : // vector results in a zero vector.
41 0 : if (IsZero()) {
42 0 : return;
43 : }
44 : // 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 :
50 0 : double invDistance = 1 / Magnitude();
51 0 : x *= invDistance;
52 0 : y *= invDistance;
53 0 : z *= invDistance;
54 : }
55 :
56 0 : ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const
57 : {
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 : }
62 :
63 0 : double DotProduct(const ThreeDPoint& rhs)
64 : {
65 0 : return x * rhs.x + y * rhs.y + z * rhs.z;
66 : }
67 :
68 0 : bool IsZero() const
69 : {
70 0 : return x == 0 && y == 0 && z == 0;
71 : }
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 0 : double MaxNorm() const
80 : {
81 0 : return std::max(fabs(x), std::max(fabs(y), fabs(z)));
82 : }
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 :
|