lightfieldApproximator.hpp 7.5 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
/*******************************************************************************
* 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 ;
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
56
	Dart dd = m.phi2(d) ;	// get 2 vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
57

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
58
	// if QEM placed new vertex at one of the previous : place same frame
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63
	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 {
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
64
		// Create two segments : v0-v1 and v0-v
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68
		VEC3 segment = this->m_position[dd] ;
		segment -= this->m_position[d] ;

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

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
71
		// Orthogonal projection of v0-v onto v0-v1 : get coefficient t
72
		REAL t = (segment * segmentNew) / segment.norm() ;
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
73
		t = std::max (std::min (t , REAL(1)) , REAL(0) ) ; // clamp it to [0,1]
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75

		VEC3 n1, n2 ;
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
76 77
		this->m_attrV[d].getSubVectorH(2, 0, n1) ; // get normal n1
		this->m_attrV[dd].getSubVectorH(2, 0, n2) ; // get normal n2
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81

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

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

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
85
		VEC3 newJ = newN ^ newI ; // (i,j,n) is a direct frame
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87
		newJ.normalize() ;

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
88
		// save into m_approx
89 90 91 92
		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
93
	}
94

Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102 103
}

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

template <typename PFP>
bool Approximator_RGBfunctions<PFP>::init()
{
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
104
	// get actual frames and hypothetical approximated frames
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106
	m_frame = this->m_map.template getAttribute<MATRIX33>(VERTEX_ORBIT, "frame") ;
	m_approxFrame = this->m_map.template getAttribute<MATRIX33>(EDGE_ORBIT, "approx_frame") ;
107

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
108 109
	// create quadric embedding for computing and set them to 0
	m_quadricRGBfunctions = new AutoAttributeHandler<QuadricRGBfunctions<REAL> >(this->m_map, EDGE_ORBIT) ;
110 111
	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
112

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
113
	// Check on embeddings
114
	if (!m_frame.isValid() || !m_approxFrame.isValid() || !m_quadricRGBfunctions->isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119 120 121 122 123 124 125
	{
		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 ;
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
126
	Dart dd = m.phi2(d) ;	// get the two vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
127

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
128
	// get hypothetical local frames
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	VEC3 i,n ;
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
130

131 132
	m_approxFrame[d].getSubVectorH(0,0,i) ;
	m_approxFrame[d].getSubVectorH(2,0,n) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
133

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
134 135 136
	// Get previous local frames
	VEC3 n1,n2,i1,i2,j1,j2 ;

137 138
	m_frame[d].getSubVectorH(0,0,i1) ;
	m_frame[dd].getSubVectorH(0,0,i2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
139

140 141
	m_frame[d].getSubVectorH(1,0,j1) ;
	m_frame[dd].getSubVectorH(1,0,j2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142

143 144
	m_frame[d].getSubVectorH(2,0,n1) ;
	m_frame[dd].getSubVectorH(2,0,n2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
145

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
146
	// Compute j1' and j2'
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	VEC3 j1pr = n1 ^ i ;
148
	j1pr.normalize() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
149
	VEC3 j2pr = n2 ^ i ;
150 151 152 153 154 155 156 157 158 159 160 161 162 163
	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) ;

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
164
	// Create and sum quadrics
165 166
	(*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
167

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
168
	// Compute new function
169
	if (! (*m_quadricRGBfunctions)[d].findOptimizedRGBfunctions(this->m_approx[d]))
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
170 171 172 173 174 175 176 177 178 179
		this->m_approx[d] = this->m_attrV[d]; // if fail take first one

//	std::cout << "Approx of : " <<std::endl ;
//	std::cout << "Frame1 : " << m_frame[d] << std::endl ;
//	std::cout << "Function1 : "<< this->m_attrV[d] << std::endl ;
//	std::cout << "Frame2 : " << m_frame[dd] << std::endl ;
//	std::cout << "Function2 : "<< this->m_attrV[dd] << std::endl ;
//	std::cout << "is " << std::endl ;
//	std::cout << "Frame :" << m_approxFrame[d] << std::endl ;
//	std::cout << "Function : " << this->m_approx[d] << std::endl << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182 183 184 185 186
}

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN