CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
2d_shape.cpp
Go to the documentation of this file.
1
12
13/*===============================================================================================================================
14This file is part of CoastalME, the Coastal Modelling Environment.
15
16CoastalME 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.
17
18This 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.
19
20You 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.
21
22===============================================================================================================================*/
23#include "cme.h"
24#include "2d_shape.h"
25
30
35
38{
39 // TODO 055 Maybe add a safety check?
40 return m_VPoints[n];
41}
42
45{
46 m_VPoints.clear();
47}
48
50void CA2DShape::Resize(int const nSize)
51{
52 m_VPoints.resize(nSize);
53}
54
55// Returns the number of elements in this 2D shape
56int CA2DShape::nGetSize(void) const
57{
58 return static_cast<int>(m_VPoints.size());
59}
60
61// void CA2DShape::InsertAtFront(double const dX, double const dY)
62// {
63// m_VPoints.insert(m_VPoints.begin(), CGeom2DPoint(dX, dY));
64// }
65
68{
69 m_VPoints.push_back(*pPtNew);
70}
71
73void CA2DShape::Append(double const dX, double const dY)
74{
75 m_VPoints.push_back(CGeom2DPoint(dX, dY));
76}
77
79void CA2DShape::AppendIfNotAlready(double const dX, double const dY)
80{
81 CGeom2DPoint PtIn(dX, dY);
82
83 if (m_VPoints.empty())
84 m_VPoints.push_back(PtIn);
85
86 else if (m_VPoints.back() != &PtIn)
87 m_VPoints.push_back(PtIn);
88}
89
92{
93 return &m_VPoints.back();
94}
95
96// void CA2DShape::SetPoints(const vector<CGeom2DPoint>* VNewPoints)
97// {
98// m_VPoints = *VNewPoints;
99// }
100
101// int CA2DShape::nLookUp(CGeom2DPoint* Pt)
102// {
103// auto it = find(m_VPoints.begin(), m_VPoints.end(), *Pt);
104// if (it != m_VPoints.end())
105// return it - m_VPoints.begin();
106// else
107// return -1;
108// }
109
110// double CA2DShape::dGetLength(void) const
111// {
112// int nSize = m_VPoints.size();
113//
114// if (nSize < 2)
115// return -1;
116//
117// double dLength = 0;
118// for (int n = 1; n < nSize; n++)
119// {
120// double dXlen = m_VPoints[n].dGetX() - m_VPoints[n-1].dGetX();
121// double dYlen = m_VPoints[n].dGetY() - m_VPoints[n-1].dGetY();
122//
123// dLength += hypot(dXlen, dYlen);
124// }
125//
126// return dLength;
127// }
128
130vector<CGeom2DPoint>* CA2DShape::pPtVGetPoints(void)
131{
132 return &m_VPoints;
133}
134
135// //! Computes the centroid of this 2D polygon (which may be outside, if this is a concave polygon). From http://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon
136// CGeom2DPoint CA2DShape::PtGetCentroid(void)
137// {
138// int nVertexCount = static_cast<int>(m_VPoints.size());
139// double dSignedArea = 0;
140// double dCentroidX = 0;
141// double dCentroidY = 0;
142//
143// // For all vertices
144// for (int i = 0; i < nVertexCount; ++i)
145// {
146// double dXThis = m_VPoints[i].dGetX();
147// double dYThis = m_VPoints[i].dGetY();
148// double dXNext = m_VPoints[(i+1) % nVertexCount].dGetX();
149// double dYNext = m_VPoints[(i+1) % nVertexCount].dGetY();
150//
151// double dA = (dXThis * dYNext) - (dXNext * dYThis);
152// dSignedArea += dA;
153//
154// dCentroidX += (dXThis + dXNext) * dA;
155// dCentroidY += (dYThis + dYNext) * dA;
156// }
157//
158// dSignedArea *= 0.5;
159// dCentroidX /= (6 * dSignedArea);
160// dCentroidY /= (6 * dSignedArea);
161//
162// return (CGeom2DPoint(dCentroidX, dCentroidY));
163// }
164
167{
168 reverse(m_VPoints.begin(), m_VPoints.end());
169}
Contains CA2DShape definitions.
vector< CGeom2DPoint > m_VPoints
The points which comprise the float-coordinate 2D shape.
Definition 2d_shape.h:40
void Append(CGeom2DPoint const *)
Appends a point to this 2D shape.
Definition 2d_shape.cpp:67
vector< CGeom2DPoint > * pPtVGetPoints(void)
Returns the address of the vector which represents this 2D shape.
Definition 2d_shape.cpp:130
void Reverse(void)
Reverses the sequence of points in the vector which represents this 2D polygon.
Definition 2d_shape.cpp:166
void Clear(void)
Clears this 2D shape.
Definition 2d_shape.cpp:44
CA2DShape(void)
Constructor.
Definition 2d_shape.cpp:27
CGeom2DPoint * pPtBack(void)
Returns the last element of this 2D shape.
Definition 2d_shape.cpp:91
void AppendIfNotAlready(double const, double const)
Appends a point to this 2D shape only if it isn't already in the shape vector.
Definition 2d_shape.cpp:79
int nGetSize(void) const
Definition 2d_shape.cpp:56
void Resize(int const)
Resizes the vector which represents this 2D shape.
Definition 2d_shape.cpp:50
CGeom2DPoint & operator[](int const)
Operator to return one point of this 2D shape.
Definition 2d_shape.cpp:37
virtual ~CA2DShape(void)
Destructor.
Definition 2d_shape.cpp:32
Geometry class used to represent 2D point objects with floating-point coordinates.
Definition 2d_point.h:27
This file contains global definitions for CoastalME.