CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
calc_curvature.cpp
Go to the documentation of this file.
1
10
11/* ==============================================================================================================================
12 This file is part of CoastalME, the Coastal Modelling Environment.
13
14 CoastalME 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; either version 3 of the License, or (at your option) any later version.
15
16 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.
17
18 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19==============================================================================================================================*/
20#include <iostream>
21using std::endl;
22
23#include <cfloat>
24
25#include <cmath>
26using std::sqrt;
27
28#include <numeric>
29using std::accumulate;
30using std::inner_product;
31
32#include "cme.h"
33#include "2d_point.h"
34#include "simulation.h"
35#include "coast.h"
36
37//===============================================================================================================================
39//===============================================================================================================================
40void CSimulation::DoCoastCurvature(int const nCoast, int const nHandedness)
41{
43 LogStream << m_ulIter << ": \tcalculating curvatures for coast " << nCoast << endl;
44
45 int const nCoastSize = m_VCoast[nCoast].nGetCoastlineSize();
46
47 // Start with detailed curvature, do every point on the coastline, apart from the first and last points
48 for (int nThisCoastPoint = 1; nThisCoastPoint < (nCoastSize - 1); nThisCoastPoint++)
49 {
50 // Calculate the signed curvature based on this point, and the points before and after
51 double const dCurvature = dCalcCurvature(nHandedness, m_VCoast[nCoast].pPtGetCoastlinePointExtCRS(nThisCoastPoint - 1), m_VCoast[nCoast].pPtGetCoastlinePointExtCRS(nThisCoastPoint), m_VCoast[nCoast].pPtGetCoastlinePointExtCRS(nThisCoastPoint + 1));
52
53 // Set the detailed curvature
54 m_VCoast[nCoast].SetDetailedCurvature(nThisCoastPoint, dCurvature);
55 }
56
57 // Set the curvature for the first and last coastline points
58 double dTemp = m_VCoast[nCoast].dGetDetailedCurvature(1);
59 m_VCoast[nCoast].SetDetailedCurvature(0, dTemp);
60
61 dTemp = m_VCoast[nCoast].dGetDetailedCurvature(nCoastSize - 2);
62 m_VCoast[nCoast].SetDetailedCurvature(nCoastSize - 1, dTemp);
63
64 // Now create the smoothed curvature
65 int const nHalfWindow = m_nCoastSmoothingWindowSize / 2;
66
67 // Apply a running mean smoothing filter, with a variable window size at both ends of the coastline
68 for (int i = 0; i < nCoastSize; i++)
69 {
70 int nTmpWindow = 0;
71 double dWindowTot = 0;
72
73 for (int j = -nHalfWindow; j < m_nCoastSmoothingWindowSize - nHalfWindow; j++)
74 {
75 // For points at both ends of the coastline, use a smaller window
76 int const k = i + j;
77
78 if ((k < 0) || (k >= nCoastSize))
79 continue;
80
81 dWindowTot += m_VCoast[nCoast].dGetDetailedCurvature(k);
82 nTmpWindow++;
83 }
84
85 m_VCoast[nCoast].SetSmoothCurvature(i, dWindowTot / static_cast<double>(nTmpWindow));
86 }
87
88 // Now calculate the mean and standard deviation of each set of curvature values
89 vector<double>* pVDetailed = m_VCoast[nCoast].pVGetDetailedCurvature();
90
91 double dSum = accumulate(pVDetailed->begin(), pVDetailed->end(), 0.0);
92 double dMean = dSum / static_cast<double>(pVDetailed->size());
93
94 m_VCoast[nCoast].SetDetailedCurvatureMean(dMean);
95
96 double dSquareSum = inner_product(pVDetailed->begin(), pVDetailed->end(), pVDetailed->begin(), 0.0);
97 double dSTD = sqrt(dSquareSum / static_cast<double>(pVDetailed->size()) - dMean * dMean);
98
99 m_VCoast[nCoast].SetDetailedCurvatureSTD(dSTD);
100
101 vector<double>* pVSmooth = m_VCoast[nCoast].pVGetSmoothCurvature();
102 dSum = accumulate(pVSmooth->begin(), pVSmooth->end(), 0.0),
103 dMean = dSum / static_cast<double>(pVSmooth->size());
104
105 m_VCoast[nCoast].SetSmoothCurvatureMean(dMean);
106
107 dSquareSum = inner_product(pVSmooth->begin(), pVSmooth->end(), pVSmooth->begin(), 0.0), dSTD = sqrt(dSquareSum / static_cast<double>(pVSmooth->size()) - dMean * dMean);
108 m_VCoast[nCoast].SetSmoothCurvatureSTD(dSTD);
109
110 double dMaxConvexDetailed = DBL_MAX;
111 double dMaxConvexSmoothed = DBL_MAX;
112
113 for (int mm = 0; mm < nCoastSize; mm++)
114 {
115 if (m_VCoast[nCoast].dGetDetailedCurvature(mm) < dMaxConvexDetailed)
116 {
117 dMaxConvexDetailed = m_VCoast[nCoast].dGetDetailedCurvature(mm);
118 }
119
120 if (m_VCoast[nCoast].dGetSmoothCurvature(mm) < dMaxConvexSmoothed)
121 {
122 dMaxConvexSmoothed = m_VCoast[nCoast].dGetSmoothCurvature(mm);
123 // nMaxConvexSmoothedCoastPoint = mm;
124 }
125 }
126
127 if (bFPIsEqual(dMaxConvexDetailed, 0.0, TOLERANCE))
128 {
129 // We have a straight-line coast, so set the point of maximum convexity at the coast mid-point
130 int const nMaxConvexCoastPoint = nCoastSize / 2;
131
132 m_VCoast[nCoast].SetDetailedCurvature(nMaxConvexCoastPoint, STRAIGHT_COAST_MAX_DETAILED_CURVATURE);
133 m_VCoast[nCoast].SetSmoothCurvature(nMaxConvexCoastPoint, STRAIGHT_COAST_MAX_SMOOTH_CURVATURE);
134 }
135
136 // LogStream << "-----------------" << endl;
137 // for (int kk = 0; kk < m_VCoast.back().nGetCoastlineSize(); kk++)
138 // LogStream << kk << " [" << m_VCoast.back().pPtiGetCellMarkedAsCoastline(kk)->nGetX() << "][" << m_VCoast.back().pPtiGetCellMarkedAsCoastline(kk)->nGetY() << "] = {" << dGridCentroidXToExtCRSX(m_VCoast.back().pPtiGetCellMarkedAsCoastline(kk)->nGetX()) << ", " << dGridCentroidYToExtCRSY(m_VCoast.back().pPtiGetCellMarkedAsCoastline(kk)->nGetY()) << "} detailed curvature = " << m_VCoast[nCoast].dGetDetailedCurvature(kk) << " smooth curvature = " << m_VCoast[nCoast].dGetSmoothCurvature(kk) << endl;
139 // LogStream << "-----------------" << endl;
140
141 // CGeom2DIPoint PtiMax = *m_VCoast[nCoast].pPtiGetCellMarkedAsCoastline(nMaxConvexDetailedCoastPoint);
142
143 // if (m_nLogFileDetail >= LOG_FILE_ALL)
144 // LogStream << m_ulIter << ": Max detailed convexity (" << m_VCoast[nCoast].dGetDetailedCurvature(nMaxConvexDetailedCoastPoint) << ") at raster coastline point " << nMaxConvexDetailedCoastPoint << " [" << PtiMax.nGetX() << "][" << PtiMax.nGetY() << "] = {" << dGridCentroidXToExtCRSX(PtiMax.nGetX()) << ", " << dGridCentroidYToExtCRSY(PtiMax.nGetY()) << "}" << endl;
145
146 // CGeom2DIPoint PtiMaxSmooth = *m_VCoast[nCoast].pPtiGetCellMarkedAsCoastline(nMaxConvexSmoothedCoastPoint);
147 // CGeom2DPoint PtMaxSmooth = *m_VCoast[nCoast].pPtGetCoastlinePointExtCRS(nMaxConvexSmoothedCoastPoint);
148
149 // if (m_nLogFileDetail >= LOG_FILE_ALL)
150 // LogStream << m_ulIter << ": Max smoothed convexity (" << m_VCoast[nCoast].dGetSmoothCurvature(nMaxConvexSmoothedCoastPoint) << ") near vector coastline point " << nMaxConvexSmoothedCoastPoint << ", at [" << PtiMaxSmooth.nGetX() << "][" << PtiMaxSmooth.nGetY() << "] = {" << PtMaxSmooth.dGetX() << ", " << PtMaxSmooth.dGetY() << "}" << endl;
151}
152
153//===============================================================================================================================
155//===============================================================================================================================
156double CSimulation::dCalcCurvature(int const nHandedness, CGeom2DPoint const* pPtBefore, CGeom2DPoint const* pPtThis, CGeom2DPoint const* pPtAfter)
157{
158 double const dAreax4 = 2 * dTriangleAreax2(pPtBefore, pPtThis, pPtAfter);
159 double dDist1 = dGetDistanceBetween(pPtBefore, pPtThis);
160 double dDist2 = dGetDistanceBetween(pPtThis, pPtAfter);
161 double dDist3 = dGetDistanceBetween(pPtBefore, pPtAfter);
162
163 // Safety checks
164 if (bFPIsEqual(dDist1, 0.0, TOLERANCE))
165 dDist1 = TOLERANCE;
166
167 if (bFPIsEqual(dDist2, 0.0, TOLERANCE))
168 dDist2 = TOLERANCE;
169
170 if (bFPIsEqual(dDist3, 0.0, TOLERANCE))
171 dDist3 = TOLERANCE;
172
173 double const dCurvature = dAreax4 / (dDist1 * dDist2 * dDist3);
174
175 // Reverse if left-handed
176 int const nShape = (nHandedness == LEFT_HANDED ? 1 : -1);
177
178 return (dCurvature * nShape * 1000);
179}
Contains CGeom2DPoint definitions.
Geometry class used to represent 2D point objects with floating-point coordinates.
Definition 2d_point.h:25
int m_nLogFileDetail
The level of detail in the log file output. Can be LOG_FILE_LOW_DETAIL, LOG_FILE_MIDDLE_DETAIL,...
Definition simulation.h:588
ofstream LogStream
vector< CRWCoast > m_VCoast
The coastline objects.
static double dGetDistanceBetween(CGeom2DPoint const *, CGeom2DPoint const *)
Returns the distance (in external CRS) between two points.
int m_nCoastSmoothingWindowSize
The size of the window used for coast smoothing. Must be an odd number.
Definition simulation.h:489
static double dTriangleAreax2(CGeom2DPoint const *, CGeom2DPoint const *, CGeom2DPoint const *)
Returns twice the signed area of a triangle, defined by three points.
static double dCalcCurvature(int const, CGeom2DPoint const *, CGeom2DPoint const *, CGeom2DPoint const *)
Calculates signed Menger curvature (https://en.wikipedia.org/wiki/Menger_curvature#Definition) from t...
void DoCoastCurvature(int const, int const)
Calculates both detailed and smoothed curvature for every point on a coastline.
unsigned long m_ulIter
The number of the current iteration (time step)
Definition simulation.h:609
This file contains global definitions for CoastalME.
double const TOLERANCE
Definition cme.h:725
bool bFPIsEqual(const T d1, const T d2, const T dEpsilon)
Definition cme.h:1213
double const STRAIGHT_COAST_MAX_DETAILED_CURVATURE
Definition cme.h:728
int const LOG_FILE_HIGH_DETAIL
Definition cme.h:394
double const STRAIGHT_COAST_MAX_SMOOTH_CURVATURE
Definition cme.h:729
int const LEFT_HANDED
Definition cme.h:414
Contains CRWCoast definitions.
Contains CSimulation definitions.