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 namespace mcl_3dl
34 {
35 class Vec3
36 {
37 public:
38  float x_;
39  float y_;
40  float z_;
41  Vec3(const float x, const float y, const float z)
42  {
43  x_ = x;
44  y_ = y;
45  z_ = z;
46  }
47  Vec3()
48  {
49  x_ = y_ = z_ = 0.0;
50  }
51  float& operator[](const size_t i)
52  {
53  switch (i)
54  {
55  case 0:
56  return x_;
57  break;
58  case 1:
59  return y_;
60  break;
61  case 2:
62  return z_;
63  break;
64  default:
65  break;
66  }
67  return x_;
68  }
69  float operator[](const size_t i) const
70  {
71  switch (i)
72  {
73  case 0:
74  return x_;
75  break;
76  case 1:
77  return y_;
78  break;
79  case 2:
80  return z_;
81  break;
82  default:
83  break;
84  }
85  return x_;
86  }
87  bool operator==(const Vec3& q) const
88  {
89  return x_ == q.x_ && y_ == q.y_ && z_ == q.z_;
90  }
91  bool operator!=(const Vec3& q) const
92  {
93  return !operator==(q);
94  }
95  Vec3 operator+(const Vec3& q) const
96  {
97  return Vec3(x_ + q.x_, y_ + q.y_, z_ + q.z_);
98  }
99  Vec3 operator-(const Vec3& q) const
100  {
101  return Vec3(x_ - q.x_, y_ - q.y_, z_ - q.z_);
102  }
103  Vec3 operator-() const
104  {
105  return Vec3(-x_, -y_, -z_);
106  }
107  Vec3 operator*(const float& s) const
108  {
109  return Vec3(x_ * s, y_ * s, z_ * s);
110  }
111  Vec3 operator/(const float& s) const
112  {
113  return Vec3(x_ / s, y_ / s, z_ / s);
114  }
115  Vec3& operator+=(const Vec3& q)
116  {
117  *this = *this + q;
118  return *this;
119  }
120  Vec3& operator-=(const Vec3& q)
121  {
122  *this = *this - q;
123  return *this;
124  }
125  Vec3& operator*=(const float& s)
126  {
127  *this = *this * s;
128  return *this;
129  }
130  Vec3& operator/=(const float& s)
131  {
132  *this = *this / s;
133  return *this;
134  }
135  float dot(const Vec3& q) const
136  {
137  return x_ * q.x_ + y_ * q.y_ + z_ * q.z_;
138  }
139  Vec3 cross(const Vec3& q) const
140  {
141  return Vec3(y_ * q.z_ - z_ * q.y_,
142  z_ * q.x_ - x_ * q.z_,
143  x_ * q.y_ - y_ * q.x_);
144  }
145  Vec3 times(const Vec3& q) const
146  {
147  return Vec3(x_ * q.x_, y_ * q.y_, z_ * q.z_);
148  }
149  float norm() const
150  {
151  return sqrtf(dot(*this));
152  }
153  Vec3 normalized() const
154  {
155  return *this / norm();
156  }
157 };
158 } // namespace mcl_3dl
159 
160 #endif // MCL_3DL_VEC3_H
float z_
Definition: vec3.h:40
Vec3 & operator+=(const Vec3 &q)
Definition: vec3.h:115
Vec3(const float x, const float y, const float z)
Definition: vec3.h:41
float dot(const Vec3 &q) const
Definition: vec3.h:135
bool operator!=(const Vec3 &q) const
Definition: vec3.h:91
Vec3 operator-() const
Definition: vec3.h:103
float norm() const
Definition: vec3.h:149
Vec3 times(const Vec3 &q) const
Definition: vec3.h:145
Vec3 operator/(const float &s) const
Definition: vec3.h:111
Vec3 cross(const Vec3 &q) const
Definition: vec3.h:139
Vec3 & operator*=(const float &s)
Definition: vec3.h:125
float y_
Definition: vec3.h:39
float operator[](const size_t i) const
Definition: vec3.h:69
bool operator==(const Vec3 &q) const
Definition: vec3.h:87
float & operator[](const size_t i)
Definition: vec3.h:51
float x_
Definition: vec3.h:38
Vec3 normalized() const
Definition: vec3.h:153
Vec3 operator-(const Vec3 &q) const
Definition: vec3.h:99
Vec3 & operator-=(const Vec3 &q)
Definition: vec3.h:120
Vec3 & operator/=(const float &s)
Definition: vec3.h:130
Vec3 operator+(const Vec3 &q) const
Definition: vec3.h:95
Vec3 operator*(const float &s) const
Definition: vec3.h:107


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36