Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppGLWidget_vec.h « app_blender « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 47643a19e8ef62efef289920d1a8b9ff8657784f (plain)
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
/****************************************************************************

 Copyright (C) 2002-2007 Gilles Debunne (Gilles.Debunne@imag.fr)

 This file is part of the QGLViewer library.
 Version 2.2.6-3, released on August 28, 2007.

 http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer

 libQGLViewer is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 libQGLViewer 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with libQGLViewer; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*****************************************************************************/

#ifndef QGLVIEWER_VEC_H
#define QGLVIEWER_VEC_H

#include "AppGLWidget_config.h"

// #include <qapplication.h>

// Included by all files as vec.h is at the end of the include hierarchy
//soc #include "config.h" // Specific configuration options.

/*! \brief The Vec class represents 3D positions and 3D vectors.
  \class Vec vec.h QGLViewer/vec.h

  Vec is used as a parameter and return type by many methods of the library. It provides classical
  algebraic computational methods and is compatible with OpenGL:

  \code
  // Draws a point located at 3.0 OpenGL units in front of the camera
  Vec pos = camera()->position() + 3.0 * camera()->viewDirection();
  glBegin(GL_POINTS);
  glVertex3fv(pos);
  glEnd();
  \endcode

  This makes of Vec a good candidate for representing positions and vectors in your programs. Since
  it is part of the \c qglviewer namespace, specify \c qglviewer::Vec or use the qglviewer
  namespace:
  \code
  using namespace qglviewer;
  \endcode

  <h3>Interface with other vector classes</h3>

  Vec implements a universal explicit converter, based on the \c [] \c operator.
  Everywhere a \c const \c Vec& argument is expected, you can use your own vector type
  instead, as long as it implements this operator (see the Vec(const C& c) documentation).

  See also the Quaternion and the Frame documentations.
  \nosubgrouping */
class Vec
{

  // If your compiler complains the "The class "qglviewer::Vec" has no member "x"."
  // Add your architecture Q_OS_XXXX flag (see qglobal.h) in this list.
#if defined (Q_OS_IRIX) || defined (Q_OS_AIX) || defined (Q_OS_HPUX)
# define QGLVIEWER_UNION_NOT_SUPPORTED
#endif

public:
  /*! The internal data representation is public. One can use v.x, v.y, v.z. See also operator[](). */
#if defined (DOXYGEN) || defined (QGLVIEWER_UNION_NOT_SUPPORTED)
  float x, y, z;
#else
  union
  {
    struct { float x, y, z; };
    float v_[3];
  };
#endif

  /*! @name Setting the value */
  //@{
  /*! Default constructor. Value is set to (0,0,0). */
  Vec() : x(0.0), y(0.0), z(0.0) {}

  /*! Standard constructor with the x, y and z values. */
  Vec(float X, float Y, float Z) : x(X), y(Y), z(Z) {}

  /*! Universal explicit converter from any class to Vec. You can use your own vector class everywhere
  a \c const \c Vec& parameter is required, as long as it implements the \c operator[ ]:

  \code
  class MyVec
  {
    // ...
    float operator[](int i) const { returns x, y or z when i=0, 1 or 2; }
  }

  MyVec v(...);
  camera()->setPosition(v);
  \endcode

  Note that standard vector types (stl, \c float[3], ...) implement this operator and can hence
  be used in place of Vec. See also operator const float*() .*/
  template <class C>
  explicit Vec(const C& c) : x(c[0]), y(c[1]), z(c[2]) {}
  // Should NOT be explicit to prevent conflicts with operator<<.

  // ! Copy constructor
  // Vec(const Vec& v) : x(v.x), y(v.y), z(v.z) {}

  /*! Equal operator. */
  Vec& operator=(const Vec& v)
  {
    x = v.x;   y = v.y;   z = v.z;
    return *this;
  }

  /*! Set the current value. Maybe faster than using operator=() with a temporary Vec(x,y,z). */
  void setValue(float X, float Y, float Z)
  { x=X; y=Y; z=Z; }

  // Universal equal operator which allows the use of any type in place of Vec,
  // as long as the [] operator is implemented (v[0]=v.x, v[1]=v.y, v[2]=v.z).
  // template <class C>
  // Vec& operator=(const C& c)
  // {
  // x=c[0]; y=c[1]; z=c[2];
  // return *this;
  // }
  //@}

  /*! @name Accessing the value */
  //@{
  /*! Bracket operator, with a constant return value. \p i must range in [0..2]. */
  float operator[](int i) const {
#ifdef QGLVIEWER_UNION_NOT_SUPPORTED
    return (&x)[i];
#else
    return v_[i];
#endif
  }

  /*! Bracket operator returning an l-value. \p i must range in [0..2]. */
  float& operator[](int i) {
#ifdef QGLVIEWER_UNION_NOT_SUPPORTED
    return (&x)[i];
#else
    return v_[i];
#endif
  }

#ifndef DOXYGEN
  /*! This method is deprecated since version 2.0. Use operator const float* instead. */
  const float* address() const { cout << "Vec::address() is deprecated, use operator const float* instead." << endl; return operator const float*(); };
#endif

  /*! Conversion operator returning the memory address of the vector.

  Very convenient to pass a Vec pointer as a parameter to OpenGL functions:
  \code
  Vec pos, normal;
  glNormal3fv(normal);
  glVertex3fv(pos);
  \endcode */
  operator const float*() const {
#ifdef QGLVIEWER_UNION_NOT_SUPPORTED
    return &x;
#else
    return v_;
#endif
  }

