Astra SDK  v2.1.3
Vector3f.hpp
1 // This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
2 // Copyright (c) 2015-2017 Orbbec 3D
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // Be excellent to each other.
17 #ifndef ASTRA_VECTOR3F_HPP
18 #define ASTRA_VECTOR3F_HPP
19 
20 #include <cmath>
21 #include "capi/astra_ctypes.h"
22 
23 namespace astra {
24 
29  struct Vector3f : public astra_vector3f_t
30  {
31  Vector3f()
32  {
33  this->x = 0.0;
34  this->y = 0.0;
35  this->z = 0.0;
36  }
37 
38  Vector3f(const astra_vector3f_t& v)
39  {
40  *this = v;
41  }
42 
43  Vector3f& operator=(const astra_vector3f_t& rhs)
44  {
45  ::astra_vector3f_t::x = rhs.x;
46  ::astra_vector3f_t::y = rhs.y;
47  ::astra_vector3f_t::z = rhs.z;
48 
49  return *this;
50  }
51 
52  Vector3f(float x, float y, float z)
53  {
54  this->x = x;
55  this->y = y;
56  this->z = z;
57  }
58 
59  inline operator ::astra_vector3f_t*() { return this; }
60  inline operator const ::astra_vector3f_t*() const { return this; }
61 
62  float length() const;
63  float length_squared() const;
64  float dot(const Vector3f& v) const;
65 
66  Vector3f cross(const Vector3f& v) const;
67 
68  static inline Vector3f zero();
69  static Vector3f normalize(Vector3f v);
70 
71  friend bool operator==(const Vector3f& lhs, const Vector3f& rhs);
72  friend bool operator!=(const Vector3f& lhs, const Vector3f& rhs);
73 
74  bool is_zero() const;
75 
76  Vector3f& operator+=(const Vector3f& rhs);
77  Vector3f& operator-=(const Vector3f& rhs);
78  Vector3f& operator*=(const float& rhs);
79  Vector3f& operator/=(const float& rhs);
80 
81  friend Vector3f operator+(const Vector3f& lhs, const Vector3f& rhs);
82  friend Vector3f operator-(const Vector3f& lhs, const Vector3f& rhs);
83  friend Vector3f operator*(const Vector3f& lhs, const float& rhs);
84  friend Vector3f operator*(const float& lhs, const Vector3f& rhs);
85  friend Vector3f operator/(const Vector3f& lhs, const float& rhs);
86 
87  Vector3f operator-();
88  };
89 
90  inline float Vector3f::length() const
91  {
92  return std::sqrt(x * x + y * y + z * z);
93  }
94 
95  inline float Vector3f::length_squared() const
96  {
97  return x * x + y * y + z * z;
98  }
99 
100  inline float Vector3f::dot(const Vector3f& v) const
101  {
102  return x * v.x + y * v.y + z * v.z;
103  }
104 
105  inline Vector3f Vector3f::cross(const Vector3f& v) const
106  {
107  return Vector3f(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
108  }
109 
110  inline Vector3f Vector3f::normalize(Vector3f v)
111  {
112  double length = std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
113  if (length < 1e-9)
114  {
115  return Vector3f(0.0f, 0.0f, 0.0f);
116  }
117  else
118  {
119  return Vector3f(
120  static_cast<float>(v.x / length),
121  static_cast<float>(v.y / length),
122  static_cast<float>(v.z / length));
123  }
124  }
125 
126  inline Vector3f Vector3f::zero()
127  {
128  static Vector3f zero;
129  return zero;
130  }
131 
132  inline bool Vector3f::is_zero() const
133  {
134  return *this == zero();
135  }
136 
137  inline Vector3f& Vector3f::operator+=(const Vector3f& rhs)
138  {
139  this->x = this->x + rhs.x;
140  this->y = this->y + rhs.y;
141  this->z = this->z + rhs.z;
142  return *this;
143  }
144 
145  inline Vector3f& Vector3f::operator-=(const Vector3f& rhs)
146  {
147  this->x = this->x - rhs.x;
148  this->y = this->y - rhs.y;
149  this->z = this->z - rhs.z;
150  return *this;
151  }
152 
153  inline Vector3f& Vector3f::operator*=(const float& rhs)
154  {
155  this->x = this->x * rhs;
156  this->y = this->y * rhs;
157  this->z = this->z * rhs;
158  return *this;
159  }
160 
161  inline Vector3f& Vector3f::operator/=(const float& rhs)
162  {
163  this->x = this->x / rhs;
164  this->y = this->y / rhs;
165  this->z = this->z / rhs;
166  return *this;
167  }
168 
169  inline Vector3f Vector3f::operator-()
170  {
171  return Vector3f(-this->x, -this->y, -this->z);
172  }
173 
174  inline bool operator==(const Vector3f& lhs, const Vector3f& rhs)
175  {
176  return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
177  }
178 
179  inline bool operator!=(const Vector3f& lhs, const Vector3f& rhs)
180  {
181  return !(lhs == rhs);
182  }
183 
184  inline Vector3f operator+(const Vector3f& lhs, const Vector3f& rhs)
185  {
186  return Vector3f(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
187  }
188 
189  inline Vector3f operator-(const Vector3f& lhs, const Vector3f& rhs)
190  {
191  return Vector3f(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
192  }
193 
194  inline Vector3f operator*(const Vector3f& lhs, const float& rhs)
195  {
196  return Vector3f(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
197  }
198 
199  inline Vector3f operator*(const float& lhs, const Vector3f& rhs)
200  {
201  return rhs * lhs;
202  }
203 
204  inline Vector3f operator/(const Vector3f& lhs, const float& rhs)
205  {
206  return Vector3f(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs);
207  }
208 }
209 
210 #endif /* ASTRA_VECTOR3F_HPP */
bool operator!=(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode not equal
Definition: Image.hpp:247
bool operator==(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode equal
Definition: Image.hpp:230
Represents a float 3d vector
Definition: Vector3f.hpp:30
Definition: astra_ctypes.h:32