Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/gfx/BasePoint3D.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_BASEPOINT3D_H_
8
#define MOZILLA_BASEPOINT3D_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 BasePoint3D {
22
  union {
23
    struct {
24
      T x, y, z;
25
    };
26
    T components[3];
27
  };
28
29
  // Constructors
30
0
  BasePoint3D() : x(0), y(0), z(0) {}
31
0
  BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
32
33
  void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; }
34
  void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; }
35
36
  // Note that '=' isn't defined so we'll get the
37
  // compiler generated default assignment operator
38
39
0
  T& operator[](int aIndex) {
40
0
    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
41
0
    return *((&x)+aIndex);
42
0
  }
43
44
  const T& operator[](int aIndex) const {
45
    MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
46
    return *((&x)+aIndex);
47
  }
48
49
0
  bool operator==(const Sub& aPoint) const {
50
0
    return x == aPoint.x && y == aPoint.y && z == aPoint.z;
51
0
  }
52
0
  bool operator!=(const Sub& aPoint) const {
53
0
    return x != aPoint.x || y != aPoint.y || z != aPoint.z;
54
0
  }
55
56
0
  Sub operator+(const Sub& aPoint) const {
57
0
    return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z);
58
0
  }
59
0
  Sub operator-(const Sub& aPoint) const {
60
0
    return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z);
61
0
  }
62
  Sub& operator+=(const Sub& aPoint) {
63
    x += aPoint.x;
64
    y += aPoint.y;
65
    z += aPoint.z;
66
    return *static_cast<Sub*>(this);
67
  }
68
  Sub& operator-=(const Sub& aPoint) {
69
    x -= aPoint.x;
70
    y -= aPoint.y;
71
    z -= aPoint.z;
72
    return *static_cast<Sub*>(this);
73
  }
74
75
0
  Sub operator*(T aScale) const {
76
0
    return Sub(x * aScale, y * aScale, z * aScale);
77
0
  }
78
  Sub operator/(T aScale) const {
79
    return Sub(x / aScale, y / aScale, z / aScale);
80
  }
81
82
0
  Sub& operator*=(T aScale) {
83
0
    x *= aScale;
84
0
    y *= aScale;
85
0
    z *= aScale;
86
0
    return *static_cast<Sub*>(this);
87
0
  }
88
89
0
  Sub& operator/=(T aScale) {
90
0
    x /= aScale;
91
0
    y /= aScale;
92
0
    z /= aScale;
93
0
    return *static_cast<Sub*>(this);
94
0
  }
95
96
0
  Sub operator-() const {
97
0
    return Sub(-x, -y, -z);
98
0
  }
99
100
0
  Sub CrossProduct(const Sub& aPoint) const {
101
0
    return Sub(y * aPoint.z - aPoint.y * z,
102
0
               z * aPoint.x - aPoint.z * x,
103
0
               x * aPoint.y - aPoint.x * y);
104
0
  }
105
106
0
  T DotProduct(const Sub& aPoint) const {
107
0
    return x * aPoint.x + y * aPoint.y + z * aPoint.z;
108
0
  }
109
110
0
  T Length() const {
111
0
    return sqrt(x*x + y*y + z*z);
112
0
  }
113
114
  // Invalid for points with distance from origin of 0.
115
0
  void Normalize() {
116
0
    *this /= Length();
117
0
  }
118
119
0
  void RobustNormalize() {
120
0
    // If the distance is infinite, we scale it by 1/(the maximum value of T)
121
0
    // before doing normalization, so we can avoid getting a zero point.
122
0
    T length = Length();
123
0
    if (mozilla::IsInfinite(length)) {
124
0
      *this /= std::numeric_limits<T>::max();
125
0
      length = Length();
126
0
    }
127
0
128
0
    *this /= length;
129
0
  }
130
131
  friend std::ostream& operator<<(std::ostream& stream, const BasePoint3D<T, Sub>& aPoint) {
132
    return stream << '(' << aPoint.x << ',' << aPoint.y << ',' << aPoint.z << ')';
133
  }
134
};
135
136
} // namespace gfx
137
} // namespace mozilla
138
139
#endif /* MOZILLA_BASEPOINT3D_H_ */