Fix triangulation and fix skpath read function

This commit is contained in:
Yousef Khadadeh 2026-05-31 13:36:12 +03:00
parent 48823ebad6
commit e2a62e852c
3 changed files with 143 additions and 118 deletions

View file

@ -67,12 +67,9 @@ void NetClient::update() {
isDisconnected = true; isDisconnected = true;
return; return;
} }
#ifdef NDEBUG
try { try {
#endif
parse_received_messages(); parse_received_messages();
send_queued_messages(); send_queued_messages();
#ifdef NDEBUG
} }
catch(const std::exception& e) { catch(const std::exception& e) {
Logger::get().log(Logger::LogType::INFO, "[NetClient::update] Exception thrown while parsing and sending messages: " + std::string(e.what())); Logger::get().log(Logger::LogType::INFO, "[NetClient::update] Exception thrown while parsing and sending messages: " + std::string(e.what()));
@ -82,7 +79,6 @@ void NetClient::update() {
Logger::get().log(Logger::LogType::INFO, "[NetClient::update] Unknown exception thrown while parsing and sending messages."); Logger::get().log(Logger::LogType::INFO, "[NetClient::update] Unknown exception thrown while parsing and sending messages.");
isDisconnected = true; isDisconnected = true;
} }
#endif
} }
void NetClient::parse_received_messages() { void NetClient::parse_received_messages() {

View file

@ -44,12 +44,9 @@ void NetServer::update() {
if(client->setToDisconnect) if(client->setToDisconnect)
disconnectCallback(client); disconnectCallback(client);
else { else {
#ifdef NDEBUG
try { try {
#endif
client->parse_received_messages(*this); client->parse_received_messages(*this);
client->send_queued_messages(*this); client->send_queued_messages(*this);
#ifdef NDEBUG
} }
catch(const std::exception& e) { catch(const std::exception& e) {
Logger::get().log(Logger::LogType::INFO, "[NetServer::update] Exception thrown while parsing and sending messages for a client: " + std::string(e.what())); Logger::get().log(Logger::LogType::INFO, "[NetServer::update] Exception thrown while parsing and sending messages for a client: " + std::string(e.what()));
@ -61,7 +58,6 @@ void NetServer::update() {
client->setToDisconnect = true; client->setToDisconnect = true;
disconnectCallback(client); disconnectCallback(client);
} }
#endif
} }
} }
} }

View file

