vector_gen.h 7.66 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                              *
 * This library is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation; either version 2.1 of the License, or (at your     *
 * option) any later version.                                                   *
 *                                                                              *
 * This library 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 Lesser General Public License  *
 * for more details.                                                            *
 *                                                                              *
 * You should have received a copy of the GNU Lesser General Public License     *
 * along with this library; if not, write to the Free Software Foundation,      *
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
 *                                                                              *
20
 * Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
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
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __VECTOR_GEN__
#define __VECTOR_GEN__

#include <cassert>
#include <iostream>
#include <math.h>
#include <string.h>

#include "Utils/static_assert.h"
#include "Utils/nameTypes.h"

namespace CGoGN
{

namespace Geom
{

/*
 * Class for the representation of vectors
 */
template <unsigned int DIM, typename T>
class Vector
{
public:
	typedef T DATA_TYPE ;
David Cazier's avatar
-  
David Cazier committed
50 51 52 53
	enum
	{
		DIMENSION = DIM
	} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56 57 58 59 60 61 62

	static std::string CGoGNnameOfType() ;

	/**********************************************/
	/*                CONSTRUCTORS                */
	/**********************************************/

	Vector() ;

David Cazier's avatar
-  
David Cazier committed
63
	Vector(const Vector<DIM, T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
64

65
	template <typename T2>
David Cazier's avatar
-  
David Cazier committed
66
	Vector(const Vector<DIM, T2>& v) ;
67

68 69 70 71 72 73
	/**
	 * @brief Vector constructor from list init. (c++11)
	 * @param args {x,y,z}
	 */
	Vector(typename std::initializer_list<T> args);

Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77 78 79 80 81 82 83
	Vector(T x, T y) ;

	Vector(T x, T y, T z) ;

	Vector(T x, T y, T z, T w) ;

	/**
	 * constructor that initialize all component to a given value
	 * @param x the value to assign to all component
	 */
84
    explicit Vector(T x) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	void set(T a) ;

	void zero() ;

	/**********************************************/
	/*                 ACCESSORS                  */
	/**********************************************/

	T& operator[](unsigned int index) ;

	const T& operator[](unsigned int index) const ;

	unsigned int dimension() const ;

	T* data() ;

	const T* data() const ;

	/**********************************************/
	/*         ARITHMETIC SELF-OPERATORS          */
	/**********************************************/

David Cazier's avatar
-  
David Cazier committed
108
	Vector<DIM, T>& operator+=(const Vector<DIM, T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109

David Cazier's avatar
-  
David Cazier committed
110
	Vector<DIM, T>& operator-=(const Vector<DIM, T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111

Sylvain Thery's avatar
Sylvain Thery committed
112
	Vector<DIM, T>& operator*=(T a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
113

Sylvain Thery's avatar
Sylvain Thery committed
114
	Vector<DIM, T>& operator/=(T a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119

	/**********************************************/
	/*            ARITHMETIC OPERATORS            */
	/**********************************************/

David Cazier's avatar
-  
David Cazier committed
120
	Vector<DIM, T> operator+(const Vector<DIM, T>& v) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
121

David Cazier's avatar
-  
David Cazier committed
122
	Vector<DIM, T> operator-(const Vector<DIM, T>& v) const ;
123 124
	
	Vector<DIM, T> operator-() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125

126 127 128 129 130
 	Vector<DIM, T> operator*(T a) const ;
	
// 	template <typename T2>
// 	Vector<DIM, T> operator*(T2 a) const ;
	
Pierre Kraemer's avatar
Pierre Kraemer committed
131

David Cazier's avatar
-  
David Cazier committed
132
	Vector<DIM, T> operator/(T a) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
133 134 135 136 137 138 139 140 141

	/**********************************************/
	/*             UTILITY FUNCTIONS              */
	/**********************************************/

	T norm2() const ;

	double norm() const ;

142 143 144
	/*
	 * normalize the vector and returns its norm
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
	double normalize() ;

147 148 149 150 151
	/*
	 * Return a normalized copy
	 */
	Vector<DIM, T> normalized() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
152
	// dot product
Pierre Kraemer's avatar
Pierre Kraemer committed
153
	T operator*(const Vector<DIM, T>& v) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155

	// cross product
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	Vector<DIM, T> operator^(const Vector<DIM, T>& v) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158

	// Equal
David Cazier's avatar
-  
David Cazier committed
159
	bool operator==(const Vector<DIM, T>& v) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160

Pierre Kraemer's avatar
Pierre Kraemer committed
161
	// Different
David Cazier's avatar
-  
David Cazier committed
162
	bool operator!=(const Vector<DIM, T>& v) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163

164 165
	bool hasNan() const ;

David Cazier's avatar
David Cazier committed
166
	bool isFinite() const ;
David Cazier's avatar
-  
David Cazier committed
167

168 169 170 171 172 173 174
	/**
	 * Tests if the vector is normalized
	 * @param epsilon tolerated error
	 * @return true if the given vector has a unit norm +/- epsilon
	 */
	bool isNormalized(const T& epsilon) const ;

David Cazier's avatar
David Cazier committed
175 176 177 178 179 180 181 182
	/**
	 * Tests if current and given vectors are near within 1/precision (equal if precision is zero)
	 * @param V a vector
	 * @param epsilon tolerated error
	 * @return true if orthogonal
	 */
	bool isNear(const Vector<DIM, T>& v, int precision) const ;

183 184 185 186 187 188
	/**
	 * Tests if current and given vectors are orthogonal
	 * @param V a vector
	 * @param epsilon tolerated error
	 * @return true if orthogonal
	 */
David Cazier's avatar
David Cazier committed
189
	bool isOrthogonal(const Vector<DIM, T>& v, const T& epsilon = 1e-5) const ;
190

Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194 195
	/**********************************************/
	/*             STREAM OPERATORS               */
	/**********************************************/

	template <unsigned int DD, typename TT>
David Cazier's avatar
-  
David Cazier committed
196
	friend std::ostream& operator<<(std::ostream& out, const Vector<DD, TT>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198

	template <unsigned int DD, typename TT>
David Cazier's avatar
-  
David Cazier committed
199
	friend std::istream& operator>>(std::istream& in, Vector<DD, TT>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
200 201 202

private:
	T m_data[DIM] ;
David Cazier's avatar
-  
David Cazier committed
203
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
204

David Cazier's avatar
David Cazier committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/***
 * Test if x is null within precision.
 * Two cases are possible :
 *  - precision == 0 : x is null if (x == 0)
 *  - precision > 0 : x is null if (|x| < 1/precision) or (precision * |x| < 1)
 */
template <typename T>
bool isNull(T x, int precision = 0) ;

/***
 * Test if the square root of x is null within precision.
 * In other words, test if x is null within precision*precision
 */
template <typename T>
bool isNull2(T x, int precision = 0) ;

221 222
// template <unsigned int DIM, typename T>
// Vector<DIM, T> operator*(T a, const Vector<DIM, T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224

template <unsigned int DIM, typename T>
David Cazier's avatar
-  
David Cazier committed
225
Vector<DIM, T> operator/(T a, const Vector<DIM, T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
226 227 228

// returns the signed volume of the parallelepiped spanned by vectors v1, v2 and v3
template <unsigned int DIM, typename T>
David Cazier's avatar
-  
David Cazier committed
229
T tripleProduct(const Vector<DIM, T>& v1, const Vector<DIM, T>& v2, const Vector<DIM, T>& v3) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232

// returns a spherical interpolation of two vectors considering parameter t ((0 <= t <= 1) => result between v1 and v2)
template <unsigned int DIM, typename T>
David Cazier's avatar
-  
David Cazier committed
233
Vector<DIM, T> slerp(const Vector<DIM, T> &v1, const Vector<DIM, T> &v2, const T &t) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
234

235 236 237
template <unsigned int DIM, typename T, typename T2>
Vector<DIM, T> operator*(T2 b, const Vector<DIM, T>& v);

Pierre Kraemer's avatar
Pierre Kraemer committed
238 239 240 241 242

/**********************************************/
/*           SOME USEFUL TYPEDEFS             */
/**********************************************/

David Cazier's avatar
-  
David Cazier committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
typedef Vector<2, float> Vec2f ;
typedef Vector<2, double> Vec2d ;
typedef Vector<2, unsigned int> Vec2ui ;
typedef Vector<2, int> Vec2i ;
typedef Vector<2, unsigned char> Vec2uc ;

typedef Vector<3, float> Vec3f ;
typedef Vector<3, double> Vec3d ;
typedef Vector<3, unsigned int> Vec3ui ;
typedef Vector<3, int> Vec3i ;
typedef Vector<3, unsigned char> Vec3uc ;

typedef Vector<4, float> Vec4f ;
typedef Vector<4, double> Vec4d ;
typedef Vector<4, unsigned int> Vec4ui ;
typedef Vector<4, int> Vec4i ;
typedef Vector<4, unsigned char> Vec4uc ;
Pierre Kraemer's avatar
Pierre Kraemer committed
260 261 262 263 264

}

}

Pierre Kraemer's avatar
Pierre Kraemer committed
265
#include "Geometry/vector_gen.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267

#endif