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