This file is indexed.

/usr/include/oce/OpenGl_Utils.hxx is in liboce-visualization-dev 0.17.2-2.

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
// Created on: 2014-09-30
// Created by: Denis BOGOLEPOV
// Copyright (c) 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 _OpenGl_Utils_H__
#define _OpenGl_Utils_H__

#include <OpenGl_Vec.hxx>
#include <NCollection_Vector.hxx>

//! Helper class that implements some functionality of GLU library.
namespace OpenGl_Utils
{

  //! Matrix type selector.
  template<class T>
  struct MatrixType {
    //
  };

  template<>
  struct MatrixType<Standard_Real> {
    typedef OpenGl_Mat4d Mat4;
  };

  template<>
  struct MatrixType<Standard_ShortReal> {
    typedef OpenGl_Mat4 Mat4;
  };

  //! Vector type selector.
  template<class T>
  struct VectorType {
    //
  };

  template<>
  struct VectorType<Standard_Real> {
    typedef OpenGl_Vec2d Vec2;
    typedef OpenGl_Vec3d Vec3;
    typedef OpenGl_Vec4d Vec4;
  };

  template<>
  struct VectorType<Standard_ShortReal> {
    typedef OpenGl_Vec2 Vec2;
    typedef OpenGl_Vec3 Vec3;
    typedef OpenGl_Vec4 Vec4;
  };

  //! Software implementation for OpenGL matrix stack.
  template<class T>
  class MatrixState
  {
  public:

    //! Constructs matrix state object.
    MatrixState()
    : myStack (8),
      myStackHead (-1)
    {
      //
    }

    //! Pushes current matrix into stack.
    void Push()
    {
      if (++myStackHead >= myStack.Size())
      {
        myStack.Append (myCurrent);
      }
      else
      {
        myStack.SetValue (myStackHead, myCurrent);
      }
    }

    //! Pops matrix from stack to current.
    void Pop()
    {
      Standard_ASSERT_RETURN (myStackHead != -1, "Matrix stack already empty when MatrixState.Pop() called.", );
      myCurrent = myStack.Value (myStackHead--);
    }

    //! @return current matrix.
    const typename MatrixType<T>::Mat4& Current()
    {
      return myCurrent;
    }

    //! Sets given matrix as current.
    void SetCurrent (const typename MatrixType<T>::Mat4& theNewCurrent)
    {
      myCurrent = theNewCurrent;
    }

    //! Sets given matrix as current.
    template <typename Other_t>
    void SetCurrent (const typename MatrixType<Other_t>::Mat4& theNewCurrent)
    {
      myCurrent.Convert (theNewCurrent);
    }

    //! Sets current matrix to identity.
    void SetIdentity()
    {
      myCurrent = typename MatrixType<T>::Mat4();
    }

  private:

    NCollection_Vector<typename MatrixType<T>::Mat4> myStack;     //!< Collection used to maintenance matrix stack
    typename MatrixType<T>::Mat4                     myCurrent;   //!< Current matrix
    Standard_Integer                                 myStackHead; //!< Index of stack head

  };

  //! Constructs a 3D orthographic projection matrix.
  template<class T>
  static void Ortho (typename MatrixType<T>::Mat4& theOut,
    const T theLeft, const T theRight, const T theBottom, const T theTop, const T theZNear, const T theZFar);

  //! Constructs a 2D orthographic projection matrix.
  template<class T>
  static void Ortho2D (typename MatrixType<T>::Mat4& theOut,
    const T theLeft, const T theRight, const T theBottom, const T theTop);

  //! Maps object coordinates to window coordinates.
  template<class T>
  static Standard_Boolean Project (const T                             theObjX,
                                   const T                             theObjY,
                                   const T                             theObjZ,
                                   const typename MatrixType<T>::Mat4& theModViewMat,
                                   const typename MatrixType<T>::Mat4& theProjectMat,
                                   const Standard_Integer              theViewport[4],
                                   T&                                  theWinX,
                                   T&                                  theWinY,
                                   T&                                  theWinZ);

  //! Maps window coordinates to object coordinates.
  template<class T>
  static Standard_Boolean UnProject (const T                             theWinX,
                                     const T                             theWinY,
                                     const T                             theWinZ,
                                     const typename MatrixType<T>::Mat4& theModViewMat,
                                     const typename MatrixType<T>::Mat4& theProjectMat,
                                     const Standard_Integer              theViewport[4],
                                     T&                                  theObjX,
                                     T&                                  theObjY,
                                     T&                                  theObjZ);

