lightfieldApproximator.hpp 6.18 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 71

		REAL t = std::max (std::min (segment * segmentNew , REAL(1)) , REAL(0) ) ; // Orthogonal projection on segment v1-v2 of new vertex

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

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

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

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

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

/************************************************************************************
 *                      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") ;
99 100 101 102
	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
103

104
	if (!m_frame.isValid() || !m_approxFrame.isValid() || !m_quadricRGBfunctions->isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	{
		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 ;
120 121
	m_approxFrame[d].getSubVectorH(0,0,i) ;
	m_approxFrame[d].getSubVectorH(2,0,n) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
122

123 124
	m_frame[d].getSubVectorH(0,0,i1) ;
	m_frame[dd].getSubVectorH(0,0,i2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125

126 127
	m_frame[d].getSubVectorH(1,0,j1) ;
	m_frame[dd].getSubVectorH(1,0,j2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128

129 130
	m_frame[d].getSubVectorH(2,0,n1) ;
	m_frame[dd].getSubVectorH(2,0,n2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
131 132 133 134 135 136 137 138 139

	VEC3 j1pr = n1 ^ i ;
	VEC3 j2pr = n2 ^ i ;

	REAL alpha1 = ((n * j1pr) > 0 ? 1 : -1) * acos(n * n1) ;
	REAL alpha2 = ((n * j2pr) > 0 ? 1 : -1) * acos(n * n2) ;
	REAL gamma1 = ((j1 * i) > 0 ? 1 : -1) * acos( i1 * i ) ;
	REAL gamma2 = ((j2 * i) > 0 ? 1 : -1) * acos( i2 * i ) ;

140 141 142 143 144 145 146 147
	alpha1 = std::min(REAL(1),std::max(REAL(-1),alpha1)) ;
	alpha2 = std::min(REAL(1),std::max(REAL(-1),alpha2)) ;

	gamma1 = std::min(REAL(1),std::max(REAL(-1),gamma1)) ;
	gamma2 = std::min(REAL(1),std::max(REAL(-1),gamma2)) ;

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

	// New RGBf
150
	if (! (*m_quadricRGBfunctions)[d].findOptimizedRGBfunctions(this->m_approx[d]))
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152 153 154 155 156 157 158
		this->m_approx[d] = this->m_attrV[d];
}

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN