Work on refactoring basic voxel types.

Also, this will probably cause a compile error on Linux but I'm committing anyway so I remember to investigate it...
This commit is contained in:
David Williams 2012-01-13 21:55:23 +00:00
parent 49b5fb3f67
commit a79633de09
5 changed files with 106 additions and 13 deletions

View File

@ -51,7 +51,7 @@ void createSphereInVolume(LargeVolume<MaterialDensityPair44>& volData, float fRa
//then we make it solid, otherwise we make it empty space. //then we make it solid, otherwise we make it empty space.
if(fDistToCenter <= fRadius) 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<MaterialDensityPair44>::MinDensity));
} }
} }
} }

View File

@ -6,6 +6,7 @@ PROJECT(PolyVoxCore)
SET(CORE_SRC_FILES SET(CORE_SRC_FILES
source/ArraySizes.cpp source/ArraySizes.cpp
source/AStarPathfinder.cpp source/AStarPathfinder.cpp
source/Density.cpp
source/GradientEstimators.cpp source/GradientEstimators.cpp
source/Log.cpp source/Log.cpp
source/MeshDecimator.cpp source/MeshDecimator.cpp

View File

@ -83,23 +83,70 @@ namespace PolyVox
DensityType m_uDensity; DensityType m_uDensity;
}; };
typedef Density<uint8_t> 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<int8_t> DensityI8;
typedef Density<uint8_t> DensityU8;
typedef Density<int16_t> DensityI16;
typedef Density<uint16_t> DensityU16;
typedef Density<float> DensityFloat; typedef Density<float> DensityFloat;
typedef Density<double> 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<uint8_t> Density8;
typedef Density<uint16_t> Density16;
template<typename Type> // We have to define all the min and max values explicitly here rather than using std::numeric_limits because we need
class VoxelTypeTraits< Density<Type> > // compile time constants. The new 'constexpr' would help here but it's not supported by all compilers at the moment.
template<>
class VoxelTypeTraits< Density<int8_t> >
{ {
public: public:
const static Type MinDensity = 0; const static int8_t MinDensity = -127;
const static Type MaxDensity = 255; const static int8_t MaxDensity = 127;
};
template<>
class VoxelTypeTraits< Density<uint8_t> >
{
public:
const static uint8_t MinDensity = 0;
const static uint8_t MaxDensity = 255;
};
template<>
class VoxelTypeTraits< Density<int16_t> >
{
public:
const static int16_t MinDensity = -32767;
const static int16_t MaxDensity = 32767;
};
template<>
class VoxelTypeTraits< Density<uint16_t> >
{
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<> template<>
class VoxelTypeTraits< DensityFloat > class VoxelTypeTraits< Density<float> >
{ {
public: public:
const static float MinDensity = -1000000.0f; const static float MinDensity;
const static float MaxDensity = 1000000.0f; const static float MaxDensity;
};
//Constants for double defined in .cpp file as they are not integers.
template<>
class VoxelTypeTraits< Density<double> >
{
public:
const static double MinDensity;
const static double MaxDensity;
}; };
} }

View File

@ -39,10 +39,10 @@ namespace PolyVox
class Voxel class Voxel
{ {
public: public:
//We expose DensityType and MaterialType in this way so that, when code is // We expose DensityType and MaterialType in this way so that, when code is
//templatised on voxel type, it can determine the underlying storage type // templatised on voxel type, it can determine the underlying storage type
//using code such as 'VoxelType::DensityType value = voxel.getDensity()' // using code such as 'VoxelType::DensityType value = voxel.getDensity()'
//or 'VoxelType::MaterialType value = voxel.getMaterial()'. // or 'VoxelType::MaterialType value = voxel.getMaterial()'.
typedef DenType DensityType; typedef DenType DensityType;
typedef MatType MaterialType; typedef MatType MaterialType;
@ -53,6 +53,16 @@ namespace PolyVox
void setMaterial(MaterialType /*uMaterial*/) { assert(false); } 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 // Syntax for templatised traits classes: http://stackoverflow.com/q/8606302/849083
template <typename Type> template <typename Type>
class VoxelTypeTraits class VoxelTypeTraits

View File

@ -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 <cfloat> //Can't use <limits> as we need compile time constants.
namespace PolyVox
{
const float VoxelTypeTraits< Density<float> >::MinDensity = FLT_MIN;
const float VoxelTypeTraits< Density<float> >::MaxDensity = FLT_MAX;
const double VoxelTypeTraits< Density<double> >::MinDensity = DBL_MIN;
const double VoxelTypeTraits< Density<double> >::MaxDensity = DBL_MAX;
}