SPH
MeshDomain.cpp
Go to the documentation of this file.
3 #include "thread/Scheduler.h"
4 
6 
7 MeshDomain::MeshDomain(IScheduler& scheduler, Array<Triangle>&& triangles, const MeshParams& params) {
8  Array<BvhTriangle> bvhTriangles;
9  for (Triangle& t : triangles) {
10  // transform vertices in place
11  for (Size i = 0; i < 3; ++i) {
12  t[i] = params.matrix * t[i];
13  }
14 
15  bvhTriangles.emplaceBack(t[0], t[1], t[2]);
16  cached.box.extend(t.getBBox());
17  }
18  const Vector center = cached.box.center();
19 
20  // compute volume (using center for optimal accuracy)
21  cached.volume = 0._f;
22  cached.area = 0._f;
23  for (const Triangle& t : triangles) {
24  cached.volume += dot(t[0] - center, cross(t[1] - center, t[2] - center)) / 6._f;
25  cached.area += t.area();
26  cached.points.push(t.center());
27  cached.normals.push(t.normal());
28  }
29  bvh.build(std::move(bvhTriangles));
30 
31  if (params.precomputeInside) {
32  Size res = params.volumeResolution;
33  mask = Volume<char>(cached.box, res);
34 
35  parallelFor(scheduler, 0, res, [this, res](const Size z) {
36  for (Size y = 0; y < res; ++y) {
37  for (Size x = 0; x < res; ++x) {
38  mask.cell(x, y, z) =
39  containImpl(cached.box.lower() + cached.box.size() / res * Vector(x, y, z)) ? 1 : 0;
40  }
41  }
42  });
43  }
44 }
45 
46 bool MeshDomain::contains(const Vector& v) const {
47  if (mask.empty()) {
48  return this->containImpl(v);
49  } else {
50  return mask(v) > 0;
51  }
52 }
53 
55  switch (type) {
57  for (Size i = 0; i < vs.size(); ++i) {
58  if (!this->contains(vs[i])) {
59  output.push(i);
60  }
61  }
62  break;
63  case SubsetType::INSIDE:
64  for (Size i = 0; i < vs.size(); ++i) {
65  if (this->contains(vs[i])) {
66  output.push(i);
67  }
68  }
69  break;
70  default:
72  }
73 }
74 
77 }
78 
80  Array<Ghost>& ghosts,
81  const Float UNUSED(eta),
82  const Float UNUSED(eps)) const {
83  ghosts.clear();
84  const Float h = vs[0][H];
85  for (Size i = 0; i < cached.points.size(); ++i) {
86  ghosts.push(Ghost{ cached.points[i] - h * cached.normals[i], 0 });
87  }
88 }
89 
90 bool MeshDomain::containImpl(const Vector& v) const {
91  // As we assume watertight mesh, we could theoretically make just one intersection test, but this
92  // could cause problems at grazing angles, returning false positives. Instead, we opt for a more
93  // robust (albeit slower) solution and cast a ray for each axis.
94  Size insideCnt = 0;
95  Size outsideCnt = 0;
96  static Array<Vector> dirs = {
97  Vector(1._f, 0._f, 0._f),
98  Vector(-1._f, 0._f, 0._f),
99  Vector(0._f, 1._f, 0._f),
100  Vector(0._f, -1._f, 0._f),
101  Vector(0._f, 0._f, 1._f),
102  Vector(0._f, 0._f, -1._f),
103  };
104 
105  for (Vector& dir : dirs) {
106  Ray ray(v, dir);
107  const Size count = bvh.getAllIntersections(ray, NullInserter{});
108  if (isOdd(count)) {
109  insideCnt++;
110  } else {
111  outsideCnt++;
112  }
113  }
114  SPH_ASSERT(insideCnt + outsideCnt == 6);
115  return insideCnt >= outsideCnt;
116 }
117 
#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
SubsetType
Definition: Domain.h:16
@ INSIDE
Marks all vectors inside of the domain.
@ OUTSIDE
Marks all vectors outside of the domain.
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
constexpr INLINE bool isOdd(const T &f)
Definition: MathBasic.h:40
Domain represented by triangular mesh.
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Helper iterators allowing to save values to containers.
Interface for executing tasks (potentially) asynchronously.
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 BasicVector< float > cross(const BasicVector< float > &v1, const BasicVector< float > &v2)
Cross product between two vectors.
Definition: Vector.h:559
BasicVector< Float > Vector
Definition: Vector.h:539
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
@ H
Definition: Vector.h:25
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Definition: Array.h:332
void clear()
Removes all elements from the array, but does NOT release the memory.
Definition: Array.h:434
void build(Array< TBvhObject > &&objects)
Contructs the BVH from given set of objects.
Definition: Bvh.inl.h:151
Size getAllIntersections(const Ray &ray, TOutIter iter) const
Returns all intersections of the ray.
Definition: Bvh.inl.h:129
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
virtual void getSubset(ArrayView< const Vector > vs, Array< Size > &output, const SubsetType type) const override
Returns an array of indices, marking vectors with given property by their index.
Definition: MeshDomain.cpp:54
virtual bool contains(const Vector &v) const override
Checks if the given point lies inside the domain.
Definition: MeshDomain.cpp:46
MeshDomain(IScheduler &scheduler, Array< Triangle > &&triangles, const MeshParams &params=MeshParams{})
Definition: MeshDomain.cpp:7
virtual void addGhosts(ArrayView< const Vector > vs, Array< Ghost > &ghosts, const Float eta, const Float eps) const override
Duplicates positions located close to the boundary, placing copies ("ghosts") symmetrically to the ot...
Definition: MeshDomain.cpp:79
virtual void project(ArrayView< Vector > vs, Optional< ArrayView< Size >> indices) const override
Projects vectors outside of the domain onto its boundary.
Definition: MeshDomain.cpp:75
Helper output iterator that simply ignores the written values.
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
Definition: Bvh.h:10
Triangle.h.
Definition: Triangle.h:14
bool empty() const
Definition: Volume.h:44
TValue & cell(const Size x, const Size y, const Size z)
Definition: Volume.h:36
Definition: Domain.h:22
bool precomputeInside
If true, cached volume is created to allow fast calls of contains.
Definition: MeshDomain.h:22
Size volumeResolution
Resolution of the volume, used if precomputeInside == true.
Definition: MeshDomain.h:25
AffineMatrix matrix
Arbitrary transformation matrix applied on the mesh.
Definition: MeshDomain.h:19