  //! Constructs a 4x4 rotation matrix.
  template<class T>
  static void ConstructRotate (typename MatrixType<T>::Mat4& theOut,
                               T                             theA,
                               T                             theX,
                               T                             theY,
                               T                             theZ);

  //! Constructs a 4x4 rotation matrix.
  template<class T>
  static void Rotate (typename MatrixType<T>::Mat4& theOut,
                      T                             theA,
                      T                             theX,
                      T                             theY,
                      T                             theZ);

  //! Constructs a 4x4 scaling matrix.
  template<class T>
  static void Scale (typename MatrixType<T>::Mat4& theOut,
                     T                             theX,
                     T                             theY,
                     T                             theZ);

  //! Constructs a 4x4 translation matrix.
  template<class T>
  static void Translate (typename MatrixType<T>::Mat4& theOut,
                         T                             theX,
                         T                             theY,
                         T                             theZ);

}

// =======================================================================
// function : Rotate
// purpose  : Constructs a 4x4 rotation matrix
// =======================================================================
template<class T>
void OpenGl_Utils::Rotate (typename MatrixType<T>::Mat4& theOut,
                           T                             theA,
                           T                             theX,
                           T                             theY,
                           T                             theZ)
{
  typename MatrixType<T>::Mat4 aMat;
  ConstructRotate (aMat, theA, theX, theY, theZ);
  theOut = theOut * aMat;
}

// =======================================================================
// function : Translate
// purpose  : Constructs a 4x4 translation matrix
// =======================================================================
template<class T>
void OpenGl_Utils::Translate (typename MatrixType<T>::Mat4& theOut,
                              T                             theX,
                              T                             theY,
                              T                             theZ)
{
  theOut.ChangeValue (0, 3) = theOut.GetValue (0, 0) * theX +
                              theOut.GetValue (0, 1) * theY +
                              theOut.GetValue (0, 2) * theZ +
                              theOut.GetValue (0, 3);

  theOut.ChangeValue (1, 3) = theOut.GetValue (1, 0) * theX +
                              theOut.GetValue (1, 1) * theY +
                              theOut.GetValue (1, 2) * theZ +
                              theOut.GetValue (1, 3);

  theOut.ChangeValue (2, 3) = theOut.GetValue (2, 0) * theX +
                              theOut.GetValue (2, 1) * theY +
                              theOut.GetValue (2, 2) * theZ +
                              theOut.GetValue (2, 3);

  theOut.ChangeValue (3, 3) = theOut.GetValue (3, 0) * theX +
                              theOut.GetValue (3, 1) * theY +
                              theOut.GetValue (3, 2) * theZ +
                              theOut.GetValue (3, 3);
}

// =======================================================================
// function : Scale
// purpose  : Constructs a 4x4 scaling matrix
// =======================================================================
template<class T>
void OpenGl_Utils::Scale (typename MatrixType<T>::Mat4& theOut,
                          T                             theX,
                          T                             theY,
                          T                             theZ)
{
  theOut.ChangeValue (0, 0) *= theX;
  theOut.ChangeValue (1, 0) *= theX;
  theOut.ChangeValue (2, 0) *= theX;
  theOut.ChangeValue (3, 0) *= theX;

  theOut.ChangeValue (0, 1) *= theY;
  theOut.ChangeValue (1, 1) *= theY;
  theOut.ChangeValue (2, 1) *= theY;
  theOut.ChangeValue (3, 1) *= theY;

  theOut.ChangeValue (0, 2) *= theZ;
  theOut.ChangeValue (1, 2) *= theZ;
  theOut.ChangeValue (2, 2) *= theZ;
  theOut.ChangeValue (3, 2) *= theZ;
}