  /*! Non const conversion operator returning the memory address of the vector.

  Useful to pass a Vec to a method that requires and fills a \c float*, as provided by certain libraries. */
  operator float*() {
#ifdef QGLVIEWER_UNION_NOT_SUPPORTED
    return &x;
#else
    return v_;
#endif
  }
  //@}

  /*! @name Algebraic computations */
  //@{
  /*! Returns the sum of the two vectors. */
  friend Vec operator+(const Vec &a, const Vec &b)
  {
    return Vec(a.x+b.x, a.y+b.y, a.z+b.z);
  }

  /*! Returns the difference of the two vectors. */
  friend Vec operator-(const Vec &a, const Vec &b)
  {
    return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
  }

  /*! Unary minus operator. */
  friend Vec operator-(const Vec &a)
  {
    return Vec(-a.x, -a.y, -a.z);
  }

  /*! Returns the product of the vector with a scalar. */
  friend Vec operator*(const Vec &a, float k)
  {
    return Vec(a.x*k, a.y*k, a.z*k);
  }

  /*! Returns the product of the vector with a scalar. */
  friend Vec operator*(float k, const Vec &a)
  {
    return a*k;
  }

  /*! Returns the division of the vector with a scalar.

  Too small \p k values are \e not tested (unless the library was compiled with the "debug" Qt \c
  CONFIG flag) and may result in \c NaN values. */
  friend Vec operator/(const Vec &a, float k)
  {
#ifndef QT_NO_DEBUG
    if (fabs(k) < 1.0E-10)
		cout << "Vec::operator / : dividing by a null value" << endl;
#endif
    return Vec(a.x/k, a.y/k, a.z/k);
  }

  /*! Returns \c true only when the two vector are not equal (see operator==()). */
  friend bool operator!=(const Vec &a, const Vec &b)
  {
    return !(a==b);
  }

  /*! Returns \c true when the squaredNorm() of the difference vector is lower than 1E-10. */
  friend bool operator==(const Vec &a, const Vec &b)
  {
    const float epsilon = 1.0E-10f;
    return (a-b).squaredNorm() < epsilon;
  }

  /*! Adds \p a to the vector. */
  Vec& operator+=(const Vec &a)
  {
    x += a.x; y += a.y; z += a.z;
    return *this;
  }

  /*! Subtracts \p a to the vector. */
  Vec& operator-=(const Vec &a)
  {
    x -= a.x; y -= a.y; z -= a.z;
    return *this;
  }

  /*! Multiply the vector by a scalar \p k. */
  Vec& operator*=(float k)
  {
    x *= k; y *= k; z *= k;
    return *this;
  }

  /*! Divides the vector by a scalar \p k.

  An absolute \p k value lower than 1E-10 will print a warning if the library was compiled with the
  "debug" Qt \c CONFIG flag. Otherwise, no test is performed for efficiency reasons. */
  Vec& operator/=(float k)
  {
#ifndef QT_NO_DEBUG
    if (fabs(k)<1.0E-10)
		cout << "Vec::operator /= : dividing by a null value" << endl;
#endif
    x /= k; y /= k; z /= k;
    return *this;
  }

  /*! Dot product of the two Vec. */
  friend float operator*(const Vec &a, const Vec &b)
  {
    return a.x*b.x + a.y*b.y + a.z*b.z;
  }

  /*! Cross product of the two vectors. Same as cross(). */
  friend Vec operator^(const Vec &a, const Vec &b)
  {
    return cross(a,b);
  }

  /*! Cross product of the two Vec. Mind the order ! */
  friend Vec cross(const Vec &a, const Vec &b)
  {
    return Vec(a.y*b.z - a.z*b.y,
	       a.z*b.x - a.x*b.z,
	       a.x*b.y - a.y*b.x);
  }

  Vec orthogonalVec() const;
  //@}

  /*! @name Norm of the vector */
  //@{
#ifndef DOXYGEN
  /*! This method is deprecated since version 2.0. Use squaredNorm() instead. */
  float sqNorm() const { return x*x + y*y + z*z; }
#endif

  /*! Returns the \e squared norm of the Vec. */
  float squaredNorm() const { return x*x + y*y + z*z; }

  /*! Returns the norm of the vector. */
  float norm() const { return sqrt(x*x + y*y + z*z); }

  /*! Normalizes the Vec and returns its original norm.

  Normalizing a null vector will result in \c NaN values. */
  float normalize()
  {
    const float n = norm();
#ifndef QT_NO_DEBUG
    if (n < 1.0E-10)
		cout << "Vec::normalize: normalizing a null vector" << endl;
#endif
    *this /= n;
    return n;
  }

  /*! Returns a unitary (normalized) \e representation of the vector. The original Vec is not modified. */
  Vec unit() const
  {
    Vec v = *this;
    v.normalize();
    return v;
  }
  //@}

  /*! @name Projection */
  //@{
  void projectOnAxis(const Vec& direction);
  void projectOnPlane(const Vec& normal);
  //@}


#ifdef DOXYGEN
  /*! @name Output stream */
  //@{
  /*! Output stream operator. Enables debugging code like:
  \code
  Vec pos(...);
  cout << "Position=" << pos << endl;
  \endcode */
  std::ostream& operator<<(std::ostream& o, const qglviewer::Vec&);
  //@}
#endif
};


std::ostream& operator<<(std::ostream& o, const Vec&);

#endif // QGLVIEWER_VEC_H