Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/BasePoint4D.h
Line
Count
Source (jump to first uncovered line)
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_BASEPOINT4D_H_
8
#define MOZILLA_BASEPOINT4D_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 BasePoint4D {
22
  union {
23
    struct {
24
      T x, y, z, w;
25
    };
26
    T components[4];
27
  };
28
29
  // Constructors
30
0
  BasePoint4D() : x(0), y(0), z(0), w(0) {}
31
0
  BasePoint4D(T aX, T aY, T aZ, T aW) : x(aX), y(aY), z(aZ), w(aW) {}
32
33
  void MoveTo(T aX, T aY, T aZ, T aW) { x = aX; y = aY; z = aZ; w = aW; }
34
  void MoveBy(T aDx, T aDy, T aDz, T aDw) { x += aDx; y += aDy; z += aDz; w += aDw; }
35
36
  // Note that '=' isn't defined so we'll get the
37
  // compiler generated default assignment operator
38
39
  bool operator==(const Sub& aPoint) const {
40
    return x == aPoint.x && y == aPoint.y && 
41
           z == aPoint.z && w == aPoint.w;
42
  }
43
  bool operator!=(const Sub& aPoint) const {
44
    return x != aPoint.x || y != aPoint.y || 
45
           z != aPoint.z || w != aPoint.w;
46
  }
47
48
0
  Sub operator+(const Sub& aPoint) const {
49
0
    return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z, w + aPoint.w);
50
0
  }
51
  Sub operator-(const Sub& aPoint) const {
52
    return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z, w - aPoint.w);
53
  }
54
  Sub& operator+=(const Sub& aPoint) {
55
    x += aPoint.x;
56
    y += aPoint.y;
57
    z += aPoint.z;
58
    w += aPoint.w;
59
    return *static_cast<Sub*>(this);
60
  }
61
  Sub& operator-=(const Sub& aPoint) {
62
    x -= aPoint.x;
63
    y -= aPoint.y;
64
    z -= aPoint.z;
65
    w -= aPoint.w;
66
    return *static_cast<Sub*>(this);
67
  }
68
69
0
  Sub operator*(T aScale) const {
70
0
    return Sub(x * aScale, y * aScale, z * aScale, w * aScale);
71
0
  }
72
  Sub operator/(T aScale) const {
73
    return Sub(x / aScale, y / aScale, z / aScale, w / aScale);
74
  }
75
76
  Sub& operator*=(T aScale) {
77
    x *= aScale;
78
    y *= aScale;
79
    z *= aScale;
80
    w *= aScale;
81
    return *static_cast<Sub*>(this);
82
  }
83
84
0
  Sub& operator/=(T aScale) {
85
0
    x /= aScale;
86
0
    y /= aScale;
87
0
    z /= aScale;
88
0
    w /= aScale;
89
0
    return *static_cast<Sub*>(this);
90
0
  }
91
92
  Sub operator-() const {
93
    return Sub(-x, -y, -z, -w);
94
  }
95
96
  T& operator[](int aIndex) {
97
    MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index");
98
    return *((&x)+aIndex);
99
  }
100
101
  const T& operator[](int aIndex) const {
102
    MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index");
103
    return *((&x)+aIndex);
104
  }
105
106
0
  T DotProduct(const Sub& aPoint) const {
107
0
    return x * aPoint.x + y * aPoint.y + z * aPoint.z + w * aPoint.w;
108
0
  }
109
110
  // Ignores the 4th component!
111
  Sub CrossProduct(const Sub& aPoint) const {
112
      return Sub(y * aPoint.z - aPoint.y * z,
113
          z * aPoint.x - aPoint.z * x,
114
          x * aPoint.y - aPoint.x * y, 
115
          0);
116
  }
117
118
0
  T Length() const {
119
0
    return sqrt(x*x + y*y + z*z + w*w);
120
0
  }
121
122
0
  void Normalize() {
123
0
    *this /= Length();
124
0
  }
125
126
  bool HasPositiveWCoord() { return w > 0; }
127
};
128
129
} // namespace gfx
130
} // namespace mozilla
131
132
#endif /* MOZILLA_BASEPOINT4D_H_ */