File size: 1,950 Bytes
7266be4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

#pragma once

// A fixed-sized vector with basic arithmetic operators useful for
// representing 3D coordinates.
// TODO: switch to Eigen if more functionality is needed.

template <
    typename T,
    typename = std::enable_if_t<
        std::is_same<T, double>::value || std::is_same<T, float>::value>>
struct vec3 {
  T x, y, z;
  typedef T scalar_t;
  vec3(T x, T y, T z) : x(x), y(y), z(z) {}
};

template <typename T>
inline vec3<T> operator+(const vec3<T>& a, const vec3<T>& b) {
  return vec3<T>(a.x + b.x, a.y + b.y, a.z + b.z);
}

template <typename T>
inline vec3<T> operator-(const vec3<T>& a, const vec3<T>& b) {
  return vec3<T>(a.x - b.x, a.y - b.y, a.z - b.z);
}

template <typename T>
inline vec3<T> operator/(const vec3<T>& a, const T b) {
  if (b == 0.0) {
    AT_ERROR(
        "denominator in vec3 division is 0"); // prevent divide by 0 errors.
  }
  return vec3<T>(a.x / b, a.y / b, a.z / b);
}

template <typename T>
inline vec3<T> operator*(const T a, const vec3<T>& b) {
  return vec3<T>(a * b.x, a * b.y, a * b.z);
}

template <typename T>
inline vec3<T> operator*(const vec3<T>& a, const vec3<T>& b) {
  return vec3<T>(a.x * b.x, a.y * b.y, a.z * b.z);
}

template <typename T>
inline T dot(const vec3<T>& a, const vec3<T>& b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

template <typename T>
inline vec3<T> cross(const vec3<T>& a, const vec3<T>& b) {
  return vec3<T>(
      a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}

template <typename T>
inline T norm(const vec3<T>& a) {
  return sqrt(dot(a, a));
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const vec3<T>& v) {
  os << "vec3(" << v.x << ", " << v.y << ", " << v.z << ")";
  return os;
}