Fermat
mixtures.h
1 /*
2  * Copyright (c) 2010-2018, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <cugar/sampling/random.h>
35 #include <cugar/linalg/vector.h>
36 #include <cugar/linalg/matrix.h>
37 
38 namespace cugar {
39 
48 template <typename TDistribution, uint32 NCOMP>
55 struct Mixture
56 {
57  typedef TDistribution distribution_type;
58 
59  static const uint32 NUM_COMPONENTS = NCOMP;
60 
61 
64  CUGAR_HOST_DEVICE Mixture()
65  {
66  // initialize the weights
67  for (uint32 i = 0; i < NUM_COMPONENTS; ++i)
68  weights[i] = 1.0f / NUM_COMPONENTS;
69  }
70 
73  CUGAR_HOST_DEVICE
74  uint32 size() const { return NCOMP; }
75 
79  CUGAR_HOST_DEVICE float density(const Vector2f x) const
80  {
81  float p = 0.0f;
82  for (uint32 i = 0; i < NCOMP; ++i)
83  p += weights[i] * comps[i].density(x);
84  return p;
85  }
86 
90  CUGAR_HOST_DEVICE float density(const uint32 i, const Vector2f x) const
91  {
92  return comps[i].density(x);
93  }
94 
98  CUGAR_HOST_DEVICE const distribution_type& component(const uint32 i) const { return comps[i]; }
99 
103  CUGAR_HOST_DEVICE distribution_type& component(const uint32 i) { return comps[i]; }
104 
108  CUGAR_HOST_DEVICE void set_component(const uint32 i, const distribution_type& c, const float w) { comps[i] = c; weights[i] = w; }
109 
113  CUGAR_HOST_DEVICE void set_component(const uint32 i, const distribution_type& c) { comps[i] = c; }
114 
118  CUGAR_HOST_DEVICE float weight(const uint32 i) const { return weights[i]; }
119 
123  CUGAR_HOST_DEVICE void set_weight(const uint32 i, const float w) { weights[i] = w; }
124 
125 private:
126  TDistribution comps[NCOMP];
127  float weights[NCOMP];
128 };
129 
133 } // namespace cugar
CUGAR_HOST_DEVICE float density(const uint32 i, const Vector2f x) const
Definition: mixtures.h:90
CUGAR_HOST_DEVICE uint32 size() const
Definition: mixtures.h:74
CUGAR_HOST_DEVICE float density(const Vector2f x) const
Definition: mixtures.h:79
CUGAR_HOST_DEVICE const distribution_type & component(const uint32 i) const
Definition: mixtures.h:98
CUGAR_HOST_DEVICE void set_component(const uint32 i, const distribution_type &c, const float w)
Definition: mixtures.h:108
Definition: mixtures.h:55
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST_DEVICE float weight(const uint32 i) const
Definition: mixtures.h:118
CUGAR_HOST_DEVICE void set_component(const uint32 i, const distribution_type &c)
Definition: mixtures.h:113
CUGAR_HOST_DEVICE Mixture()
Definition: mixtures.h:64
Defines several random samplers.
CUGAR_HOST_DEVICE distribution_type & component(const uint32 i)
Definition: mixtures.h:103
CUGAR_HOST_DEVICE void set_weight(const uint32 i, const float w)
Definition: mixtures.h:123