PolyVox  0.2.0
Open source voxel management library
MaterialDensityPair.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_MaterialDensityPair_H__
25 #define __PolyVox_MaterialDensityPair_H__
26 
27 #include "PolyVoxCore/DefaultIsQuadNeeded.h" //we'll specialise this function for this voxel type
28 #include "PolyVoxCore/DefaultMarchingCubesController.h" //We'll specialise the controller contained in here
29 
30 #include "Impl/TypeDef.h"
31 
32 namespace PolyVox
33 {
40  template <typename Type, uint8_t NoOfMaterialBits, uint8_t NoOfDensityBits>
42  {
43  public:
44  MaterialDensityPair() : m_uMaterial(0), m_uDensity(0) {}
45  MaterialDensityPair(Type uMaterial, Type uDensity) : m_uMaterial(uMaterial), m_uDensity(uDensity) {}
46 
47  bool operator==(const MaterialDensityPair& rhs) const
48  {
49  return (m_uMaterial == rhs.m_uMaterial) && (m_uDensity == rhs.m_uDensity);
50  };
51 
52  bool operator!=(const MaterialDensityPair& rhs) const
53  {
54  return !(*this == rhs);
55  }
56 
58  {
59  m_uDensity += rhs.m_uDensity;
60 
61  // What should we do with the material? Conceptually the idea of adding materials makes no sense, but for our
62  // purposes we consider the 'sum' of two materials to just be the max. At least this way it is commutative.
63  m_uMaterial = (std::max)(m_uMaterial, rhs.m_uMaterial);
64 
65  return *this;
66  }
67 
69  {
70  // There's nothing sensible we can do with the material, so this function only affects the density.
71  m_uDensity /= rhs;
72  return *this;
73  }
74 
75  Type getDensity() const { return m_uDensity; }
76  Type getMaterial() const { return m_uMaterial; }
77 
78  void setDensity(Type uDensity) { m_uDensity = uDensity; }
79  void setMaterial(Type uMaterial) { m_uMaterial = uMaterial; }
80 
81  static Type getMaxDensity() { return (0x01 << NoOfDensityBits) - 1; }
82  static Type getMinDensity() { return 0; }
83 
84  private:
85  Type m_uMaterial : NoOfMaterialBits;
86  Type m_uDensity : NoOfDensityBits;
87  };
88 
89  template<typename Type, uint8_t NoOfMaterialBits, uint8_t NoOfDensityBits>
90  class DefaultIsQuadNeeded< MaterialDensityPair<Type, NoOfMaterialBits, NoOfDensityBits> >
91  {
92  public:
94  {
95  if((back.getMaterial() > 0) && (front.getMaterial() == 0))
96  {
97  materialToUse = static_cast<uint32_t>(back.getMaterial());
98  return true;
99  }
100  else
101  {
102  return false;
103  }
104  }
105  };
106 
107  template <typename Type, uint8_t NoOfMaterialBits, uint8_t NoOfDensityBits>
108  class DefaultMarchingCubesController< MaterialDensityPair<Type, NoOfMaterialBits, NoOfDensityBits> >
109  {
110  public:
111  typedef Type DensityType;
112  typedef Type MaterialType;
113 
115  {
116  // Default to a threshold value halfway between the min and max possible values.
118  }
119 
121  {
122  m_tThreshold = tThreshold;
123  }
124 
126  {
127  return voxel.getDensity();
128  }
129 
131  {
132  return voxel.getMaterial();
133  }
134 
136  {
137  return m_tThreshold;
138  }
139 
140  private:
141  DensityType m_tThreshold;
142  };
143 
146 }
147 
148 #endif