/usr/include/OGRE/OgreVector2.h is in libogre-1.9-dev 1.9.0+dfsg1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | /*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __Vector2_H__
#define __Vector2_H__
#include "OgrePrerequisites.h"
#include "OgreMath.h"
namespace Ogre
{
/** \addtogroup Core
* @{
*/
/** \addtogroup Math
* @{
*/
/** Standard 2-dimensional vector.
@remarks
A direction in 2D space represented as distances along the 2
orthogonal axes (x, y). Note that positions, directions and
scaling factors can be represented by a vector, depending on how
you interpret the values.
*/
class _OgreExport Vector2
{
public:
Real x, y;
public:
/** Default constructor.
@note
It does <b>NOT</b> initialize the vector for efficiency.
*/
inline Vector2()
{
}
inline Vector2(const Real fX, const Real fY )
: x( fX ), y( fY )
{
}
inline explicit Vector2( const Real scaler )
: x( scaler), y( scaler )
{
}
inline explicit Vector2( const Real afCoordinate[2] )
: x( afCoordinate[0] ),
y( afCoordinate[1] )
{
}
inline explicit Vector2( const int afCoordinate[2] )
{
x = (Real)afCoordinate[0];
y = (Real)afCoordinate[1];
}
inline explicit Vector2( Real* const r )
: x( r[0] ), y( r[1] )
{
}
/** Exchange the contents of this vector with another.
*/
inline void swap(Vector2& other)
{
std::swap(x, other.x);
std::swap(y, other.y);
}
inline Real operator [] ( const size_t i ) const
{
assert( i < 2 );
return *(&x+i);
}
inline Real& operator [] ( const size_t i )
{
assert( i < 2 );
return *(&x+i);
}
/// Pointer accessor for direct copying
inline Real* ptr()
{
return &x;
}
/// Pointer accessor for direct copying
inline const Real* ptr() const
{
return &x;
}
/** Assigns the value of the other vector.
@param
rkVector The other vector
*/
inline Vector2& operator = ( const Vector2& rkVector )
{
x = rkVector.x;
y = rkVector.y;
return *this;
}
inline Vector2& operator = ( const Real fScalar)
{
x = fScalar;
y = fScalar;
return *this;
}
inline bool operator == ( const Vector2& rkVector ) const
{
return ( x == rkVector.x && y == rkVector.y );
}
inline bool operator != ( const Vector2& rkVector ) const
{
return ( x != rkVector.x || y != rkVector.y );
}
// arithmetic operations
inline Vector2 operator + ( const Vector2& rkVector ) const
{
return Vector2(
x + rkVector.x,
y + rkVector.y);
}
inline Vector2 operator - ( const Vector2& rkVector ) const
{
return Vector2(
x - rkVector.x,
y - rkVector.y);
}
inline Vector2 operator * ( const Real fScalar ) const
{
return Vector2(
x * fScalar,
y * fScalar);
}
inline Vector2 operator * ( const Vector2& rhs) const
{
return Vector2(
x * rhs.x,
y * rhs.y);
}
inline Vector2 operator / ( const Real fScalar ) const
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
return Vector2(
x * fInv,
y * fInv);
}
inline Vector2 operator / ( const Vector2& rhs) const
{
return Vector2(
x / rhs.x,
y / rhs.y);
}
inline const Vector2& operator + () const
{
return *this;
}
inline Vector2 operator - () const
{
return Vector2(-x, -y);
}
// overloaded operators to help Vector2
inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
{
return Vector2(
fScalar * rkVector.x,
fScalar * rkVector.y);
}
inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
{
return Vector2(
fScalar / rkVector.x,
fScalar / rkVector.y);
}
inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
{
return Vector2(
lhs.x + rhs,
lhs.y + rhs);
}
inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
{
return Vector2(
lhs + rhs.x,
lhs + rhs.y);
}
inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
{
return Vector2(
lhs.x - rhs,
lhs.y - rhs);
}
inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
{
return Vector2(
lhs - rhs.x,
lhs - rhs.y);
}
// arithmetic updates
inline Vector2& operator += ( const Vector2& rkVector )
{
x += rkVector.x;
y += rkVector.y;
return *this;
}
inline Vector2& operator += ( const Real fScaler )
{
x += fScaler;
y += fScaler;
return *this;
}
inline Vector2& operator -= ( const Vector2& rkVector )
{
x -= rkVector.x;
y -= rkVector.y;
return *this;
}
inline Vector2& operator -= ( const Real fScaler )
{
x -= fScaler;
y -= fScaler;
return *this;
}
inline Vector2& operator *= ( const Real fScalar )
{
x *= fScalar;
y *= fScalar;
return *this;
}
inline Vector2& operator *= ( const Vector2& rkVector )
{
x *= rkVector.x;
y *= rkVector.y;
return *this;
}
inline Vector2& operator /= ( const Real fScalar )
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
x *= fInv;
y *= fInv;
return *this;
}
inline Vector2& operator /= ( const Vector2& rkVector )
{
x /= rkVector.x;
y /= rkVector.y;
return *this;
}
/** Returns the length (magnitude) of the vector.
@warning
This operation requires a square root and is expensive in
terms of CPU operations. If you don't need to know the exact
length (e.g. for just comparing lengths) use squaredLength()
instead.
*/
inline Real length () const
{
return Math::Sqrt( x * x + y * y );
}
/** Returns the square of the length(magnitude) of the vector.
@remarks
This method is for efficiency - calculating the actual
length of a vector requires a square root, which is expensive
in terms of the operations required. This method returns the
square of the length of the vector, i.e. the same as the
length but before the square root is taken. Use this if you
want to find the longest / shortest vector without incurring
the square root.
*/
inline Real squaredLength () const
{
return x * x + y * y;
}
/** Returns the distance to another vector.
@warning
This operation requires a square root and is expensive in
terms of CPU operations. If you don't need to know the exact
distance (e.g. for just comparing distances) use squaredDistance()
instead.
*/
inline Real distance(const Vector2& rhs) const
{
return (*this - rhs).length();
}
/** Returns the square of the distance to another vector.
@remarks
This method is for efficiency - calculating the actual
distance to another vector requires a square root, which is
expensive in terms of the operations required. This method
returns the square of the distance to another vector, i.e.
the same as the distance but before the square root is taken.
Use this if you want to find the longest / shortest distance
without incurring the square root.
*/
inline Real squaredDistance(const Vector2& rhs) const
{
return (*this - rhs).squaredLength();
}
/** Calculates the dot (scalar) product of this vector with another.
@remarks
The dot product can be used to calculate the angle between 2
vectors. If both are unit vectors, the dot product is the
cosine of the angle; otherwise the dot product must be
divided by the product of the lengths of both vectors to get
the cosine of the angle. This result can further be used to
calculate the distance of a point from a plane.
@param
vec Vector with which to calculate the dot product (together
with this one).
@return
A float representing the dot product value.
*/
inline Real dotProduct(const Vector2& vec) const
{
return x * vec.x + y * vec.y;
}
/** Normalises the vector.
@remarks
This method normalises the vector such that it's
length / magnitude is 1. The result is called a unit vector.
@note
This function will not crash for zero-sized vectors, but there
will be no changes made to their components.
@return The previous length of the vector.
*/
inline Real normalise()
{
Real fLength = Math::Sqrt( x * x + y * y);
// Will also work for zero-sized vectors, but will change nothing
// We're not using epsilons because we don't need to.
// Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
if ( fLength > Real(0.0f) )
{
Real fInvLength = 1.0f / fLength;
x *= fInvLength;
y *= fInvLength;
}
return fLength;
}
/** Returns a vector at a point half way between this and the passed
in vector.
*/
inline Vector2 midPoint( const Vector2& vec ) const
{
return Vector2(
( x + vec.x ) * 0.5f,
( y + vec.y ) * 0.5f );
}
/** Returns true if the vector's scalar components are all greater
that the ones of the vector it is compared against.
*/
inline bool operator < ( const Vector2& rhs ) const
{
if( x < rhs.x && y < rhs.y )
return true;
return false;
}
/** Returns true if the vector's scalar components are all smaller
that the ones of the vector it is compared against.
*/
inline bool operator > ( const Vector2& rhs ) const
{
if( x > rhs.x && y > rhs.y )
return true;
return false;
}
/** Sets this vector's components to the minimum of its own and the
ones of the passed in vector.
@remarks
'Minimum' in this case means the combination of the lowest
value of x, y and z from both vectors. Lowest is taken just
numerically, not magnitude, so -1 < 0.
*/
inline void makeFloor( const Vector2& cmp )
{
if( cmp.x < x ) x = cmp.x;
if( cmp.y < y ) y = cmp.y;
}
/** Sets this vector's components to the maximum of its own and the
ones of the passed in vector.
@remarks
'Maximum' in this case means the combination of the highest
value of x, y and z from both vectors. Highest is taken just
numerically, not magnitude, so 1 > -3.
*/
inline void makeCeil( const Vector2& cmp )
{
if( cmp.x > x ) x = cmp.x;
if( cmp.y > y ) y = cmp.y;
}
/** Generates a vector perpendicular to this vector (eg an 'up' vector).
@remarks
This method will return a vector which is perpendicular to this
vector. There are an infinite number of possibilities but this
method will guarantee to generate one of them. If you need more
control you should use the Quaternion class.
*/
inline Vector2 perpendicular(void) const
{
return Vector2 (-y, x);
}
/** Calculates the 2 dimensional cross-product of 2 vectors, which results
in a single floating point value which is 2 times the area of the triangle.
*/
inline Real crossProduct( const Vector2& rkVector ) const
{
return x * rkVector.y - y * rkVector.x;
}
/** Generates a new random vector which deviates from this vector by a
given angle in a random direction.
@remarks
This method assumes that the random number generator has already
been seeded appropriately.
@param angle
The angle at which to deviate in radians
@return
A random vector which deviates from this vector by angle. This
vector will not be normalised, normalise it if you wish
afterwards.
*/
inline Vector2 randomDeviant(Radian angle) const
{
angle *= Math::RangeRandom(-1, 1);
Real cosa = Math::Cos(angle);
Real sina = Math::Sin(angle);
return Vector2(cosa * x - sina * y,
sina * x + cosa * y);
}
/** Returns true if this vector is zero length. */
inline bool isZeroLength(void) const
{
Real sqlen = (x * x) + (y * y);
return (sqlen < (1e-06 * 1e-06));
}
/** As normalise, except that this vector is unaffected and the
normalised vector is returned as a copy. */
inline Vector2 normalisedCopy(void) const
{
Vector2 ret = *this;
ret.normalise();
return ret;
}
/** Calculates a reflection vector to the plane with the given normal .
@remarks NB assumes 'this' is pointing AWAY FROM the plane, invert if it is not.
*/
inline Vector2 reflect(const Vector2& normal) const
{
return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
}
/// Check whether this vector contains valid values
inline bool isNaN() const
{
return Math::isNaN(x) || Math::isNaN(y);
}
/** Gets the angle between 2 vectors.
@remarks
Vectors do not have to be unit-length but must represent directions.
*/
inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
{
Ogre::Real lenProduct = length() * other.length();
// Divide by zero check
if(lenProduct < 1e-6f)
lenProduct = 1e-6f;
Ogre::Real f = dotProduct(other) / lenProduct;
f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
return Ogre::Math::ACos(f);
}
/** Gets the oriented angle between 2 vectors.
@remarks
Vectors do not have to be unit-length but must represent directions.
The angle is comprised between 0 and 2 PI.
*/
inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
{
Ogre::Radian angle = angleBetween(other);
if (crossProduct(other)<0)
angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;
return angle;
}
// special points
static const Vector2 ZERO;
static const Vector2 UNIT_X;
static const Vector2 UNIT_Y;
static const Vector2 NEGATIVE_UNIT_X;
static const Vector2 NEGATIVE_UNIT_Y;
static const Vector2 UNIT_SCALE;
/** Function for writing to a stream.
*/
inline _OgreExport friend std::ostream& operator <<
( std::ostream& o, const Vector2& v )
{
o << "Vector2(" << v.x << ", " << v.y << ")";
return o;
}
};
/** @} */
/** @} */
}
#endif
|