diff --git a/examples/OpenGL/Shapes.cpp b/examples/OpenGL/Shapes.cpp index 2f2b84f5..65bb361f 100644 --- a/examples/OpenGL/Shapes.cpp +++ b/examples/OpenGL/Shapes.cpp @@ -51,7 +51,7 @@ void createSphereInVolume(LargeVolume& volData, float fRa //then we make it solid, otherwise we make it empty space. if(fDistToCenter <= fRadius) { - volData.setVoxelAt(x,y,z, MaterialDensityPair44(uValue, uValue > 0 ? maxDen : minDen)); + volData.setVoxelAt(x,y,z, MaterialDensityPair44(uValue, uValue > 0 ? maxDen : VoxelTypeTraits::MinDensity)); } } } diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index 0b6c3ff1..dc787a4d 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -6,6 +6,7 @@ PROJECT(PolyVoxCore) SET(CORE_SRC_FILES source/ArraySizes.cpp source/AStarPathfinder.cpp + source/Density.cpp source/GradientEstimators.cpp source/Log.cpp source/MeshDecimator.cpp diff --git a/library/PolyVoxCore/include/PolyVoxCore/Density.h b/library/PolyVoxCore/include/PolyVoxCore/Density.h index 3b749dba..ae570351 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Density.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Density.h @@ -83,23 +83,70 @@ namespace PolyVox DensityType m_uDensity; }; - typedef Density Density8; + // 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 DensityI8; + typedef Density DensityU8; + typedef Density DensityI16; + typedef Density DensityU16; typedef Density DensityFloat; + typedef Density DensityDouble; + + // These types are here for backwards compatibility but they are a little ambiguous as the name doesn't indicate + // whether the values are signed. We would recommend using one of the 8 or 16 bit predefined types above instead. + typedef Density Density8; + typedef Density Density16; - template - class VoxelTypeTraits< Density > + // We have to define all the min and max values explicitly here rather than using std::numeric_limits because we need + // compile time constants. The new 'constexpr' would help here but it's not supported by all compilers at the moment. + template<> + class VoxelTypeTraits< Density > { public: - const static Type MinDensity = 0; - const static Type MaxDensity = 255; + const static int8_t MinDensity = -127; + const static int8_t MaxDensity = 127; + }; + + template<> + class VoxelTypeTraits< Density > + { + public: + const static uint8_t MinDensity = 0; + const static uint8_t MaxDensity = 255; + }; + + template<> + class VoxelTypeTraits< Density > + { + public: + const static int16_t MinDensity = -32767; + const static int16_t MaxDensity = 32767; + }; + + template<> + class VoxelTypeTraits< Density > + { + public: + const static uint16_t MinDensity = 0; + const static uint16_t MaxDensity = 65535; }; + //Constants for float defined in .cpp file as they are not integers. template<> - class VoxelTypeTraits< DensityFloat > + class VoxelTypeTraits< Density > { public: - const static float MinDensity = -1000000.0f; - const static float MaxDensity = 1000000.0f; + const static float MinDensity; + const static float MaxDensity; + }; + + //Constants for double defined in .cpp file as they are not integers. + template<> + class VoxelTypeTraits< Density > + { + public: + const static double MinDensity; + const static double MaxDensity; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Voxel.h b/library/PolyVoxCore/include/PolyVoxCore/Voxel.h index 22e0e05d..e3ddb32b 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Voxel.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Voxel.h @@ -39,10 +39,10 @@ namespace PolyVox class Voxel { public: - //We expose DensityType and MaterialType in this way so that, when code is - //templatised on voxel type, it can determine the underlying storage type - //using code such as 'VoxelType::DensityType value = voxel.getDensity()' - //or 'VoxelType::MaterialType value = voxel.getMaterial()'. + // We expose DensityType and MaterialType in this way so that, when code is + // templatised on voxel type, it can determine the underlying storage type + // using code such as 'VoxelType::DensityType value = voxel.getDensity()' + // or 'VoxelType::MaterialType value = voxel.getMaterial()'. typedef DenType DensityType; typedef MatType MaterialType; @@ -53,6 +53,16 @@ namespace PolyVox void setMaterial(MaterialType /*uMaterial*/) { assert(false); } }; + // Various properties of the voxel types can be expressed via types traits, similar to the way std::numeric_limits is implemented. + // This has some advantages compared to storing the properties directly in the voxel class. For example, by using traits it is possible + // to also apply these properties to primative types which might be desirable (the Volume classes do accept primative types as template + // parameters). Also, properties such as MinDensity and MaxDensity would be difficult to represent though class members because they + // depend ont the type (float has a very different range from int8_t for example). + // + // The properties are currently exposed as constants because we need access to them at compile time. Ideally we would like to make them + // functions flagged with 'constexpr' as we could then make use of the max() and min() functions in std::numric_limits, but this is not + // widely supported by compilers yet. We may change this in the future. + // // Syntax for templatised traits classes: http://stackoverflow.com/q/8606302/849083 template class VoxelTypeTraits diff --git a/library/PolyVoxCore/source/Density.cpp b/library/PolyVoxCore/source/Density.cpp new file mode 100644 index 00000000..d12c132e --- /dev/null +++ b/library/PolyVoxCore/source/Density.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* +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. +*******************************************************************************/ + +#include "PolyVoxCore/Density.h" + +#include //Can't use as we need compile time constants. + +namespace PolyVox +{ + const float VoxelTypeTraits< Density >::MinDensity = FLT_MIN; + const float VoxelTypeTraits< Density >::MaxDensity = FLT_MAX; + + const double VoxelTypeTraits< Density >::MinDensity = DBL_MIN; + const double VoxelTypeTraits< Density >::MaxDensity = DBL_MAX; +} \ No newline at end of file