Skip to content

Commit

Permalink
Avoid double computations (#378 | gridedit 1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior authored Oct 31, 2024
1 parent d381024 commit 0a58be0
Show file tree
Hide file tree
Showing 29 changed files with 1,829 additions and 78 deletions.
39 changes: 35 additions & 4 deletions libs/MeshKernelApi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ add_library(${TARGET_NAME} SHARED ${CMAKE_BINARY_DIR}/version.rc)

# source directory
set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
set(CACHE_SRC_DIR ${SRC_DIR}/ApiCache)

# include directory
set(INC_DIR ${PROJECT_SOURCE_DIR}/include)
set(DOMAIN_INC_DIR ${INC_DIR}/MeshKernelApi)
set(CACHE_INC_DIR ${DOMAIN_INC_DIR}/ApiCache)
set(VERSION_INC_DIR ${CMAKE_SOURCE_DIR}/tools)

# list of target sources
Expand All @@ -25,6 +27,19 @@ set(SRC_LIST
${SRC_DIR}/MeshKernel.cpp
)

set(CACHE_SRC_LIST
${CACHE_SRC_DIR}/BoundariesAsPolygonCache.cpp
${CACHE_SRC_DIR}/CachedIntegerValues.cpp
${CACHE_SRC_DIR}/CachedPointValues.cpp
${CACHE_SRC_DIR}/FacePolygonPropertyCache.cpp
${CACHE_SRC_DIR}/HangingEdgeCache.cpp
${CACHE_SRC_DIR}/NodeInPolygonCache.cpp
${CACHE_SRC_DIR}/ObtuseTriangleCentreCache.cpp
${CACHE_SRC_DIR}/PolygonRefinementCache.cpp
${CACHE_SRC_DIR}/SmallFlowEdgeCentreCache.cpp
)


# list of target headers
set(
INC_LIST
Expand All @@ -42,17 +57,33 @@ set(
${VERSION_INC_DIR}/Version/Version.hpp
)

set(
CACHE_INC_LIST
${CACHE_INC_DIR}/BoundariesAsPolygonCache.hpp
${CACHE_INC_DIR}/CachedIntegerValues.hpp
${CACHE_INC_DIR}/CachedPointValues.hpp
${CACHE_INC_DIR}/FacePolygonPropertyCache.hpp
${CACHE_INC_DIR}/HangingEdgeCache.hpp
${CACHE_INC_DIR}/NodeInPolygonCache.hpp
${CACHE_INC_DIR}/ObtuseTriangleCentreCache.hpp
${CACHE_INC_DIR}/PolygonRefinementCache.hpp
${CACHE_INC_DIR}/SmallFlowEdgeCentreCache.hpp
)


# add sources to target
target_sources(
${TARGET_NAME}
PRIVATE
${SRC_LIST}
${SRC_LIST}
${CACHE_SRC_LIST}
PUBLIC
FILE_SET HEADERS
BASE_DIRS
${INC_DIR} ${VERSION_INC_DIR}
FILES
${INC_LIST}
${INC_LIST}
${CACHE_INC_LIST}
)

# Expose the interface of the shared lib
Expand Down Expand Up @@ -88,10 +119,10 @@ target_precompile_headers(
install(TARGETS ${TARGET_NAME} FILE_SET HEADERS DESTINATION "include")

# group the sources in IDE tree
source_group("Source Files" FILES ${SRC_LIST})
source_group("Source Files" FILES ${SRC_LIST} ${CACHE_SRC_LIST})

# group the headers in IDE tree
source_group("Header Files" FILES ${INC_LIST})
source_group("Header Files" FILES ${INC_LIST} ${CACHE_INC_LIST})

# Add unit tests
if(ENABLE_UNIT_TESTING)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2024.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>

#include "MeshKernelApi/ApiCache/CachedPointValues.hpp"
#include "MeshKernelApi/GeometryList.hpp"
#include "MeshKernelApi/Utils.hpp"

namespace meshkernelapi
{

/// @brief Cache boundary polygon points
class BoundariesAsPolygonCache : public CachedPointValues
{
public:
/// @brief Constructor
BoundariesAsPolygonCache(const int lowerLeftN,
const int lowerLeftM,
const int upperRightN,
const int upperRightM,
const std::vector<meshkernel::Point>& boundaryPoints);

/// @brief Determine if current options match those used to construct the object
bool ValidOptions(const int lowerLeftN,
const int lowerLeftM,
const int upperRightN,
const int upperRightM) const;

private:
int m_lowerLeftNValue = -1; ///< Initial lower left N value
int m_lowerLeftMValue = -1; ///< Initial lower left M value
int m_upperRightNValue = -1; ///< Initial upper right N value
int m_upperRightMValue = -1; ///< Initial upper right M value
};

} // namespace meshkernelapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2024.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include <cstring>
#include <vector>

namespace meshkernelapi
{
/// @brief Caches x- and y-coordinate values for various algorithms
class CachedIntegerValues
{
public:
/// @brief Default constructor
CachedIntegerValues() {}

/// @brief Construct with point values
CachedIntegerValues(const std::vector<int>& values);

/// @brief Destructor
virtual ~CachedIntegerValues() = default;

/// @brief Number of points saved
int Size() const
{
return static_cast<int>(m_values.size());
}

/// @brief Copy cached points to geometry
void Copy(int* buffer) const;

protected:
/// @brief Reset the saved integer values.
void Reset(std::vector<int>&& values);

private:
std::vector<int> m_values; ///< integer values
};
} // namespace meshkernelapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2024.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include <cstring>
#include <vector>

#include "MeshKernel/Point.hpp"

#include "MeshKernelApi/GeometryList.hpp"

namespace meshkernelapi
{
/// @brief Caches x- and y-coordinate values for various algorithms
class CachedPointValues
{
public:
/// @brief Default constructor
CachedPointValues() {}

/// @brief Construct with point values
CachedPointValues(const std::vector<meshkernel::Point>& coordinates);

/// @brief Destructor
virtual ~CachedPointValues() = default;

/// @brief Number of points saved
int Size() const
{
return static_cast<int>(m_coordsX.size());
}

/// @brief Copy cached points to geometry
void Copy(const GeometryList& geometry) const;

protected:
/// @brief Reset the saved coordinate values.
void Reset(std::vector<double>&& xValues,
std::vector<double>&& yValues);

private:
std::vector<double> m_coordsX; ///< x-coordinate values
std::vector<double> m_coordsY; ///< y-coordinate values
};
} // namespace meshkernelapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2024.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include <vector>

#include "MeshKernel/Mesh2D.hpp"

#include "MeshKernelApi/ApiCache/CachedPointValues.hpp"

namespace meshkernelapi
{
/// @brief Cache node values of faces
class FacePolygonPropertyCache : public CachedPointValues
{

public:
/// @brief Constructor
FacePolygonPropertyCache(const int propertyValue,
const double minValue,
const double maxValue,
const meshkernel::Mesh2D& mesh,
const int validSize,
const std::vector<bool>& filterMask);

/// @brief Determine if current options match those used to construct the object
bool ValidOptions(const int propertyValue,
const double minValue,
const double maxValue) const;

private:
int m_propertyValue = 0; ///< Initial property value
double m_minimumValue = meshkernel::constants::missing::doubleValue; ///< Initial minimum value
double m_maximumValue = meshkernel::constants::missing::doubleValue; ///< Initial maximum value
};

} // namespace meshkernelapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2024.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include <cstring>
#include <vector>

#include "MeshKernel/Point.hpp"

#include "MeshKernelApi/ApiCache/CachedIntegerValues.hpp"

namespace meshkernelapi
{

/// @brief Cache edge indices for hanging nodes/edges
class HangingEdgeCache : public CachedIntegerValues
{
public:
/// @brief Constructor
HangingEdgeCache(const std::vector<meshkernel::UInt>& edgeIds);
};

} // namespace meshkernelapi
Loading

0 comments on commit 0a58be0

Please sign in to comment.