Commit 5a7ec731 authored by Nghia Truong's avatar Nghia Truong
Browse files

COMP: Fix compilation issue when compiling with MSVC 2013

parent ae197797
......@@ -38,8 +38,23 @@ template<class CellData>
class UniformSpatialGrid
{
public:
UniformSpatialGrid() = default;
UniformSpatialGrid(const Vec3r& lowerCorner, const Vec3r& upperCorner, Real cellSize) { initialize(lowerCorner, upperCorner, cellSize); }
///
/// \brief Construct a default grid ([0, 1]^3) with cell size of 1
///
UniformSpatialGrid() : UniformSpatialGrid(Vec3r(0, 0, 0), Vec3r(1, 1, 1), Real(1.0))
{}
///
/// \brief Construct a grid with given corners and cell size
/// \param lowerCorner The lower corner of the grid
/// \param upperCorner The upper corner of the grid
/// \param cellSize The length of grid cell
///
UniformSpatialGrid(const Vec3r& lowerCorner, const Vec3r& upperCorner, Real cellSize)
{
initialize(lowerCorner, upperCorner, cellSize);
}
///
/// \brief Initialize the grid data
......@@ -69,7 +84,7 @@ public:
std::to_string(m_UpperCorner[0]) + ", " + std::to_string(m_UpperCorner[1]) + ", " + std::to_string(m_UpperCorner[2]) + "], " +
"cellSize = " + std::to_string(m_CellSize);
// cell data must be resized to equal to number of cells
// cell data must be resized to match with the number of cells
m_CellData.resize(m_NTotalCells);
}
......@@ -98,9 +113,7 @@ public:
/// \brief Get the 3D index (cell_x, cell_y, cell_z) of the cell containing the given positions
///
template<class IndexType>
std::array<IndexType, 3> getCellIndexFromCoordinate(const Vec3r& ppos) const
// Implementation of templated function of templated class should not be put in .cpp file
// Otherwise, the function must be explicitly instantiated with various IndexType
std::array<IndexType, 3> getCell3DIndices(const Vec3r& ppos) const
{
std::array<IndexType, 3> cellIdx;
for(int d = 0; d < 3; ++d)
......@@ -111,30 +124,68 @@ public:
}
///
/// \brief Get data in cell
/// \brief Get all cell data
///
std::vector<CellData>& getAllCellData() { return m_CellData; }
///
/// \brief Get all cell data
///
const std::vector<CellData>& getAllCellData() const { return m_CellData; }
///
/// \brief Get data in a cell
/// \param A position in space
///
CellData& getCellData(const Vec3r& ppos) { return m_CellData[getCellFlatIndexFromCoordinate < unsigned int > (ppos)]; }
CellData& getCellData(const Vec3r& ppos) { return m_CellData[getCellLinearizedIndex<unsigned int>(ppos)]; }
///
/// \brief Get data in cell
/// \brief Get data in a cell
/// \param A position in space
///
const CellData& getCellData(const Vec3r& ppos) const { return m_CellData[getCellFlatIndexFromCoordinate < unsigned int > (ppos)]; }
const CellData& getCellData(const Vec3r& ppos) const { return m_CellData[getCellLinearizedIndex<unsigned int>(ppos)]; }
///
/// \brief Get data in a cell
/// \param A linearized index of cell
///
template<class IndexType>
CellData& getCellData(IndexType linearizedIdx) { assert(linearizedIdx < m_CellData.size()); return m_CellData[linearizedIdx]; }
///
/// \brief Get data in cell
/// \brief Get data in a cell
/// \param A linearized index of cell
///
template<class IndexType>
const CellData& getCellData(IndexType linearizedIdx) const { assert(linearizedIdx < m_CellData.size()); return m_CellData[linearizedIdx]; }
///
/// \brief Get data in a cell
/// \param 3D index of a cell
///
template<class IndexType>
CellData& getCellData(const std::array<IndexType, 3>& cellIdx) { return m_CellData[getCellLinearizedIndex(cellIdx[0], cellIdx[1], cellIdx[2])]; }
///
/// \brief Get data in a cell
/// \param 3D index of a cell
///
template<class IndexType>
CellData& getCellData(const IndexType i, const IndexType j, const IndexType k) { return m_CellData[getCellFlatIndexFrom3DIndices(i, j, k)]; }
const CellData& getCellData(const std::array<IndexType, 3>& cellIdx) const { return m_CellData[getCellLinearizedIndex(cellIdx[0], cellIdx[1], cellIdx[2])]; }
///
/// \brief Get data in cell
/// \brief Get data in a cell
/// \param 3D index of a cell
///
template<class IndexType>
const CellData& getCellData(const IndexType i, const IndexType j, const IndexType k) const { return m_CellData[getCellFlatIndexFrom3DIndices(i, j, k)]; }
CellData& getCellData(const IndexType i, const IndexType j, const IndexType k) { return m_CellData[getCellLinearizedIndex(i, j, k)]; }
///
/// \brief Get data in a cell
/// \param 3D index of a cell
///
template<class IndexType>
const CellData& getCellData(const IndexType i, const IndexType j, const IndexType k) const { return m_CellData[getCellLinearizedIndex(i, j, k)]; }
///
/// \brief Apply a function to all cell data
......@@ -148,48 +199,45 @@ public:
}
}
private:
///
/// \brief Get linearized index from cell 3D indices: index in 3D (cell_x, cell_y, cell_z) => index in 1D
/// \param 3D index of a cell
///
template<class IndexType>
IndexType getCellFlatIndexFrom3DIndices(const IndexType i, const IndexType j, const IndexType k) const
// Implementation of templated function of templated class should not be put in .cpp file
// Otherwise, the function must be explicitly instantiated with various IndexType
IndexType getCellLinearizedIndex(const IndexType i, const IndexType j, const IndexType k) const
{
auto flatIndex = (k * static_cast<IndexType>(m_Resolution[1]) + j) * static_cast<IndexType>(m_Resolution[0]) + i;
assert(flatIndex < static_cast<IndexType>(m_NTotalCells));
return flatIndex;
}
///
/// \brief Get linearized index of cell containing the positions ppos
/// \brief Get linearized index of cell containing the given position
/// \param A position in space
///
template<class IndexType>
IndexType getCellFlatIndexFromCoordinate(const Vec3r& ppos) const
// Implementation of templated function of templated class should not be put in .cpp file
// Otherwise, the function must be explicitly instantiated with various IndexType
IndexType getCellLinearizedIndex(const Vec3r& ppos) const
{
auto cellIdx = getCellIndexFromCoordinate<IndexType>(ppos);
auto cellIdx = getCell3DIndices<IndexType>(ppos);
#if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG)
LOG_IF(FATAL, !isValidCellIndices(cellIdx[0], cellIdx[1], cellIdx[2])) <<
LOG_IF(FATAL, (!isValidCellIndices(cellIdx[0], cellIdx[1], cellIdx[2]))) <<
"Invalid cell indices: " +
std::to_string(cellIdx[0]) + "/" + std::to_string(m_Resolution[0]) + ", " +
std::to_string(cellIdx[1]) + "/" + std::to_string(m_Resolution[1]) + ", " +
std::to_string(cellIdx[2]) + "/" + std::to_string(m_Resolution[2]);
#endif
return getCellFlatIndexFrom3DIndices<IndexType>(cellIdx[0], cellIdx[1], cellIdx[2]);
return getCellLinearizedIndex<IndexType>(cellIdx[0], cellIdx[1], cellIdx[2]);
}
Vec3r m_LowerCorner {0.0, 0.0, 0.0};
Vec3r m_UpperCorner {1.0, 1.0, 1.0};
Real m_CellSize {1.0};
Real m_InvCellSize {1.0};
private:
Vec3r m_LowerCorner; ///> Lower corner of the grid
Vec3r m_UpperCorner; ///> Upper corner of the grid
Real m_CellSize; ///> Length of grid cell
Real m_InvCellSize; ///> Inverse length of grid cell
std::array<unsigned int, 3> m_Resolution {{ 0u, 0u, 0u }};
unsigned int m_NTotalCells { 1u };
unsigned int m_NTotalCells; ///> Number of total cells
std::array<unsigned int, 3> m_Resolution; ///> Grid resolution (number of cells in x/y/z dimensions)
std::vector<CellData> m_CellData;
std::vector<CellData> m_CellData; ///> Data stored for each cell
};
} // end namespace imstk
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment