gfx/2d/BasePoint3D.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/BasePoint3D.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_BASEPOINT3D_H_
    1.10 +#define MOZILLA_BASEPOINT3D_H_
    1.11 +
    1.12 +#include "mozilla/Assertions.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace gfx {
    1.16 +
    1.17 +/**
    1.18 + * Do not use this class directly. Subclass it, pass that subclass as the
    1.19 + * Sub parameter, and only use that subclass. This allows methods to safely
    1.20 + * cast 'this' to 'Sub*'.
    1.21 + */
    1.22 +template <class T, class Sub>
    1.23 +struct BasePoint3D {
    1.24 +  T x, y, z;
    1.25 +
    1.26 +  // Constructors
    1.27 +  BasePoint3D() : x(0), y(0), z(0) {}
    1.28 +  BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
    1.29 +
    1.30 +  void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; }
    1.31 +  void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; }
    1.32 +
    1.33 +  // Note that '=' isn't defined so we'll get the
    1.34 +  // compiler generated default assignment operator
    1.35 +
    1.36 +  T& operator[](int aIndex) {
    1.37 +    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
    1.38 +    return *((&x)+aIndex);
    1.39 +  }
    1.40 +
    1.41 +  const T& operator[](int aIndex) const {
    1.42 +    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
    1.43 +    return *((&x)+aIndex);
    1.44 +  }
    1.45 +
    1.46 +  bool operator==(const Sub& aPoint) const {
    1.47 +    return x == aPoint.x && y == aPoint.y && z == aPoint.z;
    1.48 +  }
    1.49 +  bool operator!=(const Sub& aPoint) const {
    1.50 +    return x != aPoint.x || y != aPoint.y || z != aPoint.z;
    1.51 +  }
    1.52 +
    1.53 +  Sub operator+(const Sub& aPoint) const {
    1.54 +    return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z);
    1.55 +  }
    1.56 +  Sub operator-(const Sub& aPoint) const {
    1.57 +    return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z);
    1.58 +  }
    1.59 +  Sub& operator+=(const Sub& aPoint) {
    1.60 +    x += aPoint.x;
    1.61 +    y += aPoint.y;
    1.62 +    z += aPoint.z;
    1.63 +    return *static_cast<Sub*>(this);
    1.64 +  }
    1.65 +  Sub& operator-=(const Sub& aPoint) {
    1.66 +    x -= aPoint.x;
    1.67 +    y -= aPoint.y;
    1.68 +    z -= aPoint.z;
    1.69 +    return *static_cast<Sub*>(this);
    1.70 +  }
    1.71 +
    1.72 +  Sub operator*(T aScale) const {
    1.73 +    return Sub(x * aScale, y * aScale, z * aScale);
    1.74 +  }
    1.75 +  Sub operator/(T aScale) const {
    1.76 +    return Sub(x / aScale, y / aScale, z / aScale);
    1.77 +  }
    1.78 +
    1.79 +  Sub& operator*=(T aScale) {
    1.80 +    x *= aScale;
    1.81 +    y *= aScale;
    1.82 +    z *= aScale;
    1.83 +    return *static_cast<Sub*>(this);
    1.84 +  }
    1.85 +
    1.86 +  Sub& operator/=(T aScale) {
    1.87 +      x /= aScale;
    1.88 +      y /= aScale;
    1.89 +      z /= aScale;
    1.90 +      return *static_cast<Sub*>(this);
    1.91 +  }
    1.92 +
    1.93 +  Sub operator-() const {
    1.94 +    return Sub(-x, -y, -z);
    1.95 +  }
    1.96 +
    1.97 +  Sub CrossProduct(const Sub& aPoint) const {
    1.98 +      return Sub(y * aPoint.z - aPoint.y * z,
    1.99 +                 z * aPoint.x - aPoint.z * x,
   1.100 +                 x * aPoint.y - aPoint.x * y);
   1.101 +  }
   1.102 +
   1.103 +  T DotProduct(const Sub& aPoint) const {
   1.104 +      return x * aPoint.x + y * aPoint.y + z * aPoint.z;
   1.105 +  }
   1.106 +
   1.107 +  T Length() const {
   1.108 +      return sqrt(x*x + y*y + z*z);
   1.109 +  }
   1.110 +
   1.111 +  // Invalid for points with distance from origin of 0.
   1.112 +  void Normalize() {
   1.113 +      *this /= Length();
   1.114 +  }
   1.115 +};
   1.116 +
   1.117 +}
   1.118 +}
   1.119 +
   1.120 +#endif /* MOZILLA_BASEPOINT3D_H_ */

mercurial