Loading...
Searching...
No Matches
sampledlossfunction.h
Go to the documentation of this file.
1#ifndef _BBM_SAMPLEDLOSSFUNCTION_H_
2#define _BBM_SAMPLEDLOSSFUNCTION_H_
3
8
9#include "bbm/config.h"
10#include "util/reference.h"
11
12/***********************************************************************/
13/*! \file sampledlossfunction.h
14
15 \brief Definition of a loss function that is the sum of losses on samples.
16
17************************************************************************/
18
19namespace bbm {
20
21 /*********************************************************************/
22 /*! \brief sampledlossfunction
23
24 Computes the loss over BSDFs sampled by a linearizer.
25
26 Satisfied: concepts::sampledlossfunction
27 **********************************************************************/
28 template<typename BSDF, typename REFERENCE, typename SAMPLELOSSFUNC, typename LINEARIZER, bsdf_flag COMPONENT=bsdf_flag::All, unit_t UNIT=unit_t::Radiance>
29 requires concepts::bsdfmodel<BSDF> &&
30 concepts::bsdfmodel<REFERENCE> &&
31 concepts::inout_linearizer<LINEARIZER> &&
32 concepts::samplelossfunction<SAMPLELOSSFUNC> &&
33 concepts::matching_config<BSDF, REFERENCE, LINEARIZER>
35 {
37
38 /*******************************************************************/
39 /*! \brief Constructor
40
41 \param bsdf = bsdf model to be optimized
42 \param reference = goal bsdf model
43 \param samplelossfunc = loss over a single loss sample
44 \param linearizer = linearizer to sample the bsdf models.
45
46 ********************************************************************/
47 inline sampledlossfunction(const BSDF& bsdf, const REFERENCE& reference, const SAMPLELOSSFUNC& samplelossfunc, const LINEARIZER& linearizer) : _bsdf(bsdf), _reference(reference), _linearizer(linearizer), _samplelossfunc(samplelossfunc) {}
48
49 /*******************************************************************/
50 /*! \brief Init (does nothing)
51 *******************************************************************/
52 inline void update(void) {}
53
54 /*******************************************************************/
55 /*! \brief Returns the number of samples
56 *******************************************************************/
57 inline Size_t samples(void) const { return _linearizer.size(); }
58
59 /*******************************************************************/
60 /*! \brief Compute the loss over the idx-th sample
61 *******************************************************************/
62 inline Value operator()(Size_t idx, Mask mask=true) const
63 {
64 // check bounds
65 mask &= (idx < samples());
66 if(bbm::none(mask)) return 0;
67
68 Vec3dPair directions = _linearizer(idx);
69 return _samplelossfunc(directions.in, directions.out,
70 _bsdf.eval(directions.in, directions.out, COMPONENT, UNIT, mask),
71 _reference.eval(directions.in, directions.out, COMPONENT, UNIT, mask)
72 );
73 }
74
75 /*******************************************************************/
76 /*! \brief Compute loss over all samples
77 *******************************************************************/
78 inline Value operator()(Mask mask=true) const
79 {
80 Value err(0);
81 Size_t numsamples = samples();
82
83 for(size_t i=0; i < bbm::hmax(numsamples); ++i)
84 err += operator()(i, mask && (i < numsamples));
85
86 return err / Value(numsamples);
87 }
88
89 private:
90 /////////////////////
91 // Class Attributes
92 /////////////////////
93 const BSDF& _bsdf;
94 const REFERENCE& _reference;
95 LINEARIZER _linearizer;
96 SAMPLELOSSFUNC _samplelossfunc;
97 };
98
99 BBM_CHECK_CONCEPT( concepts::sampledlossfunction, bbm::sampledlossfunction<bsdfmodel<>, bsdfmodel<>, samplelossfunction<>, inout_linearizer<>> );
100
101} // end bbm namespace
102
103#endif /* _BBM_SAMPLEDLOSSFUNCTION_H_ */
All BBM methods are defined to operate on a variety of value types and spectrum types....
BSDF implementation of a BSDF model.
Definition: bsdf.h:30
sampled loss function concept
Definition: sampledlossfunction.h:27
bsdfmodel contract
sampled loss function contract
inout_linearizer contract
#define BBM_CHECK_CONCEPT(CONCEPTNAME, CLASSNAME,...)
Check a class for a concept with bbm::concepts::archetypes in the namespace.
Definition: macro.h:35
Definition: aggregatebsdf.h:29
Assignable reference with wrapper support for rvalues.
Non-persistent reference (i.e., cannot take rvalues)
Definition: reference.h:86
sampledlossfunction
Definition: sampledlossfunction.h:35
const BSDF & _bsdf
Definition: sampledlossfunction.h:93
SAMPLELOSSFUNC _samplelossfunc
Definition: sampledlossfunction.h:96
Value operator()(Size_t idx, Mask mask=true) const
Compute the loss over the idx-th sample.
Definition: sampledlossfunction.h:62
const REFERENCE & _reference
Definition: sampledlossfunction.h:94
sampledlossfunction(const BSDF &bsdf, const REFERENCE &reference, const SAMPLELOSSFUNC &samplelossfunc, const LINEARIZER &linearizer)
Constructor.
Definition: sampledlossfunction.h:47
void update(void)
Init (does nothing)
Definition: sampledlossfunction.h:52
Size_t samples(void) const
Returns the number of samples.
Definition: sampledlossfunction.h:57
Value operator()(Mask mask=true) const
Compute loss over all samples.
Definition: sampledlossfunction.h:78
LINEARIZER _linearizer
Definition: sampledlossfunction.h:95