Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/gridedit 785 refactor mesh refinement #263

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions libs/MeshKernel/include/MeshKernel/AveragingInterpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ namespace meshkernel
void Compute() override;

private:
/// @brief Default size for interpolation points and sample caches.
static constexpr UInt DefaultMaximumCacheSize = 100;

/// @brief Compute the averaging results in polygon
/// @param[in] polygon The bounding polygon where the samples are included
/// @param[in] interpolationPoint The interpolation point
Expand Down Expand Up @@ -151,9 +154,10 @@ namespace meshkernel
bool m_useClosestSampleIfNoneAvailable = false; ///< Whether to use the closest sample if there is none available
bool m_transformSamples = false; ///< Wheher to transform samples
UInt m_minNumSamples = 1; ///< The minimum amount of samples for a valid interpolation. Used in some interpolation algorithms.
std::vector<Point> m_interpolationPointCache; ///< Cache for interpolation points
std::vector<double> m_interpolationSampleCache; ///< Cache for interpolation samples

std::vector<bool> m_visitedSamples; ///< The visited samples

RTree m_samplesRtree; ///< The samples tree
RTree m_samplesRtree; ///< The samples tree
std::unique_ptr<averaging::AveragingStrategy> m_strategy; ///< Averaging strategy
};
} // namespace meshkernel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace meshkernel::averaging
public:
virtual ~AveragingStrategy() = default;

/// @brief Reset the state of the averaging strategy, ready for the next calculation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add param interpolationPoint to doxy

virtual void Reset(const Point& interpolationPoint) = 0;

/// @brief Adds the specified sample point.
/// @param[in] samplePoint The sample point to add to this strategy.
/// @param[in] sampleValue The value associated with the sample point.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never liked the how samplePoint is a dummy arg in most derived classes. Only ClosestAveragingStrategy and InverseWeightedAveragingStrategy use it. Having a SetSamplePoint method in the base is also a bad idea because again, only used when ClosestAveragingStrategy and InverseWeightedAveragingStrategy arte created.
Can you think of a better way to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way I can see to resolve this is going back to the orginal, allocating a new object for each interpolation point.

Expand All @@ -44,5 +47,14 @@ namespace meshkernel::averaging
/// @brief Calculates the average value based on the values added.
/// @return The calculated average
[[nodiscard]] virtual double Calculate() const = 0;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
virtual double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const = 0;
};
} // namespace meshkernel::averaging
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ namespace meshkernel::averaging
/// @brief The static method returning the strategy
/// @param[in] averagingMethod The averaging method enumeration value
/// @param[in] minNumSamples The minimum a of samples used for certain interpolation algorithms
/// @param[in] interpolationPoint The interpolation point
/// @param[in] projection The projection to use
/// @return The interpolation strategy to use
[[nodiscard]] std::unique_ptr<AveragingStrategy> static GetAveragingStrategy(AveragingInterpolation::Method averagingMethod,
size_t minNumSamples,
Point const& interpolationPoint,
Projection projection);
};
} // namespace meshkernel::averaging
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,23 @@ namespace meshkernel::averaging
/// @brief Construct a new ClosestAveragingStrategy.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] projection The projection used to calculate distances with.
ClosestAveragingStrategy(Point const& interpolationPoint,
Projection projection);
ClosestAveragingStrategy(Projection projection);

/// @brief Reset the state of the closest averaging strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;
[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The result used to calculate the final value in Calculate.
double m_result = constants::missing::doubleValue;
Expand All @@ -53,7 +64,7 @@ namespace meshkernel::averaging
double m_closestSquaredValue = std::numeric_limits<double>::max();

/// @brief The interpolation point from which the closest value is calculated.
Point const& m_interpolationPoint;
Point m_interpolationPoint{constants::missing::doubleValue, constants::missing::doubleValue};

/// @brief The projection used to calculate the squared distance.
Projection const m_projection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ namespace meshkernel::averaging
{
public:
/// @brief Construct a new InverseWeightedAveragingStrategy.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] minNumSamples The minimum amount of samples for a valid interpolation.
/// @param[in] projection The projection used in calculating the distance.
InverseWeightedAveragingStrategy(Point const& interpolationPoint,
size_t minNumSamples,
InverseWeightedAveragingStrategy(size_t minNumSamples,
Projection projection);

/// @brief Reset the state of the inverse weighted averaging strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;
[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The current result used in Calculate to calculate the final value.
double m_result = 0.0;
Expand All @@ -55,10 +65,10 @@ namespace meshkernel::averaging
double m_wall = 0.0;

/// @brief The interpolation point from which the inverse weight is calculated.
Point const& m_interpolationPoint;
Point m_interpolationPoint{constants::missing::doubleValue, constants::missing::doubleValue};

/// @brief The minimum number of samples for a valid interpolation.
size_t m_minNumSamples;
const size_t m_minNumSamples;

/// @brief The projection used to calculate the distance.
Projection const m_projection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ namespace meshkernel::averaging
class MaxAveragingStrategy final : public AveragingStrategy
{
public:
/// @brief Reset the state of the max averaging strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;

[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The current result returned in Calculate.
double m_result = std::numeric_limits<double>::lowest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ namespace meshkernel::averaging
class MinAbsAveragingStrategy final : public AveragingStrategy
{
public:
/// @brief Reset the state of the absolute-value-of-the-mininimum averaging-strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;

[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The current result returned in Calculate
double m_result = std::numeric_limits<double>::max();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,22 @@ namespace meshkernel::averaging
class MinAveragingStrategy final : public AveragingStrategy
{
public:
/// @brief Reset the state of the minimum value averaging-strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;

[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The current result returned in Calculate
double m_result = std::numeric_limits<double>::max();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ namespace meshkernel::averaging
/// @param minNumSamples[in] The minimum amount of samples for a valid interpolation
SimpleAveragingStrategy(size_t minNumSamples);

/// @brief Reset the state of the simple averaging-strategy.
void Reset(const Point& interpolationPoint) override;

void Add(Point const& samplePoint, double sampleValue) override;
[[nodiscard]] double Calculate() const override;

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] samplePoints The sample points to used by this strategy.
/// @param[in] sampleValues The sample values associated with each sample point.
/// @return The calculated average
double Calculate (const Point& interpolationPoint,
const std::vector<Point>& samplePoints,
const std::vector<double>& sampleValues) const override;

private:
/// @brief The minimum number of points for a valid interpolation.
size_t m_minNumPoints;
const size_t m_minNumPoints;

/// @brief The current result from which Calculate calculates the final value.
double m_result = 0.0;
Expand Down