Blender  V2.93
AverageAreaGridDensityProvider.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 
24 #include "BKE_global.h"
25 
26 namespace Freestyle {
27 
28 AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(OccluderSource &source,
29  const real proscenium[4],
30  real sizeFactor)
31  : GridDensityProvider(source)
32 {
33  initialize(proscenium, sizeFactor);
34 }
35 
36 AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(
37  OccluderSource &source,
38  const BBox<Vec3r> &bbox,
40  real sizeFactor)
41  : GridDensityProvider(source)
42 {
43  real proscenium[4];
44  calculateQuickProscenium(transform, bbox, proscenium);
45 
46  initialize(proscenium, sizeFactor);
47 }
48 
49 AverageAreaGridDensityProvider::AverageAreaGridDensityProvider(OccluderSource &source,
50  real sizeFactor)
51  : GridDensityProvider(source)
52 {
53  real proscenium[4];
55 
56  initialize(proscenium, sizeFactor);
57 }
58 
59 void AverageAreaGridDensityProvider::initialize(const real proscenium[4], real sizeFactor)
60 {
61  float prosceniumWidth = (proscenium[1] - proscenium[0]);
62  float prosceniumHeight = (proscenium[3] - proscenium[2]);
63 
64  real cellArea = 0.0;
65  unsigned numFaces = 0;
66  for (source.begin(); source.isValid(); source.next()) {
68  Vec3r min, max;
69  poly.getBBox(min, max);
70  cellArea += (max[0] - min[0]) * (max[1] - min[1]);
71  ++numFaces;
72  }
73  if (G.debug & G_DEBUG_FREESTYLE) {
74  cout << "Total area: " << cellArea << ". Number of faces: " << numFaces << "." << endl;
75  }
76  cellArea /= numFaces;
77  cellArea *= sizeFactor;
78  if (G.debug & G_DEBUG_FREESTYLE) {
79  cout << "Building grid with average area " << cellArea << endl;
80  }
81 
82  _cellSize = sqrt(cellArea);
83  unsigned maxCells = 931; // * 1.1 = 1024
84  if (std::max(prosceniumWidth, prosceniumHeight) / _cellSize > maxCells) {
85  if (G.debug & G_DEBUG_FREESTYLE) {
86  cout << "Scene-dependent cell size (" << _cellSize << " square) is too small." << endl;
87  }
88  _cellSize = std::max(prosceniumWidth, prosceniumHeight) / maxCells;
89  }
90  // Now we know how many cells make each side of our grid
91  _cellsX = ceil(prosceniumWidth / _cellSize);
92  _cellsY = ceil(prosceniumHeight / _cellSize);
93  if (G.debug & G_DEBUG_FREESTYLE) {
94  cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
95  }
96 
97  // Make sure the grid exceeds the proscenium by a small amount
98  float safetyZone = 0.1f;
99  if (_cellsX * _cellSize < prosceniumWidth * (1.0 + safetyZone)) {
100  _cellsX = ceil(prosceniumWidth * (1.0 + safetyZone) / _cellSize);
101  }
102  if (_cellsY * _cellSize < prosceniumHeight * (1.0 + safetyZone)) {
103  _cellsY = ceil(prosceniumHeight * (1.0 + safetyZone) / _cellSize);
104  }
105  if (G.debug & G_DEBUG_FREESTYLE) {
106  cout << _cellsX << "x" << _cellsY << " cells of size " << _cellSize << " square." << endl;
107  }
108 
109  // Find grid origin
110  _cellOrigin[0] = ((proscenium[0] + proscenium[1]) / 2.0) - (_cellsX / 2.0) * _cellSize;
111  _cellOrigin[1] = ((proscenium[2] + proscenium[3]) / 2.0) - (_cellsY / 2.0) * _cellSize;
112 }
113 
115  : sizeFactor(sizeFactor)
116 {
117 }
118 
120  OccluderSource &source, const real proscenium[4])
121 {
123  new AverageAreaGridDensityProvider(source, proscenium, sizeFactor));
124 }
125 
127  OccluderSource &source, const BBox<Vec3r> &bbox, const GridHelpers::Transform &transform)
128 {
131 }
132 
134  OccluderSource &source)
135 {
137 }
138 
139 } /* namespace Freestyle */
Class to define a cell grid surrounding the projected image of a scene.
@ G_DEBUG_FREESTYLE
Definition: BKE_global.h:140
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])
inherits from class Rep
Definition: AppCanvas.cpp:32
double real
Definition: Precision.h:26
#define min(a, b)
Definition: sort.c:51
float max
ccl_device_inline float3 ceil(const float3 &a)
#define G(x, y, z)