/******************************************************************************* Copyright (c) 2005-2009 David Williams This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. *******************************************************************************/ #ifndef __PolyVox_Density_H__ #define __PolyVox_Density_H__ #include "PolyVoxCore/DefaultMarchingCubesController.h" //We'll specialise the controller contained in here #include "Impl/TypeDef.h" #include #undef min #undef max namespace PolyVox { /// This class represents a voxel storing only a density. //////////////////////////////////////////////////////////////////////////////// /// Detailed description... //////////////////////////////////////////////////////////////////////////////// template class Density { public: /// Constructor Density() : m_uDensity(0) {} /// Copy constructor Density(Type uDensity) : m_uDensity(uDensity) {} // The LowPassFilter uses this to convert between normal and accumulated types. /// Copy constructor with cast template explicit Density(const Density& density) { m_uDensity = static_cast(density.getDensity()); } bool operator==(const Density& rhs) const { return (m_uDensity == rhs.m_uDensity); }; bool operator!=(const Density& rhs) const { return !(*this == rhs); } // For densities we can supply mathematical operators which behave in an intuitive way. // In particular the ability to add and subtract densities is important in order to // apply an averaging filter. The ability to divide by an integer is also needed for // this same purpose. Density& operator+=(const Density& rhs) { m_uDensity += rhs.m_uDensity; return *this; } Density& operator-=(const Density& rhs) { m_uDensity -= rhs.m_uDensity; return *this; } Density& operator/=(uint32_t rhs) { m_uDensity /= rhs; return *this; } /// \return The current density of the voxel Type getDensity() const { return m_uDensity; } /** * Set the density of the voxel * * \param uDensity The density to set to */ void setDensity(Type uDensity) { m_uDensity = uDensity; } /// \return The maximum allowed density of the voxel static Type getMaxDensity() { return (std::numeric_limits::max)(); } /// \return The minimum allowed density of the voxel static Type getMinDensity() { return (std::numeric_limits::min)(); } private: Type m_uDensity; }; template Density operator+(const Density& lhs, const Density& rhs) { Density result = lhs; result += rhs; return result; } template Density operator-(const Density& lhs, const Density& rhs) { Density result = lhs; result -= rhs; return result; } template Density operator/(const Density& lhs, uint32_t rhs) { Density result = lhs; result /= rhs; return result; } // These are the predefined density types. The 8-bit types are sufficient for many purposes (including // most games) but 16-bit and float types do have uses particularly in medical/scientific visualisation. typedef Density Density8; typedef Density Density16; typedef Density Density32; typedef Density DensityFloat; /** * This is a specialisation of DefaultMarchingCubesController for the Density voxel type */ template class DefaultMarchingCubesController< Density > { public: typedef Type DensityType; typedef float MaterialType; DefaultMarchingCubesController(void) { // Default to a threshold value halfway between the min and max possible values. m_tThreshold = (Density::getMinDensity() + Density::getMaxDensity()) / 2; } DefaultMarchingCubesController(DensityType tThreshold) { m_tThreshold = tThreshold; } DensityType convertToDensity(Density voxel) { return voxel.getDensity(); } MaterialType convertToMaterial(Density /*voxel*/) { return 1; } MaterialType blendMaterials(Density /*a*/, Density /*b*/, float /*weight*/) { return 1; } DensityType getThreshold(void) { return m_tThreshold; } void setThreshold(DensityType tThreshold) { m_tThreshold = tThreshold; } private: DensityType m_tThreshold; }; } #endif //__PolyVox_Density_H__