/* * 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::value || std::is_same::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 inline vec3 operator+(const vec3& a, const vec3& b) { return vec3(a.x + b.x, a.y + b.y, a.z + b.z); } template inline vec3 operator-(const vec3& a, const vec3& b) { return vec3(a.x - b.x, a.y - b.y, a.z - b.z); } template inline vec3 operator/(const vec3& a, const T b) { if (b == 0.0) { AT_ERROR( "denominator in vec3 division is 0"); // prevent divide by 0 errors. } return vec3(a.x / b, a.y / b, a.z / b); } template inline vec3 operator*(const T a, const vec3& b) { return vec3(a * b.x, a * b.y, a * b.z); } template inline vec3 operator*(const vec3& a, const vec3& b) { return vec3(a.x * b.x, a.y * b.y, a.z * b.z); } template inline T dot(const vec3& a, const vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } template inline vec3 cross(const vec3& a, const vec3& b) { return vec3( 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 inline T norm(const vec3& a) { return sqrt(dot(a, a)); } template std::ostream& operator<<(std::ostream& os, const vec3& v) { os << "vec3(" << v.x << ", " << v.y << ", " << v.z << ")"; return os; }