vec.cpp
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.5.2.
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  for (int i=0; i<attribute.size(); ++i)
83 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
84  this->operator[](i) = DomUtils::doubleFromDom(element, attribute[i], 0.0);
85 #else
86  v_[i] = DomUtils::doubleFromDom(element, attribute[i], 0.0);
87 #endif
88 }
89 
124 QDomElement Vec::domElement(const QString& name, QDomDocument& document) const
125 {
126  QDomElement de = document.createElement(name);
127  de.setAttribute("x", QString::number(x));
128  de.setAttribute("y", QString::number(y));
129  de.setAttribute("z", QString::number(z));
130  return de;
131 }
132 
154 void Vec::initFromDOMElement(const QDomElement& element)
155 {
156  const Vec v(element);
157  *this = v;
158 }
159 
160 ostream& operator<<(ostream& o, const Vec& v)
161 {
162  return o << v.x << '\t' << v.y << '\t' << v.z;
163 }
164 
double squaredNorm() const
Returns the squared norm of the Vec.
Definition: vec.h:332
double norm() const
Returns the norm of the vector.
Definition: vec.h:335
Vec orthogonalVec() const
Returns a Vec orthogonal to the Vec.
Definition: vec.cpp:59
Vec()
Default constructor.
Definition: vec.h:89
void initFromDOMElement(const QDomElement &element)
Restores the Vec state from a QDomElement created by domElement().
Definition: vec.cpp:154
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:65
void projectOnPlane(const Vec &normal)
Projects the Vec on the plane whose normal is normal that passes through the origin.
Definition: vec.cpp:47
QDomElement domElement(const QString &name, QDomDocument &document) const
Returns an XML QDomElement that represents the Vec.
Definition: vec.cpp:124
void projectOnAxis(const Vec &direction)
Projects the Vec on the axis of direction direction that passes through the origin.
Definition: vec.cpp:34