@ -19,6 +19,7 @@
#include "MeshCanvasComponent.hpp" #include "MeshCanvasComponent.hpp"
#include <Helpers/Serializers.hpp> #include <Helpers/Serializers.hpp>
#include <cereal/types/vector.hpp> #include <cereal/types/vector.hpp>
#include <chrono>
#include <include/core/SkPathTypes.h> #include <include/core/SkPathTypes.h>
#include <limits> #include <limits>
#include <Eigen/Geometry> #include <Eigen/Geometry>
@ -37,48 +38,50 @@
#include "../World.hpp" #include "../World.hpp"
#include <CDT/include/CDT.h> #include <CDT/include/CDT.h>
#include <clipper2/clipper.h> #include <clipper2/clipper.h>
#include <Helpers/Logger.hpp>
template <typename Archive> void skpath_write(const SkPath& p, Archive& a) { template <typename Archive> void skpath_write(const SkPath& p, Archive& a) {
std::vector<uint32_t> contourSizes; std::vector<std::vector<SkPoint>> contours;
uint32_t contourSize = 0;
auto verbs = p.verbs(); SkPath::Iter iter(p, true);
auto points = p.points(); for(;;) {
for(size_t verbIndex = 0; verbIndex < verbs.size(); verbIndex++) { std::optional<SkPath::IterRec> rec = iter.next();
switch(verbs[verbIndex]) { if(!rec.has_value())
break;
switch(rec->fVerb) {
case SkPathVerb::kClose: case SkPathVerb::kClose:
contourSizes.emplace_back(contourSize); if(!contours.back().empty())
contourSize = 0; contours.back().pop_back();
break;
case SkPathVerb::kLine:
contours.back().emplace_back(rec->fPoints[1]);
break; break;
case SkPathVerb::kMove: case SkPathVerb::kMove:
case SkPathVerb::kLine: contours.emplace_back();
contourSize++; contours.back().emplace_back(rec->fPoints[0]);
break; break;
default: default:
throw std::runtime_error("[skpath_write] Illegal verb " + std::to_string(static_cast<unsigned>(verbs[verbIndex]))); throw std::runtime_error("[get_predraw_data_accurate] Illegal verb " + std::to_string(static_cast<unsigned>(rec->fVerb)));
break; break;
} }
} }
a(p.getFillType() == SkPathFillType::kEvenOdd, contourSizes);
for(const SkPoint& p : points) a(p.getFillType() == SkPathFillType::kEvenOdd, contours);
a(p);
} }
template <typename Archive> SkPath skpath_read(Archive& a) { template <typename Archive> SkPath skpath_read(Archive& a) {
bool isEvenOdd; bool isEvenOdd;
SkPathBuilder builder; std::vector<std::vector<SkPoint>> contours;
std::vector<uint32_t> contourSizes; a(isEvenOdd, contours);
a(isEvenOdd, contourSizes); // NOTE: setFillType doesn't properly set the fill type for the path IN DEBUG BUILDS. Setting fill type in builder constructor meanwhile works for both debug and release builds
builder.setFillType(isEvenOdd ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding); SkPathBuilder builder(isEvenOdd ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding);
for(uint32_t contourSize : contourSizes) { for(const std::vector<SkPoint>& contour : contours) {
SkPoint p; if(contour.size() == 0)
if(contourSize == 0)
continue; continue;
a(p); builder.moveTo(contour[0]);
builder.moveTo(p); for(uint32_t i = 1; i < contour.size(); i++)
for(uint32_t i = 1; i < contourSize; i++) { builder.lineTo(contour[i]);
a(p);
builder.lineTo(p);
}
builder.close(); builder.close();
} }
return builder.detach(); return builder.detach();
@ -141,14 +144,27 @@ void MeshCanvasComponent::draw(SkCanvas* canvas, const DrawData& drawData, const
} }
std::shared_ptr<void> MeshCanvasComponent::get_predraw_data_accurate(const DrawData& drawData, const CoordSpaceHelper& coords) const { std::shared_ptr<void> MeshCanvasComponent::get_predraw_data_accurate(const DrawData& drawData, const CoordSpaceHelper& coords) const {
try {
SCollision::AABB<float> viewGenerousColliderInObjSpace = coords.world_collider_to_coords<SCollision::AABB<float>>(drawData.cam.viewingAreaGenerousCollider); SCollision::AABB<float> viewGenerousColliderInObjSpace = coords.world_collider_to_coords<SCollision::AABB<float>>(drawData.cam.viewingAreaGenerousCollider);
viewGenerousColliderInObjSpace.min -= Vector2f{1.0f, 1.0f}; viewGenerousColliderInObjSpace.min -= Vector2f{1.0f, 1.0f};
viewGenerousColliderInObjSpace.max += Vector2f{1.0f, 1.0f}; viewGenerousColliderInObjSpace.max += Vector2f{1.0f, 1.0f};
// GrTriangulator is faster, but isn't as robust
//GrCpuVertexAllocator cpuAlloc;
//bool isLinear;
//GrTriangulator::PathToTriangles(d.meshPath, 0.0001f, viewGenerousColliderInObjSpace.get_sk_rect(), &cpuAlloc, &isLinear);
//sk_sp<GrThreadSafeCache::VertexData> detachedVertexData = cpuAlloc.detachVertexData();
//if(!detachedVertexData)
// return nullptr;
//if(detachedVertexData->vertexSize() != 8)
// throw std::runtime_error("[MeshCanvasComponent::get_predraw_data_accurate] Vertex data vertex size not equal to 8");
// Triangulation implementation using CDT
SkPath::Iter iter(d.meshPath, true); SkPath::Iter iter(d.meshPath, true);
std::vector<CDT::V2d<float>> points; std::vector<CDT::V2d<float>> points;
std::vector<CDT::Edge> edges; std::vector<CDT::Edge> edges;
// Makes duplicate vertices by default, but duplicates are impossible to completely remove, so just rely on RemoveDuplicatesAndRemapEdges
for(;;) { for(;;) {
std::optional<SkPath::IterRec> rec = iter.next(); std::optional<SkPath::IterRec> rec = iter.next();
if(!rec.has_value()) if(!rec.has_value())
@ -157,10 +173,12 @@ std::shared_ptr<void> MeshCanvasComponent::get_predraw_data_accurate(const DrawD
switch(rec->fVerb) { switch(rec->fVerb) {
case SkPathVerb::kClose: case SkPathVerb::kClose:
break; break;
case SkPathVerb::kLine: case SkPathVerb::kLine: {
points.emplace_back(rec->fPoints[1].x(), rec->fPoints[1].y()); CDT::V2d<float> newPoint{rec->fPoints[1].x(), rec->fPoints[1].y()};
points.emplace_back(newPoint);
edges.emplace_back(points.size() - 2, points.size() - 1); edges.emplace_back(points.size() - 2, points.size() - 1);
break; break;
}
case SkPathVerb::kMove: case SkPathVerb::kMove:
points.emplace_back(rec->fPoints[0].x(), rec->fPoints[0].y()); points.emplace_back(rec->fPoints[0].x(), rec->fPoints[0].y());
break; break;
@ -189,8 +207,18 @@ std::shared_ptr<void> MeshCanvasComponent::get_predraw_data_accurate(const DrawD
return resultList; return resultList;
}; };
for(auto& tri : cdt.triangles) { // GrTriangulator
SCollision::Triangle triCollider(Vector2f{points[tri.vertices[0]].x, points[tri.vertices[0]].y}, Vector2f{points[tri.vertices[1]].x, points[tri.vertices[1]].y}, Vector2f{points[tri.vertices[2]].x, points[tri.vertices[2]].y}); //for(int i = 0; i < detachedVertexData->numVertices(); i += 3) {
// Vector2f v1 = {static_cast<const float*>(detachedVertexData->vertices())[i], static_cast<const float*>(detachedVertexData->vertices())[i + 1]};
// Vector2f v2 = {static_cast<const float*>(detachedVertexData->vertices())[i + 2], static_cast<const float*>(detachedVertexData->vertices())[i + 3]};
// Vector2f v3 = {static_cast<const float*>(detachedVertexData->vertices())[i + 4], static_cast<const float*>(detachedVertexData->vertices())[i + 5]};
// CDT
for(size_t i = 0; i < cdt.triangles.size(); i++) {
auto& tri = cdt.triangles[i].vertices;
Vector2f v1{cdt.vertices[tri[0]].x, cdt.vertices[tri[0]].y};
Vector2f v2{cdt.vertices[tri[1]].x, cdt.vertices[tri[1]].y};
Vector2f v3{cdt.vertices[tri[2]].x, cdt.vertices[tri[2]].y};
SCollision::Triangle triCollider(v1, v2, v3);
if(SCollision::collide(triCollider.bounds, viewGenerousColliderInObjSpace)) { if(SCollision::collide(triCollider.bounds, viewGenerousColliderInObjSpace)) {
std::vector<std::array<WorldVec, 3>> clipList; std::vector<std::array<WorldVec, 3>> clipList;
clipList.emplace_back(std::array<WorldVec, 3>{coords.from_space(triCollider.p[0]), coords.from_space(triCollider.p[1]), coords.from_space(triCollider.p[2])}); clipList.emplace_back(std::array<WorldVec, 3>{coords.from_space(triCollider.p[0]), coords.from_space(triCollider.p[1]), coords.from_space(triCollider.p[2])});
@ -234,6 +262,11 @@ std::shared_ptr<void> MeshCanvasComponent::get_predraw_data_accurate(const DrawD
pathBuilder.addPolygon({finalTriangle.data(), finalTriangle.size()}, true); pathBuilder.addPolygon({finalTriangle.data(), finalTriangle.size()}, true);
return std::make_shared<SkPath>(pathBuilder.detach()); return std::make_shared<SkPath>(pathBuilder.detach());
} }
catch(...) {
return nullptr;
}
return nullptr;
}
bool MeshCanvasComponent::accurate_draw(SkCanvas* canvas, const DrawData& drawData, const CoordSpaceHelper& coords, const std::shared_ptr<void>& predrawData) const { bool MeshCanvasComponent::accurate_draw(SkCanvas* canvas, const DrawData& drawData, const CoordSpaceHelper& coords, const std::shared_ptr<void>& predrawData) const {
if(predrawData) { if(predrawData) {