Geometrize  1.0
C++ library for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
geometrize::Model::ModelImpl Class Reference
Collaboration diagram for geometrize::Model::ModelImpl:
Collaboration graph
[legend]

Public Member Functions

 ModelImpl (const geometrize::Bitmap &target)
 
 ModelImpl (const geometrize::Bitmap &target, const geometrize::Bitmap &initial)
 
 ~ModelImpl ()=default
 
ModelImploperator= (const ModelImpl &)=delete
 
 ModelImpl (const ModelImpl &)=delete
 
void reset (const geometrize::rgba backgroundColor)
 
std::int32_t getWidth () const
 
std::int32_t getHeight () const
 
std::vector< geometrize::StategetHillClimbState (const std::function< std::shared_ptr< geometrize::Shape >(void)> shapeCreator, const std::uint8_t alpha, const std::uint32_t shapeCount, const std::uint32_t maxShapeMutations, std::uint32_t maxThreads, const geometrize::core::EnergyFunction energyFunction)
 
std::vector
< geometrize::ShapeResult
step (const std::function< std::shared_ptr< geometrize::Shape >(void)> shapeCreator, const std::uint8_t alpha, const std::uint32_t shapeCount, const std::uint32_t maxShapeMutations, const std::uint32_t maxThreads, const geometrize::core::EnergyFunction &energyFunction)
 
geometrize::ShapeResult drawShape (const std::shared_ptr< geometrize::Shape > shape, const geometrize::rgba color)
 
geometrize::BitmapgetTarget ()
 
geometrize::BitmapgetCurrent ()
 
const geometrize::BitmapgetTarget () const
 
const geometrize::BitmapgetCurrent () const
 
void setSeed (const std::uint32_t seed)
 

Private Attributes

geometrize::Bitmap m_target
 The target bitmap, the bitmap we aim to approximate. More...
 
geometrize::Bitmap m_current
 The current bitmap. More...
 
double m_lastScore
 Score derived from calculating the difference between bitmaps. More...
 
std::atomic< std::uint32_t > m_baseRandomSeed
 The base value used for seeding the random number generator (the one the user has control over). More...
 
std::atomic< std::uint32_t > m_randomSeedOffset
 Seed used for random number generation. Note: incremented by each std::async call used for model stepping. More...
 

Static Private Attributes

static const std::uint32_t defaultMaxThreads {4}
 

Constructor & Destructor Documentation

geometrize::Model::ModelImpl::ModelImpl ( const geometrize::Bitmap target)
inline
28  :
29  m_target{target},
32  m_baseRandomSeed{0U},
34  {}
std::uint32_t getWidth() const
getWidth Gets the width of the bitmap.
Definition: bitmap.cpp:20
double differenceFull(const geometrize::Bitmap &first, const geometrize::Bitmap &second)
differenceFull Calculates the root-mean-square error between two bitmaps.
Definition: core.cpp:174
geometrize::rgba getAverageImageColor(const geometrize::Bitmap &image)
getAverageImageColor Computes the average RGB color of the pixels in the bitmap.
Definition: commonutil.cpp:30
std::atomic< std::uint32_t > m_baseRandomSeed
The base value used for seeding the random number generator (the one the user has control over)...
Definition: model.cpp:197
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
std::atomic< std::uint32_t > m_randomSeedOffset
Seed used for random number generation. Note: incremented by each std::async call used for model step...
Definition: model.cpp:198
std::uint32_t getHeight() const
getHeight Gets the height of the bitmap.
Definition: bitmap.cpp:25
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
geometrize::Model::ModelImpl::ModelImpl ( const geometrize::Bitmap target,
const geometrize::Bitmap initial 
)
inline
36  :
37  m_target{target},
38  m_current{initial},
40  m_baseRandomSeed{0U},
42  {
43  assert(m_target.getWidth() == m_current.getWidth());
44  assert(m_target.getHeight() == m_current.getHeight());
45  }
std::uint32_t getWidth() const
getWidth Gets the width of the bitmap.
Definition: bitmap.cpp:20
double differenceFull(const geometrize::Bitmap &first, const geometrize::Bitmap &second)
differenceFull Calculates the root-mean-square error between two bitmaps.
Definition: core.cpp:174
std::atomic< std::uint32_t > m_baseRandomSeed
The base value used for seeding the random number generator (the one the user has control over)...
Definition: model.cpp:197
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
std::atomic< std::uint32_t > m_randomSeedOffset
Seed used for random number generation. Note: incremented by each std::async call used for model step...
Definition: model.cpp:198
std::uint32_t getHeight() const
getHeight Gets the height of the bitmap.
Definition: bitmap.cpp:25
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
geometrize::Model::ModelImpl::~ModelImpl ( )
default
geometrize::Model::ModelImpl::ModelImpl ( const ModelImpl )
delete

