|
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 { |
|
18 ThreeDPoint() |
|
19 : x(0.) |
|
20 , y(0.) |
|
21 , z(0.) |
|
22 { |
|
23 } |
|
24 ThreeDPoint(double aX, double aY, double aZ) |
|
25 : x(aX) |
|
26 , y(aY) |
|
27 , z(aZ) |
|
28 { |
|
29 } |
|
30 |
|
31 double Magnitude() const |
|
32 { |
|
33 return sqrt(x * x + y * y + z * z); |
|
34 } |
|
35 |
|
36 void Normalize() |
|
37 { |
|
38 // Normalize with the maximum norm first to avoid overflow and underflow. |
|
39 double invMax = 1 / MaxNorm(); |
|
40 x *= invMax; |
|
41 y *= invMax; |
|
42 z *= invMax; |
|
43 |
|
44 double invDistance = 1 / Magnitude(); |
|
45 x *= invDistance; |
|
46 y *= invDistance; |
|
47 z *= invDistance; |
|
48 } |
|
49 |
|
50 ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const |
|
51 { |
|
52 return ThreeDPoint(y * rhs.z - z * rhs.y, |
|
53 z * rhs.x - x * rhs.z, |
|
54 x * rhs.y - y * rhs.x); |
|
55 } |
|
56 |
|
57 double DotProduct(const ThreeDPoint& rhs) |
|
58 { |
|
59 return x * rhs.x + y * rhs.y + z * rhs.z; |
|
60 } |
|
61 |
|
62 bool IsZero() const |
|
63 { |
|
64 return x == 0 && y == 0 && z == 0; |
|
65 } |
|
66 |
|
67 // For comparing two vectors of close to unit magnitude. |
|
68 bool FuzzyEqual(const ThreeDPoint& other); |
|
69 |
|
70 double x, y, z; |
|
71 |
|
72 private: |
|
73 double MaxNorm() const |
|
74 { |
|
75 return std::max(fabs(x), std::max(fabs(y), fabs(z))); |
|
76 } |
|
77 }; |
|
78 |
|
79 ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
|
80 ThreeDPoint operator*(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
|
81 ThreeDPoint operator*(const ThreeDPoint& lhs, const double rhs); |
|
82 bool operator==(const ThreeDPoint& lhs, const ThreeDPoint& rhs); |
|
83 |
|
84 } |
|
85 } |
|
86 |
|
87 #endif |
|
88 |