tor-browser

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

BasePoint3D.h (3640B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 MOZILLA_BASEPOINT3D_H_
      8 #define MOZILLA_BASEPOINT3D_H_
      9 
     10 #include "mozilla/Assertions.h"
     11 
     12 namespace mozilla {
     13 namespace gfx {
     14 
     15 /**
     16 * Do not use this class directly. Subclass it, pass that subclass as the
     17 * Sub parameter, and only use that subclass. This allows methods to safely
     18 * cast 'this' to 'Sub*'.
     19 */
     20 template <class T, class Sub>
     21 struct BasePoint3D {
     22  union {
     23    struct {
     24      T x, y, z;
     25    };
     26    T components[3];
     27  };
     28 
     29  // Constructors
     30  BasePoint3D() : x(0), y(0), z(0) {}
     31  BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
     32 
     33  void MoveTo(T aX, T aY, T aZ) {
     34    x = aX;
     35    y = aY;
     36    z = aZ;
     37  }
     38  void MoveBy(T aDx, T aDy, T aDz) {
     39    x += aDx;
     40    y += aDy;
     41    z += aDz;
     42  }
     43 
     44  // Note that '=' isn't defined so we'll get the
     45  // compiler generated default assignment operator
     46 
     47  T& operator[](int aIndex) {
     48    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
     49    return *((&x) + aIndex);
     50  }
     51 
     52  const T& operator[](int aIndex) const {
     53    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
     54    return *((&x) + aIndex);
     55  }
     56 
     57  bool operator==(const Sub& aPoint) const {
     58    return x == aPoint.x && y == aPoint.y && z == aPoint.z;
     59  }
     60  bool operator!=(const Sub& aPoint) const {
     61    return x != aPoint.x || y != aPoint.y || z != aPoint.z;
     62  }
     63 
     64  Sub operator+(const Sub& aPoint) const {
     65    return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z);
     66  }
     67  Sub operator-(const Sub& aPoint) const {
     68    return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z);
     69  }
     70  Sub& operator+=(const Sub& aPoint) {
     71    x += aPoint.x;
     72    y += aPoint.y;
     73    z += aPoint.z;
     74    return *static_cast<Sub*>(this);
     75  }
     76  Sub& operator-=(const Sub& aPoint) {
     77    x -= aPoint.x;
     78    y -= aPoint.y;
     79    z -= aPoint.z;
     80    return *static_cast<Sub*>(this);
     81  }
     82 
     83  Sub operator*(T aScale) const {
     84    return Sub(x * aScale, y * aScale, z * aScale);
     85  }
     86  Sub operator/(T aScale) const {
     87    return Sub(x / aScale, y / aScale, z / aScale);
     88  }
     89 
     90  Sub& operator*=(T aScale) {
     91    x *= aScale;
     92    y *= aScale;
     93    z *= aScale;
     94    return *static_cast<Sub*>(this);
     95  }
     96 
     97  Sub& operator/=(T aScale) {
     98    x /= aScale;
     99    y /= aScale;
    100    z /= aScale;
    101    return *static_cast<Sub*>(this);
    102  }
    103 
    104  Sub operator-() const { return Sub(-x, -y, -z); }
    105 
    106  Sub CrossProduct(const Sub& aPoint) const {
    107    return Sub(y * aPoint.z - aPoint.y * z, z * aPoint.x - aPoint.z * x,
    108               x * aPoint.y - aPoint.x * y);
    109  }
    110 
    111  T DotProduct(const Sub& aPoint) const {
    112    return x * aPoint.x + y * aPoint.y + z * aPoint.z;
    113  }
    114 
    115  T Length() const { return sqrt(x * x + y * y + z * z); }
    116 
    117  // Invalid for points with distance from origin of 0.
    118  void Normalize() { *this /= Length(); }
    119 
    120  void RobustNormalize() {
    121    // If the distance is infinite, we scale it by 1/(the maximum value of T)
    122    // before doing normalization, so we can avoid getting a zero point.
    123    T length = Length();
    124    if (std::isinf(length)) {
    125      *this /= std::numeric_limits<T>::max();
    126      length = Length();
    127    }
    128 
    129    *this /= length;
    130  }
    131 
    132  friend std::ostream& operator<<(std::ostream& stream,
    133                                  const BasePoint3D<T, Sub>& aPoint) {
    134    return stream << '(' << aPoint.x << ',' << aPoint.y << ',' << aPoint.z
    135                  << ')';
    136  }
    137 };
    138 
    139 }  // namespace gfx
    140 }  // namespace mozilla
    141 
    142 #endif /* MOZILLA_BASEPOINT3D_H_ */