GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_vector4d.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
23 
25 #include "glc_vector4d.h"
26 #include <QtDebug>
27 
28 using namespace glc;
30 // Operators overload
32 
33 
34 // Overload dot product "^" operator
36 {
37  GLC_Vector4d VectResult;
38  VectResult.vector[0]= vector[1] * Vect.vector[2] - vector[2] * Vect.vector[1];
39  VectResult.vector[1]= vector[2] * Vect.vector[0] - vector[0] * Vect.vector[2];
40  VectResult.vector[2]= vector[0] * Vect.vector[1] - vector[1] * Vect.vector[0];
41 
42  return VectResult;
43 }
44 
45 // Overload equality "==" operator
47 {
48  bool bResult= qFuzzyCompare(vector[0], Vect.vector[0]);
49  bResult= bResult && qFuzzyCompare(vector[1], Vect.vector[1]);
50  bResult= bResult && qFuzzyCompare(vector[2], Vect.vector[2]);
51  bResult= bResult && qFuzzyCompare(vector[3], Vect.vector[3]);
52 
53  return bResult;
54 }
56 // Set Function
58 
59 GLC_Vector4d& GLC_Vector4d::setW(const double &dW)
60 {
61  if (dW != 0)
62  {
63  const double invDW= 1.0 / dW;
64  vector[0]*= invDW;
65  vector[1]*= invDW;
66  vector[2]*= invDW;
67  vector[3]= 1.0; // For calculation, W = 1.
68  }
69  return *this;
70 }
71 
72 GLC_Vector4d& GLC_Vector4d::setVect(const double &dX, const double &dY,
73  const double &dZ, const double &dW)
74 {
75  if ((dW == 1.0) || (dW <= 0.0))
76  {
77  vector[0]= dX;
78  vector[1]= dY;
79  vector[2]= dZ;
80  }
81  else
82  {
83  const double invDW= 1.0 / dW;
84  vector[0]= dX * invDW;
85  vector[1]= dY * invDW;
86  vector[2]= dZ * invDW;
87  }
88 
89  vector[3]= 1.0; // For calculation, W = 1.
90 
91  return *this;
92 }
93 
95 {
96  const double dNormeCur= sqrt( vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
97 
98  if (dNormeCur != 0)
99  {
100  const double Coef = Norme / dNormeCur;
101 
102  vector[0] = vector[0] * Coef;
103  vector[1] = vector[1] * Coef;
104  vector[2] = vector[2] * Coef;
105  }
106  return *this;
107 }
108 
110 // Get Function
112 
113 // Return the Angle with another vector
115 {
116  GLC_Vector4d ThisVect(*this);
117  ThisVect.setNormal(1);
118  Vect.setNormal(1);
119  // Rotation axis
120  const GLC_Vector4d VectAxeRot(ThisVect ^ Vect);
121  // Check if the rotation axis vector is null
122  if (!VectAxeRot.isNull())
123  {
124  return acos(ThisVect * Vect);
125  }
126  else return 0.0;
127 }
128 
129 // return the vector string
130 QString GLC_Vector4d::toString() const
131 {
132  QString result("[");
133 
134  result+= QString::number(vector[0]) + QString(" , ");
135  result+= QString::number(vector[1]) + QString(" , ");
136  result+= QString::number(vector[2]) + QString(" , ");
137  result+= QString::number(vector[3]) + QString("]");
138 
139  return result;
140 }
141 
142 // return the 2D vector
144 {
145  double x;
146  double y;
147  if (mask.vector[0] == 0.0)
148  {
149  x= vector[0];
150  if (mask.vector[1] == 0.0)
151  y= vector[1];
152  else
153  y= vector[2];
154 
155  }
156  else
157  {
158  x= vector[1];
159  y= vector[2];
160 
161  }
162  return GLC_Vector2d(x, y);
163 }
164 
166 // Services private function
168 
169 // Normalize Vector w <- 1
171 {
172  if (fabs(vector[3]) > 0.00001)
173  {
174  const double invW= 1.0 / vector[3];
175  vector[0]*= invW;
176  vector[1]*= invW;
177  vector[2]*= invW;
178  }
179  vector[3]= 1.0;
180 }
181 
182 // Non-member stream operator
183 QDataStream &operator<<(QDataStream &stream, const GLC_Vector4d &vector)
184 {
185  stream << vector.X() << vector.Y() << vector.Z() << vector.W();
186  return stream;
187 }
188 QDataStream &operator>>(QDataStream &stream, GLC_Vector4d &vector)
189 {
190  double x, y, z, w;
191  stream >> x >> y >> z >> w;
192  vector.setVect(x, y, z, w);
193  return stream;
194 }
195 
196 

©2005-2013 Laurent Ribon