Blender V4.3
AverageAreaGridDensityProvider.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include "BLI_sys_types.h"
13
14#include "BKE_global.hh"
15
16namespace Freestyle {
17
18AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(OccluderSource &source,
19 const real proscenium[4],
20 real sizeFactor)
21 : GridDensityProvider(source)
22{
23 initialize(proscenium, sizeFactor);
24}
25
26AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(
27 OccluderSource &source,
28 const BBox<Vec3r> &bbox,
29 const GridHelpers::Transform &transform,
30 real sizeFactor)
31 : GridDensityProvider(source)
32{
33 real proscenium[4];
34 calculateQuickProscenium(transform, bbox, proscenium);
35
36 initialize(proscenium, sizeFactor);
37}
38
39AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(OccluderSource &source,
40 real sizeFactor)
41 : GridDensityProvider(source)
42{
43 real proscenium[4];
45
46 initialize(proscenium, sizeFactor);
47}
48
49void AverageAreaGridDensityProvider::initialize(const real proscenium[4], real sizeFactor)
50{
51 float prosceniumWidth = (proscenium[1] - proscenium[0]);
52 float prosceniumHeight = (proscenium[3] - proscenium[2]);
53
54 real cellArea = 0.0;
55 uint numFaces = 0;
56 for (source.begin(); source.isValid(); source.next()) {
58 Vec3r min, max;
59 poly.getBBox(min, max);
60 cellArea += (max[0] - min[0]) * (max[1] - min[1]);
61 ++numFaces;
62 }
63 if (G.debug & G_DEBUG_FREESTYLE) {
64 cout << "Total area: " << cellArea << ". Number of faces: " << numFaces << "." << endl;
65 }
66 cellArea /= numFaces;
67 cellArea *= sizeFactor;
68 if (G.debug & G_DEBUG_FREESTYLE) {
69 cout << "Building grid with average area " << cellArea << endl;
70 }
71
72 _cellSize = sqrt(cellArea);
73 uint maxCells = 931; // * 1.1 = 1024
74 if (std::max(prosceniumWidth, prosceniumHeight) / _cellSize > maxCells) {
75 if (G.debug & G_DEBUG_FREESTYLE) {
76 cout << "Scene-dependent cell size (" << _cellSize << " square) is too small." << endl;
77 }
78 _cellSize = std::max(prosceniumWidth, prosceniumHeight) / maxCells;
79 }
80 // Now we know how many cells make each side of our grid
81 _cellsX = ceil(prosceniumWidth / _cellSize);
82 _cellsY = ceil(prosceniumHeight / _cellSize);
83 if (G.debug & G_DEBUG_FREESTYLE) {
84 cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
85 }
86
87 // Make sure the grid exceeds the proscenium by a small amount
88 float safetyZone = 0.1f;
89 if (_cellsX * _cellSize < prosceniumWidth * (1.0 + safetyZone)) {
90 _cellsX = ceil(prosceniumWidth * (1.0 + safetyZone) / _cellSize);
91 }
92 if (_cellsY * _cellSize < prosceniumHeight * (1.0 + safetyZone)) {
93 _cellsY = ceil(prosceniumHeight * (1.0 + safetyZone) / _cellSize);
94 }
95 if (G.debug & G_DEBUG_FREESTYLE) {
96 cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
97 }
98
99 // Find grid origin
100 _cellOrigin[0] = ((proscenium[0] + proscenium[1]) / 2.0) - (_cellsX / 2.0) * _cellSize;
101 _cellOrigin[1] = ((proscenium[2] + proscenium[3]) / 2.0) - (_cellsY / 2.0) * _cellSize;
102}
103
108
115
122
128
129} /* namespace Freestyle */
Class to define a cell grid surrounding the projected image of a scene.
@ G_DEBUG_FREESTYLE
sqrt(x)+1/max(0
unsigned int uint
void initialize()
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)
#define G(x, y, z)
inherits from class Rep
Definition AppCanvas.cpp:20
double real
Definition Precision.h:14
#define min(a, b)
Definition sort.c:32
float max