lightfieldApproximator.hpp 6.69 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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 28 29 30 31 32 33 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 61 62 63 64 65 66
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Decimation
{

/************************************************************************************
 *                      LIGHTFIELD QUADRIC METRIC : frame                           *
 ************************************************************************************/

template <typename PFP>
bool Approximator_Frame<PFP>::init()
{
	m_position = this->m_map.template getAttribute<VEC3>(VERTEX_ORBIT, "position") ;
	m_approxPosition = this->m_map.template getAttribute<VEC3>(EDGE_ORBIT, "approx_position") ;

	if (!m_position.isValid() || !m_approxPosition.isValid())
	{
		std::cerr << "Approximator_Frame::init() --> No approxPosition or no quadricRGBfunctions specified" << std::endl ;
		return false ;
	}
	return true ;
}

template <typename PFP>
void Approximator_Frame<PFP>::approximate(Dart d)
{
	MAP& m = this->m_map ;
	Dart dd = m.phi2(d) ;	// get some darts

	if (this->m_approxPosition[d] == this->m_position[d]) // new Position is position of vertex d
		this->m_approx[d] = this->m_attrV[d] ;
	else if (this->m_approxPosition[d] == this->m_position[dd]) // new Position is position of vertex dd
		this->m_approx[d] = this->m_attrV[dd] ;
	else {
		VEC3 segment = this->m_position[dd] ;
		segment -= this->m_position[d] ;

		VEC3 segmentNew = m_approxPosition[d] ;
67
		segmentNew -= this->m_position[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68

69 70
		REAL t = (segment * segmentNew) / (segment.norm()*segment.norm()) ;
		t = std::max (std::min (t , REAL(1)) , REAL(0) ) ; // Orthogonal projection on segment v1-v2 of new vertex
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72

		VEC3 n1, n2 ;
73 74
		this->m_attrV[d].getSubVectorH(2, 0, n1) ;
		this->m_attrV[dd].getSubVectorH(2, 0, n2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77 78

		VEC3 newN = slerp(n1,n2,t) ; // spherical interpolation
		newN.normalize() ;

79
		VEC3 newI = n2 ^ n1 ; // i is perpendicular to newNormal
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83 84
		newI.normalize() ;

		VEC3 newJ = newN ^ newI ;
		newJ.normalize() ;

85 86 87 88
		if (!this->m_approx[d].setSubVectorH(0,0,newI) ||
			!this->m_approx[d].setSubVectorH(1,0,newJ) ||
			!this->m_approx[d].setSubVectorH(2,0,newN) )
			assert(!"Approximator_Frame::approximate") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
89
	}
90 91 92

	//	AutoAttributeHandler<VEC3> normals = this->m_map.template getAttribute<VEC3>(VERTEX_ORBIT, "normals") ;
	//	this->m_approx[d].getSubVectorH(2,0,normals[d]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96 97 98 99 100 101 102 103
}

/************************************************************************************
 *                      LIGHTFIELD QUADRIC METRIC : functions                       *
 ************************************************************************************/

template <typename PFP>
bool Approximator_RGBfunctions<PFP>::init()
{
	m_frame = this->m_map.template getAttribute<MATRIX33>(VERTEX_ORBIT, "frame") ;
	m_approxFrame = this->m_map.template getAttribute<MATRIX33>(EDGE_ORBIT, "approx_frame") ;
104 105 106 107
	m_quadricRGBfunctions = new AutoAttributeHandler<QuadricRGBfunctions<REAL> >(this->m_map, EDGE_ORBIT) ;

	for (Dart d = this->m_map.begin() ; d != this->m_map.end() ; this->m_map.next(d))
		(*m_quadricRGBfunctions)[d].zero() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108

109
	if (!m_frame.isValid() || !m_approxFrame.isValid() || !m_quadricRGBfunctions->isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	{
		std::cerr << "Approximator_RGBfunctions::init() --> No approxPosition or no quadricRGBfunctions specified" << std::endl ;
		return false ;
	}
	return true ;
}

template <typename PFP>
void Approximator_RGBfunctions<PFP>::approximate(Dart d)
{
	MAP& m = this->m_map ;
	Dart dd = m.phi2(d) ;	// get some darts

	VEC3 i,n ;
	VEC3 n1,n2,i1,i2,j1,j2 ;
125 126
	m_approxFrame[d].getSubVectorH(0,0,i) ;
	m_approxFrame[d].getSubVectorH(2,0,n) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127

128 129
	m_frame[d].getSubVectorH(0,0,i1) ;
	m_frame[dd].getSubVectorH(0,0,i2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130

131 132
	m_frame[d].getSubVectorH(1,0,j1) ;
	m_frame[dd].getSubVectorH(1,0,j2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
133

134 135
	m_frame[d].getSubVectorH(2,0,n1) ;
	m_frame[dd].getSubVectorH(2,0,n2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137

	VEC3 j1pr = n1 ^ i ;
138
	j1pr.normalize() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
139
	VEC3 j2pr = n2 ^ i ;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	j2pr.normalize() ;

	// Rotation dans sens trigo dans le plan tangent autour de n (i1->i)
	REAL gamma1 = ((j1 * i) > 0 ? 1 : -1) * acos( std::max(std::min(1.0f, i1 * i ), -1.0f)) ; // angle positif ssi
	REAL gamma2 = ((j2 * i) > 0 ? 1 : -1) * acos( std::max(std::min(1.0f, i2 * i ), -1.0f)) ; // -PI/2 < angle(i,j1) < PI/2  ssi i*j1 > 0
	// Rotation dans le sens trigo autour de l'axe i (n1->n)
	REAL alpha1 = ((n * j1pr) > 0 ? -1 : 1) * acos( std::max(std::min(1.0f, n * n1), -1.0f) ) ; // angle positif ssi
	REAL alpha2 = ((n * j2pr) > 0 ? -1 : 1) * acos( std::max(std::min(1.0f, n * n2), -1.0f) ) ; // PI/2 < angle(j1',n) < -PI/2 ssi j1'*n < 0

	assert (-3.15 < gamma1 && gamma1 <= 3.15) ;
	assert (-3.15 < gamma2 && gamma2 <= 3.15) ;
	assert (-3.15 < alpha1 && alpha1 <= 3.15) ;
	assert (-3.15 < alpha2 && alpha2 <= 3.15) ;

	(*m_quadricRGBfunctions)[d] += QuadricRGBfunctions<REAL>(this->m_attrV[d],gamma1, alpha1) ;
	(*m_quadricRGBfunctions)[d] += QuadricRGBfunctions<REAL>(this->m_attrV[dd],gamma2, alpha2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157

	// New RGBf
158
	if (! (*m_quadricRGBfunctions)[d].findOptimizedRGBfunctions(this->m_approx[d]))
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162 163 164 165 166
		this->m_approx[d] = this->m_attrV[d];
}

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN