Skip to content

Commit

Permalink
Add output operator overrides to classes with ToString (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
CCInc and pre-commit-ci[bot] authored Oct 17, 2022
1 parent 3043620 commit 816f0a8
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- **\[C++\]** Add output operator (<<) overrides for classes with ToString methods

## [2.4.1] - 2022-09-13

### Changed
Expand Down
10 changes: 10 additions & 0 deletions cpp/include/copc-lib/copc/extents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class CopcExtent
CopcExtent(const CopcExtent &other);

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, CopcExtent const &value)
{
os << value.ToString();
return os;
}

// Operators
bool operator==(const CopcExtent &other) const
Expand Down Expand Up @@ -202,6 +207,11 @@ class CopcExtents
static size_t ByteSize(int8_t point_format_id, uint16_t num_eb_items);

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, CopcExtents const &value)
{
os << value.ToString();
return os;
}

private:
int8_t point_format_id_;
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/copc/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class CopcInfo
lazperf::copc_info_vlr ToLazPerf(const CopcExtent &gps_time) const;

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, CopcInfo const &value)
{
os << value.ToString();
return os;
}

double center_x{0};
double center_y{0};
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/geometry/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Box
bool Within(const Box &box) const;

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, Box const &value)
{
os << value.ToString();
return os;
}

double x_min{};
double y_min{};
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/geometry/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ struct Vector3
ss << "(" << x << ", " << y << ", " << z << ")";
return ss.str();
}
friend std::ostream &operator<<(std::ostream &os, Vector3 const &value)
{
os << value.ToString();
return os;
}

bool operator==(Vector3 other) const { return x == other.x && y == other.y && z == other.z; }
bool operator!=(Vector3 other) const { return !(*this == other); }
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/hierarchy/entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Entry
<< ", is_valid=" << IsValid();
return ss.str();
}
friend std::ostream &operator<<(std::ostream &os, Entry const &value)
{
os << value.ToString();
return os;
}

void Pack(std::ostream &out_stream)
{
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/hierarchy/key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class VoxelKey
bool IsValid() const { return d >= 0 && x >= 0 && y >= 0 && z >= 0; }

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, VoxelKey const &value)
{
os << value.ToString();
return os;
}

// Returns the corresponding key depending on direction [0,7]
VoxelKey Bisect(const uint64_t &direction) const;
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/las/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class LasHeader
bool eb_flag, bool extended_stats_flag) const;

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, LasHeader const &value)
{
os << value.ToString();
return os;
}

// Getters/Setters for protected attributes

Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/las/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ class Point
bool Within(const Box &box) const;

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, Point const &value)
{
os << value.ToString();
return os;
}

static std::shared_ptr<Point> Unpack(std::istream &in_stream, const int8_t &point_format_id, const Vector3 &scale,
const Vector3 &offset, const uint16_t &eb_byte_size);
Expand Down
5 changes: 5 additions & 0 deletions cpp/include/copc-lib/las/points.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Points
static Points Unpack(const std::vector<char> &point_data, const LasHeader &header);

std::string ToString() const;
friend std::ostream &operator<<(std::ostream &os, Points const &value)
{
os << value.ToString();
return os;
}

// Getters and setters
std::vector<double> X() const
Expand Down
19 changes: 9 additions & 10 deletions cpp/src/io/copc_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ bool Reader::ValidateSpatialBounds(bool verbose)
std::cout << "Validating Spatial Bounds" << std::endl;
std::cout << "File info:" << std::endl;
std::cout << "\tPoint Count: " << header.PointCount() << std::endl;
std::cout << "\tScale: " << header.Scale().ToString() << std::endl;
std::cout << "\tOffset: " << header.Offset().ToString() << std::endl;
std::cout << "\tMin Bounds: " << header.min.ToString() << std::endl;
std::cout << "\tMax Bounds: " << header.max.ToString() << std::endl;
std::cout << "\tScale: " << header.Scale() << std::endl;
std::cout << "\tOffset: " << header.Offset() << std::endl;
std::cout << "\tMin Bounds: " << header.min << std::endl;
std::cout << "\tMax Bounds: " << header.max << std::endl;
std::cout << std::endl << "Validating bounds..." << std::endl << std::endl;
}

