tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ThreeDPoint.h (1982B)


      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 <algorithm>
     11 #include <cmath>
     12 
     13 namespace mozilla::dom {
     14 
     15 struct ThreeDPoint final {
     16  ThreeDPoint() : x(0.), y(0.), z(0.) {}
     17  ThreeDPoint(double aX, double aY, double aZ) : x(aX), y(aY), z(aZ) {}
     18 
     19  double Magnitude() const { return sqrt(x * x + y * y + z * z); }
     20 
     21  void Normalize() {
     22    // Zero vectors cannot be normalized. For our purpose, normalizing a zero
     23    // vector results in a zero vector.
     24    if (IsZero()) {
     25      return;
     26    }
     27    // Normalize with the maximum norm first to avoid overflow and underflow.
     28    double invMax = 1 / MaxNorm();
     29    x *= invMax;
     30    y *= invMax;
     31    z *= invMax;
     32 
     33    double invDistance = 1 / Magnitude();
     34    x *= invDistance;
     35    y *= invDistance;
     36    z *= invDistance;
     37  }
     38 
     39  ThreeDPoint CrossProduct(const ThreeDPoint& rhs) const {
     40    return ThreeDPoint(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z,
     41                       x * rhs.y - y * rhs.x);
     42  }
     43 
     44  double DotProduct(const ThreeDPoint& rhs) {
     45    return x * rhs.x + y * rhs.y + z * rhs.z;
     46  }
     47 
     48  bool IsZero() const { return x == 0 && y == 0 && z == 0; }
     49 
     50  // For comparing two vectors of close to unit magnitude.
     51  bool FuzzyEqual(const ThreeDPoint& other);
     52 
     53  double x, y, z;
     54 
     55 private:
     56  double MaxNorm() const {
     57    return std::max(fabs(x), std::max(fabs(y), fabs(z)));
     58  }
     59 };
     60 
     61 ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs);
     62 ThreeDPoint operator*(const ThreeDPoint& lhs, const ThreeDPoint& rhs);
     63 ThreeDPoint operator*(const ThreeDPoint& lhs, const double rhs);
     64 bool operator==(const ThreeDPoint& lhs, const ThreeDPoint& rhs);
     65 
     66 }  // namespace mozilla::dom
     67 
     68 #endif