Start cleaning Grid class.

This commit is contained in:
jmsgrogan 2023-01-16 07:40:43 +00:00
parent 4fbe6279d1
commit d4bb2b2744
27 changed files with 306 additions and 191 deletions

View file

@ -8,6 +8,11 @@ list(APPEND HEADERS
Event.h Event.h
Color.h Color.h
CommandLineArgs.h CommandLineArgs.h
data_structures/RawTree.h
data_structures/List.h
data_structures/ListT.h
data_structures/ListFactory.h
data_structures/Tree.h
encoding/ByteUtils.h encoding/ByteUtils.h
encoding/StringUtils.h encoding/StringUtils.h
encoding/UnicodeUtils.h encoding/UnicodeUtils.h
@ -30,6 +35,7 @@ list(APPEND LIB_INCLUDES
CommandLineArgs.cpp CommandLineArgs.cpp
data_structures/RawTree.cpp data_structures/RawTree.cpp
data_structures/Tree.cpp data_structures/Tree.cpp
data_structures/ListFactory.cpp
loggers/FileLogger.cpp loggers/FileLogger.cpp
file_utilities/Directory.cpp file_utilities/Directory.cpp
file_utilities/File.cpp file_utilities/File.cpp

View file

@ -0,0 +1,23 @@
#pragma once
#include <cstddef>
class List
{
public:
enum class Type
{
UCHAR,
UINT8,
DOUBLE,
COLOR,
LIST,
UNKNOWN
};
~List() = default;
virtual Type getType() const = 0;
virtual std::size_t getLength() const = 0;
};

View file

@ -0,0 +1,26 @@
#include "ListFactory.h"
#include "ListT.h"
#include "Color.h"
std::unique_ptr<List> ListFactory::Create(List::Type type, std::size_t size)
{
std::unique_ptr<List> list;
if (type == List::Type::DOUBLE)
{
list = std::make_unique<ListT<double>>(type, size, 0.0);
}
else if (type == List::Type::UCHAR)
{
list = std::make_unique<ListT<unsigned char> >(type, size, 0);
}
else if (type == List::Type::UINT8)
{
list = std::make_unique<ListT<uint8_t>>(type, size, 0);
}
else if (type == List::Type::COLOR)
{
list = std::make_unique<ListT<Color>>(type, size, Color());
}
return std::move(list);
}

View file

@ -0,0 +1,11 @@
#pragma once
#include "List.h"
#include <memory>
class ListFactory
{
public:
static std::unique_ptr<List> Create(List::Type type, std::size_t size = 0);
};

View file

@ -0,0 +1,69 @@
#pragma once
#include "List.h"
#include <vector>
class ListFactory;
template<typename T>
class ListT : public List
{
public:
ListT() = delete;
ListT(List::Type type, std::size_t size, T value)
: mType(type)
{
if (size > 0)
{
initialize(size, value);
}
}
virtual Type getType() const override
{
return mType;
}
const T* getDataPtr() const
{
return mData.data();
}
const std::vector<T>& getData() const
{
return mData;
}
void setData(const std::vector<T>& data)
{
mData = data;
}
void setDataItem(std::size_t index, T item)
{
if (index < mData.size())
{
mData[index] = item;
}
}
void initialize(std::size_t size, T value)
{
mData.resize(size);
for (std::size_t idx = 0; idx < size; idx++)
{
mData[idx] = value;
}
}
std::size_t getLength() const override
{
return mData.size();
}
private:
List::Type mType{ List::Type::UNKNOWN };
std::vector<T> mData;
};

View file

@ -3,7 +3,9 @@ set(MODULE_NAME geometry)
list(APPEND HEADERS list(APPEND HEADERS
AbstractGeometricItem.h AbstractGeometricItem.h
Bounds.h Bounds.h
Grid.h grid/AbstractGrid.h
grid/Grid.h
grid/SparseGrid.h
math/Linalg.h math/Linalg.h
math/Matrix.h math/Matrix.h
math/Vector.h math/Vector.h
@ -22,8 +24,10 @@ list(APPEND HEADERS
) )
list(APPEND LIB_INCLUDES list(APPEND LIB_INCLUDES
Grid.cpp
Transform.cpp Transform.cpp
grid/AbstractGrid.cpp
grid/Grid.cpp
grid/SparseGrid.cpp
math/Linalg.cpp math/Linalg.cpp
math/Matrix.cpp math/Matrix.cpp
math/Vector.cpp math/Vector.cpp
@ -49,7 +53,9 @@ target_include_directories(${MODULE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/path ${CMAKE_CURRENT_SOURCE_DIR}/path
${CMAKE_CURRENT_SOURCE_DIR}/points ${CMAKE_CURRENT_SOURCE_DIR}/points
${CMAKE_CURRENT_SOURCE_DIR}/primitives ${CMAKE_CURRENT_SOURCE_DIR}/primitives
${CMAKE_CURRENT_SOURCE_DIR}/grid
) )
target_link_libraries( ${MODULE_NAME} PUBLIC core)
set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src) set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src)

View file

@ -1,43 +0,0 @@
#include "Grid.h"
Grid::Grid(const Bounds& bounds)
: mBounds(bounds)
{
mValues = std::vector<double>(mNumX*mNumY, 0.0);
}
const Bounds& Grid::getBounds() const
{
return mBounds;
}
double Grid::getXSpacing() const
{
const auto width = mBounds.mMaxX - mBounds.mMinX;
return width/double(mNumX);
}
double Grid::getYSpacing() const
{
const auto height = mBounds.mMaxY - mBounds.mMinY;
return height/double(mNumY);
}
const std::vector<double>& Grid::getValues() const
{
return mValues;
}
void Grid::resetBounds(const Bounds& bounds)
{
mBounds = bounds;
mValues = std::vector<double>(mNumX*mNumY, 0.0);
}
void Grid::setValues(const std::vector<std::size_t>& indices, double value)
{
for (auto index : indices)
{
mValues[index] = value;
}
}

View file

@ -1,29 +0,0 @@
#pragma once
#include "Bounds.h"
#include <vector>
class Grid
{
public:
Grid(const Bounds& bounds);
const Bounds& getBounds() const;
double getXSpacing() const;
double getYSpacing() const;
const std::vector<double>& getValues() const;
void resetBounds(const Bounds& bounds);
void setValues(const std::vector<std::size_t>& indices, double value);
private:
Bounds mBounds;
std::vector<double> mValues;
unsigned mNumX{5};
unsigned mNumY{5};
};

View file

@ -0,0 +1,39 @@
#include "AbstractGrid.h"
#include "ListT.h"
AbstractGrid::AbstractGrid(const Bounds& bounds, std::size_t numPointsX, std::size_t numPointsY, List::Type dataType)
: mBounds(bounds),
mNumX(numPointsX),
mNumY(numPointsY)
{
}
const Bounds& AbstractGrid::getBounds() const
{
return mBounds;
}
double AbstractGrid::getXSpacing() const
{
const auto width = mBounds.mMaxX - mBounds.mMinX;
return width / double(mNumX);
}
double AbstractGrid::getYSpacing() const
{
const auto height = mBounds.mMaxY - mBounds.mMinY;
return height / double(mNumY);
}
void AbstractGrid::resetBounds(const Bounds& bounds)
{
mBounds = bounds;
initializeData(mData->getType());
}
List::Type AbstractGrid::getDataType()
{
return mData->getType();
}

View file

@ -0,0 +1,54 @@
#pragma once
#include "Bounds.h"
#include "List.h"
#include "ListT.h"
#include <vector>
#include <memory>
#include <stdexcept>
class AbstractGrid
{
public:
AbstractGrid(const Bounds& bounds, std::size_t numPointsX, std::size_t numPointsY, List::Type dataType = List::Type::DOUBLE);
const Bounds& getBounds() const;
double getXSpacing() const;
double getYSpacing() const;
List::Type getDataType();
template<typename T>
const std::vector<T> getData() const
{
if (!dynamic_cast<ListT<T>*>(mData.get()))
{
throw std::logic_error("Invalid data type request for Grid data.");
}
return dynamic_cast<ListT<T>*>(mData.get())->getData();
}
template<typename T>
void setData(const std::vector<std::size_t> indices, const std::vector<T>& values)
{
if (auto list_t = dynamic_cast<ListT<T>*>(mData.get()))
{
list_t->setData(indices, values);
}
}
virtual void initializeData(List::Type dataType) = 0;
void resetBounds(const Bounds& bounds);
protected:
Bounds mBounds;
std::unique_ptr<List> mData;
std::size_t mNumX{ 0 };
std::size_t mNumY{ 0 };
};

View file

@ -0,0 +1,14 @@
#include "Grid.h"
#include "ListFactory.h"
Grid::Grid(const Bounds& bounds, std::size_t numPointsX, std::size_t numPointsY, List::Type dataType)
: AbstractGrid(bounds, numPointsX, numPointsY, dataType)
{
initializeData(dataType);
}
void Grid::initializeData(List::Type dataType)
{
mData = ListFactory::Create(dataType, mNumX * mNumY);
}

12
src/geometry/grid/Grid.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
#include "AbstractGrid.h"
class Grid : public AbstractGrid
{
public:
Grid(const Bounds& bounds, std::size_t numPointsX, std::size_t numPointsY, List::Type dataType = List::Type::DOUBLE);
private:
void initializeData(List::Type dataType) override;
};

View file

View file

@ -0,0 +1,2 @@
#pragma once

View file

@ -4,7 +4,6 @@ set(platform_LIB_INCLUDES)
list(APPEND image_HEADERS list(APPEND image_HEADERS
Image.h Image.h
ImageData.h
PlatformImage.h PlatformImage.h
PlatformImageWriter.h PlatformImageWriter.h
png/PngWriter.h png/PngWriter.h

View file

@ -1,110 +0,0 @@
#pragma once
#include <vector>
#include <type_traits>
class ImageData
{
public:
enum class Type
{
UCHAR,
UINT8,
UNKNOWN
};
virtual Type getType() const = 0;
virtual void setDataItem(std::size_t index, unsigned char item) = 0;
virtual unsigned char getAsUnsignedChar(std::size_t index) const = 0;
virtual std::size_t getLength() const = 0;
};
template<typename T>
class ImageDataT : public ImageData
{
public:
virtual Type getType() const override
{
if (std::is_same<T, unsigned char>::value)
{
return Type::UCHAR;
}
else if (std::is_same<T, uint8_t>::value)
{
return Type::UINT8;
}
else
{
return Type::UNKNOWN;
}
}
template<typename T>
const T* getDataPtr() const
{
return mData.data();
}
template<typename T>
const std::vector<T>& getData() const
{
return mData;
}
template<typename T>
void setData(const std::vector<T>& data)
{
mData = data;
}
void setDataItem(std::size_t index, unsigned char item) override
{
if (index < mData.size())
{
mData[index] = static_cast<T>(item);
}
}
template<typename T>
void setDataItem(std::size_t index, T item)
{
if (index < mData.size())
{
mData[index] = item;
}
}
template<typename T>
void initialize(std::size_t size, T value)
{
mData.resize(size);
for (std::size_t idx = 0; idx < size; idx++)
{
mData[idx] = value;
}
}
unsigned char getAsUnsignedChar(std::size_t index) const override
{
if (index < mData.size())
{
return static_cast<unsigned char>(mData[index]);
}
else
{
return 0;
}
}
std::size_t getLength() const override
{
return mData.size();
}
private:
std::vector<T> mData;
};

View file

@ -11,6 +11,8 @@ list(APPEND publishing_HEADERS
pdf/PdfWriter.h pdf/PdfWriter.h
plotting/GraphPlotter.h plotting/GraphPlotter.h
plotting/SvgConverter.h plotting/SvgConverter.h
plotting/PlotNode.h
plotting/EquationNode.h
latex/LatexDocument.h latex/LatexDocument.h
latex/LatexMathExpression.h latex/LatexMathExpression.h
latex/LatexSymbols.h latex/LatexSymbols.h
@ -33,6 +35,8 @@ list(APPEND publishing_LIB_INCLUDES
latex/LatexSymbols.cpp latex/LatexSymbols.cpp
plotting/GraphPlotter.h plotting/GraphPlotter.h
plotting/SvgConverter.cpp plotting/SvgConverter.cpp
plotting/PlotNode.cpp
plotting/EquationNode.cpp
DocumentConverter.cpp DocumentConverter.cpp
) )

View file

View file

View file

View file

View file

@ -8,7 +8,7 @@ GridNode::GridNode(const Point& location)
} }
void GridNode::setNumX(unsigned numX) void GridNode::setNumX(std::size_t numX)
{ {
if (mNumberX != numX) if (mNumberX != numX)
{ {
@ -17,7 +17,7 @@ void GridNode::setNumX(unsigned numX)
} }
} }
void GridNode::setNumY(unsigned numY) void GridNode::setNumY(std::size_t numY)
{ {
if (mNumberY != numY) if (mNumberY != numY)
{ {

View file

@ -9,9 +9,9 @@ class GridNode : public MaterialNode
public: public:
GridNode(const Point& location); GridNode(const Point& location);
void setNumX(unsigned numX); void setNumX(std::size_t numX);
void setNumY(unsigned numY); void setNumY(std::size_t numY);
void setData(const std::vector<Color>& colors); void setData(const std::vector<Color>& colors);
@ -24,8 +24,8 @@ public:
void setHeight(double height); void setHeight(double height);
private: private:
unsigned mNumberX{5}; std::size_t mNumberX{5};
unsigned mNumberY{5}; std::size_t mNumberY{5};
double mWidth{1}; double mWidth{1};
double mHeight{1}; double mHeight{1};
bool mDataDirty = true; bool mDataDirty = true;

View file

@ -9,6 +9,7 @@ set(TEST_MODULES
core core
database database
fonts fonts
geometry
graphics graphics
image image
ipc ipc

View file

@ -0,0 +1,11 @@
set(GEOMETRY_UNIT_TEST_FILES
geometry/TestGeometryCollection.cpp
geometry/TestGrid.cpp
PARENT_SCOPE
)
set(GEOMETRY_UNIT_TEST_DEPENDENCIES
geometry
PARENT_SCOPE
)

View file

@ -0,0 +1,10 @@
#include "TestFramework.h"
#include "TestUtils.h"
TEST_CASE(TestGeometryCollection, "geometry")
{
};

View file

@ -0,0 +1,10 @@
#include "TestFramework.h"
#include "TestUtils.h"
TEST_CASE(TestGrid, "geometry")
{
};