summaryrefslogtreecommitdiffstats
path: root/thirdparty/rvo2/rvo2_3d/Vector3.cc
blob: 95831d3c90a63e09f584963d0abf460b638f63fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Vector3.cc
 * RVO2-3D Library
 *
 * SPDX-FileCopyrightText: 2008 University of North Carolina at Chapel Hill
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Please send all bug reports to <geom@cs.unc.edu>.
 *
 * The authors may be contacted via:
 *
 * Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha
 * Dept. of Computer Science
 * 201 S. Columbia St.
 * Frederick P. Brooks, Jr. Computer Science Bldg.
 * Chapel Hill, N.C. 27599-3175
 * United States of America
 *
 * <https://gamma.cs.unc.edu/RVO2/>
 */

#include "Vector3.h"

#include <cmath>
#include <ostream>

namespace RVO3D {
Vector3::Vector3() : val_() {
  val_[0] = 0.0F;
  val_[1] = 0.0F;
  val_[2] = 0.0F;
}

Vector3::Vector3(const Vector3 &vector) : val_() {
  val_[0] = vector[0];
  val_[1] = vector[1];
  val_[2] = vector[2];
}

Vector3::Vector3(const float val[3]) : val_() {
  val_[0] = val[0];
  val_[1] = val[1];
  val_[2] = val[2];
}

Vector3::Vector3(float x, float y, float z) : val_() {
  val_[0] = x;
  val_[1] = y;
  val_[2] = z;
}

Vector3::~Vector3() {}

Vector3 &Vector3::operator=(const Vector3 &vector) {
  if (this != &vector) {
    val_[0] = vector[0];
    val_[1] = vector[1];
    val_[2] = vector[2];
  }

  return *this;
}

float Vector3::operator[](std::size_t i) const { return val_[i]; }

float &Vector3::operator[](std::size_t i) { return val_[i]; }

Vector3 Vector3::operator-() const {
  return Vector3(-val_[0], -val_[1], -val_[2]);
}

float Vector3::operator*(const Vector3 &vector) const {
  return val_[0] * vector[0] + val_[1] * vector[1] + val_[2] * vector[2];
}

Vector3 Vector3::operator*(float scalar) const {
  return Vector3(val_[0] * scalar, val_[1] * scalar, val_[2] * scalar);
}

Vector3 Vector3::operator/(float scalar) const {
  const float invScalar = 1.0F / scalar;

  return Vector3(val_[0] * invScalar, val_[1] * invScalar, val_[2] * invScalar);
}

Vector3 Vector3::operator+(const Vector3 &vector) const {
  return Vector3(val_[0] + vector[0], val_[1] + vector[1], val_[2] + vector[2]);
}

Vector3 Vector3::operator-(const Vector3 &vector) const {
  return Vector3(val_[0] - vector[0], val_[1] - vector[1], val_[2] - vector[2]);
}

bool Vector3::operator==(const Vector3 &vector) const {
  return val_[0] == vector[0] && val_[1] == vector[1] && val_[2] == vector[2];
}

bool Vector3::operator!=(const Vector3 &vector) const {
  return val_[0] != vector[0] || val_[1] != vector[1] || val_[2] != vector[2];
}

Vector3 &Vector3::operator*=(float scalar) {
  val_[0] *= scalar;
  val_[1] *= scalar;
  val_[2] *= scalar;

  return *this;
}

Vector3 &Vector3::operator/=(float scalar) {
  const float invScalar = 1.0F / scalar;

  val_[0] *= invScalar;
  val_[1] *= invScalar;
  val_[2] *= invScalar;

  return *this;
}

Vector3 &Vector3::operator+=(const Vector3 &vector) {
  val_[0] += vector[0];
  val_[1] += vector[1];
  val_[2] += vector[2];

  return *this;
}

Vector3 &Vector3::operator-=(const Vector3 &vector) {
  val_[0] -= vector[0];
  val_[1] -= vector[1];
  val_[2] -= vector[2];

  return *this;
}

Vector3 operator*(float scalar, const Vector3 &vector) {
  return Vector3(scalar * vector[0], scalar * vector[1], scalar * vector[2]);
}

std::ostream &operator<<(std::ostream &stream, const Vector3 &vector) {
  stream << "(" << vector[0] << "," << vector[1] << "," << vector[2] << ")";

  return stream;
}

float abs(const Vector3 &vector) { return std::sqrt(vector * vector); }

float absSq(const Vector3 &vector) { return vector * vector; }

Vector3 cross(const Vector3 &vector1, const Vector3 &vector2) {
  return Vector3(vector1[1] * vector2[2] - vector1[2] * vector2[1],
                 vector1[2] * vector2[0] - vector1[0] * vector2[2],
                 vector1[0] * vector2[1] - vector1[1] * vector2[0]);
}

Vector3 normalize(const Vector3 &vector) { return vector / abs(vector); }

} /* namespace RVO3D */