Skip to content

Commit

Permalink
Merge pull request #4238 from NREL/geometry_improvements_mod
Browse files Browse the repository at this point in the history
Lint geometry_improvements and use const refs
  • Loading branch information
ggartside authored Mar 9, 2021
2 parents e004637 + 15db807 commit 5f7742b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 79 deletions.
4 changes: 0 additions & 4 deletions src/utilities/geometry/Intersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,11 +1054,7 @@ std::vector<Point3d> simplify(const std::vector<Point3d>& vertices, bool removeC
return result;
}

/// <summary>
/// Converts a Polygon to a BoostPolygon
/// </summary>
/// <param name="polygon"></param>
/// <returns></returns>
boost::optional<BoostPolygon> BoostPolygonFromPolygon(const Polygon3d& polygon) {
BoostPolygon boostPolygon;

Expand Down
75 changes: 37 additions & 38 deletions src/utilities/geometry/Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,73 +35,72 @@ namespace openstudio {

Polygon3d::Polygon3d() {}

Polygon3d::Polygon3d(Point3dVector outerPath) {
for (auto p : outerPath) {
outerPath.push_back(p);
Polygon3d::Polygon3d(const Point3dVector& outerPath) {
for (const auto& p : outerPath) {
m_outerPath.emplace_back(p);
}
}

Polygon3d::Polygon3d(Point3dVector outerPath, Point3dVectorVector innerPaths) {
for (auto p : outerPath) {
outerPath.push_back(p);
Polygon3d::Polygon3d(const Point3dVector& outerPath, const Point3dVectorVector& innerPaths) {
for (const auto& p : outerPath) {
m_outerPath.emplace_back(p);
}

for (auto innerPath : innerPaths) {
for (const auto& innerPath : innerPaths) {
addHole(innerPath);
}
}

/// Adds a point to the polygon perimeter
void Polygon3d::addPoint(const Point3d& point) {
outerPath.push_back(point);
m_outerPath.emplace_back(point);
}


void Polygon3d::setOuterPath(Point3dVector outerPath) {
outerPath = outerPath;
/// Sets the perimeter of the polygon
void Polygon3d::setOuterPath(const Point3dVector& outerPath) {
m_outerPath = outerPath;
}

Point3dVector Polygon3d::getOuterPath() const {
return outerPath;
return m_outerPath;
}

Point3dVectorVector Polygon3d::getInnerPaths() const {
return innerPaths;
return m_innerPaths;
}

void Polygon3d::addHole(Point3dVector hole) {
innerPaths.push_back(hole);
void Polygon3d::addHole(const Point3dVector& hole) {
m_innerPaths.emplace_back(hole);
}

Vector3d Polygon3d::newellVector() {
OptionalVector3d v = openstudio::getNewallVector(outerPath);

if (v) {
Vector3d Polygon3d::newallVector() {
if (OptionalVector3d v = openstudio::getNewallVector(m_outerPath)) {
return v.get();
}

return Vector3d();
}

Vector3d Polygon3d::outwardNormal() {
return openstudio::getOutwardNormal(outerPath).get();
return openstudio::getOutwardNormal(m_outerPath).get();
}

double Polygon3d::grossArea() {

boost::optional<double> area = openstudio::getArea(outerPath);
if (area == boost::none)
return 0;
else
if (boost::optional<double> area = openstudio::getArea(m_outerPath)) {
return area.get();
}

return 0;
}

double Polygon3d::netArea() {

double netArea = grossArea();
for (auto hole : innerPaths) {
boost::optional<double> area = openstudio::getArea(hole);
if (area != boost::none) netArea -= area.get();
for (const auto& hole : m_innerPaths) {
if (boost::optional<double> area = openstudio::getArea(hole)) {
netArea -= area.get();
}
}

return netArea;
Expand All @@ -110,29 +109,29 @@ double Polygon3d::netArea() {
double Polygon3d::perimeter() {

double perimeter = 0;
for (size_t i = 0; i < outerPath.size(); i++) {
Point3d p1 = outerPath[i];
Point3d p2 = outerPath[(i + 1) % outerPath.size()];
for (size_t i = 0; i < m_outerPath.size(); i++) {
Point3d p1 = m_outerPath[i];
Point3d p2 = m_outerPath[(i + 1) % m_outerPath.size()];
perimeter += openstudio::getDistance(p1, p2);
}

return perimeter;
}

bool Polygon3d::isClockwise() {
OptionalVector3d normal = getOutwardNormal(outerPath);
if (normal == boost::none)
return true;
else
if (OptionalVector3d normal = getOutwardNormal(m_outerPath)) {
return normal.get().z() > 0;
}

return true;
}

Point3d Polygon3d::centroid() {
boost::optional p = openstudio::getCentroid(outerPath);
if (p == boost::none)
return Point3d();
else
if (auto p = openstudio::getCentroid(m_outerPath)) {
return p.get();
}

return Point3d();
}

//bool Polygon3d::PointInPolygon(Point3d testPoint) {
Expand Down
26 changes: 12 additions & 14 deletions src/utilities/geometry/Polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,32 @@ class Vector3d;
class UTILITIES_API Polygon3d
{
public:

// Constructs an empty polygon
Polygon3d();

// Construct a polygon with an outer path
Polygon3d(Point3dVector outerPath);
Polygon3d(const Point3dVector& outerPath);

// COnstructs a polygon with an oiyer path and pone or more inner paths
Polygon3d(Point3dVector outerPath, Point3dVectorVector innerPaths);
// Constructs a polygon with an outer path and one or more inner paths
Polygon3d(const Point3dVector& outerPath, const Point3dVectorVector& innerPaths);

// Assigns an outer path for the polygon
void setOuterPath(Point3dVector outerPath);
void setOuterPath(const Point3dVector& outerPath);

// Returns the polygon's outer path
Point3dVector getOuterPath() const;

// Returns the polygonm's inner paths
// Returns the polygon's inner paths
Point3dVectorVector getInnerPaths() const;

// Adds a point to the outer path of the polygon
void addPoint(const Point3d& point);

// Adds an inner path to the polygon
void addHole(Point3dVector hole);
void addHole(const Point3dVector& hole);

// Calculates the Newell VEctor for the polygon
Vector3d newellVector();
// Calculates the Newall Vector for the polygon
Vector3d newallVector();

// Calculates the outward normal of the polygon
Vector3d outwardNormal();
Expand All @@ -82,19 +81,18 @@ class UTILITIES_API Polygon3d
// Gets the perimeter of the outer path of the polygon
double perimeter();

// Determines whether the polygon os clockwise (normal down) or anti-clockwise (normal up)
// Determines whether the polygon os clockwise (normal down) or anti-clockwise (normal up)
bool isClockwise();

// Calculates the centroid of the polygon
Point3d centroid();

private:

// The polygon's outer path
Point3dVector outerPath;
Point3dVector m_outerPath;

// The polygons inner paths
Point3dVectorVector innerPaths;
Point3dVectorVector m_innerPaths;
};
} // namespace openstudio

Expand Down
10 changes: 0 additions & 10 deletions src/utilities/geometry/Test/Geometry_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,13 +1117,7 @@ TEST_F(GeometryFixture, PointLatLon_Elevation) {
EXPECT_NEAR(30.0, test.z(), 0.001);
}

// Digital Alchemy

/// <summary>
/// Basic polygon functionality tests
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
TEST_F(GeometryFixture, Polygon_Basic) {
Polygon3d testPolygon;

Expand Down Expand Up @@ -1152,11 +1146,7 @@ TEST_F(GeometryFixture, Polygon_Basic) {
EXPECT_EQ(grossArea, openstudio::getArea(testPolygon.getOuterPath()).get());
}

/// <summary>
/// Angled polygon (taken from a roof)
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
TEST_F(GeometryFixture, Polygon_Basic_Angled) {
Polygon3d testPolygon;

Expand Down
13 changes: 0 additions & 13 deletions src/utilities/geometry/Test/Intersection_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,6 @@ TEST_F(GeometryFixture, simplify7) {
/// | |
/// +----------------------------+
///
/// <param name=""></param>
/// <param name=""></param>
TEST_F(GeometryFixture, Polygon3d_Join) {

Polygon3d polygonA;
Expand Down Expand Up @@ -1907,13 +1905,7 @@ TEST_F(GeometryFixture, Polygon3d_Join) {
}

// joinAll fails on cases with an inner loop
///

/// <summary>
/// joinAll method that takes a list of polygons and returns a list of polygons
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
TEST_F(GeometryFixture, Polygon3d_JoinAll_1614) {
double tol = 0.01;

Expand Down Expand Up @@ -1991,11 +1983,6 @@ TEST_F(GeometryFixture, Polygon3d_JoinAll_1614) {
// double perimeter = result.front().getPerimeter();
}

/// <summary>
/// joinAll method that takes a list of Point3d vectors and returns a list of polygons
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
TEST_F(GeometryFixture, Polygon3d_JoinAllPolygons_1614) {
double tol = 0.01;

Expand Down

0 comments on commit 5f7742b

Please sign in to comment.