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