/usr/include/oce/NCollection_Mat4.hxx is in liboce-foundation-dev 0.17.1-1.
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 | // Created on: 2013-05-30
// Created by: Anton POLETAEV
// Copyright (c) 2013-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _NCollection_Mat4_HeaderFile
#define _NCollection_Mat4_HeaderFile
#include <NCollection_Vec4.hxx>
//! Generic matrix of 4 x 4 elements.
//! To be used in conjunction with NCollection_Vec4 entities.
//! Originally introduced for 3D space projection and orientation
//! operations.
template<typename Element_t>
class NCollection_Mat4
{
public:
//! Get number of rows.
//! @return number of rows.
static size_t Rows()
{
return 4;
}
//! Get number of columns.
//! @retur number of columns.
static size_t Cols()
{
return 4;
}
//! Empty constructor.
//! Construct the zero matrix.
NCollection_Mat4()
{
InitIdentity();
}
//! Copy constructor.
//! @param theOther [in] the matrix to copy values from.
NCollection_Mat4 (const NCollection_Mat4& theOther)
{
std::memcpy (this, &theOther, sizeof (NCollection_Mat4));
}
//! Assignment operator.
//! @param theOther [in] the matrix to copy values from.
const NCollection_Mat4& operator= (const NCollection_Mat4& theOther)
{
std::memcpy (this, &theOther, sizeof (NCollection_Mat4));
return *this;
}
//! Get element at the specified row and column.
//! @param theRow [in] the row.to address.
//! @param theCol [in] the column to address.
//! @return the value of the addressed element.
Element_t GetValue (const size_t theRow, const size_t theCol) const
{
return myMat[theCol * 4 + theRow];
}
//! Access element at the specified row and column.
//! @param theRow [in] the row.to access.
//! @param theCol [in] the column to access.
//! @return reference on the matrix element.
Element_t& ChangeValue (const size_t theRow, const size_t theCol)
{
return myMat[theCol * 4 + theRow];
}
//! Set value for the element specified by row and columns.
//! @param theRow [in] the row to change.
//! @param theCol [in] the column to change.
//! @param theValue [in] the value to set.s
void SetValue (const size_t theRow,
const size_t theCol,
const Element_t theValue)
{
myMat[theCol * 4 + theRow] = theValue;
}
//! Get vector of elements for the specified row.
//! @param theRow [in] the row to access.
//! @return vector of elements.
NCollection_Vec4<Element_t> GetRow (const size_t theRow) const
{
return NCollection_Vec4<Element_t> (GetValue (theRow, 0),
GetValue (theRow, 1),
GetValue (theRow, 2),
GetValue (theRow, 3));
}
//! Change first 3 row values by the passed vector.
//! @param theRow [in] the row to change.
//! @param theVec [in] the vector of values.
void SetRow (const size_t theRow, const NCollection_Vec3<Element_t>& theVec)
{
SetValue (theRow, 0, theVec.x());
SetValue (theRow, 1, theVec.y());
SetValue (theRow, 2, theVec.z());
}
//! Set row values by the passed 4 element vector.
//! @param theRow [in] the row to change.
//! @param theVec [in] the vector of values.
void SetRow (const size_t theRow, const NCollection_Vec4<Element_t>& theVec)
{
SetValue (theRow, 0, theVec.x());
SetValue (theRow, 1, theVec.y());
SetValue (theRow, 2, theVec.z());
SetValue (theRow, 3, theVec.w());
}
//! Get vector of elements for the specified column.
//! @param theCol [in] the column to access.
//! @return vector of elements.
NCollection_Vec4<Element_t> GetColumn (const size_t theCol) const
{
return NCollection_Vec4<Element_t> (GetValue (0, theCol),
GetValue (1, theCol),
GetValue (2, theCol),
GetValue (3, theCol));
}
//! Change first 3 column values by the passed vector.
//! @param theCol [in] the column to change.
//! @param theVec [in] the vector of values.
void SetColumn (const size_t theCol,
const NCollection_Vec3<Element_t>& theVec)
{
SetValue (0, theCol, theVec.x());
SetValue (1, theCol, theVec.y());
SetValue (2, theCol, theVec.z());
}
//! Set column values by the passed 4 element vector.
//! @param theCol [in] the column to change.
//! @param theVec [in] the vector of values.
void SetColumn (const size_t theCol,
const NCollection_Vec4<Element_t>& theVec)
{
SetValue (0, theCol, theVec.x());
SetValue (1, theCol, theVec.y());
SetValue (2, theCol, theVec.z());
SetValue (3, theCol, theVec.w());
}
//! Get vector of diagonal elements.
//! \return vector of diagonal elements.
NCollection_Vec4<Element_t> GetDiagonal() const
{
return NCollection_Vec4<Element_t> (GetValue (0, 0),
GetValue (1, 1),
GetValue (2, 2),
GetValue (3, 3));
}
//! Change first 3 elements of the diagonal matrix.
//! @param theVec the vector of values.
void SetDiagonal (const NCollection_Vec3<Element_t>& theVec)
{
SetValue (0, 0, theVec.x());
SetValue (1, 1, theVec.y());
SetValue (2, 2, theVec.z());
}
//! Set diagonal elements of the matrix by the passed vector.
//! @param theVec [in] the vector of values.
void SetDiagonal (const NCollection_Vec4<Element_t>& theVec)
{
SetValue (0, 0, theVec.x());
SetValue (1, 1, theVec.y());
SetValue (2, 2, theVec.z());
SetValue (3, 3, theVec.w());
}
//! Initialize the identity matrix.
void InitIdentity()
{
std::memcpy (this, myIdentityArray, sizeof (NCollection_Mat4));
}
//! Checks the matrix for identity.
bool IsIdentity() const
{
return std::memcmp (this, myIdentityArray, sizeof (NCollection_Mat4)) == 0;
}
//! Raw access to the data (for OpenGL exchange).
const Element_t* GetData() const { return myMat; }
Element_t* ChangeData() { return myMat; }
//! Multiply by the vector (M * V).
//! @param theVec [in] the vector to multiply.
NCollection_Vec4<Element_t> operator* (const NCollection_Vec4<Element_t>& theVec) const
{
return NCollection_Vec4<Element_t> (
GetValue (0, 0) * theVec.x() + GetValue (0, 1) * theVec.y() + GetValue (0, 2) * theVec.z() + GetValue (0, 3) * theVec.w(),
GetValue (1, 0) * theVec.x() + GetValue (1, 1) * theVec.y() + GetValue (1, 2) * theVec.z() + GetValue (1, 3) * theVec.w(),
GetValue (2, 0) * theVec.x() + GetValue (2, 1) * theVec.y() + GetValue (2, 2) * theVec.z() + GetValue (2, 3) * theVec.w(),
GetValue (3, 0) * theVec.x() + GetValue (3, 1) * theVec.y() + GetValue (3, 2) * theVec.z() + GetValue (3, 3) * theVec.w());
}
//! Compute matrix multiplication product: A * B.
//! @param theMatA [in] the matrix "A".
//! @param theMatB [in] the matrix "B".
NCollection_Mat4 Multiply (const NCollection_Mat4& theMatA,
const NCollection_Mat4& theMatB)
{
NCollection_Mat4 aMatRes;
size_t aInputElem;
for (size_t aResElem = 0; aResElem < 16; ++aResElem)
{
aMatRes.myMat[aResElem] = (Element_t )0;
for (aInputElem = 0; aInputElem < 4; ++aInputElem)
{
aMatRes.myMat[aResElem] += theMatA.GetValue(aResElem % 4, aInputElem)
* theMatB.GetValue(aInputElem, aResElem / 4);
}
}
return aMatRes;
}
//! Compute matrix multiplication.
//! @param theMat [in] the matrix to multiply.
void Multiply (const NCollection_Mat4& theMat)
{
*this = Multiply(*this, theMat);
}
//! Multiply by the another matrix.
//! @param theMat [in] the other matrix.
NCollection_Mat4& operator*= (const NCollection_Mat4& theMat)
{
Multiply (theMat);
return *this;
}
//! Compute matrix multiplication product.
//! @param theMat [in] the other matrix.
//! @return result of multiplication.
NCollection_Mat4 operator* (const NCollection_Mat4& theMat) const
{
return Multiplied (theMat);
}
//! Compute matrix multiplication product.
//! @param theMat [in] the other matrix.
//! @return result of multiplication.
NCollection_Mat4 Multiplied (const NCollection_Mat4& theMat) const
{
NCollection_Mat4 aTempMat (*this);
aTempMat *= theMat;
return aTempMat;
}
//! Compute per-component multiplication.
//! @param theFactor [in] the scale factor.
void Multiply (const Element_t theFactor)
{
for (size_t i = 0; i < 16; ++i)
{
myMat[i] *= theFactor;
}
}
//! Compute per-element multiplication.
//! @param theFactor [in] the scale factor.
NCollection_Mat4& operator*=(const Element_t theFactor)
{
Multiply (theFactor);
return *this;
}
//! Compute per-element multiplication.
//! @param theFactor [in] the scale factor.
//! @return the result of multiplicaton.
NCollection_Mat4 operator* (const Element_t theFactor) const
{
return Multiplied (theFactor);
}
//! Compute per-element multiplication.
//! @param theFactor [in] the scale factor.
//! @return the result of multiplicaton.
NCollection_Mat4 Multiplied (const Element_t theFactor) const
{
NCollection_Mat4 aTempMat (*this);
aTempMat *= theFactor;
return aTempMat;
}
//! Translate the matrix on the passed vector.
//! @param theVec [in] the translation vector.
void Translate (const NCollection_Vec3<Element_t>& theVec)
{
NCollection_Mat4 aTempMat;
aTempMat.SetColumn (3, theVec);
this->Multiply (aTempMat);
}
//! Transpose the matrix.
//! @return transposed copy of the matrix.
NCollection_Mat4 Transposed() const
{
NCollection_Mat4 aTempMat;
aTempMat.SetRow (0, GetColumn (0));
aTempMat.SetRow (1, GetColumn (1));
aTempMat.SetRow (2, GetColumn (2));
aTempMat.SetRow (3, GetColumn (3));
return aTempMat;
}
//! Transpose the matrix.
void Transpose()
{
*this = Transposed();
}
//! Compute inverted matrix.
//! @param theOutMx [out] the inverted matrix.
//! @return true if reversion success.
bool Inverted (NCollection_Mat4<Element_t>& theOutMx) const
{
Element_t* inv = theOutMx.myMat;
// use short-cut for better readability
const Element_t* m = myMat;
inv[ 0] = m[ 5] * (m[10] * m[15] - m[11] * m[14]) -
m[ 9] * (m[ 6] * m[15] - m[ 7] * m[14]) -
m[13] * (m[ 7] * m[10] - m[ 6] * m[11]);
inv[ 1] = m[ 1] * (m[11] * m[14] - m[10] * m[15]) -
m[ 9] * (m[ 3] * m[14] - m[ 2] * m[15]) -
m[13] * (m[ 2] * m[11] - m[ 3] * m[10]);
inv[ 2] = m[ 1] * (m[ 6] * m[15] - m[ 7] * m[14]) -
m[ 5] * (m[ 2] * m[15] - m[ 3] * m[14]) -
m[13] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
inv[ 3] = m[ 1] * (m[ 7] * m[10] - m[ 6] * m[11]) -
m[ 5] * (m[ 3] * m[10] - m[ 2] * m[11]) -
m[ 9] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
inv[ 4] = m[ 4] * (m[11] * m[14] - m[10] * m[15]) -
m[ 8] * (m[ 7] * m[14] - m[ 6] * m[15]) -
m[12] * (m[ 6] * m[11] - m[ 7] * m[10]);
inv[ 5] = m[ 0] * (m[10] * m[15] - m[11] * m[14]) -
m[ 8] * (m[ 2] * m[15] - m[ 3] * m[14]) -
m[12] * (m[ 3] * m[10] - m[ 2] * m[11]);
inv[ 6] = m[ 0] * (m[ 7] * m[14] - m[ 6] * m[15]) -
m[ 4] * (m[ 3] * m[14] - m[ 2] * m[15]) -
m[12] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
inv[ 7] = m[ 0] * (m[ 6] * m[11] - m[ 7] * m[10]) -
m[ 4] * (m[ 2] * m[11] - m[ 3] * m[10]) -
m[ 8] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
inv[ 8] = m[ 4] * (m[ 9] * m[15] - m[11] * m[13]) -
m[ 8] * (m[ 5] * m[15] - m[ 7] * m[13]) -
m[12] * (m[ 7] * m[ 9] - m[ 5] * m[11]);
inv[ 9] = m[ 0] * (m[11] * m[13] - m[ 9] * m[15]) -
m[ 8] * (m[ 3] * m[13] - m[ 1] * m[15]) -
m[12] * (m[ 1] * m[11] - m[ 3] * m[ 9]);
inv[10] = m[ 0] * (m[ 5] * m[15] - m[ 7] * m[13]) -
m[ 4] * (m[ 1] * m[15] - m[ 3] * m[13]) -
m[12] * (m[ 3] * m[ 5] - m[ 1] * m[ 7]);
inv[11] = m[ 0] * (m[ 7] * m[ 9] - m[ 5] * m[11]) -
m[ 4] * (m[ 3] * m[ 9] - m[ 1] * m[11]) -
m[ 8] * (m[ 1] * m[ 7] - m[ 3] * m[ 5]);
inv[12] = m[ 4] * (m[10] * m[13] - m[ 9] * m[14]) -
m[ 8] * (m[ 6] * m[13] - m[ 5] * m[14]) -
m[12] * (m[ 5] * m[10] - m[ 6] * m[ 9]);
inv[13] = m[ 0] * (m[ 9] * m[14] - m[10] * m[13]) -
m[ 8] * (m[ 1] * m[14] - m[ 2] * m[13]) -
m[12] * (m[ 2] * m[ 9] - m[ 1] * m[10]);
inv[14] = m[ 0] * (m[ 6] * m[13] - m[ 5] * m[14]) -
m[ 4] * (m[ 2] * m[13] - m[ 1] * m[14]) -
m[12] * (m[ 1] * m[ 6] - m[ 2] * m[ 5]);
inv[15] = m[ 0] * (m[ 5] * m[10] - m[ 6] * m[ 9]) -
m[ 4] * (m[ 1] * m[10] - m[ 2] * m[ 9]) -
m[ 8] * (m[ 2] * m[ 5] - m[ 1] * m[ 6]);
Element_t aDet = m[0] * inv[ 0] +
m[1] * inv[ 4] +
m[2] * inv[ 8] +
m[3] * inv[12];
if (aDet == 0)
return false;
aDet = (Element_t) 1. / aDet;
for (int i = 0; i < 16; ++i)
inv[i] *= aDet;
return true;
}
// Converts NCollection_Mat4 with different element type.
template <typename Other_t>
void Convert (const NCollection_Mat4<Other_t>& theOther)
{
for (int anIdx = 0; anIdx < 16; ++anIdx)
{
myMat[anIdx] = static_cast<Element_t> (theOther.myMat[anIdx]);
}
}
//! Maps plain C array to matrix type.
static NCollection_Mat4<Element_t>& Map (Element_t* theData)
{
return *reinterpret_cast<NCollection_Mat4<Element_t>*> (theData);
}
//! Maps plain C array to matrix type.
static const NCollection_Mat4<Element_t>& Map (const Element_t* theData)
{
return *reinterpret_cast<const NCollection_Mat4<Element_t>*> (theData);
}
private:
Element_t myMat[16];
private:
static Element_t myIdentityArray[16];
// All instantiations are friend to each other
template<class OtherType> friend class NCollection_Mat4;
};
template<typename Element_t>
Element_t NCollection_Mat4<Element_t>::myIdentityArray[] =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
#endif // _NCollection_Mat4_HeaderFile
|