// =======================================================================
// function : ConstructRotate
// purpose  : Constructs a 4x4 rotation matrix
// =======================================================================
template<class T>
void OpenGl_Utils::ConstructRotate (typename MatrixType<T>::Mat4& theOut, T theA, T theX, T theY, T theZ)
{
  const T aSin = std::sin (theA * static_cast<T> (M_PI / 180.0));
  const T aCos = std::cos (theA * static_cast<T> (M_PI / 180.0));

  const Standard_Boolean isOnlyX = (theX != static_cast<T> (0.0))
                                && (theY == static_cast<T> (0.0))
                                && (theZ == static_cast<T> (0.0));

  const Standard_Boolean isOnlyY = (theX == static_cast<T> (0.0))
                                && (theY != static_cast<T> (0.0))
                                && (theZ == static_cast<T> (0.0));

  const Standard_Boolean isOnlyZ = (theX == static_cast<T> (0.0))
                                && (theY == static_cast<T> (0.0))
                                && (theZ != static_cast<T> (0.0));

  if (isOnlyX) // Rotation only around X
  {
    theOut.SetValue (1, 1, aCos);
    theOut.SetValue (2, 2, aCos);

    if (theX < static_cast<T> (0.0))
    {
      theOut.SetValue (1, 2,  aSin);
      theOut.SetValue (2, 1, -aSin);
    }
    else
    {
      theOut.SetValue (1, 2, -aSin);
      theOut.SetValue (2, 1,  aSin);
    }

    return;
  }
  else if (isOnlyY) // Rotation only around Y
  {
    theOut.SetValue (0, 0, aCos);
    theOut.SetValue (2, 2, aCos);

    if (theY < static_cast<T> (0.0))
    {
      theOut.SetValue (0, 2, -aSin);
      theOut.SetValue (2, 0,  aSin);
    }
    else
    {
      theOut.SetValue (0, 2,  aSin);
      theOut.SetValue (2, 0, -aSin);
    }

    return;
  }
  else if (isOnlyZ) // Rotation only around Z
  {
    theOut.SetValue (0, 0, aCos);
    theOut.SetValue (1, 1, aCos);

    if (theZ < static_cast<T> (0.0))
    {
      theOut.SetValue (0, 1,  aSin);
      theOut.SetValue (1, 0, -aSin);
    }
    else
    {
      theOut.SetValue (0, 1, -aSin);
      theOut.SetValue (1, 0,  aSin);
    }

    return;
  }

  T aNorm = std::sqrt (theX * theX + theY * theY + theZ * theZ);

  if (aNorm <= static_cast<T> (1.0e-4))
  {
    return; // negligible rotation
  }

  aNorm = static_cast<T> (1.0) / aNorm;

  theX *= aNorm;
  theY *= aNorm;
  theZ *= aNorm;

  const T aXX = theX * theX;
  const T aYY = theY * theY;
  const T aZZ = theZ * theZ;
  const T aXY = theX * theY;
  const T aYZ = theY * theZ;
  const T aZX = theZ * theX;
  const T aSinX = theX * aSin;
  const T aSinY = theY * aSin;
  const T aSinZ = theZ * aSin;

  const T aOneMinusCos = static_cast<T> (1.0) - aCos;

  theOut.SetValue (0, 0, aOneMinusCos * aXX + aCos);
  theOut.SetValue (0, 1, aOneMinusCos * aXY - aSinZ);
  theOut.SetValue (0, 2, aOneMinusCos * aZX + aSinY);

  theOut.SetValue (1, 0, aOneMinusCos * aXY + aSinZ);
  theOut.SetValue (1, 1, aOneMinusCos * aYY + aCos);
  theOut.SetValue (1, 2, aOneMinusCos * aYZ - aSinX);

  theOut.SetValue (2, 0, aOneMinusCos * aZX - aSinY);
  theOut.SetValue (2, 1, aOneMinusCos * aYZ + aSinX);
  theOut.SetValue (2, 2, aOneMinusCos * aZZ + aCos);
}

// =======================================================================
// function : Ortho
// purpose  : Constructs a 3D orthographic projection matrix
// =======================================================================
template<class T>
void OpenGl_Utils::Ortho (typename MatrixType<T>::Mat4& theOut,
  const T theLeft, const T theRight, const T theBottom, const T theTop, const T theZNear, const T theZFar)
{
  theOut.InitIdentity();

  T* aData = theOut.ChangeData();

  const T anInvDx = static_cast<T> (1.0) / (theRight - theLeft);
  const T anInvDy = static_cast<T> (1.0) / (theTop - theBottom);
  const T anInvDz = static_cast<T> (1.0) / (theZFar - theZNear);

  aData[0]  = static_cast<T> ( 2.0) * anInvDx;
  aData[5]  = static_cast<T> ( 2.0) * anInvDy;
  aData[10] = static_cast<T> (-2.0) * anInvDz;

  aData[12] = -(theRight + theLeft) * anInvDx;
  aData[13] = -(theTop + theBottom) * anInvDy;
  aData[14] = -(theZFar + theZNear) * anInvDz;
}

