Blender  V3.3
ArbitraryGridDensityProvider.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 
10 #include "BKE_global.h"
11 
12 namespace Freestyle {
13 
14 ArbitraryGridDensityProvider::ArbitraryGridDensityProvider(OccluderSource &source,
15  const real proscenium[4],
16  unsigned numCells)
17  : GridDensityProvider(source), numCells(numCells)
18 {
19  initialize(proscenium);
20 }
21 
22 ArbitraryGridDensityProvider::ArbitraryGridDensityProvider(OccluderSource &source,
23  const BBox<Vec3r> &bbox,
25  unsigned numCells)
26  : GridDensityProvider(source), numCells(numCells)
27 {
28  real proscenium[4];
29  calculateQuickProscenium(transform, bbox, proscenium);
30 
31  initialize(proscenium);
32 }
33 
34 ArbitraryGridDensityProvider::ArbitraryGridDensityProvider(OccluderSource &source,
35  unsigned numCells)
36  : GridDensityProvider(source), numCells(numCells)
37 {
38  real proscenium[4];
40 
41  initialize(proscenium);
42 }
43 
44 void ArbitraryGridDensityProvider::initialize(const real proscenium[4])
45 {
46  float prosceniumWidth = (proscenium[1] - proscenium[0]);
47  float prosceniumHeight = (proscenium[3] - proscenium[2]);
48  real cellArea = prosceniumWidth * prosceniumHeight / numCells;
49  if (G.debug & G_DEBUG_FREESTYLE) {
50  cout << prosceniumWidth << " x " << prosceniumHeight << " grid with cells of area " << cellArea
51  << "." << endl;
52  }
53 
54  _cellSize = sqrt(cellArea);
55  // Now we know how many cells make each side of our grid
56  _cellsX = ceil(prosceniumWidth / _cellSize);
57  _cellsY = ceil(prosceniumHeight / _cellSize);
58  if (G.debug & G_DEBUG_FREESTYLE) {
59  cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
60  }
61 
62  // Make sure the grid exceeds the proscenium by a small amount
63  float safetyZone = 0.1f;
64  if (_cellsX * _cellSize < prosceniumWidth * (1.0 + safetyZone)) {
65  _cellsX = ceil(prosceniumWidth * (1.0 + safetyZone) / _cellSize);
66  }
67  if (_cellsY * _cellSize < prosceniumHeight * (1.0 + safetyZone)) {
68  _cellsY = ceil(prosceniumHeight * (1.0 + safetyZone) / _cellSize);
69  }
70  if (G.debug & G_DEBUG_FREESTYLE) {
71  cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
72  }
73 
74  // Find grid origin
75  _cellOrigin[0] = ((proscenium[0] + proscenium[1]) / 2.0) - (_cellsX / 2.0) * _cellSize;
76  _cellOrigin[1] = ((proscenium[2] + proscenium[3]) / 2.0) - (_cellsY / 2.0) * _cellSize;
77 }
78 
80  : numCells(numCells)
81 {
82 }
83 
85  OccluderSource &source, const real proscenium[4])
86 {
88  new ArbitraryGridDensityProvider(source, proscenium, numCells));
89 }
90 
92  OccluderSource &source, const BBox<Vec3r> &bbox, const GridHelpers::Transform &transform)
93 {
95  new ArbitraryGridDensityProvider(source, bbox, transform, numCells));
96 }
97 
99  OccluderSource &source)
100 {
102 }
103 
104 } /* namespace Freestyle */
Class to define a cell grid surrounding the projected image of a scene.
@ G_DEBUG_FREESTYLE
Definition: BKE_global.h:181
sqrt(x)+1/max(0
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
AutoPtr< GridDensityProvider > newGridDensityProvider(OccluderSource &source, const real proscenium[4])
static void calculateOptimalProscenium(OccluderSource &source, real proscenium[4])
static void calculateQuickProscenium(const GridHelpers::Transform &transform, const BBox< Vec3r > &bbox, real proscenium[4])
ccl_device_inline float3 ceil(const float3 &a)
Definition: math_float3.h:363
#define G(x, y, z)
inherits from class Rep
Definition: AppCanvas.cpp:18
double real
Definition: Precision.h:12