localFrame.h 7.13 KB
Newer Older
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef LOCALFRAME_H_
#define LOCALFRAME_H_

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
28 29
#include <cmath>

30 31 32 33
namespace CGoGN {

namespace Utils {

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
/**
 * Util for rotation of a 3D point (or vector) around a given line (going through the origin) and of a given angle
 * @param axis the rotation axis direction
 * @param angle the rotation angle
 * @param p the point to rotate
 */
template <typename REAL>
Geom::Vector<3,REAL> rotate (Geom::Vector<3,REAL> axis, REAL angle, Geom::Vector<3,REAL> p) ;

/**
 * Util for conversion from spherical to carthesian coordinates.
 * The spherical coordinates are in radius-longitude-latitude
 * @param sph the spherical coordinates
 * @return the carthesian coordinates
 */
template<typename REAL>
Geom::Vector<3,REAL> sphericalToCarth (const Geom::Vector<3,REAL>& sph) ;

/**
 * Util for conversion from carthesian to spherical coordinates.
 * The spherical coordinates are in radius-longitude-latitude
 * @param carth the carthesian coordinates
 * @return the spherical coordinates
 */
template<typename REAL>
Geom::Vector<3,REAL> carthToSpherical (const Geom::Vector<3,REAL>& carth) ;

61 62 63
/**
 * Class for representing a direct local frame composed of 3 orthonormal vectors T (tangent), B (bitangent) and N (normal).
 * This class can compress/decompress a local frame, switching from its explicit representation (3 vectors) to its compressed representation (1 vector).
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
64
 * Two different compressions coexist : one which is not correct on the extreme values (VEC3 getCompressed()) and one which is totally correct (VEC4 getCompressedSecure()).
65 66
 * Usage :
 *  VEC3 T,B,N ;							// current set of orthonormal vectors composing the direct frame.
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
67
 *  LocalFrame<PFP> lf(T,B,N) ; 				// Constructor from explicit expression.
68
 *  if (lf.isOrthoNormalDirect())			// test if the frame is Orthogonal, Normalized and Direct
69
 *   VEC3 compressed = lf.getCompressed() ;	// Extract compressed frame
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
70
 *  LocalFrame<PFP> decompressed(compressed) ;	// Constructor from implicit (compressed) expression.
71 72 73 74 75 76 77
 *
 * All formulae were provided by "Représentation compacte d'un repère local", june 14th, 2011 by K. Vanhoey
 */
template <typename PFP>
class LocalFrame
{
	typedef typename PFP::REAL REAL ;
78
	typedef typename Geom::Vector<2,REAL> VEC2 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
79
	typedef typename Geom::Vector<3,REAL> VEC3 ;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

private: // fields
	/**
	 * The three explicit vectors
	 */
	VEC3 m_T,m_B,m_N ;

public: // methods
	/**
	 * Constructor from explicit data
	 * @param T the tangent vector
	 * @param B the bitangent vector
	 * @param N the normal vector
	 */
	LocalFrame(const VEC3& T, const VEC3& B, const VEC3& N) ;
95

96 97
	/**
	 * Constructor from implicit (compressed representation)
98
	 * @param compressedFrame an implicit (compressed) version of the local frame (can be produced by localFrame.getCompressed())
99 100
	 */
	LocalFrame(const VEC3& compressedFrame) ;
101

102 103 104 105
	~LocalFrame() {} ;

	/**
	 * Returns a compressed version of the current local frame
106
	 * A VEC3 is not sufficient to completely define a local frame (if phiN=0, the decompression is not unique).
107
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
108
	VEC3 getCompressed() const ;
109

110 111 112 113 114 115
	/**
	 * Tests if the frames are identical
	 * @param lf the frame to compare to the current frame
	 * @param epsilon the authorized deviation
	 * @return true if frames are identical (or deviate less than epsilon)
	 */
116
	bool equals(const LocalFrame<PFP>& lf, REAL epsilon = 1e-6) const ;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

	/**
	 * Equality of frames
	 * Identical to calling equals with default epsilon
	 * @return true if frames are identical
	 */
	bool operator==(const LocalFrame<PFP>& lf) const ;

	/**
	 * Inequality of frames
	 * Identical to calling !equals with default epsilon
	 * @return false if frames are identical
	 */
	bool operator!=(const LocalFrame<PFP>& lf) const ;

132 133 134 135
	/**
	 * Tests if the frame is direct
	 * @return true if the frame is direct
	 */
136
	bool isDirect(REAL epsilon = 1e-7) const ;
137 138 139 140 141

	/**
	 * Tests if the frame is orthogonal
	 * @return true if the frame is orthogonal
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
142
	bool isOrthogonal(REAL epsilon = 1e-5) const ;
143 144 145 146 147

	/**
	 * Tests if the frame is normalized
	 * @return true if the frame is normalized
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
148
	bool isNormalized(REAL epsilon = 1e-5) const ;
149 150 151 152 153

	/**
	 * Tests if the frame is direct, normalized and orthogonal
	 * @return true if the frame is direct, normalized and orthogonal
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
154
	bool isOrthoNormalDirect(REAL epsilon = 1e-5) const ;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

	/**
	 * @return current tangent vector
	 */
	VEC3& getT() { return m_T ; }
	const VEC3& getT() const { return m_T ; }

	/**
	 * @return current bitangent vector
	 */
	VEC3& getB() { return m_B ; }
	const VEC3& getB() const { return m_B ; }

	/**
	 * @return current normal vector
	 */
	VEC3& getN() { return m_N ; }
	const VEC3& getN() const { return m_N ; }

	friend std::ostream& operator<< (std::ostream &out, const LocalFrame& lf) {
		out << "T : " << lf.m_T << std::endl ;
		out << "B : " << lf.m_B << std::endl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
177
		out << "N : " << lf.m_N ;
178 179 180
		return out ;
	} ;

181
private : // private constants
182 183 184 185 186 187 188 189 190 191 192 193
	// (T,B,N) can be any orthonormal direct frame
	// zeros are not recommended since they can
	// generate zero vectors after a dot product
	static const REAL Tx = 0.267261 ;
	static const REAL Ty = 0.534522 ;
	static const REAL Tz = 0.801784 ;
	static const REAL Bx = 0.844416 ;
	static const REAL By = -0.530776 ;
	static const REAL Bz = 0.0723785 ;
	static const REAL Nx = 0.464255 ;
	static const REAL Ny = 0.657695 ;
	static const REAL Nz = -0.593215 ;
194 195 196 197 198 199 200 201 202
} ;

} // Utils

} // CGoGN

#include "localFrame.hpp"

#endif /* LOCALFRAME_H_ */