SPH
Texture.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "gui/objects/Bitmap.h"
4 #include "quantities/IMaterial.h"
5 #include "quantities/Quantity.h"
6 #include "quantities/Storage.h"
7 
9 
13 };
14 
15 class Texture : public Noncopyable {
16 private:
17  Bitmap<Rgba> bitmap;
18  TextureFiltering filtering;
19 
20 public:
21  Texture() = default;
22 
23  explicit Texture(Bitmap<Rgba>&& bitmap, const TextureFiltering filtering)
24  : bitmap(std::move(bitmap))
25  , filtering(filtering) {}
26 
27  explicit Texture(const Path& path, const TextureFiltering filtering)
28  : filtering(std::move(filtering)) {
29  bitmap = loadBitmapFromFile(path);
30  }
31 
32  Rgba eval(const Vector& uvw) const {
33  switch (filtering) {
35  return this->evalNearestNeighbour(uvw);
37  return this->evalBilinear(uvw);
38  default:
40  }
41  }
42 
43  Texture clone() const {
44  Texture cloned;
45  cloned.filtering = filtering;
46  cloned.bitmap = Bitmap<Rgba>(bitmap.size());
47  for (int y = 0; y < bitmap.size().y; ++y) {
48  for (int x = 0; x < bitmap.size().x; ++x) {
49  cloned.bitmap[Pixel(x, y)] = bitmap[Pixel(x, y)];
50  }
51  }
52  return cloned;
53  }
54 
55  bool empty() const {
56  return bitmap.empty();
57  }
58 
59 private:
60  Rgba evalNearestNeighbour(const Vector& uvw) const {
61  const Pixel size = bitmap.size();
62  const Size u = clamp(int(uvw[X] * size.x), 0, size.x - 1);
63  const Size v = clamp(int(uvw[Y] * size.y), 0, size.y - 1);
64  return Rgba(bitmap[Pixel(u, v)]);
65  }
66 
67  Rgba evalBilinear(const Vector& uvw) const {
68  const Pixel size = bitmap.size();
69  const Vector textureUvw = clamp(Vector(uvw[X] * size.x, uvw[Y] * size.y, 0._f),
70  Vector(0._f),
71  Vector(size.x - 1, size.y - 1, 0._f) - Vector(EPS));
72  const Size u1 = int(textureUvw[X]);
73  const Size v1 = int(textureUvw[Y]);
74  const Size u2 = u1 + 1;
75  const Size v2 = v1 + 1;
76  const float a = float(textureUvw[X] - u1);
77  const float b = float(textureUvw[Y] - v1);
78  SPH_ASSERT(a >= 0.f && a <= 1.f, a);
79  SPH_ASSERT(b >= 0.f && b <= 1.f, b);
80 
81  return Rgba(bitmap[Pixel(u1, v1)]) * (1.f - a) * (1.f - b) +
82  Rgba(bitmap[Pixel(u2, v1)]) * a * (1.f - b) + //
83  Rgba(bitmap[Pixel(u1, v2)]) * (1.f - a) * b + //
84  Rgba(bitmap[Pixel(u2, v2)]) * a * b;
85  }
86 };
87 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Bitmap< Rgba > loadBitmapFromFile(const Path &path)
Definition: Bitmap.cpp:70
Wrapper of wxBitmap, will be possibly replaced by custom implementation.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Base class for all particle materials.
constexpr INLINE T clamp(const T &f, const T &f1, const T &f2)
Definition: MathBasic.h:35
constexpr Float EPS
Definition: MathUtils.h:30
#define NAMESPACE_SPH_END
Definition: Object.h:12
Holder of quantity values and their temporal derivatives.
Container for storing particle quantities and materials.
TextureFiltering
Definition: Texture.h:10
@ BILINEAR
Definition: Texture.h:12
@ NEAREST_NEIGHBOUR
Definition: Texture.h:11
BasicVector< Float > Vector
Definition: Vector.h:539
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
bool empty() const
Definition: Bitmap.h:76
Pixel size() const
Definition: Bitmap.h:72
Object representing a path on a filesystem.
Definition: Path.h:17
Definition: Color.h:8
bool empty() const
Definition: Texture.h:55
Texture()=default
Rgba eval(const Vector &uvw) const
Definition: Texture.h:32
Texture(Bitmap< Rgba > &&bitmap, const TextureFiltering filtering)
Definition: Texture.h:23
Texture clone() const
Definition: Texture.h:43
Texture(const Path &path, const TextureFiltering filtering)
Definition: Texture.h:27
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Object with deleted copy constructor and copy operator.
Definition: Object.h:54
Definition: Point.h:101