// =======================================================================
// function : Ortho2D
// purpose  : Constructs a 2D orthographic projection matrix
// =======================================================================
template<class T>
void OpenGl_Utils::Ortho2D (typename MatrixType<T>::Mat4& theOut,
  const T theLeft, const T theRight, const T theBottom, const T theTop)
{
  Ortho (theOut, theLeft, theRight, theBottom, theTop, static_cast<T> (-1.0), static_cast<T> (1.0));
}

// =======================================================================
// function : Project
// purpose  : Maps object coordinates to window coordinates
// =======================================================================
template<class T>
static Standard_Boolean OpenGl_Utils::Project (const T                             theObjX,
                                               const T                             theObjY,
                                               const T                             theObjZ,
                                               const typename MatrixType<T>::Mat4& theModViewMat,
                                               const typename MatrixType<T>::Mat4& theProjectMat,
                                               const Standard_Integer              theViewport[4],
                                               T&                                  theWinX,
                                               T&                                  theWinY,
                                               T&                                  theWinZ)
{
  typename VectorType<T>::Vec4 anIn (theObjX, theObjY, theObjZ, static_cast<T> (1.0));

  typename VectorType<T>::Vec4 anOut = theProjectMat * (theModViewMat * anIn);

  if (anOut.w() == static_cast<T> (0.0))
  {
    return Standard_False;
  }

  anOut.w() = static_cast<T> (1.0) / anOut.w();

  anOut.x() *= anOut.w();
  anOut.y() *= anOut.w();
  anOut.z() *= anOut.w();

  // Map x, y and z to range 0-1
  anOut.x() = anOut.x() * 0.5 + 0.5;
  anOut.y() = anOut.y() * 0.5 + 0.5;
  anOut.z() = anOut.z() * 0.5 + 0.5;

  // Map x,y to viewport
  anOut.x() = anOut.x() * theViewport[2] + theViewport[0];
  anOut.y() = anOut.y() * theViewport[3] + theViewport[1];

  theWinX = anOut.x();
  theWinY = anOut.y();
  theWinZ = anOut.z();

  return Standard_True;
}

// =======================================================================
// function : UnProject
// purpose  : Maps window coordinates to object coordinates
// =======================================================================
template<class T>
static Standard_Boolean OpenGl_Utils::UnProject (const T                             theWinX,
                                                 const T                             theWinY,
                                                 const T                             theWinZ,
                                                 const typename MatrixType<T>::Mat4& theModViewMat,
                                                 const typename MatrixType<T>::Mat4& theProjectMat,
                                                 const Standard_Integer              theViewport[4],
                                                 T&                                  theObjX,
                                                 T&                                  theObjY,
                                                 T&                                  theObjZ)
{
  typename MatrixType<T>::Mat4 anUnviewMat;

  if (!(theProjectMat * theModViewMat).Inverted (anUnviewMat))
  {
    return Standard_False;
  }

  typename VectorType<T>::Vec4 anIn (theWinX, theWinY, theWinZ, static_cast<T> (1.0));

  // Map x and y from window coordinates
  anIn.x() = (anIn.x() - theViewport[0]) / theViewport[2];
  anIn.y() = (anIn.y() - theViewport[1]) / theViewport[3];

  // Map to range -1 to 1
  anIn.x() = anIn.x() * static_cast<T> (2.0) - static_cast<T> (1.0);
  anIn.y() = anIn.y() * static_cast<T> (2.0) - static_cast<T> (1.0);
  anIn.z() = anIn.z() * static_cast<T> (2.0) - static_cast<T> (1.0);

  typename VectorType<T>::Vec4 anOut = anUnviewMat * anIn;

  if (anOut.w() == static_cast<T> (0.0))
  {
    return Standard_False;
  }

  anOut.w() = static_cast<T> (1.0) / anOut.w();

  anOut.x() *= anOut.w();
  anOut.y() *= anOut.w();
  anOut.z() *= anOut.w();

  theObjX = anOut.x();
  theObjY = anOut.y();
  theObjZ = anOut.z();

  return Standard_True;
}

#endif // _OpenGl_Utils_H__