PolyVox  0.2.1
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 <cassert>
32 #include <limits>
33 
34 #undef min
35 #undef max
36 
37 namespace PolyVox
38 {
43 
44  // int32_t template parameter is a dummy, required as the compiler expects to be able to declare an
45  // instance of VoxelType::MaterialType without knowing that VoxelType doesn't actually have a material.
46  template <typename Type>
47  class Density
48  {
49  public:
51  Density() : m_uDensity(0) {}
52 
54  Density(Type uDensity) : m_uDensity(uDensity) {}
55 
56  // The LowPassFilter uses this to convert between normal and accumulated types.
58  template <typename CastType> explicit Density(const Density<CastType>& density)
59  {
60  m_uDensity = static_cast<Type>(density.getDensity());
61  }
62 
63  bool operator==(const Density& rhs) const
64  {
65  return (m_uDensity == rhs.m_uDensity);
66  };
67 
68  bool operator!=(const Density& rhs) const
69  {
70  return !(*this == rhs);
71  }
72 
73  // For densities we can supply mathematical operators which behave in an intuitive way.
74  // In particular the ability to add and subtract densities is important in order to
75  // apply an averaging filter. The ability to divide by an integer is also needed for
76  // this same purpose.
78  {
79  m_uDensity += rhs.m_uDensity;
80  return *this;
81  }
82 
84  {
85  m_uDensity -= rhs.m_uDensity;
86  return *this;
87  }
88 
89  Density<Type>& operator/=(uint32_t rhs)
90  {
91  m_uDensity /= rhs;
92  return *this;
93  }
94 
96  Type getDensity() const { return m_uDensity; }
102  void setDensity(Type uDensity) { m_uDensity = uDensity; }
103 
105  static Type getMaxDensity() { return (std::numeric_limits<Type>::max)(); }
107  static Type getMinDensity() { return (std::numeric_limits<Type>::min)(); }
108 
109  private:
110  Type m_uDensity;
111  };
112 
113  template <typename Type>
115  {
116  Density<Type> result = lhs;
117  result += rhs;
118  return result;
119  }
120 
121  template <typename Type>
123  {
124  Density<Type> result = lhs;
125  result -= rhs;
126  return result;
127  }
128 
129  template <typename Type>
130  Density<Type> operator/(const Density<Type>& lhs, uint32_t rhs)
131  {
132  Density<Type> result = lhs;
133  result /= rhs;
134  return result;
135  }
136 
137  // These are the predefined density types. The 8-bit types are sufficient for many purposes (including
138  // most games) but 16-bit and float types do have uses particularly in medical/scientific visualisation.
143 
147  template <typename Type>
149  {
150  public:
151  typedef Type DensityType;
152  typedef float MaterialType;
153 
155  {
156  // Default to a threshold value halfway between the min and max possible values.
158  }
159 
161  {
162  m_tThreshold = tThreshold;
163  }
164 
166  {
167  return voxel.getDensity();
168  }
169 
171  {
172  return 1;
173  }
174 
176  {
177  return m_tThreshold;
178  }
179 
180  private:
181  DensityType m_tThreshold;
182  };
183 }
184 
185 #endif //__PolyVox_Density_H__