src/utilities/geometry/Polyhedron.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 "Polyhedron.hpp" | ||
| 7 | #include "Polygon3d.hpp" | ||
| 8 | |||
| 9 | #include "Geometry.hpp" | ||
| 10 | #include "Intersection.hpp" | ||
| 11 | #include "Plane.hpp" | ||
| 12 | #include "Point3d.hpp" | ||
| 13 | #include "Transformation.hpp" | ||
| 14 | #include "Vector3d.hpp" | ||
| 15 | |||
| 16 | #include <fmt/core.h> | ||
| 17 | |||
| 18 | #include <algorithm> | ||
| 19 | #include <numeric> | ||
| 20 | #include <utility> | ||
| 21 | #include <vector> | ||
| 22 | |||
| 23 | namespace openstudio { | ||
| 24 | |||
| 25 | ✗ | Surface3dEdge::Surface3dEdge(Point3d start, Point3d end, const Surface3d& firstSurface) | |
| 26 | ✗ | : m_start(std::move(start)), m_end(std::move(end)), m_firstSurfaceName(firstSurface.name) { | |
| 27 | ✗ | m_allSurfNums.push_back(firstSurface.surfNum); | |
| 28 | ✗ | } | |
| 29 | |||
| 30 | 49758 | Surface3dEdge::Surface3dEdge(Point3d start, Point3d end, std::string t_name, size_t t_surfNum) | |
| 31 | 49758 | : m_start(std::move(start)), m_end(std::move(end)), m_firstSurfaceName(std::move(t_name)) { | |
| 32 |
1/1✓ Branch 1 taken 49758 times.
|
49758 | m_allSurfNums.push_back(t_surfNum); |
| 33 | 49758 | } | |
| 34 | |||
| 35 | /// check equality | ||
| 36 | 839610 | bool Surface3dEdge::operator==(const Surface3dEdge& other) const { | |
| 37 |
2/2✓ Branch 2 taken 36119 times.
✓ Branch 3 taken 269 times.
|
875998 | return ((isAlmostEqual3dPt(m_start, other.m_start) && isAlmostEqual3dPt(m_end, other.m_end)) |
| 38 |
6/6✓ Branch 0 taken 36388 times.
✓ Branch 1 taken 803222 times.
✓ Branch 3 taken 36766 times.
✓ Branch 4 taken 802575 times.
✓ Branch 6 taken 17146 times.
✓ Branch 7 taken 19620 times.
|
875998 | || (isAlmostEqual3dPt(m_start, other.m_end) && isAlmostEqual3dPt(m_end, other.m_start))); |
| 39 | } | ||
| 40 | |||
| 41 | /// check inequality | ||
| 42 | ✗ | bool Surface3dEdge::operator!=(const Surface3dEdge& other) const { | |
| 43 | ✗ | return !(*this == other); | |
| 44 | } | ||
| 45 | |||
| 46 | 16999 | bool Surface3dEdge::reverseEqual(const Surface3dEdge& other) const { | |
| 47 |
3/4✓ Branch 1 taken 16830 times.
✓ Branch 2 taken 169 times.
✓ Branch 4 taken 16830 times.
✗ Branch 5 not taken.
|
16999 | return isAlmostEqual3dPt(m_start, other.m_end) && isAlmostEqual3dPt(m_end, other.m_start); |
| 48 | } | ||
| 49 | |||
| 50 | 28195 | const Point3d& Surface3dEdge::start() const { | |
| 51 | 28195 | return m_start; | |
| 52 | } | ||
| 53 | 34651 | const Point3d& Surface3dEdge::end() const { | |
| 54 | 34651 | return m_end; | |
| 55 | } | ||
| 56 | |||
| 57 | 102676 | size_t Surface3dEdge::count() const { | |
| 58 | 102676 | return m_allSurfNums.size(); | |
| 59 | } | ||
| 60 | |||
| 61 | 16997 | size_t Surface3dEdge::firstSurfNum() const { | |
| 62 | 16997 | return m_allSurfNums.front(); | |
| 63 | } | ||
| 64 | |||
| 65 | 1321 | std::string Surface3dEdge::firstSurfaceName() const { | |
| 66 | 1321 | return m_firstSurfaceName; | |
| 67 | } | ||
| 68 | |||
| 69 | 51059 | const std::vector<size_t>& Surface3dEdge::allSurfNums() const { | |
| 70 | 51059 | return m_allSurfNums; | |
| 71 | } | ||
| 72 | |||
| 73 | 338 | void Surface3dEdge::markConflictedOrientation() { | |
| 74 | 338 | m_conflictedOrientation = true; | |
| 75 | 338 | } | |
| 76 | |||
| 77 | 1646 | bool Surface3dEdge::hasConflictedOrientation() const { | |
| 78 | 1646 | return m_conflictedOrientation; | |
| 79 | } | ||
| 80 | |||
| 81 | 2362 | void Surface3dEdge::markCreated() { | |
| 82 | 2362 | m_hasBeenCreated = true; | |
| 83 | 2362 | } | |
| 84 | |||
| 85 | 228 | bool Surface3dEdge::hasBeenCreated() const { | |
| 86 | 228 | return m_hasBeenCreated; | |
| 87 | } | ||
| 88 | |||
| 89 | 33998 | void Surface3dEdge::appendSurface(const Surface3d& surface) { | |
| 90 | 33998 | m_allSurfNums.emplace_back(surface.surfNum); | |
| 91 | 33998 | } | |
| 92 | |||
| 93 | 10844 | void Surface3dEdge::resetEdgeMatching() { | |
| 94 |
1/1✓ Branch 5 taken 10844 times.
|
21688 | m_allSurfNums.erase(std::next(m_allSurfNums.begin()), m_allSurfNums.end()); |
| 95 | 10844 | m_conflictedOrientation = false; | |
| 96 | 10844 | } | |
| 97 | |||
| 98 | 423165 | bool Surface3dEdge::containsPoint(const Point3d& testVertex) const { | |
| 99 |
6/6✓ Branch 1 taken 407316 times.
✓ Branch 2 taken 15849 times.
✓ Branch 4 taken 391313 times.
✓ Branch 5 taken 16003 times.
✓ Branch 7 taken 1181 times.
✓ Branch 8 taken 390132 times.
|
423165 | return !isAlmostEqual3dPt(m_start, testVertex) && !isAlmostEqual3dPt(m_end, testVertex) && isPointOnLineBetweenPoints(m_start, m_end, testVertex); |
| 100 | } | ||
| 101 | |||
| 102 | 11244 | Vector3d Surface3dEdge::asVector() const { | |
| 103 | 11244 | return m_end - m_start; | |
| 104 | } | ||
| 105 | |||
| 106 | 1321 | std::ostream& operator<<(std::ostream& os, const Surface3dEdge& edge) { | |
| 107 | 1321 | os << "Surface3dEdge: start=" << edge.start() << ", end=" << edge.end() << ", count=" << edge.count() << ", firstSurface='" | |
| 108 |
3/3✓ Branch 2 taken 1321 times.
✓ Branch 5 taken 1321 times.
✓ Branch 8 taken 1321 times.
|
1321 | << edge.firstSurfaceName() << "'"; |
| 109 | 1321 | return os; | |
| 110 | } | ||
| 111 | |||
| 112 | 11829 | Surface3d::Surface3d(std::vector<Point3d> t_vertices, std::string t_name, size_t t_surfNum) | |
| 113 | 11829 | : vertices(std::move(t_vertices)), name(std::move(t_name)), surfNum(t_surfNum) { | |
| 114 | |||
| 115 |
1/1✓ Branch 2 taken 11829 times.
|
11829 | edges.reserve(vertices.size()); |
| 116 |
2/2✓ Branch 4 taken 49758 times.
✓ Branch 5 taken 11829 times.
|
61587 | for (auto it = vertices.begin(); it != vertices.end(); ++it) { |
| 117 | |||
| 118 | 49758 | auto itnext = std::next(it); | |
| 119 |
2/2✓ Branch 2 taken 11829 times.
✓ Branch 3 taken 37929 times.
|
99516 | if (itnext == std::end(vertices)) { |
| 120 | 23658 | itnext = std::begin(vertices); | |
| 121 | } | ||
| 122 | |||
| 123 |
1/1✓ Branch 3 taken 49758 times.
|
49758 | edges.emplace_back(*it, *itnext, name, surfNum); |
| 124 | } | ||
| 125 | 11829 | } | |
| 126 | |||
| 127 | ✗ | std::ostream& operator<<(std::ostream& os, const Surface3d& surface3d) { | |
| 128 | ✗ | os << "Surface3d "; | |
| 129 | ✗ | if (!surface3d.name.empty()) { | |
| 130 | ✗ | os << "'" << surface3d.name << "' "; | |
| 131 | } | ||
| 132 | ✗ | os << "= [\n"; | |
| 133 | ✗ | for (const auto& pt : surface3d.vertices) { | |
| 134 | ✗ | os << " " << pt << ",\n"; | |
| 135 | } | ||
| 136 | ✗ | os << "]"; | |
| 137 | ✗ | return os; | |
| 138 | } | ||
| 139 | |||
| 140 | 102 | bool Surface3d::operator<(const Surface3d& rhs) const { | |
| 141 | 102 | return this->name < rhs.name; | |
| 142 | } | ||
| 143 | |||
| 144 | 272 | size_t Surface3d::numConflictedEdges() const { | |
| 145 | 1598 | return std::count_if(edges.cbegin(), edges.cend(), [](const auto& edge) { return edge.hasConflictedOrientation(); }); | |
| 146 | } | ||
| 147 | |||
| 148 | 136 | double Surface3d::ratioOfConflictedEdges() const { | |
| 149 | 136 | return static_cast<double>(numConflictedEdges()) / static_cast<double>(edges.size()); | |
| 150 | } | ||
| 151 | |||
| 152 | 2269 | void Surface3d::resetEdgeMatching() { | |
| 153 |
2/2✓ Branch 5 taken 10844 times.
✓ Branch 6 taken 2269 times.
|
13113 | for (auto& edge : edges) { |
| 154 |
1/1✓ Branch 1 taken 10844 times.
|
10844 | edge.resetEdgeMatching(); |
| 155 | } | ||
| 156 | 2269 | } | |
| 157 | |||
| 158 | 5622 | bool Surface3d::isConvex() const { | |
| 159 | |||
| 160 | // for each triangle of 3 points, we compute the triangleNormal by taking the cross product of AB x AC. | ||
| 161 | // We then take the dot product with the initial outwardNormal. If the dot product is negative, this means the triangleNomal is pointing in the | ||
| 162 | // opposite direction and we therefore have a non convex surface | ||
| 163 | |||
| 164 | // const auto& a = edges.front().start(); | ||
| 165 | // const auto& b = edges.front().end(); | ||
| 166 | // const auto& c = edges.at(1).end(); | ||
| 167 | |||
| 168 |
1/1✓ Branch 2 taken 5622 times.
|
5622 | auto ab1 = edges.front().asVector(); |
| 169 |
1/1✓ Branch 2 taken 5622 times.
|
5622 | auto bc1 = edges[1].asVector(); |
| 170 |
1/1✓ Branch 1 taken 5622 times.
|
5622 | auto outwardNormal = ab1.cross(bc1); |
| 171 |
1/1✓ Branch 1 taken 5622 times.
|
5622 | outwardNormal.normalize(); |
| 172 | |||
| 173 |
2/2✓ Branch 4 taken 16665 times.
✓ Branch 5 taken 5323 times.
|
27610 | for (auto it = std::next(edges.begin()); it != edges.end(); ++it) { |
| 174 | 16665 | auto itnext = std::next(it); | |
| 175 |
2/2✓ Branch 2 taken 5323 times.
✓ Branch 3 taken 11342 times.
|
33330 | if (itnext == std::end(edges)) { |
| 176 | 10646 | itnext = std::begin(edges); | |
| 177 | } | ||
| 178 |
1/1✓ Branch 2 taken 16665 times.
|
16665 | const Point3d& a = it->start(); |
| 179 |
1/1✓ Branch 2 taken 16665 times.
|
16665 | const Point3d& b = it->end(); |
| 180 |
1/1✓ Branch 2 taken 16665 times.
|
16665 | const Point3d& c = itnext->end(); |
| 181 | |||
| 182 |
1/1✓ Branch 1 taken 16665 times.
|
16665 | auto ab = b - a; |
| 183 |
1/1✓ Branch 1 taken 16665 times.
|
16665 | auto ac = c - a; |
| 184 | |||
| 185 |
1/1✓ Branch 1 taken 16665 times.
|
16665 | auto triangleNormal = ab.cross(ac); |
| 186 |
1/1✓ Branch 1 taken 16665 times.
|
16665 | auto d = outwardNormal.dot(triangleNormal); |
| 187 |
2/2✓ Branch 0 taken 299 times.
✓ Branch 1 taken 16366 times.
|
16665 | if (d < 0) { |
| 188 | 299 | return false; | |
| 189 | } | ||
| 190 | 17263 | } | |
| 191 | 5323 | return true; | |
| 192 | 5622 | } | |
| 193 | |||
| 194 | 877 | Polyhedron::Polyhedron(std::vector<Surface3d> surfaces) : m_surfaces(std::move(surfaces)) { | |
| 195 |
1/1✓ Branch 1 taken 877 times.
|
877 | performEdgeMatching(); |
| 196 | |||
| 197 | // if all edges had two counts then it is fully enclosed | ||
| 198 |
3/3✓ Branch 1 taken 877 times.
✓ Branch 3 taken 622 times.
✓ Branch 4 taken 255 times.
|
877 | if (!hasEdgesNot2()) { |
| 199 | 622 | m_isEnclosedVolume = true; | |
| 200 | } else { | ||
| 201 |
5/5✓ Branch 1 taken 255 times.
✓ Branch 4 taken 255 times.
✓ Branch 7 taken 255 times.
✓ Branch 10 taken 255 times.
✓ Branch 13 taken 255 times.
|
255 | LOG(Warn, "Polyhedron is not enclosed in original testing. Trying to add missing colinear points."); |
| 202 |
1/1✓ Branch 1 taken 255 times.
|
255 | updateZonePolygonsForMissingColinearPoints(); |
| 203 | } | ||
| 204 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 842 times.
|
877 | if (!m_isEnclosedVolume) { |
| 205 |
5/5✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
✓ Branch 7 taken 35 times.
✓ Branch 10 taken 35 times.
✓ Branch 13 taken 35 times.
|
35 | LOG(Warn, "Polyhedron is not enclosed."); |
| 206 | } else { | ||
| 207 | // We call the volume calculation... if this ends up being negative, we're in the case where ALL surfaces are in the wrong orientation | ||
| 208 |
2/2✓ Branch 0 taken 825 times.
✓ Branch 1 taken 17 times.
|
842 | if (!m_hasAnySurfaceWithIncorrectOrientation) { |
| 209 |
1/1✓ Branch 1 taken 825 times.
|
825 | m_polyhedronVolume = calcPolyhedronVolume(); |
| 210 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 819 times.
|
825 | if (m_polyhedronVolume < 0) { |
| 211 |
5/5✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
|
6 | LOG(Error, "It seems that ALL surfaces are reversed"); |
| 212 | 6 | m_hasAnySurfaceWithIncorrectOrientation = true; | |
| 213 | 6 | m_isCompletelyInsideOut = true; | |
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | 877 | }; | |
| 218 | |||
| 219 | 1099 | bool Polyhedron::hasEdgesNot2() const { | |
| 220 | 1099 | return std::any_of(m_surfaces.cbegin(), m_surfaces.cend(), [](const auto& surface) { | |
| 221 | 33479 | return std::any_of(surface.edges.cbegin(), surface.edges.cend(), [](const auto& edge) { return edge.count() != 2; }); | |
| 222 | 1099 | }); | |
| 223 | } | ||
| 224 | |||
| 225 | 240 | bool Polyhedron::hasAnySurfaceWithIncorrectOrientation() const { | |
| 226 | 240 | return m_hasAnySurfaceWithIncorrectOrientation; | |
| 227 | } | ||
| 228 | |||
| 229 | ✗ | bool Polyhedron::hasAddedColinearPoints() const { | |
| 230 | ✗ | return m_hasAddedColinearPoints; | |
| 231 | } | ||
| 232 | |||
| 233 | 843 | bool Polyhedron::isEnclosedVolume() const { | |
| 234 | 843 | return m_isEnclosedVolume; | |
| 235 | } | ||
| 236 | |||
| 237 | 238 | bool Polyhedron::isCompletelyInsideOut() const { | |
| 238 | 238 | return m_isCompletelyInsideOut; | |
| 239 | } | ||
| 240 | |||
| 241 | 1099 | void Polyhedron::performEdgeMatching() { | |
| 242 | |||
| 243 | 1099 | m_hasAnySurfaceWithIncorrectOrientation = false; | |
| 244 | |||
| 245 | // We use **Combinations** (rather than Permutations) to avoid traversing unnecessarily | ||
| 246 |
2/2✓ Branch 1 taken 8493 times.
✓ Branch 2 taken 1099 times.
|
9592 | for (size_t i = 0; i < m_surfaces.size(); ++i) { |
| 247 |
2/2✓ Branch 1 taken 39753 times.
✓ Branch 2 taken 8493 times.
|
48246 | for (size_t j = i + 1; j < m_surfaces.size(); ++j) { |
| 248 | 39753 | auto& surface1 = m_surfaces[i]; | |
| 249 | 39753 | auto& surface2 = m_surfaces[j]; | |
| 250 |
2/2✓ Branch 5 taken 179188 times.
✓ Branch 6 taken 39753 times.
|
218941 | for (Surface3dEdge& edge1 : surface1.edges) { |
| 251 |
2/2✓ Branch 5 taken 830282 times.
✓ Branch 6 taken 179188 times.
|
1009470 | for (Surface3dEdge& edge2 : surface2.edges) { |
| 252 |
3/3✓ Branch 1 taken 830282 times.
✓ Branch 3 taken 16997 times.
✓ Branch 4 taken 813285 times.
|
830282 | if (edge1 == edge2) { |
| 253 |
6/7✓ Branch 1 taken 16997 times.
✓ Branch 5 taken 16997 times.
✓ Branch 8 taken 16997 times.
✓ Branch 12 taken 16997 times.
✓ Branch 16 taken 16997 times.
✓ Branch 19 taken 16997 times.
✗ Branch 20 not taken.
|
16997 | if (std::find(edge1.allSurfNums().begin(), edge1.allSurfNums().cend(), edge2.firstSurfNum()) == edge1.allSurfNums().end()) { |
| 254 | // appendSurface will allow use to check edge.count() later to check if count == 2. | ||
| 255 | // All edges must be count == 2 in an Enclosed Polyhedron | ||
| 256 |
1/1✓ Branch 1 taken 16997 times.
|
16997 | edge1.appendSurface(surface2); |
| 257 |
1/1✓ Branch 1 taken 16997 times.
|
16997 | edge2.appendSurface(surface1); |
| 258 | // In a Polyhedron that has a consistent orientation (typically all faces are in counter clockwise order), | ||
| 259 | // each edge must be matched by an edge in the **opposite** direction. If not, mark conflicted | ||
| 260 |
3/3✓ Branch 1 taken 16997 times.
✓ Branch 3 taken 169 times.
✓ Branch 4 taken 16828 times.
|
16997 | if (!edge1.reverseEqual(edge2)) { |
| 261 |
1/1✓ Branch 1 taken 169 times.
|
169 | edge1.markConflictedOrientation(); |
| 262 |
1/1✓ Branch 1 taken 169 times.
|
169 | edge2.markConflictedOrientation(); |
| 263 | 169 | m_hasAnySurfaceWithIncorrectOrientation = true; | |
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | // #5002 - special case to find edges that are used to "cut" in to a surface to remove an interior hole | ||
| 273 | // we allow these edges to double count the first surface since they bound the same surface on two sides | ||
| 274 |
2/2✓ Branch 5 taken 8493 times.
✓ Branch 6 taken 1099 times.
|
9592 | for (auto& surface : m_surfaces) { |
| 275 | 8493 | auto& edges = surface.edges; | |
| 276 |
2/2✓ Branch 1 taken 36675 times.
✓ Branch 2 taken 8493 times.
|
45168 | for (size_t i = 0; i < edges.size(); ++i) { |
| 277 |
2/2✓ Branch 1 taken 69052 times.
✓ Branch 2 taken 36675 times.
|
105727 | for (size_t j = i + 1; j < edges.size(); ++j) { |
| 278 |
13/14✓ Branch 2 taken 69052 times.
✓ Branch 4 taken 4612 times.
✓ Branch 5 taken 64440 times.
✓ Branch 8 taken 4612 times.
✓ Branch 10 taken 1786 times.
✓ Branch 11 taken 2826 times.
✓ Branch 15 taken 1786 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 1784 times.
✓ Branch 22 taken 2 times.
✓ Branch 24 taken 2 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 2 times.
✓ Branch 27 taken 69050 times.
|
69052 | if ((edges[i].count() == 1) && (edges[j].count() == 1) && (edges[i] == edges[j]) && edges[i].reverseEqual(edges[j])) { |
| 279 | // appendSurface will allow use to check edge.count() later to check if count == 2. | ||
| 280 | // All edges must be count == 2 in an Enclosed Polyhedron | ||
| 281 |
1/1✓ Branch 2 taken 2 times.
|
2 | edges[i].appendSurface(surface); |
| 282 |
1/1✓ Branch 2 taken 2 times.
|
2 | edges[j].appendSurface(surface); |
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | 1099 | } | |
| 288 | |||
| 289 | 222 | void Polyhedron::resetEdgeMatching() { | |
| 290 |
2/2✓ Branch 5 taken 2269 times.
✓ Branch 6 taken 222 times.
|
2491 | for (auto& surface : m_surfaces) { |
| 291 |
1/1✓ Branch 1 taken 2269 times.
|
2269 | surface.resetEdgeMatching(); |
| 292 | } | ||
| 293 | 222 | m_hasAnySurfaceWithIncorrectOrientation = false; | |
| 294 | 222 | } | |
| 295 | |||
| 296 | 255 | void Polyhedron::updateZonePolygonsForMissingColinearPoints() { | |
| 297 |
1/1✓ Branch 1 taken 255 times.
|
255 | const std::vector<Point3d> uniqVertices = uniqueVertices(); |
| 298 | |||
| 299 | 255 | bool anyInserted = false; | |
| 300 | |||
| 301 |
2/2✓ Branch 5 taken 2398 times.
✓ Branch 6 taken 255 times.
|
2653 | for (auto& surface : m_surfaces) { |
| 302 |
5/5✓ Branch 1 taken 2398 times.
✓ Branch 4 taken 2398 times.
✓ Branch 7 taken 2398 times.
✓ Branch 10 taken 2398 times.
✓ Branch 13 taken 2398 times.
|
2398 | LOG(Debug, surface.name) |
| 303 | 2398 | bool insertedVertext = true; | |
| 304 |
2/2✓ Branch 0 taken 3579 times.
✓ Branch 1 taken 2398 times.
|
5977 | while (insertedVertext) { |
| 305 | 3579 | insertedVertext = false; | |
| 306 | |||
| 307 |
2/2✓ Branch 4 taken 16280 times.
✓ Branch 5 taken 2398 times.
|
18678 | for (auto it = surface.edges.begin(); it != surface.edges.end(); ++it) { |
| 308 | |||
| 309 | // now go through all the vertices and see if they are colinear with start and end vertices | ||
| 310 |
2/2✓ Branch 5 taken 423165 times.
✓ Branch 6 taken 15099 times.
|
438264 | for (const auto& testVertex : uniqVertices) { |
| 311 |
4/4✓ Branch 2 taken 423165 times.
✓ Branch 5 taken 423165 times.
✓ Branch 9 taken 1181 times.
✓ Branch 10 taken 421984 times.
|
846330 | if (const boost::optional<Surface3dEdge> newEdge = it->splitEdge(testVertex)) { |
| 312 |
7/7✓ Branch 1 taken 1181 times.
✓ Branch 4 taken 1181 times.
✓ Branch 7 taken 1181 times.
✓ Branch 11 taken 1181 times.
✓ Branch 14 taken 1181 times.
✓ Branch 17 taken 1181 times.
✓ Branch 20 taken 1181 times.
|
1181 | LOG(Debug, testVertex << " is on " << *it); |
| 313 | 1181 | auto itnext = std::next(it); | |
| 314 |
2/2✓ Branch 2 taken 257 times.
✓ Branch 3 taken 924 times.
|
2362 | if (itnext == std::end(surface.edges)) { |
| 315 | 514 | itnext = std::begin(surface.edges); | |
| 316 | } | ||
| 317 | 1181 | auto before_pos = std::distance(surface.edges.begin(), itnext); | |
| 318 |
2/2✓ Branch 1 taken 1181 times.
✓ Branch 5 taken 1181 times.
|
1181 | surface.edges.insert(itnext, newEdge.get()); |
| 319 |
1/1✓ Branch 3 taken 1181 times.
|
2362 | surface.vertices.insert(std::next(surface.vertices.begin(), before_pos), testVertex); |
| 320 | |||
| 321 | 1181 | insertedVertext = true; | |
| 322 | 1181 | anyInserted = true; | |
| 323 | 1181 | break; | |
| 324 | 423165 | } | |
| 325 | } | ||
| 326 | // Break out of the surface loop too, and start again at the while | ||
| 327 |
2/2✓ Branch 0 taken 1181 times.
✓ Branch 1 taken 15099 times.
|
16280 | if (insertedVertext) { |
| 328 | 1181 | break; | |
| 329 | } | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 33 times.
|
255 | if (anyInserted) { |
| 335 |
1/1✓ Branch 1 taken 222 times.
|
222 | resetEdgeMatching(); |
| 336 |
1/1✓ Branch 1 taken 222 times.
|
222 | performEdgeMatching(); |
| 337 | 222 | m_hasAddedColinearPoints = true; | |
| 338 |
1/1✓ Branch 1 taken 222 times.
|
222 | m_isEnclosedVolume = !hasEdgesNot2(); |
| 339 | } | ||
| 340 | 255 | } | |
| 341 | |||
| 342 | 308 | size_t Polyhedron::numVertices() const { | |
| 343 | 3008 | return std::accumulate(m_surfaces.cbegin(), m_surfaces.cend(), 0, [](size_t sum, const auto& surface) { return sum + surface.edges.size(); }); | |
| 344 | } | ||
| 345 | |||
| 346 | 256 | std::vector<Point3d> Polyhedron::uniqueVertices() const { | |
| 347 | |||
| 348 | 256 | std::vector<Point3d> uniqVertices; | |
| 349 |
2/2✓ Branch 1 taken 256 times.
✓ Branch 4 taken 256 times.
|
256 | uniqVertices.reserve(numVertices()); |
| 350 | |||
| 351 |
2/2✓ Branch 5 taken 2405 times.
✓ Branch 6 taken 256 times.
|
2661 | for (const auto& surface : m_surfaces) { |
| 352 |
2/2✓ Branch 5 taken 10209 times.
✓ Branch 6 taken 2405 times.
|
12614 | for (const auto& edge : surface.edges) { |
| 353 |
1/1✓ Branch 1 taken 10209 times.
|
10209 | const auto& pt = edge.start(); |
| 354 |
1/1✓ Branch 4 taken 10209 times.
|
123322 | if (std::find_if(uniqVertices.cbegin(), uniqVertices.cend(), [&pt](const auto& unqV) { return isAlmostEqual3dPt(pt, unqV); }) |
| 355 |
2/2✓ Branch 2 taken 3811 times.
✓ Branch 3 taken 6398 times.
|
20418 | == uniqVertices.cend()) { |
| 356 |
1/1✓ Branch 1 taken 3811 times.
|
3811 | uniqVertices.push_back(pt); |
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | 256 | return uniqVertices; | |
| 361 | ✗ | } | |
| 362 | |||
| 363 | 17 | std::vector<Surface3dEdge> Polyhedron::uniqueEdges() const { | |
| 364 | |||
| 365 | 17 | std::vector<Surface3dEdge> uniqueSurface3dEdges; | |
| 366 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
|
17 | uniqueSurface3dEdges.reserve(numVertices()); |
| 367 | |||
| 368 | // construct list of unique edges | ||
| 369 |
2/2✓ Branch 5 taken 141 times.
✓ Branch 6 taken 17 times.
|
158 | for (const auto& surface : m_surfaces) { |
| 370 |
2/2✓ Branch 5 taken 640 times.
✓ Branch 6 taken 141 times.
|
781 | for (const Surface3dEdge& thisSurface3dEdge : surface.edges) { |
| 371 |
1/1✓ Branch 3 taken 640 times.
|
640 | auto itFound = std::find(uniqueSurface3dEdges.begin(), uniqueSurface3dEdges.end(), thisSurface3dEdge); |
| 372 |
2/2✓ Branch 2 taken 320 times.
✓ Branch 3 taken 320 times.
|
640 | if (itFound == uniqueSurface3dEdges.end()) { |
| 373 |
1/1✓ Branch 1 taken 320 times.
|
320 | uniqueSurface3dEdges.push_back(thisSurface3dEdge); |
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | 17 | return uniqueSurface3dEdges; | |
| 379 | ✗ | } | |
| 380 | |||
| 381 | 33 | std::vector<Surface3dEdge> Polyhedron::edgesNotTwo(bool includeCreatedEdges) const { | |
| 382 | |||
| 383 | 33 | std::vector<Surface3dEdge> edgesNotTwo; | |
| 384 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
|
33 | edgesNotTwo.reserve(numVertices()); |
| 385 | |||
| 386 | // All edges for an enclosed polyhedron should be shared by two (and only two) side | ||
| 387 |
2/2✓ Branch 5 taken 140 times.
✓ Branch 6 taken 33 times.
|
173 | for (const auto& surface : m_surfaces) { |
| 388 |
2/2✓ Branch 5 taken 582 times.
✓ Branch 6 taken 140 times.
|
722 | for (const Surface3dEdge& thisSurface3dEdge : surface.edges) { |
| 389 | // only return a list of those edges that appear in both the original edge and the revised edges: | ||
| 390 | // this eliminates added edges that will confuse users (edges that were caught by the updateZonePolygonsForMissingColinearPoints routine) | ||
| 391 |
8/10✓ Branch 1 taken 582 times.
✓ Branch 3 taken 228 times.
✓ Branch 4 taken 354 times.
✓ Branch 5 taken 228 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 228 times.
✓ Branch 10 taken 228 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 228 times.
✓ Branch 13 taken 354 times.
|
582 | if ((thisSurface3dEdge.count() != 2) && (includeCreatedEdges || !thisSurface3dEdge.hasBeenCreated())) { |
| 392 |
1/1✓ Branch 3 taken 228 times.
|
228 | auto itFound = std::find(edgesNotTwo.begin(), edgesNotTwo.end(), thisSurface3dEdge); |
| 393 |
2/2✓ Branch 2 taken 132 times.
✓ Branch 3 taken 96 times.
|
228 | if (itFound == edgesNotTwo.end()) { |
| 394 |
1/1✓ Branch 1 taken 132 times.
|
132 | edgesNotTwo.push_back(thisSurface3dEdge); |
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 |
2/2✓ Branch 5 taken 132 times.
✓ Branch 6 taken 33 times.
|
165 | for (const auto& edge : edgesNotTwo) { |
| 400 |
5/5✓ Branch 1 taken 132 times.
✓ Branch 4 taken 132 times.
✓ Branch 7 taken 132 times.
✓ Branch 10 taken 132 times.
✓ Branch 13 taken 132 times.
|
132 | LOG(Debug, edge); |
| 401 | } | ||
| 402 | |||
| 403 | 33 | return edgesNotTwo; | |
| 404 | ✗ | } | |
| 405 | |||
| 406 | 423165 | boost::optional<Surface3dEdge> Surface3dEdge::splitEdge(Point3d testVertex) { | |
| 407 |
2/2✓ Branch 1 taken 1181 times.
✓ Branch 2 taken 421984 times.
|
423165 | if (containsPoint(testVertex)) { |
| 408 |
1/1✓ Branch 1 taken 1181 times.
|
1181 | Surface3dEdge newEdge(*this); |
| 409 |
1/1✓ Branch 1 taken 1181 times.
|
1181 | this->m_end = testVertex; |
| 410 | 1181 | newEdge.m_start = std::move(testVertex); | |
| 411 |
1/1✓ Branch 1 taken 1181 times.
|
1181 | this->markCreated(); |
| 412 |
1/1✓ Branch 1 taken 1181 times.
|
1181 | newEdge.markCreated(); |
| 413 |
1/1✓ Branch 1 taken 1181 times.
|
1181 | return newEdge; |
| 414 | 1181 | } | |
| 415 | |||
| 416 | 421984 | return boost::none; | |
| 417 | } | ||
| 418 | |||
| 419 | 592 | std::vector<Surface3d> Polyhedron::findSurfacesWithIncorrectOrientation() const { | |
| 420 | |||
| 421 |
2/2✓ Branch 0 taken 571 times.
✓ Branch 1 taken 21 times.
|
592 | if (!m_hasAnySurfaceWithIncorrectOrientation) { |
| 422 | 571 | return {}; | |
| 423 | } | ||
| 424 | |||
| 425 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
|
21 | if (m_isCompletelyInsideOut) { |
| 426 |
5/5✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
|
4 | LOG(Error, "It seems that ALL surfaces are reversed"); |
| 427 |
1/1✓ Branch 1 taken 4 times.
|
4 | return m_surfaces; |
| 428 | } | ||
| 429 | |||
| 430 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
|
17 | if (!m_isEnclosedVolume) { |
| 431 |
5/5✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
|
4 | LOG(Warn, "Polyhedron is not enclosed. Looking surfaces with incorrect orientations can return false negatives."); |
| 432 | // return {}; | ||
| 433 | } | ||
| 434 | |||
| 435 |
1/1✓ Branch 1 taken 17 times.
|
17 | std::vector<Surface3dEdge> uniqueSurface3dEdges = uniqueEdges(); |
| 436 | |||
| 437 | // Remove non-conflicted edges (Note: this also removes any edge that doesn't have at least one match... since these can't be conflicted) | ||
| 438 |
1/1✓ Branch 3 taken 17 times.
|
34 | uniqueSurface3dEdges.erase( |
| 439 |
1/1✓ Branch 4 taken 17 times.
|
337 | std::remove_if(uniqueSurface3dEdges.begin(), uniqueSurface3dEdges.end(), [](const auto& edge) { return !edge.hasConflictedOrientation(); }), |
| 440 | 17 | uniqueSurface3dEdges.end()); | |
| 441 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
|
17 | if (uniqueSurface3dEdges.empty()) { |
| 442 | ✗ | return {}; | |
| 443 | } | ||
| 444 | |||
| 445 | 17 | std::set<Surface3d> conflictedSurfaces; | |
| 446 |
2/2✓ Branch 5 taken 68 times.
✓ Branch 6 taken 17 times.
|
85 | for (const auto& edge : uniqueSurface3dEdges) { |
| 447 |
1/1✓ Branch 1 taken 68 times.
|
68 | const auto& surfNums = edge.allSurfNums(); |
| 448 | 68 | const size_t sfIndex1 = surfNums.front(); | |
| 449 | 68 | const size_t sfIndex2 = surfNums.back(); | |
| 450 | |||
| 451 |
1/1✓ Branch 1 taken 68 times.
|
68 | const auto& sf1 = m_surfaces.at(sfIndex1); |
| 452 |
1/1✓ Branch 1 taken 68 times.
|
68 | const auto& sf2 = m_surfaces.at(sfIndex2); |
| 453 | |||
| 454 |
1/1✓ Branch 1 taken 68 times.
|
68 | const auto c1 = sf1.ratioOfConflictedEdges(); |
| 455 |
1/1✓ Branch 1 taken 68 times.
|
68 | const auto c2 = sf2.ratioOfConflictedEdges(); |
| 456 | |||
| 457 |
7/7✓ Branch 1 taken 68 times.
✓ Branch 5 taken 68 times.
✓ Branch 9 taken 68 times.
✓ Branch 13 taken 68 times.
✓ Branch 17 taken 68 times.
✓ Branch 20 taken 68 times.
✓ Branch 23 taken 68 times.
|
136 | LOG(Trace, fmt::format("'{}' has {:.2f}% conflicted edges ({}/{}), while '{}' has {:.2f}% conflicted edges ({}/{})", sf1.name, c1, |
| 458 | sf1.numConflictedEdges(), sf1.edges.size(), sf2.name, c2, sf2.numConflictedEdges(), sf2.edges.size())); | ||
| 459 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 39 times.
|
68 | if (c1 > c2) { |
| 460 |
1/1✓ Branch 1 taken 29 times.
|
29 | conflictedSurfaces.insert(sf1); |
| 461 | } else { | ||
| 462 |
1/1✓ Branch 1 taken 39 times.
|
39 | conflictedSurfaces.insert(sf2); |
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 |
1/1✓ Branch 3 taken 17 times.
|
34 | return {conflictedSurfaces.begin(), conflictedSurfaces.end()}; |
| 467 | 17 | } | |
| 468 | |||
| 469 | // boost::optional<double> Polyhedron::calculatedVolume() const { | ||
| 470 | // if (m_m_isEnclosedVolume) { | ||
| 471 | // return calcPolyhedronVolume(); | ||
| 472 | // } | ||
| 473 | // return boost::none; | ||
| 474 | // } | ||
| 475 | |||
| 476 | 825 | double Polyhedron::calcPolyhedronVolume() const { | |
| 477 | |||
| 478 |
1/1✓ Branch 1 taken 825 times.
|
825 | const Point3d p0{}; |
| 479 | 825 | double volume = 0.0; | |
| 480 |
2/2✓ Branch 5 taken 5932 times.
✓ Branch 6 taken 825 times.
|
6757 | for (const auto& surface : m_surfaces) { |
| 481 | 5932 | const std::vector<Point3d>& vertices = surface.vertices; | |
| 482 |
1/1✓ Branch 2 taken 5932 times.
|
5932 | const Vector3d p3FaceOrigin = vertices[1] - p0; |
| 483 |
1/1✓ Branch 1 taken 5932 times.
|
5932 | boost::optional<Vector3d> newellAreaVector = getNewellVector(vertices); |
| 484 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5932 times.
|
5932 | if (!newellAreaVector) { |
| 485 | ✗ | LOG_AND_THROW("Cannot compute newellAreaVector"); | |
| 486 | } | ||
| 487 |
2/2✓ Branch 1 taken 5932 times.
✓ Branch 4 taken 5932 times.
|
5932 | const double pyramidVolume = newellAreaVector->dot(p3FaceOrigin); |
| 488 | 5932 | volume += pyramidVolume; | |
| 489 | 5932 | } | |
| 490 | // for (int NFace = 1; NFace <= Poly.NumSurfaceFaces; ++NFace) { | ||
| 491 | // p3FaceOrigin = Poly.SurfaceFace(NFace).FacePoints(2); | ||
| 492 | // double pyramidVolume = dot(Poly.SurfaceFace(NFace).NewellAreaVector, (p3FaceOrigin - state.dataVectors->p0)); | ||
| 493 | // Volume += PyramidVolume / 3.0; | ||
| 494 | // } | ||
| 495 | 825 | return volume / 6.0; // Our newellArea vector has twice the length | |
| 496 | 825 | } | |
| 497 | |||
| 498 | 239 | double Polyhedron::polyhedronVolume() const { | |
| 499 |
4/6✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 237 times.
✗ Branch 5 not taken.
|
239 | if (m_isEnclosedVolume && (m_isCompletelyInsideOut || !m_hasAnySurfaceWithIncorrectOrientation)) { |
| 500 | 239 | return m_polyhedronVolume; | |
| 501 | } | ||
| 502 | ✗ | if (!m_isEnclosedVolume) { | |
| 503 | ✗ | LOG(Error, "Polyhedron volume calculation for a non-enclosed Polyhedron will return bogus values"); | |
| 504 | ✗ | } else if (m_hasAnySurfaceWithIncorrectOrientation && !m_isCompletelyInsideOut) { | |
| 505 | ✗ | LOG(Error, "Polyhedron volume calculation for an enclosed Polyhedron but with incorrectly oriented surfaces will return bogus values"); | |
| 506 | } | ||
| 507 | ✗ | return calcPolyhedronVolume(); | |
| 508 | } | ||
| 509 | |||
| 510 | 3 | double Polyhedron::calcDivergenceTheoremVolume() const { | |
| 511 | 3 | double volume = 0.0; | |
| 512 |
2/2✓ Branch 4 taken 24 times.
✓ Branch 5 taken 3 times.
|
27 | for (const auto& surface : m_surfaces) { |
| 513 | 24 | const std::vector<Point3d>& vertices = surface.vertices; | |
| 514 |
1/1✓ Branch 1 taken 24 times.
|
24 | const Plane plane(vertices); |
| 515 |
1/1✓ Branch 1 taken 24 times.
|
24 | boost::optional<double> area = getArea(vertices); |
| 516 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if (!area) { |
| 517 | ✗ | LOG_AND_THROW("Cannot compute area for " << surface.name); | |
| 518 | } | ||
| 519 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
|
24 | volume -= plane.d() * area.get() / 3.0; |
| 520 | } | ||
| 521 | 3 | return volume; | |
| 522 | } | ||
| 523 | |||
| 524 | ✗ | std::vector<Surface3d> Polyhedron::surface3ds() const { | |
| 525 | ✗ | return m_surfaces; | |
| 526 | } | ||
| 527 | |||
| 528 | } // namespace openstudio | ||
| 529 |