PolyVox  0.3.0-dev
Open source voxel management library
Density.h
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 #ifndef __PolyVox_Density_H__
25 #define __PolyVox_Density_H__
26 
27 #include "PolyVoxCore/DefaultMarchingCubesController.h" //We'll specialise the controller contained in here
28 
29 #include "Impl/TypeDef.h"
30 
31 #include <limits>
32 
33 #undef min
34 #undef max
35 
36 namespace PolyVox
37 {
42  template <typename Type>
43  class Density
44  {
45  public:
47  Density() : m_uDensity(0) {}
48 
50  Density(Type uDensity) : m_uDensity(uDensity) {}
51 
52  // The LowPassFilter uses this to convert between normal and accumulated types.
54  template <typename CastType> explicit Density(const Density<CastType>& density)
55  {
56  m_uDensity = static_cast<Type>(density.getDensity());
57  }
58 
59  bool operator==(const Density& rhs) const
60  {
61  return (m_uDensity == rhs.m_uDensity);
62  };
63 
64  bool operator!=(const Density& rhs) const
65  {
66  return !(*this == rhs);
67  }
68 
69  // For densities we can supply mathematical operators which behave in an intuitive way.
70  // In particular the ability to add and subtract densities is important in order to
71  // apply an averaging filter. The ability to divide by an integer is also needed for
72  // this same purpose.
74  {
75  m_uDensity += rhs.m_uDensity;
76  return *this;
77  }
78 
80  {
81  m_uDensity -= rhs.m_uDensity;
82  return *this;
83  }
84 
86  {
87  m_uDensity /= rhs;
88  return *this;
89  }
90 
92  Type getDensity() const { return m_uDensity; }
98  void setDensity(Type uDensity) { m_uDensity = uDensity; }
99 
101  static Type getMaxDensity() { return (std::numeric_limits<Type>::max)(); }
103  static Type getMinDensity() { return (std::numeric_limits<Type>::min)(); }
104 
105  private:
106  Type m_uDensity;
107  };
108 
109  template <typename Type>
111  {
112  Density<Type> result = lhs;
113  result += rhs;
114  return result;
115  }
116 
117  template <typename Type>
119  {
120  Density<Type> result = lhs;
121  result -= rhs;
122  return result;
123  }
124 
125  template <typename Type>
127  {
128  Density<Type> result = lhs;
129  result /= rhs;
130  return result;
131  }
132 
133  // These are the predefined density types. The 8-bit types are sufficient for many purposes (including
134  // most games) but 16-bit and float types do have uses particularly in medical/scientific visualisation.
139 
143  template <typename Type>
145  {
146  public:
147  typedef Type DensityType;
148  typedef float MaterialType;
149 
151  {
152  // Default to a threshold value halfway between the min and max possible values.
154  }
155 
157  {
158  m_tThreshold = tThreshold;
159  }
160 
162  {
163  return voxel.getDensity();
164  }
165 
167  {
168  return 1;
169  }
170 
172  {
173  return m_tThreshold;
174  }
175 
176  void setThreshold(DensityType tThreshold)
177  {
178  m_tThreshold = tThreshold;
179  }
180 
181  private:
182  DensityType m_tThreshold;
183  };
184 }
185 
186 #endif //__PolyVox_Density_H__