Nori  23
block.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /* =======================================================================
20  This file contains classes for parallel rendering of "image blocks".
21  * ======================================================================= */
22 
23 #if !defined(__NORI_PARALLEL_H)
24 #define __NORI_PARALLEL_H
25 
26 #include <nori/color.h>
27 #include <nori/vector.h>
28 #include <tbb/mutex.h>
29 
30 #define NORI_BLOCK_SIZE 32 /* Block size used for parallelization */
31 
32 NORI_NAMESPACE_BEGIN
33 
48 class ImageBlock : public Eigen::Array<Color4f, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> {
49 public:
58  ImageBlock(const Vector2i &size, const ReconstructionFilter *filter);
59 
61  ~ImageBlock();
62 
63  void init(const Vector2i &size, const ReconstructionFilter *filter);
64 
66  void setOffset(const Point2i &offset) { m_offset = offset; }
67 
69  inline const Point2i &getOffset() const { return m_offset; }
70 
72  void setSize(const Point2i &size) { m_size = size; }
73 
75  inline const Vector2i &getSize() const { return m_size; }
76 
78  inline int getBorderSize() const { return m_borderSize; }
79 
80  inline uint32_t getBlockId() const { return m_blockId; }
81  void setBlockId(uint32_t id) { m_blockId = id;}
82 
89  Bitmap *toBitmap() const;
90 
92  void fromBitmap(const Bitmap &bitmap);
93 
95  void clear() { setConstant(Color4f()); }
96 
98  void put(const Point2f &pos, const Color3f &value);
99 
106  void put(ImageBlock &b);
107 
109  inline void lock() const { m_mutex.lock(); }
110 
112  inline void unlock() const { m_mutex.unlock(); }
113 
115  std::string toString() const;
116 protected:
117  Point2i m_offset;
118  Vector2i m_size;
119  int m_borderSize = 0;
120  float *m_filter = nullptr;
121  float m_filterRadius = 0;
122  float *m_weightsX = nullptr;
123  float *m_weightsY = nullptr;
124  float m_lookupFactor = 0;
125  uint32_t m_blockId; // id given by the block generator
126  mutable tbb::mutex m_mutex;
127 };
128 
138 public:
146  BlockGenerator(const Vector2i &size, int blockSize);
147 
155  bool next(ImageBlock &block);
156 
162  void reset();
163 
165  int getBlockCount() const { return m_blocksLeft; }
166 protected:
167  enum EDirection { ERight = 0, EDown, ELeft, EUp };
168 
169  Point2i m_block;
170  Vector2i m_numBlocks;
171  Vector2i m_size;
172  int m_blockSize;
173  int m_numSteps;
174  int m_blocksLeft;
175  int m_stepsLeft;
176  int m_direction;
177  tbb::mutex m_mutex;
178 };
179 
180 NORI_NAMESPACE_END
181 
182 #endif /* __NORI_PARALLEL_H */
Stores a RGB high dynamic-range bitmap.
Definition: bitmap.h:32
Spiraling block generator.
Definition: block.h:137
int getBlockCount() const
Return the total number of blocks.
Definition: block.h:165
BlockGenerator(const Vector2i &size, int blockSize)
Create a block generator with.
Definition: block.cpp:140
void reset()
Reset to the first block.
Definition: block.cpp:148
bool next(ImageBlock &block)
Return the next block to be rendered.
Definition: block.cpp:156
Weighted pixel storage for a rectangular subregion of an image.
Definition: block.h:48
void put(const Point2f &pos, const Color3f &value)
Record a sample with the given position and radiance value.
Definition: block.cpp:93
ImageBlock(const Vector2i &size, const ReconstructionFilter *filter)
Definition: block.cpp:27
void unlock() const
Unlock the image block.
Definition: block.h:112
const Vector2i & getSize() const
Return the size of the block within the main image.
Definition: block.h:75
void clear()
Clear all contents.
Definition: block.h:95
void fromBitmap(const Bitmap &bitmap)
Convert a bitmap into an image block.
Definition: block.cpp:84
int getBorderSize() const
Return the border size in pixels.
Definition: block.h:78
~ImageBlock()
Release all memory.
Definition: block.cpp:31
void lock() const
Lock the image block (using an internal mutex)
Definition: block.h:109
std::string toString() const
Return a human-readable string summary.
Definition: block.cpp:135
void setSize(const Point2i &size)
Configure the size of the block within the main image.
Definition: block.h:72
Bitmap * toBitmap() const
Turn the block into a proper bitmap.
Definition: block.cpp:76
void setOffset(const Point2i &offset)
Configure the offset of the block within the main image.
Definition: block.h:66
const Point2i & getOffset() const
Return the offset of the block within the main image.
Definition: block.h:69
Generic radially symmetric image reconstruction filter.
Definition: rfilter.h:41
Represents a linear RGB color value.
Definition: color.h:29
Represents a linear RGB color and a weight.
Definition: color.h:89