vec3.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef MCL_3DL_VEC3_H
31 #define MCL_3DL_VEC3_H
32 
33 #include <cmath>
34 
35 namespace mcl_3dl
36 {
37 class Vec3
38 {
39 public:
40  float x_;
41  float y_;
42  float z_;
43  inline constexpr Vec3(const float x, const float y, const float z)
44  : x_(x)
45  , y_(y)
46  , z_(z)
47  {
48  }
49  inline constexpr Vec3()
50  : x_(0)
51  , y_(0)
52  , z_(0)
53  {
54  }
55  inline float& operator[](const size_t i)
56  {
57  switch (i)
58  {
59  case 0:
60  return x_;
61  break;
62  case 1:
63  return y_;
64  break;
65  case 2:
66  return z_;
67  break;
68  default:
69  break;
70  }
71  return x_;
72  }
73  inline float operator[](const size_t i) const
74  {
75  switch (i)
76  {
77  case 0:
78  return x_;
79  break;
80  case 1:
81  return y_;
82  break;
83  case 2:
84  return z_;
85  break;
86  default:
87  break;
88  }
89  return x_;
90  }
91  inline constexpr bool operator==(const Vec3& q) const
92  {
93  return x_ == q.x_ && y_ == q.y_ && z_ == q.z_;
94  }
95  inline constexpr bool operator!=(const Vec3& q) const
96  {
97  return !operator==(q);
98  }
99  inline constexpr Vec3 operator+(const Vec3& q) const
100  {
101  return Vec3(x_ + q.x_, y_ + q.y_, z_ + q.z_);
102  }
103  inline constexpr Vec3 operator-(const Vec3& q) const
104  {
105  return Vec3(x_ - q.x_, y_ - q.y_, z_ - q.z_);
106  }
107  inline constexpr Vec3 operator-() const
108  {
109  return Vec3(-x_, -y_, -z_);
110  }
111  inline constexpr Vec3 operator*(const float s) const
112  {
113  return Vec3(x_ * s, y_ * s, z_ * s);
114  }
115  inline constexpr Vec3 operator/(const float s) const
116  {
117  return Vec3(x_ / s, y_ / s, z_ / s);
118  }
119  inline Vec3& operator+=(const Vec3& q)
120  {
121  *this = *this + q;
122  return *this;
123  }
124  inline Vec3& operator-=(const Vec3& q)
125  {
126  *this = *this - q;
127  return *this;
128  }
129  inline Vec3& operator*=(const float& s)
130  {
131  *this = *this * s;
132  return *this;
133  }
134  inline Vec3& operator/=(const float& s)
135  {
136  *this = *this / s;
137  return *this;
138  }
139  inline constexpr float dot(const Vec3& q) const
140  {
141  return x_ * q.x_ + y_ * q.y_ + z_ * q.z_;
142  }
143  inline constexpr Vec3 cross(const Vec3& q) const
144  {
145  return Vec3(y_ * q.z_ - z_ * q.y_,
146  z_ * q.x_ - x_ * q.z_,
147  x_ * q.y_ - y_ * q.x_);
148  }
149  inline constexpr Vec3 times(const Vec3& q) const
150  {
151  return Vec3(x_ * q.x_, y_ * q.y_, z_ * q.z_);
152  }
153  inline float norm() const
154  {
155  return std::sqrt(dot(*this));
156  }
157  inline Vec3 normalized() const
158  {
159  return *this / norm();
160  }
161 };
162 } // namespace mcl_3dl
163 
164 #endif // MCL_3DL_VEC3_H
float z_
Definition: vec3.h:42
Vec3 & operator+=(const Vec3 &q)
Definition: vec3.h:119
constexpr Vec3()
Definition: vec3.h:49
constexpr float dot(const Vec3 &q) const
Definition: vec3.h:139
float norm() const
Definition: vec3.h:153
constexpr Vec3 operator*(const float s) const
Definition: vec3.h:111
constexpr bool operator!=(const Vec3 &q) const
Definition: vec3.h:95
Vec3 & operator*=(const float &s)
Definition: vec3.h:129
float y_
Definition: vec3.h:41
float operator[](const size_t i) const
Definition: vec3.h:73
constexpr Vec3 operator-(const Vec3 &q) const
Definition: vec3.h:103
constexpr Vec3(const float x, const float y, const float z)
Definition: vec3.h:43
constexpr Vec3 times(const Vec3 &q) const
Definition: vec3.h:149
float & operator[](const size_t i)
Definition: vec3.h:55
constexpr Vec3 operator/(const float s) const
Definition: vec3.h:115
float x_
Definition: vec3.h:40
Vec3 normalized() const
Definition: vec3.h:157
constexpr Vec3 cross(const Vec3 &q) const
Definition: vec3.h:143
Vec3 & operator-=(const Vec3 &q)
Definition: vec3.h:124
Vec3 & operator/=(const float &s)
Definition: vec3.h:134
constexpr bool operator==(const Vec3 &q) const
Definition: vec3.h:91
constexpr Vec3 operator-() const
Definition: vec3.h:107
constexpr Vec3 operator+(const Vec3 &q) const
Definition: vec3.h:99


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29