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_BASEPOINT3D_H_ michael@0: #define MOZILLA_BASEPOINT3D_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 BasePoint3D { michael@0: T x, y, z; michael@0: michael@0: // Constructors michael@0: BasePoint3D() : x(0), y(0), z(0) {} michael@0: BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {} michael@0: michael@0: void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; } michael@0: void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; } 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: T& operator[](int aIndex) { michael@0: MOZ_ASSERT(aIndex >= 0 && aIndex <= 2); michael@0: return *((&x)+aIndex); michael@0: } michael@0: michael@0: const T& operator[](int aIndex) const { michael@0: MOZ_ASSERT(aIndex >= 0 && aIndex <= 2); michael@0: return *((&x)+aIndex); michael@0: } michael@0: michael@0: bool operator==(const Sub& aPoint) const { michael@0: return x == aPoint.x && y == aPoint.y && z == aPoint.z; michael@0: } michael@0: bool operator!=(const Sub& aPoint) const { michael@0: return x != aPoint.x || y != aPoint.y || z != aPoint.z; 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); michael@0: } michael@0: Sub operator-(const Sub& aPoint) const { michael@0: return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z); 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: 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: 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); michael@0: } michael@0: Sub operator/(T aScale) const { michael@0: return Sub(x / aScale, y / aScale, z / 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: 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: return *static_cast(this); michael@0: } michael@0: michael@0: Sub operator-() const { michael@0: return Sub(-x, -y, -z); michael@0: } michael@0: 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: } michael@0: michael@0: T DotProduct(const Sub& aPoint) const { michael@0: return x * aPoint.x + y * aPoint.y + z * aPoint.z; michael@0: } michael@0: michael@0: T Length() const { michael@0: return sqrt(x*x + y*y + z*z); michael@0: } michael@0: michael@0: // Invalid for points with distance from origin of 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_BASEPOINT3D_H_ */