halfEdgeSelectorRadiance.h 5.33 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef _HALFEDGESELECTOR_RADIANCE_H_
#define _HALFEDGESELECTOR_RADIANCE_H_

#include "Algo/Decimation/selector.h"
#include "Algo/Decimation/approximator.h"
#include "Utils/qem.h"
#include "Utils/sphericalHarmonics.h"

#include "SphericalFunctionIntegratorCartesian.h"

namespace CGoGN
{

namespace SCHNApps
{

template <typename PFP>
class HalfEdgeSelector_Radiance : public Algo::Surface::Decimation::Selector<PFP>
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename Utils::SphericalHarmonics<REAL, VEC3> SH;

private:
	VertexAttribute<VEC3, MAP>& m_position;
	VertexAttribute<VEC3, MAP>& m_normal;
	VertexAttribute<SH, MAP>& m_radiance;
	Algo::Surface::Decimation::Approximator<PFP, VEC3, DART>& m_positionApproximator;
	Algo::Surface::Decimation::Approximator<PFP, VEC3, DART>& m_normalApproximator;
	Algo::Surface::Decimation::Approximator<PFP, SH, DART>& m_radianceApproximator;

	typedef	struct
	{
		typename std::multimap<float, Dart>::iterator it;
		bool valid;
		static std::string CGoGNnameOfType() { return "LightfieldGradHalfEdgeInfo"; }
	} LightfieldHalfEdgeInfo;
	typedef NoTypeNameAttribute<LightfieldHalfEdgeInfo> HalfEdgeInfo;

	DartAttribute<HalfEdgeInfo, PFP2::MAP> halfEdgeInfo;
	VertexAttribute<Utils::Quadric<PFP2::REAL>, PFP2::MAP> m_quadric;
44
//	VertexAttribute<PFP2::VEC3, PFP2::MAP> m_avgColor;
45
46
47
48
49
50
51
52
53
54
55
56

	unsigned int m_nb_coefs;

	SphericalFunctionIntegratorCartesian m_integrator;

	std::multimap<float, Dart> halfEdges;
	typename std::multimap<float, Dart>::iterator cur;

	void initHalfEdgeInfo(Dart d);
	void updateHalfEdgeInfo(Dart d);
	void computeHalfEdgeInfo(Dart d, HalfEdgeInfo& einfo);
	void recomputeQuadric(const Dart d);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

	typename PFP::REAL computeRadianceError(Dart d);

	static bool isInHemisphere(double x, double y, double z, void* u)
	{ // true iff [x,y,z] and u have the same direction
		REAL* n = (REAL*)(u);
		return x*n[0] + y*n[1] + z*n[2] >= 0.0;
	}

	static double SHEvalCartesian_Error(double x, double y, double z, void* u)
	{
		SH& e = *(SH*)(u);
		VEC3 c = e.evaluate_at(x, y, z);
		return c.norm2();
	}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public:
	HalfEdgeSelector_Radiance(
		MAP& m,
		VertexAttribute<VEC3, MAP>& pos,
		VertexAttribute<VEC3, MAP>& norm,
		VertexAttribute<SH, MAP>& rad,
		Algo::Surface::Decimation::Approximator<PFP, VEC3, DART>& posApprox,
		Algo::Surface::Decimation::Approximator<PFP, VEC3, DART>& normApprox,
		Algo::Surface::Decimation::Approximator<PFP, SH, DART>& radApprox
	) :
		Algo::Surface::Decimation::Selector<PFP2>(m),
		m_position(pos),
		m_normal(norm),
		m_radiance(rad),
		m_positionApproximator(posApprox),
		m_normalApproximator(normApprox),
		m_radianceApproximator(radApprox),
90
		m_nb_coefs(0)
91
92
93
	{
		halfEdgeInfo = m.template checkAttribute<HalfEdgeInfo, DART, PFP2::MAP>("halfEdgeInfo");
		m_quadric = m.template checkAttribute<Utils::Quadric<PFP2::REAL>, VERTEX, PFP2::MAP>("QEMquadric");
94
//		m_avgColor = m.template checkAttribute<PFP2::VEC3, VERTEX, PFP2::MAP>("avgColor");
95
96
97
98
99
100
	}

	~HalfEdgeSelector_Radiance()
	{
		this->m_map.removeAttribute(halfEdgeInfo);
		this->m_map.removeAttribute(m_quadric);
101
//		this->m_map.removeAttribute(m_avgColor);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		m_integrator.Release();
	}

	Algo::Surface::Decimation::SelectorType getType() { return Algo::Surface::Decimation::S_OTHER; }
	bool init();
	bool nextEdge(Dart& d) const;
	void updateBeforeCollapse(Dart d);
	void updateAfterCollapse(Dart d2, Dart);

	void updateWithoutCollapse() { }

	void getEdgeErrors(EdgeAttribute<PFP2::REAL, PFP2::MAP> *errors) const
	{
		assert(errors != NULL || !"HalfEdgeSelector_Radiance::getEdgeErrors requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "HalfEdgeSelector_Radiance::getEdgeErrors requires valid edgeattribute argument" << std::endl ;
		assert(halfEdgeInfo.isValid());

		TraversorE<PFP2::MAP> travE(this->m_map);
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
			Dart dd = this->m_map.phi2(d);
			if (halfEdgeInfo[d].valid)
			{
				(*errors)[d] = halfEdgeInfo[d].it->first;
			}
			if (halfEdgeInfo[dd].valid && halfEdgeInfo[dd].it->first < (*errors)[d])
			{
				(*errors)[d] = halfEdgeInfo[dd].it->first;
			}
			if (!(halfEdgeInfo[d].valid || halfEdgeInfo[dd].valid))
				(*errors)[d] = -1;
		}
	}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

	class ErrorEvaluator
	{
	private:
		double wA;
		double wB;
		double wC;

		SH* sA;
		SH* sB;
		SH* sC;
		SH* sRef;

		VEC3 nA;
		VEC3 nB;
		VEC3 nC;
		VEC3 nRef;

		VEC3 cA;
		VEC3 cB;
		VEC3 cC;
		VEC3 cRef;

	public:
		ErrorEvaluator(
			double p_wA,
			double p_wB,
			double p_wC,
			SH* p_sA,
			SH* p_sB,
			SH* p_sC,
			SH* p_sRef,
			VEC3 p_nA,
			VEC3 p_nB,
			VEC3 p_nC,
			VEC3 p_nRef,
			VEC3 p_cA,
			VEC3 p_cB,
			VEC3 p_cC,
			VEC3 p_cRef
		)
		{
			wA = p_wA; wB = p_wB; wC = p_wC;
			sA = p_sA; sB = p_sB; sC = p_sC; sRef = p_sRef;
			nA = p_nA; nB = p_nB; nC = p_nC; nRef = p_nRef;
			cA = p_cA; cB = p_cB; cC = p_cC; cRef = p_cRef;
		}

		void set_eval_direction (double x, double y, double z) // fix the direction in which the error has to be evaluated
		{
			SH::set_eval_direction (x,y,z);
		}

		double evaluate () // evaluates at a fixed direction
		{
			VEC3 color_difference ( sRef->evaluate() - wA*sA->evaluate()- wB*sB->evaluate() - wC*sC->evaluate() );
			return color_difference.squaredNorm();
		}
	};
195
196
197
198
199
200
201
202
203
};

} // namespace SCHNApps

} // namespace CGoGN

#include "halfEdgeSelectorRadiance.hpp"

#endif // _HALFEDGESELECTOR_RADIANCE_H_