1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/BasePoint4D.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 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_BASEPOINT4D_H_ 1.10 +#define MOZILLA_BASEPOINT4D_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 BasePoint4D { 1.24 + T x, y, z, w; 1.25 + 1.26 + // Constructors 1.27 + BasePoint4D() : x(0), y(0), z(0), w(0) {} 1.28 + BasePoint4D(T aX, T aY, T aZ, T aW) : x(aX), y(aY), z(aZ), w(aW) {} 1.29 + 1.30 + void MoveTo(T aX, T aY, T aZ, T aW) { x = aX; y = aY; z = aZ; w = aW; } 1.31 + void MoveBy(T aDx, T aDy, T aDz, T aDw) { x += aDx; y += aDy; z += aDz; w += aDw; } 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 + bool operator==(const Sub& aPoint) const { 1.37 + return x == aPoint.x && y == aPoint.y && 1.38 + z == aPoint.z && w == aPoint.w; 1.39 + } 1.40 + bool operator!=(const Sub& aPoint) const { 1.41 + return x != aPoint.x || y != aPoint.y || 1.42 + z != aPoint.z || w != aPoint.w; 1.43 + } 1.44 + 1.45 + Sub operator+(const Sub& aPoint) const { 1.46 + return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z, w + aPoint.w); 1.47 + } 1.48 + Sub operator-(const Sub& aPoint) const { 1.49 + return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z, w - aPoint.w); 1.50 + } 1.51 + Sub& operator+=(const Sub& aPoint) { 1.52 + x += aPoint.x; 1.53 + y += aPoint.y; 1.54 + z += aPoint.z; 1.55 + w += aPoint.w; 1.56 + return *static_cast<Sub*>(this); 1.57 + } 1.58 + Sub& operator-=(const Sub& aPoint) { 1.59 + x -= aPoint.x; 1.60 + y -= aPoint.y; 1.61 + z -= aPoint.z; 1.62 + w -= aPoint.w; 1.63 + return *static_cast<Sub*>(this); 1.64 + } 1.65 + 1.66 + Sub operator*(T aScale) const { 1.67 + return Sub(x * aScale, y * aScale, z * aScale, w * aScale); 1.68 + } 1.69 + Sub operator/(T aScale) const { 1.70 + return Sub(x / aScale, y / aScale, z / aScale, w / aScale); 1.71 + } 1.72 + 1.73 + Sub& operator*=(T aScale) { 1.74 + x *= aScale; 1.75 + y *= aScale; 1.76 + z *= aScale; 1.77 + w *= aScale; 1.78 + return *static_cast<Sub*>(this); 1.79 + } 1.80 + 1.81 + Sub& operator/=(T aScale) { 1.82 + x /= aScale; 1.83 + y /= aScale; 1.84 + z /= aScale; 1.85 + w /= aScale; 1.86 + return *static_cast<Sub*>(this); 1.87 + } 1.88 + 1.89 + Sub operator-() const { 1.90 + return Sub(-x, -y, -z, -w); 1.91 + } 1.92 + 1.93 + T& operator[](int aIndex) { 1.94 + MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index"); 1.95 + return *((&x)+aIndex); 1.96 + } 1.97 + 1.98 + const T& operator[](int aIndex) const { 1.99 + MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index"); 1.100 + return *((&x)+aIndex); 1.101 + } 1.102 + 1.103 + T DotProduct(const Sub& aPoint) const { 1.104 + return x * aPoint.x + y * aPoint.y + z * aPoint.z + w * aPoint.w; 1.105 + } 1.106 + 1.107 + // Ignores the 4th component! 1.108 + Sub CrossProduct(const Sub& aPoint) const { 1.109 + return Sub(y * aPoint.z - aPoint.y * z, 1.110 + z * aPoint.x - aPoint.z * x, 1.111 + x * aPoint.y - aPoint.x * y, 1.112 + 0); 1.113 + } 1.114 + 1.115 + T Length() const { 1.116 + return sqrt(x*x + y*y + z*z + w*w); 1.117 + } 1.118 + 1.119 + void Normalize() { 1.120 + *this /= Length(); 1.121 + } 1.122 +}; 1.123 + 1.124 +} 1.125 +} 1.126 + 1.127 +#endif /* MOZILLA_BASEPOINT4D_H_ */