Blender  V2.93
GridDensityProvider.h
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 
17 #pragma once
18 
24 #include <algorithm>
25 #include <memory>
26 #include <stdexcept>
27 
28 #include "AutoPtrHelper.h"
29 #include "OccluderSource.h"
30 
31 #include "../geometry/BBox.h"
32 
33 #include "BKE_global.h"
34 
35 #ifdef WITH_CXX_GUARDEDALLOC
36 # include "MEM_guardedalloc.h"
37 #endif
38 
39 namespace Freestyle {
40 
42  // Disallow copying and assignment
44  GridDensityProvider &operator=(const GridDensityProvider &other);
45 
46  public:
48  {
49  }
50 
51  virtual ~GridDensityProvider(){};
52 
53  float cellSize()
54  {
55  return _cellSize;
56  }
57 
58  unsigned cellsX()
59  {
60  return _cellsX;
61  }
62 
63  unsigned cellsY()
64  {
65  return _cellsY;
66  }
67 
68  float cellOrigin(int index)
69  {
70  if (index < 2) {
71  return _cellOrigin[index];
72  }
73  else {
74  throw new out_of_range("GridDensityProvider::cellOrigin can take only indexes of 0 or 1.");
75  }
76  }
77 
79  {
80  source.begin();
81  if (source.isValid()) {
82  const Vec3r &initialPoint = source.getGridSpacePolygon().getVertices()[0];
83  proscenium[0] = proscenium[1] = initialPoint[0];
84  proscenium[2] = proscenium[3] = initialPoint[1];
85  while (source.isValid()) {
87  source.next();
88  }
89  }
90  if (G.debug & G_DEBUG_FREESTYLE) {
91  cout << "Proscenium: (" << proscenium[0] << ", " << proscenium[1] << ", " << proscenium[2]
92  << ", " << proscenium[3] << ")" << endl;
93  }
94  }
95 
97  const BBox<Vec3r> &bbox,
98  real proscenium[4])
99  {
100  // Transform the coordinates of the 8 corners of the 3D bounding box
101  real xm = bbox.getMin()[0], xM = bbox.getMax()[0];
102  real ym = bbox.getMin()[1], yM = bbox.getMax()[1];
103  real zm = bbox.getMin()[2], zM = bbox.getMax()[2];
104  Vec3r p1 = transform(Vec3r(xm, ym, zm));
105  Vec3r p2 = transform(Vec3r(xm, ym, zM));
106  Vec3r p3 = transform(Vec3r(xm, yM, zm));
107  Vec3r p4 = transform(Vec3r(xm, yM, zM));
108  Vec3r p5 = transform(Vec3r(xM, ym, zm));
109  Vec3r p6 = transform(Vec3r(xM, ym, zM));
110  Vec3r p7 = transform(Vec3r(xM, yM, zm));
111  Vec3r p8 = transform(Vec3r(xM, yM, zM));
112  // Determine the proscenium face according to the min and max values of the transformed x and y
113  // coordinates
114  proscenium[0] = std::min(std::min(std::min(p1.x(), p2.x()), std::min(p3.x(), p4.x())),
115  std::min(std::min(p5.x(), p6.x()), std::min(p7.x(), p8.x())));
116  proscenium[1] = std::max(std::max(std::max(p1.x(), p2.x()), std::max(p3.x(), p4.x())),
117  std::max(std::max(p5.x(), p6.x()), std::max(p7.x(), p8.x())));
118  proscenium[2] = std::min(std::min(std::min(p1.y(), p2.y()), std::min(p3.y(), p4.y())),
119  std::min(std::min(p5.y(), p6.y()), std::min(p7.y(), p8.y())));
120  proscenium[3] = std::max(std::max(std::max(p1.y(), p2.y()), std::max(p3.y(), p4.y())),
121  std::max(std::max(p5.y(), p6.y()), std::max(p7.y(), p8.y())));
122  if (G.debug & G_DEBUG_FREESTYLE) {
123  cout << "Proscenium: " << proscenium[0] << ", " << proscenium[1] << ", " << proscenium[2]
124  << ", " << proscenium[3] << endl;
125  }
126  }
127 
128  protected:
130  unsigned _cellsX, _cellsY;
131  float _cellSize;
132  float _cellOrigin[2];
133 
134 #ifdef WITH_CXX_GUARDEDALLOC
135  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GridDensityProvider")
136 #endif
137 };
138 
140  // Disallow copying and assignment
143 
144  public:
146  {
147  }
148 
150  const real proscenium[4]) = 0;
151 
153  OccluderSource &source,
154  const BBox<Vec3r> &bbox,
155  const GridHelpers::Transform &transform) = 0;
156 
158 
160  {
161  }
162 
163 #ifdef WITH_CXX_GUARDEDALLOC
164  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GridDensityProviderFactory")
165 #endif
166 };
167 
168 } /* namespace Freestyle */
Utility header for auto_ptr/unique_ptr selection.
@ G_DEBUG_FREESTYLE
Definition: BKE_global.h:140
Read Guarded memory(de)allocation.
Class to define a cell grid surrounding the projected image of a scene.
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
const Point & getMax() const
Definition: BBox.h:86
const Point & getMin() const
Definition: BBox.h:81
const vector< Point > & getVertices() const
Definition: Polygon.h:81
virtual AutoPtr< GridDensityProvider > newGridDensityProvider(OccluderSource &source)=0
virtual AutoPtr< GridDensityProvider > newGridDensityProvider(OccluderSource &source, const real proscenium[4])=0
virtual AutoPtr< GridDensityProvider > newGridDensityProvider(OccluderSource &source, const BBox< Vec3r > &bbox, const GridHelpers::Transform &transform)=0
static void calculateOptimalProscenium(OccluderSource &source, real proscenium[4])
GridDensityProvider(OccluderSource &source)
static void calculateQuickProscenium(const GridHelpers::Transform &transform, const BBox< Vec3r > &bbox, real proscenium[4])
value_type x() const
Definition: VecMat.h:532
value_type y() const
Definition: VecMat.h:542
VecMat::Vec3< real > Vec3r
Definition: Geom.h:42
void expandProscenium(real proscenium[4], const Polygon3r &polygon)
Definition: GridHelpers.h:166
inherits from class Rep
Definition: AppCanvas.cpp:32
double real
Definition: Precision.h:26
#define min(a, b)
Definition: sort.c:51
float max
#define G(x, y, z)