--- a/src/misc/ringFinder.h Sun Jun 22 17:27:50 2014 +0300 +++ b/src/misc/ringFinder.h Sun Jun 22 17:55:27 2014 +0300 @@ -17,113 +17,81 @@ */ #pragma once - #include "../main.h" -//! -//! \brief Provides an algorithm for finding solutions of rings between given radii. -//! -//! The RingFinder is a class which implements a ring finding algorithm. It is passed -//! two radii and it tries to find solutions of rings that would fill the given space. -//! -//! \note It is not fool-proof and does not always yield a solution, never assume -//! \note that one is a available as none is guaranteed. -//! +// +// Implements a ring finding algorithm. It is passed two radii and it tries to +// find solutions of rings that would fill the given space. +// +// Note: it is not fool-proof and does not always yield a solution. +// class RingFinder { - public: - //! A single component in a solution - struct Component - { - int num; - double scale; - }; - - //! A solution whose components would fill the desired ring space. - class Solution - { - public: - //! \returns components of this solution - inline const QVector<Component>& getComponents() const - { - return m_components; - } - - //! Add a component to this solution - inline void addComponent (const Component& a) - { - m_components.push_back (a); - } +public: + // A single component in a solution + struct Component + { + int num; + double scale; + }; - //! \brief Compare solutions. - //! - //! Compares this solution with \c other and determines which - //! one is superior. - //! - //! A solution is considered superior if solution has less - //! components than the other one. If both solution have an - //! equal amount components, the solution with a lesser maximum - //! ring number is found superior, as such solutions should - //! yield less new primitives and cleaner definitions. - //! - //! The solution which is found superior to every other solution - //! will be the one returned by \c RingFinder::bestSolution(). - //! - //! \param other the solution to check against - //! \returns whether this solution is considered superior - //! \returns to \c other. - //! - bool isSuperiorTo (const Solution* other) const; - - private: - QVector<Component> m_components; - }; - - //! Constructs a ring finder. - RingFinder() {} - - //! \brief Tries to find rings between \c r0 and \c r1. - //! - //! This is the main algorithm of the ring finder. It tries to use math - //! to find the one ring between r0 and r1. If it fails (the ring number - //! is non-integral), it finds an intermediate radius (ceil of the ring - //! number times scale) and splits the radius at this point, calling this - //! function again to try find the rings between r0 - r and r - r1. - //! - //! This does not always yield into usable results. If at some point r == - //! r0 or r == r1, there is no hope of finding the rings, at least with - //! this algorithm, as it would fall into an infinite recursion. - //! - //! \param r0 the lower radius of the ring space - //! \param r1 the higher radius of the ring space - //! \returns whether it was possible to find a solution for the given - //! \returns ring space. - //! - bool findRings (double r0, double r1); - - //! \returns the solution that was considered best. Returns \c null - //! \returns if no suitable solution was found. - //! \see \c RingFinder::Solution::isSuperiorTo() - inline const Solution* bestSolution() - { - return m_bestSolution; - } - - //! \returns all found solutions. The list is empty if no solutions - //! \returns were found. - inline const QVector<Solution>& allSolutions() const - { - return m_solutions; - } + // A solution whose components fill the desired ring space. + class Solution + { + public: + inline void addComponent (const Component& a); + inline const QVector<Component>& getComponents() const; + bool isSuperiorTo (const Solution* other) const; private: - QVector<Solution> m_solutions; - const Solution* m_bestSolution; - int m_stack; + QVector<Component> m_components; + }; + + RingFinder(); - //! Helper function for \c findRings - bool findRingsRecursor (double r0, double r1, Solution& currentSolution); + inline const QVector<Solution>& allSolutions() const; + inline const Solution* bestSolution() const; + bool findRings (double r0, double r1); + +private: + QVector<Solution> m_solutions; + const Solution* m_bestSolution; + int m_stack; + + bool findRingsRecursor (double r0, double r1, Solution& currentSolution); }; +// +// Gets the components of a solution +// +inline const QVector<RingFinder::Component>& RingFinder::Solution::getComponents() const +{ + return m_components; +} + +// +// Adds a component to a solution +// +inline void RingFinder::Solution::addComponent (const Component& a) +{ + m_components.push_back (a); +} + +// +// Returns the solution that was considered best. Returns null +// if there isn't any suitable solution. +// +inline const RingFinder::Solution* RingFinder::bestSolution() const +{ + return m_bestSolution; +} + +// +// Returns all found solutions. +// +inline const QVector<RingFinder::Solution>& RingFinder::allSolutions() const +{ + return m_solutions; +} + extern RingFinder g_RingFinder; -