Member Function Documentation

geometrize::ShapeResult geometrize::Model::ModelImpl::drawShape ( const std::shared_ptr< geometrize::Shape shape,
const geometrize::rgba  color 
)
inline
156  {
157  const std::vector<geometrize::Scanline> lines{shape->rasterize(*shape)};
158  const geometrize::Bitmap before{m_current};
159  geometrize::drawLines(m_current, color, lines);
160 
162 
163  const geometrize::ShapeResult result{m_lastScore, color, shape};
164  return result;
165  }
void drawLines(geometrize::Bitmap &image, const geometrize::rgba color, const std::vector< geometrize::Scanline > &lines)
drawLines Draws scanlines onto an image.
Definition: rasterizer.cpp:74
The ShapeResult struct is a container for info about a shape added to the model.
Definition: shaperesult.h:19
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
The Bitmap class is a helper class for working with bitmap data.
Definition: bitmap.h:15
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
double differencePartial(const geometrize::Bitmap &target, const geometrize::Bitmap &before, const geometrize::Bitmap &after, const double score, const std::vector< Scanline > &lines)
differencePartial Calculates the root-mean-square error between the parts of the two bitmaps within t...
Definition: core.cpp:198

Here is the call graph for this function:

geometrize::Bitmap& geometrize::Model::ModelImpl::getCurrent ( )
inline
173  {
174  return m_current;
175  }
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
const geometrize::Bitmap& geometrize::Model::ModelImpl::getCurrent ( ) const
inline
183  {
184  return m_current;
185  }
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
std::int32_t geometrize::Model::ModelImpl::getHeight ( ) const
inline
63  {
64  return m_target.getHeight();
65  }
std::uint32_t getHeight() const
getHeight Gets the height of the bitmap.
Definition: bitmap.cpp:25
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193

Here is the call graph for this function:

