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
|
/****************************************************************************
* *
* PrimeSense Sensor 5.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Sensor. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify*
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>.*
* *
****************************************************************************/
#ifndef _XNV_3DVECTOR_H_
#define _XNV_3DVECTOR_H_
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnEE/XnVPoint.h>
#include "XnVMathCommon.h"
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
class XN_EE_FW_API XnV3DVector : public XnVPoint
{
public:
// Constructors
inline XnV3DVector();
inline XnV3DVector(XnFloat fX, XnFloat fY, XnFloat fZ);
inline XnV3DVector(const XnVPoint& ptOther);
inline XnV3DVector(const XnV3DVector& v3Other);
inline XnV3DVector(XnFloat f);
inline XnV3DVector(XnFloat* pf);
// Assignment
inline XnV3DVector& operator=(const XnV3DVector& v3Rhs);
inline XnV3DVector& operator=(const XnVPoint& ptRhs);
// Access
inline XnFloat operator[](XnUInt32 index) const;
inline XnFloat& operator[](XnUInt32 index);
// Set
inline XnV3DVector& Set(const XnV3DVector& v3Other);
inline XnV3DVector& Set(const XnVPoint& ptOther);
inline XnV3DVector& Set(XnFloat fX, XnFloat fY, XnFloat fZ);
inline XnV3DVector& Set(XnFloat f);
// Zero
inline XnV3DVector& SetZero();
inline XnBool IsZero() const;
// Comparison
inline XnBool operator==(const XnV3DVector& v3Rhs) const;
inline XnBool operator!=(const XnV3DVector& v3Rhs) const;
// Negation
inline XnV3DVector operator-() const;
inline XnV3DVector& Negate(const XnV3DVector& v3Other);
inline XnV3DVector& Negate();
// Multiplication
inline XnV3DVector operator*(const XnV3DVector& v3Rhs) const;
inline XnV3DVector& operator*=(const XnV3DVector& v3Rhs);
inline XnV3DVector& Multiply(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs);
friend inline XnV3DVector operator*(XnFloat f, const XnV3DVector& v3Rhs);
inline XnV3DVector& Multiply(XnFloat f, const XnV3DVector& v3Rhs);
inline XnV3DVector operator*(XnFloat f) const;
inline XnV3DVector& operator*=(XnFloat f);
inline XnV3DVector& Multiply(const XnV3DVector& v3Lhs, XnFloat f);
// Division
inline XnV3DVector operator/(XnFloat f) const;
inline XnV3DVector& operator/=(XnFloat f);
inline XnV3DVector& Divide(const XnV3DVector& v3Lhs, XnFloat f);
// Addition
inline XnV3DVector operator+(const XnV3DVector& v3Rhs) const;
inline XnV3DVector& operator+=(const XnV3DVector& v3Rhs);
inline XnV3DVector& Add(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs);
inline XnV3DVector operator+(XnFloat f) const;
inline XnV3DVector& operator+=(XnFloat f);
inline XnV3DVector& Add(const XnV3DVector& v3Lhs, XnFloat f);
// Subtraction
inline XnV3DVector operator-(const XnV3DVector& v3Rhs) const;
inline XnV3DVector& operator-=(const XnV3DVector& v3Rhs);
inline XnV3DVector& Subtract(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs);
inline XnV3DVector operator-(XnFloat f) const;
inline XnV3DVector& operator-=(XnFloat f);
inline XnV3DVector& Subtract(const XnV3DVector& v3Lhs, XnFloat f);
inline XnV3DVector& Sqrt();
inline XnV3DVector& Sqrt(const XnV3DVector& v3Other);
// Magnitude
inline XnFloat Magnitude() const;
inline XnFloat MagnitudeSquared() const;
// Distance
inline XnFloat Distance(const XnV3DVector& v3Other) const;
inline XnFloat DistanceSquared(const XnV3DVector& v3Other) const;
// Normalize
inline XnFloat Normalize();
// Orthogonal
inline XnV3DVector& OrthogonalVector(const XnV3DVector& v3Other);
inline XnV3DVector& UnitOrthogonalVector(const XnV3DVector& v3Other);
// Cross product
inline XnV3DVector operator^(const XnV3DVector& v3Rhs) const;
inline XnV3DVector& CrossProduct(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs);
// Dot Product
inline XnFloat operator|(const XnV3DVector& v3Rhs) const;
friend inline XnFloat DotProduct(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs);
// Interpolation
inline XnV3DVector& Interpolate(const XnV3DVector& v3Vec1, const XnV3DVector& v3Vec2, XnFloat fAlpha);
inline XnBool IsSameDirection(const XnV3DVector& v3Other) const;
inline XnFloat GetTolerance() const;
inline void SetTolerance(XnFloat fTolerance);
protected:
XnFloat m_fTolerance;
};
XnFloat XnV3DVector::GetTolerance() const
{
return m_fTolerance;
}
void XnV3DVector::SetTolerance(XnFloat fTolerance)
{
m_fTolerance = fTolerance;
}
XnV3DVector::XnV3DVector() :
m_fTolerance(1e-5f)
{
Set(0);
}
XnV3DVector::XnV3DVector(XnFloat fX, XnFloat fY, XnFloat fZ) :
m_fTolerance(1e-5f)
{
Set(fX, fY, fZ);
}
XnV3DVector::XnV3DVector(const XnV3DVector& v3Other)
{
Set(v3Other);
}
XnV3DVector::XnV3DVector(const XnVPoint& ptOther)
{
Set(ptOther);
}
XnV3DVector::XnV3DVector(XnFloat f) :
m_fTolerance(1e-5f)
{
Set(f);
}
XnV3DVector::XnV3DVector(XnFloat* pf) :
m_fTolerance(1e-5f)
{
Set(pf[0], pf[1], pf[2]);
}
// Assignment
XnV3DVector& XnV3DVector::operator=(const XnV3DVector& v3Rhs)
{
m_fTolerance = v3Rhs.m_fTolerance;
return Set(v3Rhs.X(), v3Rhs.Y(), v3Rhs.Z());
}
XnV3DVector& XnV3DVector::operator=(const XnVPoint& ptRhs)
{
m_fTolerance = 1e-5f;
return Set(ptRhs.X(), ptRhs.Y(), ptRhs.Z());
}
// Access
XnFloat XnV3DVector::operator[](XnUInt32 nIndex) const
{
if (nIndex > 2)
return elements[3];
return elements[nIndex];
}
XnFloat& XnV3DVector::operator[](XnUInt32 nIndex)
{
if (nIndex > 2)
return elements[3];
return elements[nIndex];
}
// Set
XnV3DVector& XnV3DVector::Set(const XnV3DVector& v3Other)
{
return *this = v3Other;
}
XnV3DVector& XnV3DVector::Set(const XnVPoint& ptOther)
{
return *this = ptOther;
// return Set(ptOther.X(), ptOther.Y(), ptOther.Z())
}
XnV3DVector& XnV3DVector::Set(XnFloat x, XnFloat y, XnFloat z)
{
SetPoint(x, y, z);
return *this;
}
XnV3DVector& XnV3DVector::Set(XnFloat f)
{
return Set(f, f, f);
}
// Zero
XnV3DVector& XnV3DVector::SetZero()
{
return Set(0);
}
XnBool XnV3DVector::IsZero() const
{
return (fabs(X()) < m_fTolerance &&
fabs(Y()) < m_fTolerance &&
fabs(Z()) < m_fTolerance);
}
// Comparison
XnBool XnV3DVector::operator==(const XnV3DVector& v3Rhs) const
{
return (fabs(X() - v3Rhs.X()) < m_fTolerance &&
fabs(Y() - v3Rhs.Y()) < m_fTolerance &&
fabs(Z() - v3Rhs.Z()) < m_fTolerance);
}
XnBool XnV3DVector::operator!=(const XnV3DVector& v3Rhs) const
{
return !this->operator==(v3Rhs);
}
// Negation
XnV3DVector XnV3DVector::operator-() const
{
return XnV3DVector(-X(), -Y(), -Z());
}
XnV3DVector& XnV3DVector::Negate(const XnV3DVector& v3Other)
{
return Set(-v3Other.X(), -v3Other.Y(), -v3Other.Z());
}
XnV3DVector& XnV3DVector::Negate()
{
return Set(-X(), -Y(), -Z());
}
// Multiplication
XnV3DVector XnV3DVector::operator*(const XnV3DVector& v3Rhs) const
{
return XnV3DVector(X() * v3Rhs.X(), Y() * v3Rhs.Y(), Z()*v3Rhs.Z());
}
XnV3DVector& XnV3DVector::operator*=(const XnV3DVector& v3Rhs)
{
return Set(X() * v3Rhs.X(), Y() * v3Rhs.Y(), Z() * v3Rhs.Z());
}
XnV3DVector& XnV3DVector::Multiply(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs)
{
return Set(v3Lhs.X() * v3Rhs.X(), v3Lhs.Y() * v3Rhs.Y(), v3Lhs.Z() * v3Rhs.Z());
}
XnV3DVector operator*(XnFloat f, const XnV3DVector& v3Rhs)
{
return XnV3DVector(f * v3Rhs.X(), f * v3Rhs.Y(), f * v3Rhs.Z());
}
XnV3DVector& XnV3DVector::Multiply(XnFloat f, const XnV3DVector& v3Rhs)
{
return Set(f * v3Rhs.X(), f * v3Rhs.Y(), f * v3Rhs.Z());
}
XnV3DVector XnV3DVector::operator*(XnFloat f) const
{
return XnV3DVector(X() * f, Y() * f, Z() * f);
}
XnV3DVector& XnV3DVector::operator*=(XnFloat f)
{
return Set(X() * f, Y() * f, Z() * f);
}
XnV3DVector& XnV3DVector::Multiply(const XnV3DVector& v3Lhs, XnFloat f)
{
return Set(v3Lhs.X() * f, v3Lhs.Y() * f, v3Lhs.Z() * f);
}
// Division
XnV3DVector XnV3DVector::operator/(XnFloat f) const
{
return XnV3DVector(X() / f, Y() / f, Z() / f);
}
XnV3DVector& XnV3DVector::operator/=(XnFloat f)
{
return Set(X() / f, Y() / f, Z() / f);
}
XnV3DVector& XnV3DVector::Divide(const XnV3DVector& v3Lhs, XnFloat f)
{
return Set(v3Lhs.X() / f, v3Lhs.Y() / f, v3Lhs.Z() / f);
}
// Addition
XnV3DVector XnV3DVector::operator+(const XnV3DVector& v3Rhs) const
{
return XnV3DVector(X() + v3Rhs.X(), Y() + v3Rhs.Y(), Z() + v3Rhs.Z());
}
XnV3DVector& XnV3DVector::operator+=(const XnV3DVector& v3Rhs)
{
return Set(X() + v3Rhs.X(), Y() + v3Rhs.Y(), Z() + v3Rhs.Z());
}
XnV3DVector& XnV3DVector::Add(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs)
{
return Set(v3Lhs.X() + v3Rhs.X(), v3Lhs.Y() + v3Rhs.Y(), v3Lhs.Z() + v3Rhs.Z());
}
XnV3DVector XnV3DVector::operator+(XnFloat f) const
{
return XnV3DVector(X() + f, Y() + f, Z() + f);
}
XnV3DVector& XnV3DVector::operator+=(XnFloat f)
{
return Set(X() + f, Y() + f, Z() + f);
}
XnV3DVector& XnV3DVector::Add(const XnV3DVector& v3Lhs, XnFloat f)
{
return Set(v3Lhs.X() + f, v3Lhs.Y() + f, v3Lhs.Z() + f);
}
// Subtraction
XnV3DVector XnV3DVector::operator-(const XnV3DVector& v3Rhs) const
{
return XnV3DVector(X() - v3Rhs.X(), Y() - v3Rhs.Y(), Z() - v3Rhs.Z());
}
XnV3DVector& XnV3DVector::operator-=(const XnV3DVector& v3Rhs)
{
return Set(X() - v3Rhs.X(), Y() - v3Rhs.Y(), Z() - v3Rhs.Z());
}
XnV3DVector& XnV3DVector::Subtract(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs)
{
return Set(v3Lhs.X() - v3Rhs.X(), v3Lhs.Y() - v3Rhs.Y(), v3Lhs.Z() - v3Rhs.Z());
}
XnV3DVector XnV3DVector::operator-(XnFloat f) const
{
return XnV3DVector(X() - f, Y() - f, Z() - f);
}
XnV3DVector& XnV3DVector::operator-=(XnFloat f)
{
return Set(X() - f, Y() - f, Z() - f);
}
XnV3DVector& XnV3DVector::Subtract(const XnV3DVector& v3Lhs, XnFloat f)
{
return Set(v3Lhs.X() - f, v3Lhs.Y() - f, v3Lhs.Z() - f);
}
XnV3DVector& XnV3DVector::Sqrt()
{
return Set(sqrt(X()), sqrt(Y()), sqrt(Z()));
}
XnV3DVector& XnV3DVector::Sqrt(const XnV3DVector& v3Other)
{
Set(v3Other);
return Sqrt();
}
// Magnitude
XnFloat XnV3DVector::Magnitude() const
{
return sqrt(MagnitudeSquared());
}
XnFloat XnV3DVector::MagnitudeSquared() const
{
return X() * X() +
Y() * Y() +
Z() * Z();
}
// Distance
XnFloat XnV3DVector::Distance(const XnV3DVector& v3Other) const
{
return sqrt(DistanceSquared(v3Other));
}
XnFloat XnV3DVector::DistanceSquared(const XnV3DVector& v3Other) const
{
return (*this-v3Other).MagnitudeSquared();
}
// Normalize
XnFloat XnV3DVector::Normalize()
{
XnFloat fLen = Magnitude();
if (fLen > m_fTolerance)
*this /= fLen;
else
Set(1, 0, 0);
return fLen;
}
// Orthogonal
XnV3DVector& XnV3DVector::OrthogonalVector(const XnV3DVector& v3Other)
{
XnFloat abs_x = fabs(v3Other.X()), abs_y = fabs(v3Other.Y()), abs_z = fabs(v3Other.Z());
if (abs_x < abs_y)
if (abs_x < abs_z)
Set(0, v3Other.Z(), -v3Other.Y());
else
Set(v3Other.Y(), -v3Other.X(), 0);
else
if (abs_y < abs_z)
Set(-v3Other.Z(), 0, v3Other.X());
else
Set(v3Other.Y(), -v3Other.X(), 0);
return *this;
}
XnV3DVector& XnV3DVector::UnitOrthogonalVector(const XnV3DVector& v3Other)
{
OrthogonalVector(v3Other);
Normalize();
return *this;
}
// Cross product
XnV3DVector XnV3DVector::operator^(const XnV3DVector& v3Rhs) const
{
XnV3DVector v3Result;
v3Result.CrossProduct(*this, v3Rhs);
return v3Result;
}
XnV3DVector& XnV3DVector::CrossProduct(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs)
{
return Set(v3Lhs.Y() * v3Rhs.Z() - v3Lhs.Z() * v3Rhs.Y(),
v3Lhs.Z() * v3Rhs.X() - v3Lhs.X() * v3Rhs.Z(),
v3Lhs.X() * v3Rhs.Y() - v3Lhs.Y() * v3Rhs.X());
}
// Dot Product
XnFloat XnV3DVector::operator|(const XnV3DVector& v3Rhs) const
{
return DotProduct(*this, v3Rhs);
}
XnFloat DotProduct(const XnV3DVector& v3Lhs, const XnV3DVector& v3Rhs)
{
return v3Lhs.X() * v3Rhs.X() +
v3Lhs.Y() * v3Rhs.Y() +
v3Lhs.Z() * v3Rhs.Z();
}
// Interpolation
XnV3DVector& XnV3DVector::Interpolate(const XnV3DVector& v3Vec1, const XnV3DVector& v3Vec2, XnFloat fAlpha)
{
return Set(v3Vec1.X() + fAlpha * (v3Vec2.X() - v3Vec1.X()),
v3Vec1.Y() + fAlpha * (v3Vec2.Y() - v3Vec1.Y()),
v3Vec1.Z() + fAlpha * (v3Vec2.Z() - v3Vec1.Z()));
}
XnBool XnV3DVector::IsSameDirection(const XnV3DVector& v3Other) const
{
if (IsZero() || v3Other.IsZero())
{
return true;
}
XnFloat fRatio = 0;
if (!XnVMathCommon::IsZero(X(), m_fTolerance) && !XnVMathCommon::IsZero(v3Other.X(), m_fTolerance))
fRatio = v3Other.X()/X();
else if (!XnVMathCommon::IsZero(Y(), m_fTolerance) && !XnVMathCommon::IsZero(v3Other.Y(), m_fTolerance))
fRatio = v3Other.Y()/Y();
else if (!XnVMathCommon::IsZero(Z(), m_fTolerance) && !XnVMathCommon::IsZero(v3Other.Z(), m_fTolerance))
fRatio = v3Other.Z()/Z();
else
{
// Combination sof zeros, though not all zeros!
return false;
}
if (v3Other/fRatio == (*this))
return true;
return false;
}
#endif
|