mantaflow  0.10
A framework for fluid simulation
mesh.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * MantaFlow fluid solver framework
5  * Copyright 2011 Tobias Pfaff, Nils Thuerey
6  *
7  * This program is free software, distributed under the terms of the
8  * GNU General Public License (GPL)
9  * http://www.gnu.org/licenses
10  *
11  * Meshes
12  *
13  * note: this is only a temporary solution, details are bound to change
14  * long term goal is integration with Split&Merge code by Wojtan et al.
15  *
16  ******************************************************************************/
17 
18 #ifndef _MESH_H
19 #define _MESH_H
20 
21 #include <vector>
22 #include "manta.h"
23 #include "vectorbase.h"
24 #include <set>
25 namespace Manta {
26 
27 // fwd decl
28 class GridBase;
29 class LevelsetGrid;
30 class FlagGrid;
31 class MACGrid;
32 class Shape;
33 
35 struct Node {
36  Node() : flags(0), pos(Vec3::Zero), normal(Vec3::Zero) {}
37  Node(const Vec3& p) : flags(0), pos(p) {}
38  int flags;
39  Vec3 pos, normal;
40 };
41 
43 struct Triangle {
44  Triangle() : flags(0) { c[0] = c[1] = c[2] = 0; }
45  Triangle(int n0, int n1, int n2) : flags(0) { c[0]=n0; c[1]=n1; c[2]=n2; }
46 
47  int c[3];
48  int flags;
49 };
50 
52 struct Corner {
53  Corner() : tri(-1), node(-1), opposite(-1), next(-1), prev(-1) {};
54  Corner(int t, int n) : tri(t), node(n), opposite(-1), next(-1), prev(-1) {}
55 
56  int tri;
57  int node;
58  int opposite;
59  int next;
60  int prev;
61 };
62 
64 struct NodeChannel {
65  virtual ~NodeChannel() {};
66  virtual void resize(int num) = 0;
67  virtual int size() = 0;
68  virtual NodeChannel* clone() = 0;
69 
70  virtual void addInterpol(int a, int b, Real alpha) = 0;
71  virtual void mergeWith(int node, int delnode, Real alpha) = 0;
72  virtual void renumber(const std::vector<int>& newIndex, int newsize) = 0;
73 };
74 
76 template<class T>
77 struct SimpleNodeChannel : public NodeChannel {
78  SimpleNodeChannel() {};
79  SimpleNodeChannel(const SimpleNodeChannel<T>& a) : data(a.data) {}
80  void resize(int num) { data.resize(num); }
81  virtual int size() { return data.size(); }
82  virtual void renumber(const std::vector<int>& newIndex, int newsize);
83 
84  //virtual void addSplit(int from, Real alpha) { data.push_back(data[from]); }
85 
86  std::vector<T> data;
87 };
88 
90 struct TriChannel {
91  virtual ~TriChannel() {};
92  virtual void resize(int num) = 0;
93  virtual TriChannel* clone() = 0;
94  virtual int size() = 0;
95 
96  virtual void addNew() = 0;
97  virtual void addSplit(int from, Real alpha) = 0;
98  virtual void remove(int tri) = 0;
99 };
100 
102 template<class T>
103 struct SimpleTriChannel : public TriChannel {
104  SimpleTriChannel() {};
105  SimpleTriChannel(const SimpleTriChannel<T>& a) : data(a.data) {}
106  void resize(int num) { data.resize(num); }
107  void remove(int tri) { if (tri!=(int)data.size()-1) data[tri] = *data.rbegin(); data.pop_back(); }
108  virtual int size() { return data.size(); }
109 
110  virtual void addSplit(int from, Real alpha) { data.push_back(data[from]); }
111  virtual void addNew() { data.push_back(T()); }
112 
113  std::vector<T> data;
114 };
115 
116 struct OneRing {
117  OneRing() {}
118  std::set<int> nodes;
119  std::set<int> tris;
120 };
121 
123 
125 PYTHON class Mesh : public PbClass {
127 public:
128  PYTHON() Mesh(FluidSolver* parent);
129  virtual ~Mesh();
130  virtual Mesh* clone();
131 
132  enum NodeFlags { NfNone = 0, NfFixed = 1, NfMarked = 2, NfKillme = 4, NfCollide = 8 };
133  enum FaceFlags { FfNone = 0, FfDoubled = 1, FfMarked = 2 };
134  enum MeshType { TypeNormal = 0, TypeVortexSheet };
135 
136  virtual MeshType getType() { return TypeNormal; }
137 
138  Real computeCenterOfMass(Vec3& cm) const;
139  void computeVertexNormals();
140 
141  // plugins
142  PYTHON() void clear();
143  PYTHON() void load (std::string name, bool append = false);
144  PYTHON() void fromShape (Shape& shape, bool append = false);
145  PYTHON() void save (std::string name);
146  PYTHON() void advectInGrid(FlagGrid& flags, MACGrid& vel, int integrationMode);
147  PYTHON() void scale(Vec3 s);
148  PYTHON() void offset(Vec3 o);
149 
150  PYTHON() void computeLevelset(LevelsetGrid& levelset, Real sigma, Real cutoff=-1.);
152  PYTHON() void applyMeshToGrid(GridBase* grid, FlagGrid* respectFlags=0, Real cutoff=-1.);
153 
154  // ops
155  Mesh& operator=(const Mesh& o);
156 
157  // accessors
158  inline int numTris() const { return mTris.size(); }
159  inline int numNodes() const { return mNodes.size(); }
160  inline int numTriChannels() const { return mTriChannels.size(); }
161  inline int numNodeChannels() const { return mNodeChannels.size(); }
162 
163  inline Triangle& tris(int i) { return mTris[i]; }
164  inline Node& nodes(int i) { return mNodes[i]; }
165  inline Corner& corners(int tri, int c) { return mCorners[tri*3+c]; }
166  inline Corner& corners(int c) { return mCorners[c]; }
167  inline NodeChannel* nodeChannel(int i) { return mNodeChannels[i]; }
168  inline TriChannel* triChannel(int i) { return mTriChannels[i]; }
169 
170  // allocate memory (eg upon load)
171  void resizeTris(int numTris);
172  void resizeNodes(int numNodes);
173 
174  inline bool isNodeFixed(int n) { return mNodes[n].flags & NfFixed; }
175  inline bool isTriangleFixed(int t) { return (mNodes[mTris[t].c[0]].flags & NfFixed) || (mNodes[mTris[t].c[1]].flags & NfFixed) || (mNodes[mTris[t].c[2]].flags & NfFixed); }
176 
177  inline const Vec3 getNode(int tri, int c) const { return mNodes[mTris[tri].c[c]].pos; }
178  inline Vec3& getNode(int tri, int c) { return mNodes[mTris[tri].c[c]].pos; }
179  inline const Vec3 getEdge(int tri, int e) const { return getNode(tri,(e+1)%3) - getNode(tri,e); }
180  inline OneRing& get1Ring(int node) { return m1RingLookup[node]; }
181  inline Real getFaceArea(int t) { Vec3 c0 = mNodes[mTris[t].c[0]].pos; return 0.5*norm(cross(mNodes[mTris[t].c[1]].pos - c0, mNodes[mTris[t].c[2]].pos - c0)); }
182  inline Vec3 getFaceNormal(int t) { Vec3 c0 = mNodes[mTris[t].c[0]].pos; return getNormalized(cross(mNodes[mTris[t].c[1]].pos - c0, mNodes[mTris[t].c[2]].pos - c0)); }
183  inline Vec3 getFaceCenter(int t) { return (mNodes[mTris[t].c[0]].pos + mNodes[mTris[t].c[1]].pos + mNodes[mTris[t].c[2]].pos) / 3.0; }
184  inline std::vector<Node>& getNodeData() { return mNodes; }
185 
186  void mergeNode(int node, int delnode);
187  int addNode(Node a);
188  int addTri(Triangle a);
189  void addCorner(Corner a);
190  void removeTri(int tri);
191  void removeTriFromLookup(int tri);
192  void removeNodes(const std::vector<int>& deletedNodes);
193  void rebuildCorners(int from=0, int to=-1);
194  void rebuildLookup(int from=0, int to=-1);
195  void rebuildQuickCheck();
196  void fastNodeLookupRebuild(int corner);
197  void sanityCheck(bool strict=true, std::vector<int>* deletedNodes=0, std::map<int,bool>* taintedTris=0);
198 
199  void addTriChannel(TriChannel* c) { mTriChannels.push_back(c); rebuildChannels(); }
200  void addNodeChannel(NodeChannel* c) { mNodeChannels.push_back(c); rebuildChannels(); }
201 
202 protected:
203  void rebuildChannels();
204 
205  std::vector<Node> mNodes;
206  std::vector<Triangle> mTris;
207  std::vector<Corner> mCorners;
208  std::vector<NodeChannel*> mNodeChannels;
209  std::vector<TriChannel*> mTriChannels;
210  std::vector<OneRing> m1RingLookup;
211 };
212 
213 
214 
215 
216 // ***************************************************************************************************************
217 // Implementation
218 
219 template<class T>
220 void SimpleNodeChannel<T>::renumber(const std::vector<int>& newIndex, int newsize) {
221  for(size_t i=0; i<newIndex.size(); i++) {
222  if(newIndex[i]!=-1)
223  data[newIndex[i]] = data[newsize+i];
224  }
225  data.resize(newsize);
226 }
227 
228 
229 
230 } //namespace
231 #endif
232 
233 
Definition: commonkernels.h:22
For fast access to nodes and neighboring triangles.
Definition: mesh.h:52
Base class for mesh data channels (texture coords, vorticity, ...)
Definition: mesh.h:90
Definition: grid.h:267
Definition: grid.h:229
Basic inlined vector class.
Definition: vectorbase.h:71
Node channel using only a vector.
Definition: mesh.h:77
static const Vector3D< S > Zero
zero element
Definition: vectorbase.h:212
Definition: mesh.h:116
Node position and flags.
Definition: mesh.h:35
Carries indices of its nodes.
Definition: mesh.h:43
Triangle mesh class.
Definition: mesh.h:126
Definition: levelset.h:25
Definition: shapes.h:29
Base class for mesh data channels (texture coords, vorticity, ...)
Definition: mesh.h:64
Tri channel using only a vector.
Definition: mesh.h:103
Definition: grid.h:29
Definition: fluidsolver.h:28