std::vector<geometrize::State> geometrize::Model::ModelImpl::getHillClimbState ( const std::function< std::shared_ptr< geometrize::Shape >(void)>  shapeCreator,
const std::uint8_t  alpha,
const std::uint32_t  shapeCount,
const std::uint32_t  maxShapeMutations,
std::uint32_t  maxThreads,
const geometrize::core::EnergyFunction  energyFunction 
)
inline
74  {
75  // Ensure that the maximum number of threads is a sane value
76  if(maxThreads == 0) {
77  maxThreads = std::thread::hardware_concurrency();
78  if(maxThreads == 0) {
79  assert(0 && "Failed to get the number of concurrent threads supported by the implementation");
80  maxThreads = defaultMaxThreads;
81  }
82  }
83 
84  std::vector<std::future<geometrize::State>> futures{maxThreads};
85  for(std::uint32_t i = 0; i < futures.size(); i++) {
86  std::future<geometrize::State> handle{std::async(std::launch::async, [&](const std::uint32_t seed, const double lastScore) {
87  // Ensure that the results of the random generation are the same between tasks with identical settings
88  // The RNG is thread-local and std::async may use a thread pool (which is why this is necessary)
89  // Note this implementation requires maxThreads to be the same between tasks for each task to produce the same results.
91 
93  return core::bestHillClimbState(shapeCreator, alpha, shapeCount, maxShapeMutations, m_target, m_current, buffer, lastScore, energyFunction);
95  futures[i] = std::move(handle);
96  }
97 
98  std::vector<geometrize::State> states;
99 
100  for(auto& f : futures) {
101  try {
102  states.emplace_back(f.get());
103  } catch(std::exception& e) {
104  assert(0 && "Encountered exception when getting hill climb state");
105  std::cout << e.what() << std::endl;
106  throw e;
107  } catch (...) {
108  assert(0 && "Encountered exception when getting hill climb state");
109  throw;
110  }
111  }
112  return states;
113  }
geometrize::State bestHillClimbState(const std::function< std::shared_ptr< geometrize::Shape >(void)> &shapeCreator, const std::uint32_t alpha, const std::uint32_t n, const std::uint32_t age, const geometrize::Bitmap &target, const geometrize::Bitmap &current, geometrize::Bitmap &buffer, const double lastScore, const EnergyFunction &customEnergyFunction)
bestHillClimbState Gets the best state using a hill climbing algorithm.
Definition: core.cpp:233
void seedRandomGenerator(const std::uint32_t seed)
seedRandomGenerator Seeds the (thread-local) random number generators.
Definition: commonutil.cpp:19
std::atomic< std::uint32_t > m_baseRandomSeed
The base value used for seeding the random number generator (the one the user has control over)...
Definition: model.cpp:197
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
std::atomic< std::uint32_t > m_randomSeedOffset
Seed used for random number generation. Note: incremented by each std::async call used for model step...
Definition: model.cpp:198
The Bitmap class is a helper class for working with bitmap data.
Definition: bitmap.h:15
static const std::uint32_t defaultMaxThreads
Definition: model.cpp:196
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193

Here is the call graph for this function:

Here is the caller graph for this function:

geometrize::Bitmap& geometrize::Model::ModelImpl::getTarget ( )
inline
168  {
169  return m_target;
170  }
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
const geometrize::Bitmap& geometrize::Model::ModelImpl::getTarget ( ) const
inline
178  {
179  return m_target;
180  }
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
std::int32_t geometrize::Model::ModelImpl::getWidth ( ) const
inline
58  {
59  return m_target.getWidth();
60  }
std::uint32_t getWidth() const
getWidth Gets the width of the bitmap.
Definition: bitmap.cpp:20
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193

Here is the call graph for this function:

ModelImpl& geometrize::Model::ModelImpl::operator= ( const ModelImpl )
delete
void geometrize::Model::ModelImpl::reset ( const geometrize::rgba  backgroundColor)
inline
52  {
53  m_current.fill(backgroundColor);
55  }
double differenceFull(const geometrize::Bitmap &first, const geometrize::Bitmap &second)
differenceFull Calculates the root-mean-square error between two bitmaps.
Definition: core.cpp:174
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
void fill(geometrize::rgba color)
fill Fills the bitmap with the given color.
Definition: bitmap.cpp:55
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193

Here is the call graph for this function:

void geometrize::Model::ModelImpl::setSeed ( const std::uint32_t  seed)
inline
188  {
189  m_baseRandomSeed = seed;
190  }
std::atomic< std::uint32_t > m_baseRandomSeed
The base value used for seeding the random number generator (the one the user has control over)...
Definition: model.cpp:197
std::vector<geometrize::ShapeResult> geometrize::Model::ModelImpl::step ( const std::function< std::shared_ptr< geometrize::Shape >(void)>  shapeCreator,
const std::uint8_t  alpha,
const std::uint32_t  shapeCount,
const std::uint32_t  maxShapeMutations,
const std::uint32_t  maxThreads,
const geometrize::core::EnergyFunction energyFunction 
)
inline
122  {
123  std::vector<geometrize::State> states{getHillClimbState(shapeCreator, alpha, shapeCount, maxShapeMutations, maxThreads, energyFunction)};
124  if(states.empty()) {
125  assert(0 && "Failed to get a hill climb state");
126  return {};
127  }
128 
129  std::vector<geometrize::State>::iterator it = std::min_element(states.begin(), states.end(), [](const geometrize::State& a, const geometrize::State& b) {
130  return a.m_score < b.m_score;
131  });
132 
133  // Draw the shape onto the image
134  const std::shared_ptr<geometrize::Shape> shape = it->m_shape;
135  const std::vector<geometrize::Scanline> lines{shape->rasterize(*shape)};
137  const geometrize::Bitmap before{m_current};
138  geometrize::drawLines(m_current, color, lines);
139 
140  // Check for an improvement - if not, roll back and return no result
141  const double newScore = geometrize::core::differencePartial(m_target, before, m_current, m_lastScore, lines);
142  if(newScore >= m_lastScore) {
143  m_current = before;
144  return {};
145  }
146 
147  // Improvement - set new baseline and return the new shape
148  m_lastScore = newScore;
149  const geometrize::ShapeResult result{m_lastScore, color, shape};
150  return { result };
151  }
void drawLines(geometrize::Bitmap &image, const geometrize::rgba color, const std::vector< geometrize::Scanline > &lines)
drawLines Draws scanlines onto an image.
Definition: rasterizer.cpp:74
The ShapeResult struct is a container for info about a shape added to the model.
Definition: shaperesult.h:19
std::vector< geometrize::State > getHillClimbState(const std::function< std::shared_ptr< geometrize::Shape >(void)> shapeCreator, const std::uint8_t alpha, const std::uint32_t shapeCount, const std::uint32_t maxShapeMutations, std::uint32_t maxThreads, const geometrize::core::EnergyFunction energyFunction)
Definition: model.cpp:67
double m_score
The score of the state, a measure of the improvement applying the state to the current bitmap will ha...
Definition: state.h:41
geometrize::rgba computeColor(const geometrize::Bitmap &target, const geometrize::Bitmap &current, const std::vector< geometrize::Scanline > &lines, const std::uint8_t alpha)
computeColor Calculates the color of the scanlines.
Definition: core.cpp:122
double m_lastScore
Score derived from calculating the difference between bitmaps.
Definition: model.cpp:195
geometrize::Bitmap m_current
The current bitmap.
Definition: model.cpp:194
The Bitmap class is a helper class for working with bitmap data.
Definition: bitmap.h:15
The rgba struct is a helper for manipulating RGBA8888 color data.
Definition: rgba.h:12
The State class relates a shape and related properties to a measure of how close it brings the workin...
Definition: state.h:20
geometrize::Bitmap m_target
The target bitmap, the bitmap we aim to approximate.
Definition: model.cpp:193
double differencePartial(const geometrize::Bitmap &target, const geometrize::Bitmap &before, const geometrize::Bitmap &after, const double score, const std::vector< Scanline > &lines)
differencePartial Calculates the root-mean-square error between the parts of the two bitmaps within t...
Definition: core.cpp:198

Here is the call graph for this function:

Member Data Documentation

const std::uint32_t geometrize::Model::ModelImpl::defaultMaxThreads {4}
staticprivate
std::atomic<std::uint32_t> geometrize::Model::ModelImpl::m_baseRandomSeed
private

The base value used for seeding the random number generator (the one the user has control over).

geometrize::Bitmap geometrize::Model::ModelImpl::m_current
private

The current bitmap.

double geometrize::Model::ModelImpl::m_lastScore
private

Score derived from calculating the difference between bitmaps.

std::atomic<std::uint32_t> geometrize::Model::ModelImpl::m_randomSeedOffset
private

Seed used for random number generation. Note: incremented by each std::async call used for model stepping.

geometrize::Bitmap geometrize::Model::ModelImpl::m_target
private

The target bitmap, the bitmap we aim to approximate.


The documentation for this class was generated from the following file: