Skip to content

Commit

Permalink
fix: correct bounds for geometries (#35)
Browse files Browse the repository at this point in the history
* Get correct bounds on geometries

* Add a unit test for combining the edges
  • Loading branch information
rprospero authored Jun 19, 2024
1 parent 219498a commit 5f8b997
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/plotGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2024 Team Dissolve and contributors

#include "plotGeometry.h"
#include <algorithm>

PlotGeometry::PlotGeometry() : xAxis_(nullptr), yAxis_(nullptr)
{
Expand Down Expand Up @@ -43,9 +44,13 @@ void PlotGeometry::updateData()
p = t.writeByteArray(p);
}

std::vector<Edge> boxes(ts.size());
std::transform(ts.begin(), ts.end(), boxes.begin(), [](const auto x) { return x.bounds(); });
auto bounds = std::reduce(boxes.begin(), boxes.end(), boxes[0], [](const auto a, const auto b) { return a.combine(b); });

setVertexData(vertexData);
setStride(stride);
setBounds(QVector3D(-1.0f, -1.0f, 0.0f), QVector3D(+1.0f, +1.0f, 0.0f));
setBounds(QVector3D(bounds.start.x, bounds.start.y, bounds.start.z), QVector3D(bounds.end.x, bounds.end.y, bounds.end.z));

// setPrimitiveType(QQuick3DGeometry::PrimitiveType::TriangleStrip);

Expand All @@ -58,15 +63,6 @@ std::vector<Triangle> PlotGeometry::faces_([[maybe_unused]] std::vector<Point> p

bool outOfBounds(const Point &p) { return p.x < -1 || p.x > 1 || p.y < -1 || p.y > 1 || p.z < -1 || p.z > 1; }

/** A line segment between two vertices of a polygon */
struct Edge
{
/** The beginning point of the edge */
Point start;
/** The stopping point of the edge */
Point end;
};

std::optional<Edge> clipEdge(const Edge &e)
{
// Check if edge is entirely in within bounds
Expand Down
27 changes: 27 additions & 0 deletions src/triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,41 @@
// Copyright (c) 2024 Team Dissolve and contributors

#include "triangle.h"
#include <algorithm>

Point::Point(float a, float b, float c) : x(a), y(b), z(c) {}

bool Point::operator==(const Point &other) { return x == other.x && y == other.y && z == other.z; }
bool Point::operator!=(const Point &other) { return x != other.x || y != other.y || z != other.z; }

Edge Edge::combine(const Edge &other) const
{
Edge result;
result.start.x = std::min(start.x, other.start.x);
result.start.y = std::min(start.y, other.start.y);
result.start.z = std::min(start.z, other.start.z);
result.end.x = std::max(end.x, other.end.x);
result.end.y = std::max(end.y, other.end.y);
result.end.z = std::max(end.z, other.end.z);
return result;
}

Triangle::Triangle(Point i, Point j, Point k) : a(i), b(j), c(k) {}

Edge Triangle::bounds() const
{
Edge result;
result.start.x = std::min({a.x, b.x, c.x});
result.start.y = std::min({a.y, b.y, c.y});
result.start.z = std::min({a.z, b.z, c.z});

result.end.x = std::max({a.x, b.x, c.x});
result.end.y = std::max({a.y, b.y, c.y});
result.end.z = std::max({a.z, b.z, c.z});

return result;
}

float *Triangle::writeByteArray(float *p)
{
*p++ = a.x;
Expand Down
15 changes: 15 additions & 0 deletions src/triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class Point
bool operator!=(const Point &other);
};

/** A line segment between two vertices of a polygon */
class Edge
{
public:
/** The beginning point of the edge */
Point start;
/** The stopping point of the edge */
Point end;
/** Treat two edges as the corners of a bounding box and return
the bouning box of their union. */
Edge combine(const Edge &other) const;
};

/** An individual triangle face in the mesh. */
class Triangle
{
Expand All @@ -34,4 +47,6 @@ class Triangle
have enough space to write the vertex.
*/
float *writeByteArray(float *p);
/** find the bounding box of the triangle and return as the diagonal from min to max */
Edge bounds() const;
};
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_policy(SET CMP0054 NEW)
find_package(GTest REQUIRED)
include(GoogleTest)

set(SOURCES axis.cpp)
set(SOURCES axis.cpp triangles.cpp)
add_executable(axis ${SOURCES})

target_include_directories(
Expand Down
40 changes: 40 additions & 0 deletions tests/triangles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2024 Team Dissolve and contributors

#include "triangle.h"
#include <gtest/gtest.h>
#include <numeric>

namespace UnitTest
{

TEST(TriangleTest, Combine)
{
std::vector<Triangle> ts = {{{-1, 5, 9}, {4, 8, 3}, {-7, 2, 6}}, {{1, -2, -3}, {-4, -8, -6}, {7, -5, -9}}};

std::vector<Edge> boxes(ts.size());
std::transform(ts.begin(), ts.end(), boxes.begin(), [](const auto x) { return x.bounds(); });
EXPECT_EQ(boxes[1].start.x, -4);
EXPECT_EQ(boxes[1].start.y, -8);
EXPECT_EQ(boxes[1].start.z, -9);
EXPECT_EQ(boxes[1].end.x, 7);
EXPECT_EQ(boxes[1].end.y, -2);
EXPECT_EQ(boxes[1].end.z, -3);

EXPECT_EQ(boxes[0].start.x, -7);
EXPECT_EQ(boxes[0].start.y, 2);
EXPECT_EQ(boxes[0].start.z, 3);
EXPECT_EQ(boxes[0].end.x, 4);
EXPECT_EQ(boxes[0].end.y, 8);
EXPECT_EQ(boxes[0].end.z, 9);

auto bounds = std::reduce(boxes.begin(), boxes.end(), boxes[0], [](const auto a, const auto b) { return a.combine(b); });

EXPECT_EQ(bounds.start.x, -7);
EXPECT_EQ(bounds.start.y, -8);
EXPECT_EQ(bounds.start.z, -9);
EXPECT_EQ(bounds.end.x, 7);
EXPECT_EQ(bounds.end.y, 8);
EXPECT_EQ(bounds.end.z, 9);
}
} // namespace UnitTest

0 comments on commit 5f8b997

Please sign in to comment.