ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Vec3.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // system
11 #include <cmath>
12 
13 class Vec3 // a minimal vector class of 3 doubles and overloaded math operators
14 {
15 public:
16  union {
17  struct {
18  double x, y, z;
19  };
20  double f[3];
21  };
22 
23  Vec3(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
24 
25  Vec3() : x(0), y(0), z(0) {}
26 
27  Vec3(const Vec3 &v) : x(v.x), y(v.y), z(v.z) {}
28 
29  inline double length() const { return sqrt(x * x + y * y + z * z); }
30 
31  inline Vec3 normalized() const {
32  double l = length();
33  return Vec3(x / l, y / l, z / l);
34  }
35 
36  inline void operator+=(const Vec3 &v) {
37  x += v.x;
38  y += v.y;
39  z += v.z;
40  }
41 
42  inline Vec3 operator/(const double &a) const {
43  return Vec3(x / a, y / a, z / a);
44  }
45 
46  inline Vec3 operator-(const Vec3 &v) const {
47  return Vec3(x - v.x, y - v.y, z - v.z);
48  }
49 
50  inline Vec3 operator+(const Vec3 &v) const {
51  return Vec3(x + v.x, y + v.y, z + v.z);
52  }
53 
54  inline Vec3 operator*(const double &a) const {
55  return Vec3(x * a, y * a, z * a);
56  }
57 
58  inline Vec3 operator-() const { return Vec3(-x, -y, -z); }
59 
60  inline Vec3 cross(const Vec3 &v) const {
61  return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
62  }
63 
64  inline double dot(const Vec3 &v) const {
65  return x * v.x + y * v.y + z * v.z;
66  }
67 };
Definition: Vec3.h:14
double z
Definition: Vec3.h:18
Vec3(const Vec3 &v)
Definition: Vec3.h:27
Vec3 operator*(const double &a) const
Definition: Vec3.h:54
double y
Definition: Vec3.h:18
Vec3 cross(const Vec3 &v) const
Definition: Vec3.h:60
void operator+=(const Vec3 &v)
Definition: Vec3.h:36
Vec3 operator/(const double &a) const
Definition: Vec3.h:42
Vec3(double _x, double _y, double _z)
Definition: Vec3.h:23
double dot(const Vec3 &v) const
Definition: Vec3.h:64
Vec3()
Definition: Vec3.h:25
Vec3 operator-() const
Definition: Vec3.h:58
Vec3 operator-(const Vec3 &v) const
Definition: Vec3.h:46
double length() const
Definition: Vec3.h:29
double x
Definition: Vec3.h:18
Vec3 normalized() const
Definition: Vec3.h:31
Vec3 operator+(const Vec3 &v) const
Definition: Vec3.h:50
double f[3]
Definition: Vec3.h:20