KrisLibrary  1.0.0
SparseVolumeGrid.h
1 #ifndef SPARSE_VOLUME_GRID_H
2 #define SPARSE_VOLUME_GRID_H
3 
4 #include "GridSubdivision.h"
5 #include "MultiVolumeGrid.h"
6 #include <KrisLibrary/meshing/TriMesh.h>
7 
8 namespace Geometry {
9 
10  using namespace Math3D;
11 
21 {
22  public:
23  class Block {
24  public:
25  int id;
26  IntTriple index;
27  MultiVolumeGrid grid;
28  };
29 
30  SparseVolumeGrid(Real blockRes);
31  SparseVolumeGrid(const Vector3& blockRes);
33  inline bool IsEmpty() const { return hash.buckets.empty(); }
34  void Clear();
35  int AddChannel(const std::string& name);
36  int GetChannel(const std::string& name) const;
37  size_t GetNumChannels() const { return channelNames.size(); }
38  inline void SetBlockSize(int m,int n,int p) { SetBlockSize(IntTriple(m,n,p)); }
39  void SetBlockSize(const IntTriple& size);
40  inline IntTriple GetBlockSize() const { return blockSize; }
41  void SetBlockRes(const Vector3& blockRes);
42  inline void SetBlockRes(Real blockRes) { SetBlockRes(Vector3(blockRes)); }
43  Vector3 GetBlockRes() const;
44  inline void SetDefaultValue(Real _defaultValue) { defaultValue.set(_defaultValue); }
45  inline void SetDefaultValue(const Vector& _defaultValue) { defaultValue = _defaultValue; }
47  void MakeSimilar(const SparseVolumeGrid& grid);
49  bool IsSimilar(const SparseVolumeGrid& grid) const;
50  void GetCell(int i,int j,int k,AABB3D& cell) const;
51  void GetCellCenter(int i,int j,int k,Vector3& center) const;
52  inline Vector3 GetCellSize() const { return cellRes; }
53  void GetIndex(const Vector3& pt,int& i,int& j,int& k) const;
54  void GetIndexAndParams(const Vector3& pt,IntTriple& index,Vector3& params) const;
55  void GetIndexRange(const AABB3D& range,IntTriple& imin,IntTriple& imax) const;
56  inline void GetCell(const IntTriple& index,AABB3D& cell) const { GetCell(index.a,index.b,index.c,cell); }
57  inline void GetCenter(const IntTriple& index,Vector3& center) const { GetCellCenter(index.a,index.b,index.c,center); }
58  inline void GetIndex(const Vector3& pt,IntTriple& index) const { GetIndex(pt,index.a,index.b,index.c); }
60  void SetValue(int i,int j,int k,Real value,int channel=0);
62  void SetValue(const Vector3& pt,Real value,int channel=0);
64  void SetValue(int i,int j,int k,const Vector& values);
66  void SetValue(const Vector3& pt,const Vector& values);
68  Real GetValue(int i,int j,int k,int channel=0) const;
70  Real GetValue(const Vector3& pt,int channel=0) const;
72  void GetValue(int i,int j,int k,Vector& values) const;
74  void GetValue(const Vector3& pt,Vector& values) const;
76  void GetBlock(int i,int j,int k,IntTriple& blockIndex) const;
78  void GetBlock(const Vector3& pt,IntTriple& blockIndex) const;
80  void GetBlockRange(const AABB3D& range,IntTriple& blockMin,IntTriple& blockMax) const;
82  Block* BlockPtr(const IntTriple& blockIndex) const;
85  Block* GetMakeBlock(const IntTriple& blockIndex);
88  bool MakeBlock(const IntTriple& blockIndex);
90  bool EraseBlock(const IntTriple& blockIndex);
92  void AddBlocks(const SparseVolumeGrid& grid);
94  void SubdivideBlock(const IntTriple& block);
95 
98  void GetSamples(MultiVolumeGrid& range) const;
99  void GetSamples(Meshing::VolumeGrid& range,int channel=0) const;
101  void GetSamples_Trilinear(MultiVolumeGrid& range) const;
102  void GetSamples_Trilinear(Meshing::VolumeGrid& range,int channel=0) const;
104  void GetSamples_Average(MultiVolumeGrid& range) const;
105  void GetSamples_Average(Meshing::VolumeGrid& range,int channel=0) const;
107  void SetSamples(const MultiVolumeGrid& range);
108  void SetSamples(const Meshing::VolumeGrid& range,int channel=0);
109 
111  Real TrilinearInterpolate(const Vector3& pt,int channel=0) const;
112  void TrilinearInterpolate(const Vector3& pt,Vector& values) const;
114  Real Average(const AABB3D& range,int channel=0) const;
115  void Average(const AABB3D& range,Vector& values) const;
118  void Gradient(const Vector3& pt,Vector3& grad,int channel=0) const;
119  void Add(const SparseVolumeGrid& grid);
120  void Subtract(const SparseVolumeGrid& grid);
121  void Multiply(const SparseVolumeGrid& grid);
122  void Max(const SparseVolumeGrid& grid);
123  void Min(const SparseVolumeGrid& grid);
124  void Add(Real val,int channel=0);
125  void Multiply(Real val,int channel=0);
126  void Max(Real val,int channel=0);
127  void Min(Real val,int channel=0);
128 
130  void ExtractMesh(float isosurface,Meshing::TriMesh& mesh);
131 
132  GridHash3D hash;
133  int blockIDCounter;
134  IntTriple blockSize;
135  std::vector<std::string> channelNames;
136  Vector3 cellRes;
137  Vector defaultValue;
138 };
139 
140 
141 } //namespace Geometry
142 
143 #endif
144 
A lightweight integer 3-tuple class.
Definition: IntTriple.h:9
A grid containing objects (referenced by void pointers)
Definition: GridSubdivision.h:32
A 3D vector class.
Definition: math3d/primitives.h:136
A 3D array over a sparse, axis aligned 3D volume. Has a similar interface as VolumeGrid, but uses a hash grid data structure to achieve better performance in large grids with infinite domain.
Definition: SparseVolumeGrid.h:20
A 3D axis-aligned bounding box.
Definition: AABB3D.h:13
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:12
Definition: SparseVolumeGrid.h:23
A basic triangle mesh.
Definition: TriMesh.h:41
A 3D array over an axis-aligned 3D volume, containing one or more channels.
Definition: MultiVolumeGrid.h:20
A 3D array over an axis-aligned 3D volume, containing Real values.
Definition: AnyGeometry.h:10
Contains all definitions in the Geometry package.
Definition: AnyGeometry.h:11