Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #613: Have a way to save Curves in the show #614

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LATEST_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Other changes:
* [#602](../../issues/602) Have Background images use CalChart::Draw
* [#602](../../issues/602) Have Background images use CalChart::Draw
* [#608](../../issues/608) Add the ability to draw a curve in CalChart
* [#613](../../issues/613) Have a way to save Curves in the show

1 change: 1 addition & 0 deletions src/core/CalChartFileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ constexpr auto INGL_VCNT = Make4CharWord('V', 'C', 'N', 'T');
constexpr auto INGL_EVCT = Make4CharWord('E', 'V', 'C', 'T');
constexpr auto INGL_PCNT = Make4CharWord('P', 'C', 'N', 'T');
constexpr auto INGL_BACK = Make4CharWord('B', 'A', 'C', 'K');
constexpr auto INGL_CURV = Make4CharWord('C', 'U', 'R', 'V');
constexpr auto INGL_PNTS = Make4CharWord('P', 'N', 'T', 'S');
constexpr auto INGL_PONT = Make4CharWord('P', 'O', 'N', 'T');
constexpr auto INGL_END = Make4CharWord('E', 'N', 'D', ' ');
Expand Down
24 changes: 24 additions & 0 deletions src/core/CalChartShapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CalChartShapes.h"
#include "CalChartConfiguration.h"
#include "CalChartDrawCommand.h"
#include "CalChartFileFormat.h"
#include "CalChartRanges.h"
#include <cmath>
#include <format>
Expand Down Expand Up @@ -326,4 +327,27 @@ auto Curve::LowerControlPointOnLine(Coord point, Coord::units searchBound) const
return isPointOnCurve(drawPoints, point, searchBound, kNumberSegments);
}

auto Curve::Serialize() const -> std::vector<std::byte>
{
auto result = std::vector<std::byte>{};
Parser::Append(result, static_cast<int32_t>(pntlist.size()));
for (auto&& point : pntlist) {
Parser::Append(result, static_cast<int32_t>(point.x));
Parser::Append(result, static_cast<int32_t>(point.y));
}
return result;
}

auto CreateCurve(Reader reader) -> std::pair<Curve, Reader>
{
auto numControlPoints = reader.Get<int32_t>();
auto controlPoints = std::vector<Coord>{};
while (numControlPoints--) {
auto x = static_cast<Coord::units>(reader.Get<int32_t>());
auto y = static_cast<Coord::units>(reader.Get<int32_t>());
controlPoints.emplace_back(x, y);
}
return { Curve{ controlPoints }, reader };
}

}
11 changes: 11 additions & 0 deletions src/core/CalChartShapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

namespace CalChart {

class Reader;

using RawPolygon_t = std::vector<Coord>;
auto Inside(Coord p, RawPolygon_t const& polygon) -> bool;
auto CrossesLine(Coord start, Coord end, Coord p) -> bool;
Expand Down Expand Up @@ -360,11 +362,20 @@ class Curve : public Shape {
[[nodiscard]] auto GetControlPoints() const -> std::vector<Coord> { return pntlist; }
[[nodiscard]] auto LowerControlPointOnLine(Coord point, Coord::units searchBound) const -> std::optional<size_t>;

[[nodiscard]] auto Serialize() const -> std::vector<std::byte>;

[[nodiscard]] auto operator==(Curve const& other) const -> bool
{
return pntlist == other.pntlist && movingPoint == other.movingPoint;
}

private:
std::vector<Coord> pntlist;
std::optional<Coord> movingPoint;

void OnMoveImpl(Coord p) override { movingPoint = p; }
};

auto CreateCurve(Reader) -> std::pair<Curve, Reader>;

}
25 changes: 25 additions & 0 deletions src/core/CalChartSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ Sheet::Sheet(size_t numPoints, Reader reader, ParseErrorHandlers const* correcti
throw CC_FileException("Bad Background chunk", INGL_BACK);
}
};
auto parse_INGL_CURV = [](Sheet* sheet, Reader reader) {
auto num = reader.Get<int32_t>();
while (num--) {
auto [curve, new_reader] = CreateCurve(reader);
sheet->mCurves.push_back(curve);
reader = new_reader;
}
if (reader.size() != 0) {
throw CC_FileException("Bad Background chunk", INGL_BACK);
}
};

std::map<uint32_t, std::function<void(Sheet*, Reader)>> const
parser
Expand All @@ -411,6 +422,7 @@ Sheet::Sheet(size_t numPoints, Reader reader, ParseErrorHandlers const* correcti
{ INGL_VCNT, parse_INGL_VCNT },
{ INGL_PCNT, parse_INGL_PCNT },
{ INGL_BACK, parse_INGL_BACK },
{ INGL_CURV, parse_INGL_CURV },
};

auto table = reader.ParseOutLabels();
Expand Down Expand Up @@ -468,6 +480,16 @@ auto Sheet::SerializeBackgroundImageInfo() const -> std::vector<std::byte>
return result;
}

auto Sheet::SerializeCurves() const -> std::vector<std::byte>
{
std::vector<std::byte> result;
Parser::Append(result, static_cast<uint32_t>(mCurves.size()));
for (auto&& curve : mCurves) {
Parser::Append(result, curve.Serialize());
}
return result;
}

auto Sheet::SerializeSheetData() const -> std::vector<std::byte>
{
// SHEET_DATA = NAME , DURATION , ALL_POINTS , CONTINUITY,
Expand Down Expand Up @@ -497,6 +519,9 @@ auto Sheet::SerializeSheetData() const -> std::vector<std::byte>
// Write Background
Parser::Append(result, Parser::Construct_block(INGL_BACK, SerializeBackgroundImageInfo()));

// Write Curves
Parser::Append(result, Parser::Construct_block(INGL_CURV, SerializeCurves()));

return result;
}

Expand Down
1 change: 1 addition & 0 deletions src/core/CalChartSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Sheet {
[[nodiscard]] auto SerializeContinuityData() const -> std::vector<std::byte>;
[[nodiscard]] auto SerializePrintContinuityData() const -> std::vector<std::byte>;
[[nodiscard]] auto SerializeBackgroundImageInfo() const -> std::vector<std::byte>;
[[nodiscard]] auto SerializeCurves() const -> std::vector<std::byte>;
[[nodiscard]] auto SerializeSheetData() const -> std::vector<std::byte>;

public:
Expand Down
16 changes: 16 additions & 0 deletions tests/CalChartShapesTests.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "CalChartFileFormat.h"
#include "CalChartShapes.h"
#include <catch2/catch_test_macros.hpp>

Expand Down Expand Up @@ -573,4 +574,19 @@ TEST_CASE("Shape_Curve", "CalChartShapeTests")
CHECK(uut_draw == drawCommands);
}

TEST_CASE("Shape_Curve_SerializeDeserialize", "CalChartShapeTests")
{
{
auto uut = CalChart::Curve(std::vector<CalChart::Coord>{
{ 10, 10 },
{ 500, 400 },
{ 10, 400 },
{ 500, 10 } });
auto serialized = uut.Serialize();
auto reader = CalChart::Reader({ serialized.data(), serialized.size() });
auto [curve, newReader] = CalChart::CreateCurve(reader);
CHECK(curve == uut);
}
}

// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity)