SPH
VolumeRenderer.cpp
Go to the documentation of this file.
2 #include "gui/Factory.h"
3 #include "gui/objects/Camera.h"
8 
10 
12  : IRaytracer(scheduler, settings) {}
13 
15 
16 const float MAX_DISTENTION = 50;
17 const float MIN_NEIGHS = 8;
18 
19 void VolumeRenderer::initialize(const Storage& storage,
20  const IColorizer& colorizer,
21  const ICamera& UNUSED(camera)) {
22  cached.r = storage.getValue<Vector>(QuantityId::POSITION).clone();
23  cached.colors.resize(cached.r.size());
24  for (Size i = 0; i < cached.r.size(); ++i) {
25  cached.colors[i] = colorizer.evalColor(i);
26  }
27 
28  cached.distention.resize(cached.r.size());
29 
30  KdTree<KdNode> tree;
31  tree.build(*scheduler, cached.r, FinderFlag::SKIP_RANK);
32 
33  Array<BvhSphere> spheres(cached.r.size());
34  spheres.reserve(cached.r.size());
36  parallelFor(*scheduler, neighs, 0, cached.r.size(), [&](const Size i, Array<NeighbourRecord>& local) {
37  const float initialRadius = cached.r[i][H];
38  float radius = initialRadius;
39  while (radius < MAX_DISTENTION * initialRadius) {
40  tree.findAll(cached.r[i], radius, local);
41  if (local.size() >= MIN_NEIGHS) {
42  break;
43  } else {
44  radius *= 1.5f;
45  }
46  }
47 
48  BvhSphere s(cached.r[i], radius);
49  s.userData = i;
50  spheres[i] = s;
51 
52  cached.distention[i] = min(radius / initialRadius, MAX_DISTENTION);
53  });
54  bvh.build(std::move(spheres));
55 
56  for (ThreadData& data : threadData) {
57  data.data = RayData{};
58  }
59 
60  shouldContinue = true;
61 }
62 
64  return !cached.r.empty();
65 }
66 
67 Rgba VolumeRenderer::shade(const RenderParams& params, const CameraRay& cameraRay, ThreadData& data) const {
68  const Vector dir = getNormalized(cameraRay.target - cameraRay.origin);
69  const Ray ray(cameraRay.origin, dir);
70 
71  RayData& rayData(data.data);
72  Array<IntersectionInfo>& intersections = rayData.intersections;
73  intersections.clear();
74  bvh.getAllIntersections(ray, backInserter(intersections));
75  if (params.volume.absorption > 0.f) {
76  std::sort(intersections.begin(), intersections.end());
77  }
78 
79  Rgba result = this->getEnviroColor(cameraRay);
80  for (const IntersectionInfo& is : reverse(intersections)) {
81  const BvhSphere* s = static_cast<const BvhSphere*>(is.object);
82  const Size i = s->userData;
83  const Vector hit = ray.origin() + ray.direction() * is.t;
84  const Vector center = cached.r[i];
85  const Vector toCenter = getNormalized(center - hit);
86  const float cosPhi = abs(dot(toCenter, ray.direction()));
87  const float distention = cached.distention[i];
88  const float secant = 2._f * getLength(center - hit) * cosPhi;
89  result = result * exp(-params.volume.absorption * secant);
90  // 3th power of cosPhi to give more weight to the sphere center,
91  // divide by distention^3; distention should not affect the total emission
92  result += cached.colors[i] * params.volume.emission * pow<3>(cosPhi / distention) * secant;
93  }
94  return result;
95 }
96 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Defines projection transforming 3D particles onto 2D screen.
Object converting quantity values of particles into colors.
const float radius
Definition: CurveDialog.cpp:18
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
ReverseAdapter< TContainer > reverse(TContainer &&container)
K-d tree for efficient search of neighbouring particles.
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
INLINE T exp(const T f)
Definition: MathUtils.h:269
INLINE auto abs(const T &f)
Definition: MathUtils.h:276
@ SKIP_RANK
The rank of particles is not created. 'Dummy' option that can be used to improve readability.
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Helper iterators allowing to save values to containers.
BackInserter< TContainer > backInserter(TContainer &c)
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
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
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
INLINE Vector getNormalized(const Vector &v)
Definition: Vector.h:590
const float MAX_DISTENTION
const float MIN_NEIGHS
void reserve(const TCounter newMaxSize)
Allocates enough memory to store the given number of elements.
Definition: Array.h:279
INLINE Iterator< StorageType > end() noexcept
Definition: Array.h:462
void clear()
Removes all elements from the array, but does NOT release the memory.
Definition: Array.h:434
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
Trait for finding intersections with a sphere.
Definition: Bvh.h:122
Size getAllIntersections(const Ray &ray, TOutIter iter) const
Returns all intersections of the ray.
Definition: Bvh.inl.h:129
Interface defining a camera or view, used by a renderer.
Definition: Camera.h:62
Interface for objects assigning colors to particles.
Definition: Colorizer.h:34
virtual Rgba evalColor(const Size idx) const =0
Returns the color of idx-th particle.
Base class for renderers based on raytracing.
Definition: IRenderer.h:196
Rgba getEnviroColor(const CameraRay &ray) const
Definition: IRenderer.cpp:149
SharedPtr< IScheduler > scheduler
Definition: IRenderer.h:198
void build(IScheduler &scheduler, ArrayView< const Vector > points, Flags< FinderFlag > flags=FinderFlag::MAKE_RANK)
Constructs the struct with an array of vectors.
K-d tree, used for hierarchical clustering of particles and accelerated Kn queries.
Definition: KdTree.h:136
Definition: Bvh.h:10
Definition: Color.h:8
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
Template for storing a copy of a value for every thread in given scheduler.
Definition: ThreadLocal.h:36
virtual bool isInitialized() const override
Checks if the renderer has been initialized.
Array< float > distention
Distention factor of each particle.
virtual void initialize(const Storage &storage, const IColorizer &colorizer, const ICamera &camera) override
Prepares the objects for rendering and updates its data.
VolumeRenderer(SharedPtr< IScheduler > scheduler, const GuiSettings &settings)
Size userData
Generic user data, can be used to store additional information to the primitives.
Definition: Bvh.h:49
Ray given by origin and target point.
Definition: Camera.h:56
Vector origin
Definition: Camera.h:57
Vector target
Definition: Camera.h:58
Holds intormation about intersection.
Definition: Bvh.h:53
Parameters of the rendered image.
Definition: IRenderer.h:60
struct RenderParams::@36 volume
Parameters of volumetric renderer.
float absorption
Absorption coefficient [m^-1].
Definition: IRenderer.h:140
float emission
Emission multiplier.
Definition: IRenderer.h:127