vec.cpp
1 /****************************************************************************
2 
3  Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.3.10.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #include "domUtils.h"
24 #include "vec.h"
25 
26 // Most of the methods are declared inline in vec.h
27 
28 using namespace qglviewer;
29 using namespace std;
30 
34 void Vec::projectOnAxis(const Vec& direction)
35 {
36 #ifndef QT_NO_DEBUG
37  if (direction.squaredNorm() < 1.0E-10)
38  qWarning("Vec::projectOnAxis: axis direction is not normalized (norm=%f).", direction.norm());
39 #endif
40 
41  *this = (((*this)*direction) / direction.squaredNorm()) * direction;
42 }
43 
47 void Vec::projectOnPlane(const Vec& normal)
48 {
49 #ifndef QT_NO_DEBUG
50  if (normal.squaredNorm() < 1.0E-10)
51  qWarning("Vec::projectOnPlane: plane normal is not normalized (norm=%f).", normal.norm());
52 #endif
53 
54  *this -= (((*this)*normal) / normal.squaredNorm()) * normal;
55 }
56 
60 {
61  // Find smallest component. Keep equal case for null values.
62  if ((fabs(y) >= 0.9*fabs(x)) && (fabs(z) >= 0.9*fabs(x)))
63  return Vec(0.0, -z, y);
64  else
65  if ((fabs(x) >= 0.9*fabs(y)) && (fabs(z) >= 0.9*fabs(y)))
66  return Vec(-z, 0.0, x);
67  else
68  return Vec(-y, x, 0.0);
69 }
70 
78 Vec::Vec(const QDomElement& element)
79 {
80  QStringList attribute;
81  attribute << "x" << "y" << "z";
82 #if QT_VERSION >= 0x040000
83  for (int i=0; i<attribute.size(); ++i)
84 #else
85  for (unsigned int i=0; i<attribute.count(); ++i)
86 #endif
87 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
88  this->operator[](i) = DomUtils::doubleFromDom(element, attribute[i], 0.0);
89 #else
90  v_[i] = DomUtils::doubleFromDom(element, attribute[i], 0.0);
91 #endif
92 }
93 
128 QDomElement Vec::domElement(const QString& name, QDomDocument& document) const
129 {
130  QDomElement de = document.createElement(name);
131  de.setAttribute("x", QString::number(x));
132  de.setAttribute("y", QString::number(y));
133  de.setAttribute("z", QString::number(z));
134  return de;
135 }
136 
158 void Vec::initFromDOMElement(const QDomElement& element)
159 {
160  const Vec v(element);
161  *this = v;
162 }
163 
164 ostream& operator<<(ostream& o, const Vec& v)
165 {
166  return o << v.x << '\t' << v.y << '\t' << v.z;
167 }
168