Astra SDK  v2.1.3
Vector3i.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_VECTOR3I_HPP
18 #define ASTRA_VECTOR3I_HPP
19 
20 #include <cmath>
21 #include <cstdint>
22 #include "capi/astra_ctypes.h"
23 
24 namespace astra {
25 
30  struct Vector3i : public astra_vector3i_t
31  {
32  Vector3i()
33  {
34  this->x = 0;
35  this->y = 0;
36  this->z = 0;
37  }
38 
39  Vector3i(const astra_vector3i_t& v)
40  {
41  *this = v;
42  }
43 
44  Vector3i& operator=(const astra_vector3i_t& rhs)
45  {
46  ::astra_vector3i_t::x = rhs.x;
47  ::astra_vector3i_t::y = rhs.y;
48  ::astra_vector3i_t::z = rhs.z;
49 
50  return *this;
51  }
52 
53  Vector3i(int x, int y, int z)
54  {
55  this->x = x;
56  this->y = y;
57  this->z = z;
58  }
59 
60  inline operator ::astra_vector3i_t*() { return this; }
61  inline operator const ::astra_vector3i_t*() const { return this; }
62 
63  float length() const;
64  std::int32_t length_squared() const;
65  std::int32_t dot(const Vector3i& v) const;
66 
67  Vector3i cross(const Vector3i& v) const;
68 
69  static Vector3i normalize(const Vector3i& v);
70 
71  static inline const Vector3i& zero();
72 
73  friend bool operator==(const Vector3i& lhs, const Vector3i& rhs);
74  friend bool operator!=(const Vector3i& lhs, const Vector3i& rhs);
75 
76  bool is_zero() const;
77 
78  Vector3i& operator+=(const Vector3i& rhs);
79  Vector3i& operator-=(const Vector3i& rhs);
80  Vector3i& operator*=(const std::int32_t rhs);
81  Vector3i& operator/=(const std::int32_t rhs);
82 
83  friend Vector3i operator+(const Vector3i& lhs, const Vector3i& rhs);
84  friend Vector3i operator-(const Vector3i& lhs, const Vector3i& rhs);
85  friend Vector3i operator*(const Vector3i& lhs, const std::int32_t rhs);
86  friend Vector3i operator*(const float lhs, const Vector3i& rhs);
87  friend Vector3i operator/(const Vector3i& lhs, const std::int32_t rhs);
88 
89  Vector3i operator-();
90  };
91 
92  inline float Vector3i::length() const
93  {
94  return std::sqrt(static_cast<float>(x * x + y * y + z * z));
95  }
96 
97  inline std::int32_t Vector3i::length_squared() const
98  {
99  return x * x + y * y + z * z;
100  }
101 
102  inline std::int32_t Vector3i::dot(const Vector3i& v) const
103  {
104  return x * v.x + y * v.y + z * v.z;
105  }
106 
107  inline Vector3i Vector3i::cross(const Vector3i& v) const
108  {
109  return Vector3i(
110  static_cast<std::int32_t>(y*v.z - z*v.y),
111  static_cast<std::int32_t>(z*v.x - x*v.z),
112  static_cast<std::int32_t>(x*v.y - y*v.x));
113  }
114 
115  inline Vector3i Vector3i::normalize(const Vector3i& v)
116  {
117  double length = sqrt(
118  static_cast<float>(v.x*v.x + v.y*v.y + v.z*v.z));
119 
120  if (length < 1e-9)
121  {
122  return Vector3i(0, 0, 0);
123  }
124  else
125  {
126  return Vector3i(
127  static_cast<std::int32_t>(v.x / length),
128  static_cast<std::int32_t>(v.y / length),
129  static_cast<std::int32_t>(v.z / length));
130  }
131  }
132 
133  inline bool Vector3i::is_zero() const
134  {
135  Vector3i zero;
136  return *this == zero;
137  }
138 
139  inline Vector3i& Vector3i::operator+=(const Vector3i& rhs)
140  {
141  this->x = this->x + rhs.x;
142  this->y = this->y + rhs.y;
143  this->z = this->z + rhs.z;
144  return *this;
145  }
146 
147  inline Vector3i& Vector3i::operator-=(const Vector3i& rhs)
148  {
149  this->x = this->x - rhs.x;
150  this->y = this->y - rhs.y;
151  this->z = this->z - rhs.z;
152  return *this;
153  }
154 
155  inline Vector3i& Vector3i::operator*=(const std::int32_t rhs)
156  {
157  this->x = this->x * rhs;
158  this->y = this->y * rhs;
159  this->z = this->z * rhs;
160  return *this;
161  }
162 
163  inline Vector3i& Vector3i::operator/=(const std::int32_t rhs)
164  {
165  this->x = static_cast<std::int32_t>(this->x / rhs);
166  this->y = static_cast<std::int32_t>(this->y / rhs);
167  this->z = static_cast<std::int32_t>(this->z / rhs);
168  return *this;
169  }
170 
171  inline Vector3i Vector3i::operator-()
172  {
173  return Vector3i(-this->x, -this->y, -this->z);
174  }
175 
176  inline bool operator==(const Vector3i& lhs, const Vector3i& rhs)
177  {
178  return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
179  }
180 
181  inline bool operator!=(const Vector3i& lhs, const Vector3i& rhs)
182  {
183  return !(lhs == rhs);
184  }
185 
186  inline Vector3i operator+(const Vector3i& lhs, const Vector3i& rhs)
187  {
188  return Vector3i(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
189  }
190 
191  inline Vector3i operator-(const Vector3i& lhs, const Vector3i& rhs)
192  {
193  return Vector3i(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
194  }
195 
196  inline Vector3i operator*(const Vector3i& lhs, const std::int32_t rhs)
197  {
198  return Vector3i(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
199  }
200 
201  inline Vector3i operator*(const std::int32_t lhs, const Vector3i& rhs)
202  {
203  return rhs * lhs;
204  }
205 
206  inline Vector3i operator/(const Vector3i& lhs, const std::int32_t rhs)
207  {
208  return Vector3i(
209  static_cast<std::int32_t>(lhs.x / rhs),
210  static_cast<std::int32_t>(lhs.y / rhs),
211  static_cast<std::int32_t>(lhs.z / rhs));
212  }
213 
214  inline const Vector3i& Vector3i::zero()
215  {
216  static Vector3i zero;
217  return zero;
218  }
219 }
220 
221 #endif /* ASTRA_VECTOR3I_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: Vector3i.hpp:31
Definition: astra_ctypes.h:38