Hash :
a971e5b4
Author :
Date :
2024-02-28T17:13:38
Account for zero vector axes in Mat4::Rotate(...) When the axis passed in to the Rotate function is (0, 0, 0), normalizing that vector will result in NaN values. Prevent this by returning an identity matrix and early out instead. Bug: angleproject:2306 Tests: MatrixBuiltinsTest.RotateAxisZero Change-Id: I65fd0b9944885daf56a4a35201d424e7f0aa9ba6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5333834 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// vector_utils.h: Utility classes implementing various vector operations
#ifndef COMMON_VECTOR_UTILS_H_
#define COMMON_VECTOR_UTILS_H_
#include <cmath>
#include <cstddef>
#include <ostream>
#include <type_traits>
#include "common/debug.h"
namespace angle
{
template <size_t Dimension, typename Type>
class Vector;
using Vector2 = Vector<2, float>;
using Vector3 = Vector<3, float>;
using Vector4 = Vector<4, float>;
using Vector2I = Vector<2, int>;
using Vector3I = Vector<3, int>;
using Vector4I = Vector<4, int>;
using Vector2U = Vector<2, unsigned int>;
using Vector3U = Vector<3, unsigned int>;
using Vector4U = Vector<4, unsigned int>;
template <size_t Dimension, typename Type>
class VectorBase
{
public:
using VectorN = Vector<Dimension, Type>;
// Constructors
VectorBase() = default;
explicit VectorBase(Type element);
template <typename Type2>
VectorBase(const VectorBase<Dimension, Type2> &other);
template <typename Arg1, typename Arg2, typename... Args>
VectorBase(const Arg1 &arg1, const Arg2 &arg2, const Args &...args);
// Access the vector backing storage directly
const Type *data() const { return mData; }
Type *data() { return mData; }
constexpr size_t size() const { return Dimension; }
// Load or store the pointer from / to raw data
static VectorN Load(const Type *source);
static void Store(const VectorN &source, Type *destination);
// Index the vector
Type &operator[](size_t i) { return mData[i]; }
const Type &operator[](size_t i) const { return mData[i]; }
// Basic arithmetic operations
VectorN operator+() const;
VectorN operator-() const;
VectorN operator+(const VectorN &other) const;
VectorN operator-(const VectorN &other) const;
VectorN operator*(const VectorN &other) const;
VectorN operator/(const VectorN &other) const;
VectorN operator*(Type other) const;
VectorN operator/(Type other) const;
friend VectorN operator*(Type a, const VectorN &b) { return b * a; }
// Compound arithmetic operations
VectorN &operator+=(const VectorN &other);
VectorN &operator-=(const VectorN &other);
VectorN &operator*=(const VectorN &other);
VectorN &operator/=(const VectorN &other);
VectorN &operator*=(Type other);
VectorN &operator/=(Type other);
// Comparison operators
bool operator==(const VectorBase<Dimension, Type> &other) const;
bool operator!=(const VectorBase<Dimension, Type> &other) const;
// Other arithmetic operations
Type length() const;
Type lengthSquared() const;
Type dot(const VectorBase<Dimension, Type> &other) const;
VectorN normalized() const;
protected:
template <size_t CurrentIndex, size_t OtherDimension, typename OtherType, typename... Args>
void initWithList(const Vector<OtherDimension, OtherType> &arg1, const Args &...args);
// Some old compilers consider this function an alternative for initWithList(Vector)
// when the variant above is more precise. Use SFINAE on the return value to hide
// this variant for non-arithmetic types. The return value is still void.
template <size_t CurrentIndex, typename OtherType, typename... Args>
typename std::enable_if<std::is_arithmetic<OtherType>::value>::type initWithList(
OtherType arg1,
const Args &...args);
template <size_t CurrentIndex>
void initWithList() const;
template <size_t Dimension2, typename Type2>
friend class VectorBase;
Type mData[Dimension];
};
template <size_t Dimension, typename Type>
std::ostream &operator<<(std::ostream &ostream, const VectorBase<Dimension, Type> &vector);
template <typename Type>
class Vector<2, Type> : public VectorBase<2, Type>
{
public:
// Import the constructors defined in VectorBase
using VectorBase<2, Type>::VectorBase;
// Element shorthands
Type &x() { return this->mData[0]; }
Type &y() { return this->mData[1]; }
const Type &x() const { return this->mData[0]; }
const Type &y() const { return this->mData[1]; }
};
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<2, Type> &vector);
template <typename Type>
class Vector<3, Type> : public VectorBase<3, Type>
{
public:
// Import the constructors defined in VectorBase
using VectorBase<3, Type>::VectorBase;
// Additional operations
Vector<3, Type> cross(const Vector<3, Type> &other) const;
// Element shorthands
Type &x() { return this->mData[0]; }
Type &y() { return this->mData[1]; }
Type &z() { return this->mData[2]; }
const Type &x() const { return this->mData[0]; }
const Type &y() const { return this->mData[1]; }
const Type &z() const { return this->mData[2]; }
};
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<3, Type> &vector);
template <typename Type>
class Vector<4, Type> : public VectorBase<4, Type>
{
public:
// Import the constructors defined in VectorBase
using VectorBase<4, Type>::VectorBase;
// Element shorthands
Type &x() { return this->mData[0]; }
Type &y() { return this->mData[1]; }
Type &z() { return this->mData[2]; }
Type &w() { return this->mData[3]; }
const Type &x() const { return this->mData[0]; }
const Type &y() const { return this->mData[1]; }
const Type &z() const { return this->mData[2]; }
const Type &w() const { return this->mData[3]; }
};
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<4, Type> &vector);
// Implementation of constructors and misc operations
template <size_t Dimension, typename Type>
VectorBase<Dimension, Type>::VectorBase(Type element)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] = element;
}
}
template <size_t Dimension, typename Type>
template <typename Type2>
VectorBase<Dimension, Type>::VectorBase(const VectorBase<Dimension, Type2> &other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] = static_cast<Type>(other.mData[i]);
}
}
// Ideally we would like to have only two constructors:
// - a scalar constructor that takes Type as a parameter
// - a compound constructor
// However if we define the compound constructor for when it has a single arguments, then calling
// Vector2(0.0) will be ambiguous. To solve this we take advantage of there being a single compound
// constructor with a single argument, which is the copy constructor. We end up with three
// constructors:
// - the scalar constructor
// - the copy constructor
// - the compound constructor for two or more arguments, hence the arg1, and arg2 here.
template <size_t Dimension, typename Type>
template <typename Arg1, typename Arg2, typename... Args>
VectorBase<Dimension, Type>::VectorBase(const Arg1 &arg1, const Arg2 &arg2, const Args &...args)
{
initWithList<0>(arg1, arg2, args...);
}
template <size_t Dimension, typename Type>
template <size_t CurrentIndex, size_t OtherDimension, typename OtherType, typename... Args>
void VectorBase<Dimension, Type>::initWithList(const Vector<OtherDimension, OtherType> &arg1,
const Args &...args)
{
static_assert(CurrentIndex + OtherDimension <= Dimension,
"Too much data in the vector constructor.");
for (size_t i = 0; i < OtherDimension; ++i)
{
mData[CurrentIndex + i] = static_cast<Type>(arg1.mData[i]);
}
initWithList<CurrentIndex + OtherDimension>(args...);
}
template <size_t Dimension, typename Type>
template <size_t CurrentIndex, typename OtherType, typename... Args>
typename std::enable_if<std::is_arithmetic<OtherType>::value>::type
VectorBase<Dimension, Type>::initWithList(OtherType arg1, const Args &...args)
{
static_assert(CurrentIndex + 1 <= Dimension, "Too much data in the vector constructor.");
mData[CurrentIndex] = static_cast<Type>(arg1);
initWithList<CurrentIndex + 1>(args...);
}
template <size_t Dimension, typename Type>
template <size_t CurrentIndex>
void VectorBase<Dimension, Type>::initWithList() const
{
static_assert(CurrentIndex == Dimension, "Not enough data in the vector constructor.");
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::Load(const Type *source)
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = source[i];
}
return result;
}
template <size_t Dimension, typename Type>
void VectorBase<Dimension, Type>::Store(const Vector<Dimension, Type> &source, Type *destination)
{
for (size_t i = 0; i < Dimension; ++i)
{
destination[i] = source.mData[i];
}
}
// Implementation of basic arithmetic operations
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator+() const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = +mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator-() const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = -mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator+(
const Vector<Dimension, Type> &other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] + other.mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator-(
const Vector<Dimension, Type> &other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] - other.mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator*(
const Vector<Dimension, Type> &other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] * other.mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator/(
const Vector<Dimension, Type> &other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] / other.mData[i];
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator*(Type other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] * other;
}
return result;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::operator/(Type other) const
{
Vector<Dimension, Type> result;
for (size_t i = 0; i < Dimension; ++i)
{
result.mData[i] = mData[i] / other;
}
return result;
}
// Implementation of compound arithmetic operations
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator+=(
const Vector<Dimension, Type> &other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] += other.mData[i];
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator-=(
const Vector<Dimension, Type> &other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] -= other.mData[i];
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator*=(
const Vector<Dimension, Type> &other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] *= other.mData[i];
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator/=(
const Vector<Dimension, Type> &other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] /= other.mData[i];
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator*=(Type other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] *= other;
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> &VectorBase<Dimension, Type>::operator/=(Type other)
{
for (size_t i = 0; i < Dimension; ++i)
{
mData[i] /= other;
}
return *static_cast<Vector<Dimension, Type> *>(this);
}
// Implementation of comparison operators
template <size_t Dimension, typename Type>
bool VectorBase<Dimension, Type>::operator==(const VectorBase<Dimension, Type> &other) const
{
for (size_t i = 0; i < Dimension; ++i)
{
if (mData[i] != other.mData[i])
{
return false;
}
}
return true;
}
template <size_t Dimension, typename Type>
bool VectorBase<Dimension, Type>::operator!=(const VectorBase<Dimension, Type> &other) const
{
return !(*this == other);
}
// Implementation of other arithmetic operations
template <size_t Dimension, typename Type>
Type VectorBase<Dimension, Type>::length() const
{
static_assert(std::is_floating_point<Type>::value,
"VectorN::length is only defined for floating point vectors");
return std::sqrt(lengthSquared());
}
template <size_t Dimension, typename Type>
Type VectorBase<Dimension, Type>::lengthSquared() const
{
return dot(*this);
}
template <size_t Dimension, typename Type>
Type VectorBase<Dimension, Type>::dot(const VectorBase<Dimension, Type> &other) const
{
Type sum = Type();
for (size_t i = 0; i < Dimension; ++i)
{
sum += mData[i] * other.mData[i];
}
return sum;
}
template <size_t Dimension, typename Type>
std::ostream &operator<<(std::ostream &ostream, const VectorBase<Dimension, Type> &vector)
{
ostream << "[ ";
for (size_t elementIdx = 0; elementIdx < Dimension; elementIdx++)
{
if (elementIdx > 0)
{
ostream << ", ";
}
ostream << vector.data()[elementIdx];
}
ostream << " ]";
return ostream;
}
template <size_t Dimension, typename Type>
Vector<Dimension, Type> VectorBase<Dimension, Type>::normalized() const
{
static_assert(std::is_floating_point<Type>::value,
"VectorN::normalized is only defined for floating point vectors");
ASSERT(length() != Type());
return *this / length();
}
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<2, Type> &vector)
{
return ostream << static_cast<const VectorBase<2, Type> &>(vector);
}
template <typename Type>
Vector<3, Type> Vector<3, Type>::cross(const Vector<3, Type> &other) const
{
return Vector<3, Type>(y() * other.z() - z() * other.y(), z() * other.x() - x() * other.z(),
x() * other.y() - y() * other.x());
}
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<3, Type> &vector)
{
return ostream << static_cast<const VectorBase<3, Type> &>(vector);
}
template <typename Type>
std::ostream &operator<<(std::ostream &ostream, const Vector<4, Type> &vector)
{
return ostream << static_cast<const VectorBase<4, Type> &>(vector);
}
} // namespace angle
#endif // COMMON_VECTOR_UTILS_H_