File: distribution2d.cpp

package info (click to toggle)
embree 3.13.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,924 kB
  • sloc: cpp: 180,815; xml: 3,877; ansic: 2,957; python: 1,466; sh: 502; makefile: 229; csh: 42
file content (55 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download | duplicates (2)
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
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "distribution2d.h"

namespace embree
{
  Distribution2D::Distribution2D()
    : width(0), height(0) {}

  Distribution2D::Distribution2D(const float* f, const size_t width, const size_t height)
    : width(width), height(height)
  {
    init(f, width, height);
  }

  void Distribution2D::init(const float* f, const size_t w, const size_t h)
  {
    /*! create arrays */
    width = w; height = h;
    xDists.resize(height);
    std::vector<float> fy(height);

    /*! compute y distribution and initialize row distributions */
    for (size_t y=0; y<height; y++)
    {
      /*! accumulate row to compute y distribution */
      fy[y] = 0.0f;
      for (size_t x=0; x<width; x++)
        fy[y] += f[y*w+x];

      /*! initialize distribution for current row */
      xDists[y].init(f+y*width, width);
    }

    /*! initializes the y distribution */
    yDist.init(fy.data(), height);
  }

  Vec2f Distribution2D::sample(const Vec2f& u) const
  {
    /*! use u.y to sample a row */
    float sy = yDist.sample(u.y);
    int idx = clamp(int(sy),0,int(height)-1);

    /*! use u.x to sample inside the row */
    float sx = xDists[idx].sample(u.x);
    return Vec2f(sx,sy);
  }

  float Distribution2D::pdf(const Vec2f& p) const {
    int idx = clamp(int(p.y*height),0,int(height)-1);
    return xDists[idx].pdf(p.x) * yDist.pdf(p.y);
  }
}