michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_BASEPOINT4D_H_ michael@0: #define MOZILLA_BASEPOINT4D_H_ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: /** michael@0: * Do not use this class directly. Subclass it, pass that subclass as the michael@0: * Sub parameter, and only use that subclass. This allows methods to safely michael@0: * cast 'this' to 'Sub*'. michael@0: */ michael@0: template michael@0: struct BasePoint4D { michael@0: T x, y, z, w; michael@0: michael@0: // Constructors michael@0: BasePoint4D() : x(0), y(0), z(0), w(0) {} michael@0: BasePoint4D(T aX, T aY, T aZ, T aW) : x(aX), y(aY), z(aZ), w(aW) {} michael@0: michael@0: void MoveTo(T aX, T aY, T aZ, T aW) { x = aX; y = aY; z = aZ; w = aW; } michael@0: void MoveBy(T aDx, T aDy, T aDz, T aDw) { x += aDx; y += aDy; z += aDz; w += aDw; } michael@0: michael@0: // Note that '=' isn't defined so we'll get the michael@0: // compiler generated default assignment operator michael@0: michael@0: bool operator==(const Sub& aPoint) const { michael@0: return x == aPoint.x && y == aPoint.y && michael@0: z == aPoint.z && w == aPoint.w; michael@0: } michael@0: bool operator!=(const Sub& aPoint) const { michael@0: return x != aPoint.x || y != aPoint.y || michael@0: z != aPoint.z || w != aPoint.w; michael@0: } michael@0: michael@0: Sub operator+(const Sub& aPoint) const { michael@0: return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z, w + aPoint.w); michael@0: } michael@0: Sub operator-(const Sub& aPoint) const { michael@0: return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z, w - aPoint.w); michael@0: } michael@0: Sub& operator+=(const Sub& aPoint) { michael@0: x += aPoint.x; michael@0: y += aPoint.y; michael@0: z += aPoint.z; michael@0: w += aPoint.w; michael@0: return *static_cast(this); michael@0: } michael@0: Sub& operator-=(const Sub& aPoint) { michael@0: x -= aPoint.x; michael@0: y -= aPoint.y; michael@0: z -= aPoint.z; michael@0: w -= aPoint.w; michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: Sub operator*(T aScale) const { michael@0: return Sub(x * aScale, y * aScale, z * aScale, w * aScale); michael@0: } michael@0: Sub operator/(T aScale) const { michael@0: return Sub(x / aScale, y / aScale, z / aScale, w / aScale); michael@0: } michael@0: michael@0: Sub& operator*=(T aScale) { michael@0: x *= aScale; michael@0: y *= aScale; michael@0: z *= aScale; michael@0: w *= aScale; michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: Sub& operator/=(T aScale) { michael@0: x /= aScale; michael@0: y /= aScale; michael@0: z /= aScale; michael@0: w /= aScale; michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: Sub operator-() const { michael@0: return Sub(-x, -y, -z, -w); michael@0: } michael@0: michael@0: T& operator[](int aIndex) { michael@0: MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index"); michael@0: return *((&x)+aIndex); michael@0: } michael@0: michael@0: const T& operator[](int aIndex) const { michael@0: MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index"); michael@0: return *((&x)+aIndex); michael@0: } michael@0: michael@0: T DotProduct(const Sub& aPoint) const { michael@0: return x * aPoint.x + y * aPoint.y + z * aPoint.z + w * aPoint.w; michael@0: } michael@0: michael@0: // Ignores the 4th component! michael@0: Sub CrossProduct(const Sub& aPoint) const { michael@0: return Sub(y * aPoint.z - aPoint.y * z, michael@0: z * aPoint.x - aPoint.z * x, michael@0: x * aPoint.y - aPoint.x * y, michael@0: 0); michael@0: } michael@0: michael@0: T Length() const { michael@0: return sqrt(x*x + y*y + z*z + w*w); michael@0: } michael@0: michael@0: void Normalize() { michael@0: *this /= Length(); michael@0: } michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_BASEPOINT4D_H_ */