Expand All @@ -354,8 +354,8 @@ bool Reader::ValidateSpatialBounds(bool verbose)
is_valid = false;
if (!verbose)
return false;
std::cout << "Node " << node.key.ToString() << " is outside of las header bounds ("
<< header.Bounds().ToString() << ")." << std::endl;
std::cout << "Node " << node.key << " is outside of las header bounds (" << header.Bounds() << ")."
<< std::endl;
total_points_outside_header_bounds += node.point_count;
}
else
Expand All @@ -372,8 +372,8 @@ bool Reader::ValidateSpatialBounds(bool verbose)
if (!verbose)
return false;
std::cout << "Point (" << point->X() << "," << point->Y() << "," << point->Z() << ") from node "
<< node.key.ToString() << " is outside of las header bounds ("
<< header.Bounds().ToString() << ")." << std::endl;
<< node.key << " is outside of las header bounds (" << header.Bounds() << ")."
<< std::endl;
total_points_outside_header_bounds++;
}
}
Expand All @@ -388,8 +388,7 @@ bool Reader::ValidateSpatialBounds(bool verbose)
if (!verbose)
return false;
std::cout << "Point (" << point->X() << "," << point->Y() << "," << point->Z()
<< ") is outside of node " << node.key.ToString() << " bounds (" << box.ToString() << ")."
<< std::endl;
<< ") is outside of node " << node.key << " bounds (" << box << ")." << std::endl;
total_points_outside_node_bounds++;
}
}
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/las/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ std::string LasHeader::ToString() const
ss << "\tPoint Format ID: " << static_cast<int>(point_format_id_) << std::endl;
ss << "\tPoint Record Length: " << point_record_length_ << std::endl;
ss << "\tPoint Count: " << point_count_ << std::endl;
ss << "\tScale: " << scale_.ToString() << std::endl;
ss << "\tOffset: " << offset_.ToString() << std::endl;
ss << "\tMax: " << max.ToString() << std::endl;
ss << "\tMin: " << min.ToString() << std::endl;
ss << "\tScale: " << scale_ << std::endl;
ss << "\tOffset: " << offset_ << std::endl;
ss << "\tMax: " << max << std::endl;
ss << "\tMin: " << min << std::endl;
ss << "\tEVLR Offset: " << evlr_offset_ << std::endl;
ss << "\tEVLR count: " << evlr_count_ << std::endl;
ss << "\tPoints By Return:" << std::endl;
Expand Down
7 changes: 7 additions & 0 deletions test/box_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ TEST_CASE("Box constructor", "[Box]")
REQUIRE_THROWS(Box(2, 0, 0, 1, 1, 1));
REQUIRE_THROWS(Box(0, 2, 0, 1, 1, 1));
REQUIRE_THROWS(Box(0, 0, 2, 1, 1, 1));

SECTION("Box ToString")
{
box.ToString();
std::stringstream ss;
ss << box;
}
}

SECTION("Vector3 constructor")
Expand Down
8 changes: 8 additions & 0 deletions test/copc_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ TEST_CASE("CopcConfigWriter", "[CopcConfigWriter]")
{
CopcInfo copc_info;
copc_info.spacing = test_spacing;

SECTION("CopcInfo ToString")
{
copc_info.ToString();
std::stringstream ss;
ss << copc_info;
}

CopcExtents copc_extents(point_format_id, num_eb_items);
copc_extents.Intensity()->minimum = test_intensity_min;
copc_extents.Intensity()->maximum = test_intensity_max;
Expand Down
7 changes: 7 additions & 0 deletions test/extents_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ TEST_CASE("COPC Extent", "[CopcExtent]")
REQUIRE(extent != other);

REQUIRE_THROWS(CopcExtent(1, 0));

SECTION("ToString")
{
extent.ToString();
std::stringstream ss;
ss << extent;
}
}

TEST_CASE("COPC Extents", "[CopcExtents]")
Expand Down
7 changes: 7 additions & 0 deletions test/key_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ TEST_CASE("Voxel Key checks key validity", "[Key]")
REQUIRE(VoxelKey(1, 1, 1, 1).IsValid() == true);
}

TEST_CASE("VoxelKey ToString", "[Key]")
{
VoxelKey(1, 1, 1, 1).ToString();
std::stringstream ss;
ss << VoxelKey(1, 1, 1, 1);
}

TEST_CASE("GetParent Checks", "[Key]")
{
REQUIRE(VoxelKey(-1, -1, -1, -1).GetParent().IsValid() == false);
Expand Down
7 changes: 6 additions & 1 deletion test/las_header_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ TEST_CASE("Test reader and conversions", "[LasHeader]")
REQUIRE(las_header_origin.EvlrOffset() == las_header.EvlrOffset());
REQUIRE(las_header_origin.EvlrCount() == las_header.EvlrCount());

las_header_origin.ToString();
SECTION("LasHeader ToString")
{
las_header_origin.ToString();
std::stringstream ss;
ss << las_header_origin;
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ TEST_CASE("Point tests", "[Point]")
REQUIRE(point.Green() == 0);
REQUIRE(point.Blue() == 0);
REQUIRE(point.Nir() == 0);

SECTION("Point ToString")
{
point.ToString();
std::stringstream ss;
ss << point;
}
}

SECTION("Point conversion")
Expand Down
7 changes: 6 additions & 1 deletion test/points_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ TEST_CASE("Points tests", "[Point]")
REQUIRE(points.Get(0)->Y() == 11.2);
REQUIRE(points.Get(0)->Z() == 11.3);

points.ToString();
SECTION("Points ToString")
{
points.ToString();
std::stringstream ss;
ss << points;
}
}

SECTION("Adding Point to Points")
Expand Down
7 changes: 7 additions & 0 deletions test/reader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ TEST_CASE("FindKey Check", "[Reader]")
REQUIRE(hier_entry.IsValid() == true);
REQUIRE(hier_entry.key == key);
REQUIRE(hier_entry.point_count == 12021);

SECTION("Node ToString")
{
hier_entry.ToString();
std::stringstream ss;
ss << hier_entry;
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/vector3_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ TEST_CASE("Vector3", "[Vector3]")
REQUIRE(vec.x == 0.0);
REQUIRE(vec.y == 0.0);
REQUIRE(vec.z == 0.0);

SECTION("Vector3 ToString")
{
vec.ToString();
std::stringstream ss;
ss << vec;
}
}
SECTION("Vector3 Operators")
{
Expand Down

0 comments on commit 816f0a8

Please sign in to comment.