SPH
IRenderer.cpp
Go to the documentation of this file.
2 #include "gui/Factory.h"
3 #include "gui/Settings.h"
4 #include "gui/objects/Camera.h"
6 #include "system/Profiler.h"
7 #include <iostream>
8 
10 
13  showKey = gui.get<bool>(GuiSettingsId::SHOW_KEY);
15  particles.grayScale = gui.get<bool>(GuiSettingsId::FORCE_GRAYSCALE);
16  particles.doAntialiasing = gui.get<bool>(GuiSettingsId::ANTIALIASED);
17  particles.smoothed = gui.get<bool>(GuiSettingsId::SMOOTH_PARTICLES);
18  particles.renderGhosts = gui.get<bool>(GuiSettingsId::RENDER_GHOST_PARTICLES);
19  surface.level = float(gui.get<Float>(GuiSettingsId::SURFACE_LEVEL));
20  surface.ambientLight = float(gui.get<Float>(GuiSettingsId::SURFACE_AMBIENT));
21  surface.sunLight = float(gui.get<Float>(GuiSettingsId::SURFACE_SUN_INTENSITY));
22  surface.emission = float(gui.get<Float>(GuiSettingsId::SURFACE_EMISSION));
23  volume.emission = float(gui.get<Float>(GuiSettingsId::VOLUME_EMISSION));
24  volume.absorption = float(gui.get<Float>(GuiSettingsId::VOLUME_ABSORPTION));
25  volume.compressionFactor = float(gui.get<Float>(GuiSettingsId::COLORMAP_LOGARITHMIC_FACTOR));
26  contours.isoStep = float(gui.get<Float>(GuiSettingsId::CONTOUR_SPACING));
27  contours.gridSize = gui.get<int>(GuiSettingsId::CONTOUR_GRID_SIZE);
28  contours.showLabels = gui.get<bool>(GuiSettingsId::CONTOUR_SHOW_LABELS);
29 }
30 
31 inline auto seeder() {
32  return [seed = 1337]() mutable { return seed++; };
33 }
34 
36  : rng(seed) {}
37 
40  , threadData(*scheduler, seeder()) {
41  fixed.colorMap = Factory::getColorMap(settings);
42  fixed.subsampling = settings.get<int>(GuiSettingsId::RAYTRACE_SUBSAMPLING);
43  fixed.iterationLimit = settings.get<int>(GuiSettingsId::RAYTRACE_ITERATION_LIMIT);
44 
45  fixed.enviro.color = settings.get<Rgba>(GuiSettingsId::BACKGROUND_COLOR);
46  std::string hdriPath = settings.get<std::string>(GuiSettingsId::RAYTRACE_HDRI);
47  if (!hdriPath.empty()) {
48  fixed.enviro.hdri = Texture(Path(hdriPath), TextureFiltering::BILINEAR);
49  }
50 
51  shouldContinue = true;
52 }
53 
54 void IRaytracer::render(const RenderParams& params, Statistics& UNUSED(stats), IRenderOutput& output) const {
55  shouldContinue = true;
56  // std::cout << "IRaytracer::render" << std::endl;
57 
58  if (RawPtr<LogarithmicColorMap> logMap = dynamicCast<LogarithmicColorMap>(fixed.colorMap.get())) {
59  logMap->setFactor(params.volume.compressionFactor);
60  }
61 
62  FrameBuffer fb(params.camera->getSize());
63  for (Size iteration = 0; iteration < fixed.iterationLimit && shouldContinue; ++iteration) {
64  // std::cout << "IRaytracer::refine" << std::endl;
65  this->refine(params, iteration, fb);
66 
67  const bool isFinal = (iteration == fixed.iterationLimit - 1);
68  if (fixed.colorMap) {
69  Bitmap<Rgba> bitmap = fixed.colorMap->map(fb.getBitmap());
70  // std::cout << "IRaytracer - update" << std::endl;
71  output.update(std::move(bitmap), {}, isFinal);
72  } else {
73  // std::cout << "IRaytracer - update" << std::endl;
74  output.update(fb.getBitmap(), {}, isFinal);
75  }
76  }
77  /*if (!shouldContinue) {
78  std::cout << "! Leaving render as cancelled" << std::endl;
79  }*/
80 }
81 
82 INLINE float sampleTent(const float x) {
83  if (x < 0.5f) {
84  return sqrt(2.f * x) - 1.f;
85  } else {
86  return 1.f - sqrt(1.f - 2.f * (x - 0.5f));
87  }
88 }
89 
90 INLINE Coords sampleTent2d(const Size level, const float halfWidth, UniformRng& rng) {
91  if (level == 1) {
92  const float x = 0.5f + sampleTent(float(rng())) * halfWidth;
93  const float y = 0.5f + sampleTent(float(rng())) * halfWidth;
94  return Coords(x, y);
95  } else {
96  // center of the pixel
97  return Coords(0.5f, 0.5f);
98  }
99 }
100 
101 void IRaytracer::refine(const RenderParams& params, const Size iteration, FrameBuffer& fb) const {
102  MEASURE_SCOPE("Rendering frame");
103  const Size level = 1 << max(int(fixed.subsampling) - int(iteration), 0);
104  const Pixel size = params.camera->getSize();
105  Pixel actSize;
106  actSize.x = size.x / level + sgn(size.x % level);
107  actSize.y = size.y / level + sgn(size.y % level);
108  Bitmap<Rgba> bitmap(actSize);
109 
110  const bool first = (iteration == 0);
112  threadData,
113  0,
114  Size(bitmap.size().y),
115  1,
116  [this, &bitmap, &params, level, first](Size y, ThreadData& data) {
117  if (!shouldContinue && !first) {
118  return;
119  }
120  for (Size x = 0; x < Size(bitmap.size().x); ++x) {
121  const Coords pixel = Coords(x * level, y * level) +
122  sampleTent2d(level, params.surface.filterWidth / 2.f, data.rng);
123  const Optional<CameraRay> cameraRay = params.camera->unproject(pixel);
124  if (!cameraRay) {
125  bitmap[Pixel(x, y)] = Rgba::black();
126  continue;
127  }
128 
129  bitmap[Pixel(x, y)] = this->shade(params, cameraRay.value(), data);
130  }
131  });
132 
133  if (!shouldContinue && !first) {
134  return;
135  }
136  if (level == 1) {
137  fb.accumulate(bitmap);
138  } else {
139  Bitmap<Rgba> full(size);
140  for (Size y = 0; y < Size(full.size().y); ++y) {
141  for (Size x = 0; x < Size(full.size().x); ++x) {
142  full[Pixel(x, y)] = bitmap[Pixel(x / level, y / level)];
143  }
144  }
145  fb.override(std::move(full));
146  }
147 }
148 
150  if (fixed.enviro.hdri.empty()) {
151  return fixed.enviro.color;
152  } else {
153  const Vector dir = ray.target - ray.origin;
155  const SphericalCoords spherical = cartensianToSpherical(Vector(dir[X], dir[Z], dir[Y]));
156  const Vector uvw(spherical.phi / (2._f * PI) + 0.5_f, spherical.theta / PI, 0._f);
157  return fixed.enviro.hdri.eval(uvw);
158  }
159 }
160 
161 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Defines projection transforming 3D particles onto 2D screen.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
INLINE float sampleTent(const float x)
Definition: IRenderer.cpp:82
INLINE Coords sampleTent2d(const Size level, const float halfWidth, UniformRng &rng)
Definition: IRenderer.cpp:90
auto seeder()
Definition: IRenderer.cpp:31
Interface for renderers.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
INLINE int sgn(const T val)
Definition: MathUtils.h:336
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Tool to measure time spent in functions and profile the code.
#define MEASURE_SCOPE(name)
Definition: Profiler.h:70
INLINE void parallelFor(IScheduler &scheduler, const Size from, const Size to, TFunctor &&functor)
Executes a functor concurrently from all available threads.
Definition: Scheduler.h:98
@ BILINEAR
Definition: Texture.h:12
INLINE SphericalCoords cartensianToSpherical(const Vector &v)
Converts vector in cartesian coordinates to spherical coordinates.
Definition: Vector.h:806
BasicVector< Float > Vector
Definition: Vector.h:539
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
const Bitmap< Rgba > & getBitmap() const
Definition: FrameBuffer.h:97
void accumulate(const Bitmap< Rgba > &pass)
Definition: FrameBuffer.h:80
void override(Bitmap< Rgba > &&pass)
Definition: FrameBuffer.h:92
INLINE TValue get(const GuiSettingsId id) const
Definition: Settings.h:245
virtual Pixel getSize() const =0
Returns the current resolution of the camera.
virtual Optional< CameraRay > unproject(const Coords &coords) const =0
Returns a ray in particle coordinates corresponding to given coordinates in the image plane.
virtual void render(const RenderParams &params, Statistics &stats, IRenderOutput &output) const final
Draws particles into the bitmap, given the data provided in initialize.
Definition: IRenderer.cpp:54
virtual Rgba shade(const RenderParams &params, const CameraRay &ray, ThreadData &data) const =0
Rgba getEnviroColor(const CameraRay &ray) const
Definition: IRenderer.cpp:149
ThreadLocal< ThreadData > threadData
Definition: IRenderer.h:210
SharedPtr< IScheduler > scheduler
Definition: IRenderer.h:198
IRaytracer(SharedPtr< IScheduler > scheduler, const GuiSettings &gui)
Definition: IRenderer.cpp:38
std::atomic_bool shouldContinue
Definition: IRenderer.h:212
virtual void update(const Bitmap< Rgba > &bitmap, Array< Label > &&labels, const bool isFinal)=0
May be called once after render finishes or multiple times for progressive renderers.
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
INLINE Type & value()
Returns the reference to the stored value.
Definition: Optional.h:172
Object representing a path on a filesystem.
Definition: Path.h:17
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Definition: Color.h:8
static Rgba black()
Definition: Color.h:190
Object holding various statistics about current run.
Definition: Statistics.h:22
Random number generator with uniform distribution.
Definition: Rng.h:16
@ COLORMAP_LOGARITHMIC_FACTOR
@ RAYTRACE_ITERATION_LIMIT
@ PARTICLE_RADIUS
Displayed radius of particle in units of smoothing length.
@ SURFACE_LEVEL
Value of iso-surface being constructed; lower value means larget bodies.
@ SURFACE_SUN_INTENSITY
Intentity of the sun.
@ SURFACE_AMBIENT
Ambient color for surface renderer.
AutoPtr< IColorMap > getColorMap(const GuiSettings &settings)
Definition: Factory.cpp:112
Ray given by origin and target point.
Definition: Camera.h:56
Vector origin
Definition: Camera.h:57
Vector target
Definition: Camera.h:58
Definition: Point.h:115
Definition: Point.h:101
Parameters of the rendered image.
Definition: IRenderer.h:60
struct RenderParams::@36 volume
Parameters of volumetric renderer.
Rgba background
Background color of the rendered image.
Definition: IRenderer.h:71
struct RenderParams::@33 particles
Parameters of the particle renderer.
AutoPtr< ICamera > camera
Camera used for rendering.
Definition: IRenderer.h:63
bool showKey
If true, a color palette and a distance scale is included in the image.
Definition: IRenderer.h:74
struct RenderParams::@35 surface
Parameters of rendered surface.
struct RenderParams::@37 contours
void initialize(const GuiSettings &gui)
Sets up parameters using values stored in settings.
Definition: IRenderer.cpp:11
float compressionFactor
Compression factor of the logarithmic tonemapper.
Definition: IRenderer.h:143
float filterWidth
Width of the image reconstruction filter.
Definition: IRenderer.h:130
Float phi
longitude
Definition: Vector.h:802
Float theta
latitude
Definition: Vector.h:801