Blender  V2.93
Grid.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 <cstring> // for memset
25 #include <float.h>
26 #include <stdint.h> // For POINTER_FROM_UINT, i.e. uintptr_t.
27 #include <vector>
28 
29 #include "Geom.h"
30 #include "GeomUtils.h"
31 #include "Polygon.h"
32 
33 #include "../system/FreestyleConfig.h"
34 
35 #include "BLI_utildefines.h"
36 
37 #ifdef WITH_CXX_GUARDEDALLOC
38 # include "MEM_guardedalloc.h"
39 #endif
40 
41 using namespace std;
42 
43 namespace Freestyle {
44 
45 using namespace Geometry;
46 
47 typedef vector<Polygon3r *> OccludersSet;
48 
49 //
50 // Class to define cells used by the regular grid
51 //
53 
54 class Cell {
55  public:
56  Cell(Vec3r &orig)
57  {
58  _orig = orig;
59  }
60 
61  virtual ~Cell()
62  {
63  }
64 
65  inline void addOccluder(Polygon3r *o)
66  {
67  if (o) {
68  _occluders.push_back(o);
69  }
70  }
71 
72  inline const Vec3r &getOrigin()
73  {
74  return _orig;
75  }
76 
78  {
79  return _occluders;
80  }
81 
82  private:
83  Vec3r _orig;
84  OccludersSet _occluders;
85 
86 #ifdef WITH_CXX_GUARDEDALLOC
87  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Cell")
88 #endif
89 };
90 
91 class GridVisitor {
92  public:
93  virtual ~GridVisitor(){}; // soc
94 
95  virtual void discoverCell(Cell * /*cell*/)
96  {
97  }
98 
99  virtual void examineOccluder(Polygon3r * /*occ*/)
100  {
101  }
102 
103  virtual void finishCell(Cell * /*cell*/)
104  {
105  }
106 
107  virtual bool stop()
108  {
109  return false;
110  }
111 
112 #ifdef WITH_CXX_GUARDEDALLOC
113  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GridVisitor")
114 #endif
115 };
116 
119  public:
120  allOccludersGridVisitor(OccludersSet &occluders) : GridVisitor(), occluders_(occluders)
121  {
122  }
123 
124  virtual void examineOccluder(Polygon3r *occ);
125 
127  {
128  return occluders_;
129  }
130 
131  void clear()
132  {
133  occluders_.clear();
134  }
135 
136  private:
137  OccludersSet &occluders_;
138 };
139 
144  // soc - changed order to remove warnings
145  public:
146  double u_, v_, t_;
147 
148  private:
149  Polygon3r *occluder_;
150  Vec3r ray_org_, ray_dir_, cell_size_;
151  Cell *current_cell_;
152 
153  public:
154  firstIntersectionGridVisitor(const Vec3r &ray_org, const Vec3r &ray_dir, const Vec3r &cell_size)
155  : GridVisitor(),
156  u_(0),
157  v_(0),
158  t_(DBL_MAX),
159  occluder_(0),
160  ray_org_(ray_org),
161  ray_dir_(ray_dir),
162  cell_size_(cell_size),
163  current_cell_(0)
164  {
165  }
166 
168  {
169  }
170 
171  virtual void discoverCell(Cell *cell)
172  {
173  current_cell_ = cell;
174  }
175 
176  virtual void examineOccluder(Polygon3r *occ);
177 
178  virtual bool stop();
179 
181  {
182  return occluder_;
183  }
184 };
185 
186 //
187 // Class to define a regular grid used for ray casting computations
188 //
190 
191 class Grid {
192  public:
195  {
196  }
197 
198  virtual ~Grid()
199  {
200  clear();
201  }
202 
206  virtual void clear();
207 
216  virtual void configure(const Vec3r &orig, const Vec3r &size, unsigned nb);
217 
223  inline void getCellCoordinates(const Vec3r &p, Vec3u &res)
224  {
225  int tmp;
226  for (int i = 0; i < 3; i++) {
227  tmp = (int)((p[i] - _orig[i]) / _cell_size[i]);
228  if (tmp < 0) {
229  res[i] = 0;
230  }
231  else if ((unsigned int)tmp >= _cells_nb[i]) {
232  res[i] = _cells_nb[i] - 1;
233  }
234  else {
235  res[i] = tmp;
236  }
237  }
238  }
239 
241  virtual void fillCell(const Vec3u &coord, Cell &cell) = 0;
242 
244  virtual Cell *getCell(const Vec3u &coord) = 0;
245 
251  inline Cell *getCell(const Vec3r &p)
252  {
253  Vec3u coord;
254  getCellCoordinates(p, coord);
255  return getCell(coord);
256  }
257 
265  inline void getCellOrigin(const Vec3u &cell_coord, Vec3r &orig)
266  {
267  for (unsigned int i = 0; i < 3; i++) {
268  orig[i] = _orig[i] + cell_coord[i] * _cell_size[i];
269  }
270  }
271 
280  inline void getCellBox(const Vec3u &cell_coord, Vec3r &min_out, Vec3r &max_out)
281  {
282  getCellOrigin(cell_coord, min_out);
283  max_out = min_out + _cell_size;
284  }
285 
290  void insertOccluder(Polygon3r *occluder);
291 
293  void addOccluder(Polygon3r *occluder)
294  {
295  _occluders.push_back(occluder);
296  }
297 
302  void castRay(const Vec3r &orig, const Vec3r &end, OccludersSet &occluders, unsigned timestamp);
303 
304  // Prepares to cast ray without generating OccludersSet
305  void initAcceleratedRay(const Vec3r &orig, const Vec3r &end, unsigned timestamp);
306 
311  void castInfiniteRay(const Vec3r &orig,
312  const Vec3r &dir,
313  OccludersSet &occluders,
314  unsigned timestamp);
315 
316  // Prepares to cast ray without generating OccludersSet.
317  bool initAcceleratedInfiniteRay(const Vec3r &orig, const Vec3r &dir, unsigned timestamp);
318 
323  Polygon3r *castRayToFindFirstIntersection(
324  const Vec3r &orig, const Vec3r &dir, double &t, double &u, double &v, unsigned timestamp);
325 
327  void initRay(const Vec3r &orig, const Vec3r &end, unsigned timestamp);
328 
332  bool initInfiniteRay(const Vec3r &orig, const Vec3r &dir, unsigned timestamp);
333 
335  inline const Vec3r &getOrigin() const
336  {
337  return _orig;
338  }
339 
340  inline Vec3r gridSize() const
341  {
342  return _size;
343  }
344 
345  inline Vec3r getCellSize() const
346  {
347  return _cell_size;
348  }
349 
350  // ARB profiling only:
352  {
353  return &_occluders;
354  }
355 
357  {
358  cerr << "Cells nb : " << _cells_nb << endl;
359  cerr << "Cell size : " << _cell_size << endl;
360  cerr << "Origin : " << _orig << endl;
361  cerr << "Occluders nb : " << _occluders.size() << endl;
362  }
363 
364  protected:
366  inline void castRayInternal(GridVisitor &visitor)
367  {
368  Cell *current_cell = NULL;
369  do {
370  current_cell = getCell(_current_cell);
371  if (current_cell) {
372  visitor.discoverCell(current_cell);
373  OccludersSet &occluders =
374  current_cell->getOccluders(); // FIXME: I had forgotten the ref &
375  for (OccludersSet::iterator it = occluders.begin(); it != occluders.end(); it++) {
376  if (POINTER_AS_UINT((*it)->userdata2) != _timestamp) {
377  (*it)->userdata2 = POINTER_FROM_UINT(_timestamp);
378  visitor.examineOccluder(*it);
379  }
380  }
381  visitor.finishCell(current_cell);
382  }
383  } while ((!visitor.stop()) && (nextRayCell(_current_cell, _current_cell)));
384  }
385 
387  bool nextRayCell(Vec3u &current_cell, Vec3u &next_cell);
388 
389  unsigned int _timestamp;
390 
391  Vec3u _cells_nb; // number of cells for x,y,z axis
392  Vec3r _cell_size; // cell x,y,z dimensions
393  Vec3r _size; // grid x,y,x dimensions
394  Vec3r _orig; // grid origin
395 
396  Vec3r _ray_dir; // direction vector for the ray
397  Vec3u _current_cell; // The current cell being processed (designated by its 3 coordinates)
398  Vec3r _pt; // Points corresponding to the incoming and outgoing intersections of one cell with
399  // the ray
400  real _t_end; // To know when we are at the end of the ray
402 
403  // OccludersSet _ray_occluders; // Set storing the occluders contained in the cells traversed by
404  // a ray
405  OccludersSet _occluders; // List of all occluders inserted in the grid
406 
407 #ifdef WITH_CXX_GUARDEDALLOC
408  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Grid")
409 #endif
410 };
411 
412 //
413 // Class to walk through occluders in grid without building intermediate data structures
414 //
416 
418  public:
419  VirtualOccludersSet(Grid &_grid) : grid(_grid){};
422  Polygon3r *next(bool stopOnNewCell);
423 
424  private:
425  Polygon3r *firstOccluderFromNextCell();
426  Grid &grid;
427  OccludersSet::iterator it, end;
428 
429 #ifdef WITH_CXX_GUARDEDALLOC
430  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:VirtualOccludersSet")
431 #endif
432 };
433 
434 } /* namespace Freestyle */
#define POINTER_AS_UINT(i)
#define POINTER_FROM_UINT(i)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Various tools for geometry.
Vectors and Matrices (useful type definitions)
Read Guarded memory(de)allocation.
Class to define a polygon.
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void addOccluder(Polygon3r *o)
Definition: Grid.h:65
virtual ~Cell()
Definition: Grid.h:61
const Vec3r & getOrigin()
Definition: Grid.h:72
Cell(Vec3r &orig)
Definition: Grid.h:56
OccludersSet & getOccluders()
Definition: Grid.h:77
virtual void discoverCell(Cell *)
Definition: Grid.h:95
virtual void examineOccluder(Polygon3r *)
Definition: Grid.h:99
virtual void finishCell(Cell *)
Definition: Grid.h:103
virtual bool stop()
Definition: Grid.h:107
virtual ~GridVisitor()
Definition: Grid.h:93
OccludersSet _occluders
Definition: Grid.h:405
Vec3r gridSize() const
Definition: Grid.h:340
void getCellCoordinates(const Vec3r &p, Vec3u &res)
Definition: Grid.h:223
void getCellBox(const Vec3u &cell_coord, Vec3r &min_out, Vec3r &max_out)
Definition: Grid.h:280
real _t_end
Definition: Grid.h:400
void addOccluder(Polygon3r *occluder)
Definition: Grid.h:293
Vec3r getCellSize() const
Definition: Grid.h:345
Vec3u _current_cell
Definition: Grid.h:397
const Vec3r & getOrigin() const
Definition: Grid.h:335
Vec3r _cell_size
Definition: Grid.h:392
void getCellOrigin(const Vec3u &cell_coord, Vec3r &orig)
Definition: Grid.h:265
virtual ~Grid()
Definition: Grid.h:198
Vec3r _pt
Definition: Grid.h:398
Vec3r _size
Definition: Grid.h:393
OccludersSet * getOccluders()
Definition: Grid.h:351
bool initAcceleratedInfiniteRay(const Vec3r &orig, const Vec3r &dir, unsigned timestamp)
virtual void fillCell(const Vec3u &coord, Cell &cell)=0
Cell * getCell(const Vec3r &p)
Definition: Grid.h:251
void initAcceleratedRay(const Vec3r &orig, const Vec3r &end, unsigned timestamp)
virtual Cell * getCell(const Vec3u &coord)=0
void castRayInternal(GridVisitor &visitor)
Definition: Grid.h:366
Vec3u _cells_nb
Definition: Grid.h:391
unsigned int _timestamp
Definition: Grid.h:389
Vec3r _orig
Definition: Grid.h:394
void displayDebug()
Definition: Grid.h:356
Vec3r _ray_dir
Definition: Grid.h:396
VirtualOccludersSet(Grid &_grid)
Definition: Grid.h:419
Polygon3r * next(bool stopOnNewCell)
OccludersSet & occluders()
Definition: Grid.h:126
allOccludersGridVisitor(OccludersSet &occluders)
Definition: Grid.h:120
virtual void discoverCell(Cell *cell)
Definition: Grid.h:171
firstIntersectionGridVisitor(const Vec3r &ray_org, const Vec3r &ray_dir, const Vec3r &cell_size)
Definition: Grid.h:154
static void clear(Message *msg)
Definition: msgfmt.c:294
inherits from class Rep
Definition: AppCanvas.cpp:32
vector< Polygon3r * > OccludersSet
Definition: Grid.h:47
double real
Definition: Precision.h:26