src/utilities/geometry/Polygon3d.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Polygon3d.hpp" | ||
| 7 | #include "Vector3d.hpp" | ||
| 8 | #include "Geometry.hpp" | ||
| 9 | #include "Intersection.hpp" | ||
| 10 | #include <utilities/geometry/Transformation.hpp> | ||
| 11 | |||
| 12 | namespace openstudio { | ||
| 13 | |||
| 14 | 4 | Polygon3d::Polygon3d(const Point3dVector& outerPath) { | |
| 15 |
2/2✓ Branch 5 taken 16 times.
✓ Branch 6 taken 4 times.
|
20 | for (const auto& p : outerPath) { |
| 16 |
1/1✓ Branch 1 taken 16 times.
|
16 | m_outerPath.emplace_back(p); |
| 17 | } | ||
| 18 | 4 | } | |
| 19 | |||
| 20 | ✗ | Polygon3d::Polygon3d(const Point3dVector& outerPath, const Point3dVectorVector& innerPaths) { | |
| 21 | ✗ | for (const auto& p : outerPath) { | |
| 22 | ✗ | m_outerPath.emplace_back(p); | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | for (const auto& innerPath : innerPaths) { | |
| 26 | ✗ | addHole(innerPath); | |
| 27 | } | ||
| 28 | ✗ | } | |
| 29 | |||
| 30 | /// Adds a point to the polygon perimeter | ||
| 31 | 6082 | void Polygon3d::addPoint(const Point3d& point) { | |
| 32 | 6082 | m_outerPath.emplace_back(point); | |
| 33 | 6082 | } | |
| 34 | |||
| 35 | /// Sets the perimeter of the polygon | ||
| 36 | ✗ | void Polygon3d::setOuterPath(const Point3dVector& outerPath) { | |
| 37 | ✗ | m_outerPath = outerPath; | |
| 38 | ✗ | } | |
| 39 | |||
| 40 | 8121 | Point3dVector Polygon3d::getOuterPath() const { | |
| 41 | 8121 | return m_outerPath; | |
| 42 | } | ||
| 43 | |||
| 44 | 26 | Point3dVectorVector Polygon3d::getInnerPaths() const { | |
| 45 | 26 | return m_innerPaths; | |
| 46 | } | ||
| 47 | |||
| 48 | 26 | void Polygon3d::addHole(const Point3dVector& hole) { | |
| 49 | 26 | m_innerPaths.emplace_back(hole); | |
| 50 | 26 | } | |
| 51 | |||
| 52 | ✗ | Vector3d Polygon3d::newellVector() const { | |
| 53 | ✗ | if (OptionalVector3d v = openstudio::getNewellVector(m_outerPath)) { | |
| 54 | ✗ | return v.get(); | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | ✗ | return {}; | |
| 58 | } | ||
| 59 | |||
| 60 | 6 | Vector3d Polygon3d::outwardNormal() const { | |
| 61 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
6 | return openstudio::getOutwardNormal(m_outerPath).get(); |
| 62 | } | ||
| 63 | |||
| 64 | 29 | double Polygon3d::grossArea() const { | |
| 65 | |||
| 66 |
2/3✓ Branch 1 taken 29 times.
✓ Branch 3 taken 29 times.
✗ Branch 4 not taken.
|
58 | if (boost::optional<double> area = openstudio::getArea(m_outerPath)) { |
| 67 |
1/1✓ Branch 1 taken 29 times.
|
29 | return area.get(); |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | return 0; | |
| 71 | } | ||
| 72 | |||
| 73 | 13 | double Polygon3d::netArea() const { | |
| 74 | |||
| 75 | 13 | double netArea = grossArea(); | |
| 76 |
2/2✓ Branch 5 taken 10 times.
✓ Branch 6 taken 13 times.
|
23 | for (const auto& hole : m_innerPaths) { |
| 77 |
2/3✓ Branch 1 taken 10 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
20 | if (boost::optional<double> area = openstudio::getArea(hole)) { |
| 78 |
1/1✓ Branch 1 taken 10 times.
|
10 | netArea -= area.get(); |
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | 13 | return netArea; | |
| 83 | } | ||
| 84 | |||
| 85 | 17 | double Polygon3d::perimeter() const { | |
| 86 | |||
| 87 | 17 | double perimeter = 0; | |
| 88 |
2/2✓ Branch 1 taken 108 times.
✓ Branch 2 taken 17 times.
|
125 | for (size_t i = 0; i < m_outerPath.size(); i++) { |
| 89 |
1/1✓ Branch 2 taken 108 times.
|
108 | Point3d p1 = m_outerPath[i]; |
| 90 |
1/1✓ Branch 3 taken 108 times.
|
108 | Point3d p2 = m_outerPath[(i + 1) % m_outerPath.size()]; |
| 91 |
1/1✓ Branch 1 taken 108 times.
|
108 | perimeter += openstudio::getDistance(p1, p2); |
| 92 | 108 | } | |
| 93 | |||
| 94 | 17 | return perimeter; | |
| 95 | } | ||
| 96 | |||
| 97 | ✗ | bool Polygon3d::isClockwise() const { | |
| 98 | ✗ | if (OptionalVector3d normal = getOutwardNormal(m_outerPath)) { | |
| 99 | ✗ | return normal.get().z() > 0; | |
| 100 | ✗ | } | |
| 101 | |||
| 102 | ✗ | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | ✗ | Point3d Polygon3d::centroid() const { | |
| 106 | ✗ | if (auto p = openstudio::getCentroid(m_outerPath)) { | |
| 107 | ✗ | return p.get(); | |
| 108 | ✗ | } | |
| 109 | |||
| 110 | ✗ | return {}; | |
| 111 | } | ||
| 112 | |||
| 113 | 56 | bool Polygon3d::pointInPolygon(const Point3d& point, double tol) const { | |
| 114 | 56 | bool inside = false; | |
| 115 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 47 times.
|
56 | if (openstudio::pointInPolygon(point, m_outerPath, tol)) { |
| 116 | 9 | inside = true; | |
| 117 | } else { | ||
| 118 |
1/2✗ Branch 5 not taken.
✓ Branch 6 taken 47 times.
|
47 | for (const auto& innerPath : m_innerPaths) { |
| 119 | ✗ | if (openstudio::pointInPolygon(point, innerPath, tol)) { | |
| 120 | ✗ | inside = true; | |
| 121 | ✗ | break; | |
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | 56 | return inside; | |
| 126 | } | ||
| 127 | |||
| 128 | 32 | bool Polygon3d::within(const Point3d& point, double tol) const { | |
| 129 | 32 | bool inside = false; | |
| 130 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 28 times.
|
32 | if (openstudio::within(point, m_outerPath, tol)) { |
| 131 | 4 | inside = true; | |
| 132 |
1/2✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
|
4 | for (const auto& innerPath : m_innerPaths) { |
| 133 | ✗ | if (openstudio::within(point, innerPath, tol)) { | |
| 134 | ✗ | inside = false; | |
| 135 | ✗ | break; | |
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | 32 | return inside; | |
| 140 | } | ||
| 141 | |||
| 142 | ✗ | bool Polygon3d::inside(const Point3d& point, double tol) const { | |
| 143 | ✗ | return pointInPolygon(point, tol) || within(point, tol); | |
| 144 | } | ||
| 145 | |||
| 146 | ✗ | size_t Polygon3d::getSize() const { | |
| 147 | ✗ | return m_outerPath.size(); | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | Point3dVector Polygon3d::getEdge(size_t i) const { | |
| 151 | ✗ | Point3d p1 = m_outerPath[i]; | |
| 152 | ✗ | Point3d p2 = m_outerPath[(i + 1) % m_outerPath.size()]; | |
| 153 | ✗ | return Point3dVector{p1, p2}; | |
| 154 | ✗ | } | |
| 155 | |||
| 156 | 609 | std::vector<Point3dVector> Polygon3d::overlap(const Point3dVector& line) const { | |
| 157 | 609 | Point3dVectorVector result; | |
| 158 | |||
| 159 |
1/1✓ Branch 2 taken 609 times.
|
609 | Point3d sp = line[0]; |
| 160 |
1/1✓ Branch 3 taken 609 times.
|
609 | Vector3d v = line[1] - line[0]; |
| 161 |
2/2✓ Branch 1 taken 609 times.
✓ Branch 4 taken 609 times.
|
609 | double rad = std::atan2(v.y(), v.x()) - std::atan2(0, 1); |
| 162 | // Make a transform so that the line being compared starts at 0 and is x = length | ||
| 163 |
7/7✓ Branch 1 taken 609 times.
✓ Branch 4 taken 609 times.
✓ Branch 7 taken 609 times.
✓ Branch 10 taken 609 times.
✓ Branch 13 taken 609 times.
✓ Branch 16 taken 609 times.
✓ Branch 19 taken 609 times.
|
609 | Transformation t = Transformation::translation(Vector3d(sp.x(), sp.y(), 0)) * Transformation::rotation(Vector3d(0, 0, 1), rad); |
| 164 |
1/1✓ Branch 1 taken 609 times.
|
609 | Transformation ti = t.inverse(); |
| 165 |
1/1✓ Branch 1 taken 609 times.
|
609 | Point3dVector lbase = ti * line; |
| 166 | |||
| 167 | // Do a little swaperoo if the transformed line's arent along the +ve x axis | ||
| 168 |
3/4✓ Branch 2 taken 609 times.
✓ Branch 6 taken 609 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 609 times.
|
609 | if (lbase[1].x() < lbase[0].x()) { |
| 169 | ✗ | std::reverse(lbase.begin(), lbase.end()); | |
| 170 | } | ||
| 171 | |||
| 172 |
2/2✓ Branch 1 taken 7308 times.
✓ Branch 2 taken 609 times.
|
7917 | for (size_t i = 0; i < m_outerPath.size(); i++) { |
| 173 |
1/1✓ Branch 2 taken 7308 times.
|
7308 | Point3d pp1 = m_outerPath[i]; |
| 174 |
1/1✓ Branch 3 taken 7308 times.
|
7308 | Point3d pp2 = m_outerPath[(i + 1) % m_outerPath.size()]; |
| 175 | |||
| 176 |
1/1✓ Branch 2 taken 7308 times.
|
7308 | Point3d p1 = ti * m_outerPath[i]; |
| 177 |
1/1✓ Branch 3 taken 7308 times.
|
7308 | Point3d p2 = ti * m_outerPath[(i + 1) % m_outerPath.size()]; |
| 178 | |||
| 179 | // The two line segments must be parallel and coincident within tolerance | ||
| 180 |
8/8✓ Branch 1 taken 7308 times.
✓ Branch 4 taken 1008 times.
✓ Branch 5 taken 6300 times.
✓ Branch 7 taken 1008 times.
✓ Branch 10 taken 507 times.
✓ Branch 11 taken 501 times.
✓ Branch 12 taken 6807 times.
✓ Branch 13 taken 501 times.
|
7308 | if (std::abs(p1.y()) > 0.01 || std::abs(p2.y()) > 0.01) { |
| 181 | 6807 | continue; | |
| 182 | } | ||
| 183 | |||
| 184 | // Do a little swaperoo if the transformed line's arent along the +ve x axis | ||
| 185 | 501 | Point3dVector edge; | |
| 186 |
4/4✓ Branch 1 taken 501 times.
✓ Branch 4 taken 501 times.
✓ Branch 6 taken 369 times.
✓ Branch 7 taken 132 times.
|
501 | if (p2.x() > p1.x()) { |
| 187 |
1/1✓ Branch 1 taken 369 times.
|
369 | edge.push_back(p1); |
| 188 |
1/1✓ Branch 1 taken 369 times.
|
369 | edge.push_back(p2); |
| 189 | } else { | ||
| 190 |
1/1✓ Branch 1 taken 132 times.
|
132 | edge.push_back(p2); |
| 191 |
1/1✓ Branch 1 taken 132 times.
|
132 | edge.push_back(p1); |
| 192 | } | ||
| 193 | |||
| 194 | // Now we have two lines in the +ve x direction that are coincident and parallel but do they overlap? | ||
| 195 | //if (line1.ep.X - line2.sp.X >= 0 && line2.ep.X - line1.sp.X >= 0) { | ||
| 196 |
10/10✓ Branch 2 taken 501 times.
✓ Branch 6 taken 501 times.
✓ Branch 8 taken 381 times.
✓ Branch 9 taken 120 times.
✓ Branch 12 taken 381 times.
✓ Branch 16 taken 381 times.
✓ Branch 18 taken 255 times.
✓ Branch 19 taken 126 times.
✓ Branch 20 taken 255 times.
✓ Branch 21 taken 246 times.
|
501 | if ((lbase[1].x() - edge[0].x() >= 0) && (edge[1].x() - lbase[0].x() >= 0)) { |
| 197 |
3/3✓ Branch 2 taken 255 times.
✓ Branch 6 taken 255 times.
✓ Branch 10 taken 255 times.
|
255 | Point3d overlap1 = Point3d(std::max(edge[0].x(), lbase[0].x()), 0, 0); |
| 198 |
3/3✓ Branch 2 taken 255 times.
✓ Branch 6 taken 255 times.
✓ Branch 10 taken 255 times.
|
255 | Point3d overlap2 = Point3d(std::min(edge[1].x(), lbase[1].x()), 0, 0); |
| 199 |
3/3✓ Branch 1 taken 255 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 72 times.
|
255 | if (getDistance(overlap1, overlap2) > 0.01) { |
| 200 | 183 | Point3dVector overlap; | |
| 201 |
2/2✓ Branch 1 taken 183 times.
✓ Branch 4 taken 183 times.
|
183 | overlap.push_back(t * overlap1); |
| 202 |
2/2✓ Branch 1 taken 183 times.
✓ Branch 4 taken 183 times.
|
183 | overlap.push_back(t * overlap2); |
| 203 |
1/1✓ Branch 1 taken 183 times.
|
183 | result.push_back(overlap); |
| 204 | 183 | } | |
| 205 | 255 | } | |
| 206 | 27729 | } | |
| 207 | 1218 | return result; | |
| 208 | 609 | } | |
| 209 | |||
| 210 | } // namespace openstudio | ||
| 211 |