2025-07-06 20:43:53 +03:00
|
|
|
#include "DrawBrushStroke.hpp"
|
|
|
|
|
#include <Helpers/Serializers.hpp>
|
|
|
|
|
#include <cereal/types/vector.hpp>
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
|
#ifndef IS_SERVER
|
|
|
|
|
#include <Eigen/Geometry>
|
|
|
|
|
#include "Helpers/MathExtras.hpp"
|
|
|
|
|
#include "../TimePoint.hpp"
|
|
|
|
|
#include <include/core/SkVertices.h>
|
2025-08-25 20:31:18 +03:00
|
|
|
#include <include/pathops/SkPathOps.h>
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "../DrawCollision.hpp"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define AABB_PRECHECK_BVH_LEVELS 2
|
2025-07-14 17:00:28 +03:00
|
|
|
#define DEFAULT_SMOOTHNESS 5
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
void DrawBrushStroke::save(cereal::PortableBinaryOutputArchive& a) const {
|
2025-07-29 14:11:36 +03:00
|
|
|
a(d->points, d->color, d->hasRoundCaps);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawBrushStroke::load(cereal::PortableBinaryInputArchive& a) {
|
2025-07-29 14:11:36 +03:00
|
|
|
a(d->points, d->color, d->hasRoundCaps);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawComponentType DrawBrushStroke::get_type() const {
|
|
|
|
|
return DRAWCOMPONENT_BRUSHSTROKE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef IS_SERVER
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<DrawComponent> DrawBrushStroke::copy() const {
|
|
|
|
|
auto a = std::make_shared<DrawBrushStroke>();
|
|
|
|
|
a->d = d;
|
|
|
|
|
a->coords = coords;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 14:11:36 +03:00
|
|
|
std::shared_ptr<DrawComponent> DrawBrushStroke::deep_copy() const {
|
|
|
|
|
auto a = std::make_shared<DrawBrushStroke>();
|
|
|
|
|
a->d = d;
|
|
|
|
|
a->coords = coords;
|
|
|
|
|
// Just copy pointers over, brush strokes cant be edited so they can share the same memory
|
2025-08-23 13:28:01 +03:00
|
|
|
a->brushPath = brushPath;
|
2025-07-29 14:11:36 +03:00
|
|
|
a->bounds = bounds;
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 09:34:17 +03:00
|
|
|
void DrawBrushStroke::update_from_delayed_ptr(const std::shared_ptr<DrawComponent>& delayedUpdatePtr) {
|
2025-07-06 20:43:53 +03:00
|
|
|
std::shared_ptr<DrawBrushStroke> newPtr = std::static_pointer_cast<DrawBrushStroke>(delayedUpdatePtr);
|
|
|
|
|
d = newPtr->d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2f two_dim_vec_slerp(const Vector2f& v1, const Vector2f& v2) {
|
|
|
|
|
Vector2f v1Norm = v1.normalized();
|
|
|
|
|
Vector2f v2Norm = v2.normalized();
|
|
|
|
|
Vector2f avg = (v1Norm + v2Norm) * 0.5f;
|
|
|
|
|
if(std::fabs(avg.x()) < 0.00001f && std::fabs(avg.y()) < 0.00001f)
|
|
|
|
|
return perpendicular_vec2(v1Norm).normalized();
|
|
|
|
|
else
|
|
|
|
|
return avg.normalized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawBrushStroke::draw(SkCanvas* canvas, const DrawData& drawData) {
|
2025-08-23 13:28:01 +03:00
|
|
|
if(drawSetupData.shouldDraw && brushPath) {
|
2025-07-06 20:43:53 +03:00
|
|
|
//bool deepPrecheck = false;
|
|
|
|
|
//for(auto& aabb : precheckAABBLevels) {
|
|
|
|
|
// if(SCollision::collide(drawData.cam.viewingAreaGenerousCollider, aabb)) {
|
|
|
|
|
// deepPrecheck = true;
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//if(!deepPrecheck && !precheckAABBLevels.empty())
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
|
|
SkPaint paint;
|
2025-08-23 13:28:01 +03:00
|
|
|
//paint.setStroke(true);
|
|
|
|
|
//paint.setStrokeWidth(0.0f);
|
|
|
|
|
canvas->save();
|
|
|
|
|
paint.setColor4f(SkColor4f{d->color.x(), d->color.y(), d->color.z(), d->color.w()});
|
2025-07-06 20:43:53 +03:00
|
|
|
canvas_do_calculated_transform(canvas);
|
2025-08-23 13:28:01 +03:00
|
|
|
canvas->drawPath(*brushPath, paint);
|
2025-07-06 20:43:53 +03:00
|
|
|
//SkPaint p2(SkColor4f{1, 0, 0, 1});
|
|
|
|
|
//p2.setStroke(true);
|
|
|
|
|
//SkPaint p3(SkColor4f{0, 1, 0, 1});
|
|
|
|
|
//SkPaint p4(SkColor4f{0, 0, 1, 1});
|
|
|
|
|
//p3.setStroke(true);
|
|
|
|
|
//p4.setStroke(true);
|
|
|
|
|
//for(auto& precheckAABBLevel : precheckAABBLevels)
|
|
|
|
|
// draw_collider(canvas, drawData, precheckAABBLevel, p4);
|
|
|
|
|
//draw_collider(canvas, drawData, collisionTree, p3);
|
|
|
|
|
canvas->restore();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawBrushStroke::initialize_draw_data(DrawingProgram& drawP) {
|
2025-07-29 14:11:36 +03:00
|
|
|
if(!d->points.empty()) {
|
|
|
|
|
std::vector<DrawBrushStrokePoint> points = smooth_points(0, d->points.size() - 1, DEFAULT_SMOOTHNESS);
|
2025-07-14 17:00:28 +03:00
|
|
|
auto triangleFunc = [&](Vector2f a, Vector2f b, Vector2f c) {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-23 13:28:01 +03:00
|
|
|
brushPath = std::make_shared<SkPath>();
|
2025-07-14 17:00:28 +03:00
|
|
|
|
2025-08-23 13:28:01 +03:00
|
|
|
create_triangles(triangleFunc, points, 0, brushPath);
|
2025-07-14 17:00:28 +03:00
|
|
|
}
|
2025-07-31 09:34:17 +03:00
|
|
|
create_collider();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<size_t> DrawBrushStroke::get_wedge_indices(const std::vector<DrawBrushStrokePoint>& points) {
|
|
|
|
|
if(points.size() < 2)
|
|
|
|
|
return {};
|
|
|
|
|
std::vector<size_t> toRet;
|
|
|
|
|
toRet.emplace_back(0);
|
|
|
|
|
for(size_t i = 1; i < points.size() - 1; i++) {
|
|
|
|
|
Vector2f v1Norm = (points[i - 1].pos - points[i].pos).normalized();
|
|
|
|
|
Vector2f v2Norm = (points[i + 1].pos - points[i].pos).normalized();
|
|
|
|
|
if(v1Norm.dot(v2Norm) > -0.6f)
|
|
|
|
|
toRet.emplace_back(i);
|
|
|
|
|
}
|
|
|
|
|
toRet.emplace_back(points.size() - 1);
|
|
|
|
|
return toRet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 17:00:28 +03:00
|
|
|
std::vector<DrawBrushStrokePoint> DrawBrushStroke::smooth_points(size_t beginIndex, size_t endIndex, unsigned numOfDivisions) {
|
2025-07-29 14:11:36 +03:00
|
|
|
std::vector<DrawBrushStrokePoint>& points = d->points;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
size_t pointsSize = endIndex - beginIndex + 1;
|
|
|
|
|
if(pointsSize < 2)
|
|
|
|
|
return {points[beginIndex], points[endIndex]};
|
|
|
|
|
|
|
|
|
|
std::vector<DrawBrushStrokePoint> toRet;
|
|
|
|
|
|
|
|
|
|
DrawBrushStrokePoint p0 = points[beginIndex];
|
|
|
|
|
p0.pos = p0.pos + p0.width * (p0.pos - points[beginIndex + 1].pos).normalized() * DRAW_MINIMUM_LIMIT;
|
|
|
|
|
DrawBrushStrokePoint p1 = points[beginIndex];
|
|
|
|
|
DrawBrushStrokePoint p2 = points[beginIndex + 1];
|
|
|
|
|
DrawBrushStrokePoint p3;
|
|
|
|
|
|
|
|
|
|
for(size_t i = beginIndex; i < endIndex; i++) {
|
|
|
|
|
|
|
|
|
|
if(i == endIndex - 1) {
|
|
|
|
|
p3 = points[i + 1];
|
|
|
|
|
p3.pos = p3.pos + p3.width * (p3.pos - points[i].pos).normalized() * DRAW_MINIMUM_LIMIT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
p3 = points[i + 2];
|
|
|
|
|
|
|
|
|
|
toRet.emplace_back(p1);
|
|
|
|
|
|
|
|
|
|
float tMove = 1.0 / static_cast<float>(numOfDivisions);
|
|
|
|
|
for(unsigned d = 1; d < numOfDivisions; d++) {
|
|
|
|
|
float t = tMove * static_cast<float>(d);
|
|
|
|
|
DrawBrushStrokePoint pToAdd;
|
|
|
|
|
pToAdd.pos = catmull_rom(p0.pos, p1.pos, p2.pos, p3.pos, t);
|
|
|
|
|
pToAdd.width = std::lerp(p1.width, p2.width, t);
|
|
|
|
|
if(!(std::isnan(pToAdd.pos.x()) || std::isnan(pToAdd.pos.y()))) // Don't add NAN values
|
|
|
|
|
toRet.emplace_back(pToAdd);
|
|
|
|
|
}
|
|
|
|
|
p0 = p1;
|
|
|
|
|
p1 = p2;
|
|
|
|
|
p2 = p3;
|
|
|
|
|
}
|
|
|
|
|
toRet.emplace_back(points[endIndex]);
|
|
|
|
|
|
|
|
|
|
return toRet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 17:00:28 +03:00
|
|
|
std::vector<DrawBrushStrokePoint> DrawBrushStroke::smooth_points_avg(size_t beginIndex, size_t endIndex, unsigned numOfDivisions) {
|
2025-07-29 14:11:36 +03:00
|
|
|
std::vector<DrawBrushStrokePoint>& points = d->points;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
size_t pointsSize = endIndex - beginIndex + 1;
|
|
|
|
|
if(pointsSize < 2)
|
|
|
|
|
return {points[beginIndex], points[endIndex]};
|
|
|
|
|
|
|
|
|
|
std::vector<DrawBrushStrokePoint> toRet;
|
|
|
|
|
|
|
|
|
|
for(size_t i = beginIndex; i < endIndex; i++) {
|
|
|
|
|
const DrawBrushStrokePoint& p1 = points[i];
|
|
|
|
|
const DrawBrushStrokePoint& p2 = points[i + 1];
|
|
|
|
|
|
|
|
|
|
float tMove = 1.0 / static_cast<float>(numOfDivisions);
|
|
|
|
|
for(unsigned d = 0; d < numOfDivisions; d++) {
|
|
|
|
|
float t = tMove * static_cast<float>(d);
|
|
|
|
|
DrawBrushStrokePoint pToAdd;
|
|
|
|
|
pToAdd.pos = lerp_vec(p1.pos, p2.pos, t);
|
|
|
|
|
pToAdd.width = std::lerp(p1.width, p2.width, t);
|
|
|
|
|
if(!(std::isnan(pToAdd.pos.x()) || std::isnan(pToAdd.pos.y()))) // Don't add NAN values
|
|
|
|
|
toRet.emplace_back(pToAdd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toRet.emplace_back(points[endIndex]);
|
|
|
|
|
|
|
|
|
|
return toRet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 13:28:01 +03:00
|
|
|
void DrawBrushStroke::create_triangles(const std::function<bool(Vector2f, Vector2f, Vector2f)>& passTriangleFunc, const std::vector<DrawBrushStrokePoint>& points, unsigned skipVertexCount, std::shared_ptr<SkPath> bPath) {
|
2025-07-06 20:43:53 +03:00
|
|
|
const int ARC_SMOOTHNESS = 10;
|
|
|
|
|
const int CIRCLE_SMOOTHNESS = 20;
|
2025-07-29 14:11:36 +03:00
|
|
|
std::vector<DrawBrushStrokePoint>& pointsN = d->points;
|
2025-08-23 13:28:01 +03:00
|
|
|
std::vector<SkPoint> topPoints;
|
|
|
|
|
std::vector<SkPoint> bottomPoints;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2025-07-29 14:11:36 +03:00
|
|
|
if(pointsN.size() == 1 && d->hasRoundCaps) {
|
2025-07-06 20:43:53 +03:00
|
|
|
std::vector<Vector2f> circlePoints = gen_circle_points(pointsN.front().pos, pointsN.front().width / 2.0f, CIRCLE_SMOOTHNESS);
|
2025-08-23 13:28:01 +03:00
|
|
|
for(size_t i = 0; i < circlePoints.size(); i++) {
|
2025-07-06 20:43:53 +03:00
|
|
|
if(passTriangleFunc(pointsN.front().pos, circlePoints[i], circlePoints[(i + 1) % circlePoints.size()])) return;
|
2025-08-23 13:28:01 +03:00
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(circlePoints[i]));
|
|
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-23 13:28:01 +03:00
|
|
|
if(pointsN.size() < 2) {
|
2025-08-25 20:34:02 +03:00
|
|
|
if(bPath && !topPoints.empty())
|
|
|
|
|
bPath->addPoly(topPoints.data(), topPoints.size(), false);
|
2025-07-06 20:43:53 +03:00
|
|
|
return;
|
2025-08-23 13:28:01 +03:00
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
std::vector<size_t> wedgeIndices = get_wedge_indices(points);
|
|
|
|
|
for(size_t i = 0; i < wedgeIndices.size() - 1; i++) {
|
2025-07-14 17:00:28 +03:00
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
size_t pointsBegin = wedgeIndices[i];
|
|
|
|
|
size_t pointsEnd = wedgeIndices[i + 1];
|
|
|
|
|
|
|
|
|
|
std::array<Vector2f, 3> vertArray;
|
|
|
|
|
size_t newIndex = 0;
|
|
|
|
|
|
|
|
|
|
Vector2f perpPrev = perpendicular_vec2((points[pointsBegin + 1].pos - points[pointsBegin].pos).eval());
|
|
|
|
|
perpPrev.normalize();
|
|
|
|
|
|
|
|
|
|
vertArray[0] = points[pointsBegin].pos + perpPrev * points[pointsBegin].width * 0.5f;
|
2025-08-23 13:28:01 +03:00
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(vertArray[0]));
|
2025-07-06 20:43:53 +03:00
|
|
|
vertArray[1] = points[pointsBegin].pos - perpPrev * points[pointsBegin].width * 0.5f;
|
2025-08-23 13:28:01 +03:00
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(vertArray[1]));
|
2025-07-06 20:43:53 +03:00
|
|
|
newIndex = 2;
|
|
|
|
|
|
2025-07-29 14:11:36 +03:00
|
|
|
if(d->hasRoundCaps && pointsBegin == 0) {
|
2025-07-06 20:43:53 +03:00
|
|
|
Vector2f arcDirStart = (convert_vec2<Vector2f>(vertArray[0]) - points[0].pos).normalized();
|
|
|
|
|
Vector2f arcDirEnd = (convert_vec2<Vector2f>(vertArray[1]) - points[0].pos).normalized();
|
|
|
|
|
Vector2f arcDirCenter = (points[0].pos - points[1].pos).normalized();
|
|
|
|
|
std::vector<Vector2f> arcPoints = arc_vec<float>(points[0].pos, arcDirStart, arcDirEnd, arcDirCenter, points[0].width * 0.5f, ARC_SMOOTHNESS);
|
|
|
|
|
arcPoints.emplace(arcPoints.begin(), convert_vec2<Vector2f>(vertArray[0]));
|
|
|
|
|
arcPoints.emplace_back(convert_vec2<Vector2f>(vertArray[1]));
|
2025-08-23 13:28:01 +03:00
|
|
|
for(size_t i = 0; i < arcPoints.size() - 1; i++) {
|
2025-07-06 20:43:53 +03:00
|
|
|
if(passTriangleFunc(points[0].pos, arcPoints[i], arcPoints[i + 1])) return;
|
2025-08-23 13:28:01 +03:00
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(arcPoints[i]));
|
|
|
|
|
}
|
|
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(arcPoints.back()));
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(size_t j = pointsBegin + 1; j < pointsEnd; j++) {
|
2025-07-14 17:00:28 +03:00
|
|
|
j += skipVertexCount;
|
|
|
|
|
if(j >= pointsEnd)
|
|
|
|
|
j = pointsEnd - 1;
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
Vector2f v1 = points[j - 1].pos - points[j].pos;
|
|
|
|
|
Vector2f v2 = points[j].pos - points[j + 1].pos;
|
|
|
|
|
Vector2f perp = perpendicular_vec2((v1 + v2).eval()).normalized();
|
|
|
|
|
if(std::isnan(perp.dot(perpPrev))) {
|
|
|
|
|
std::cout << "NAN FOUND" << std::endl;
|
|
|
|
|
std::cout << v1 << std::endl;
|
|
|
|
|
std::cout << v2 << std::endl;
|
|
|
|
|
std::cout << perp << std::endl;
|
|
|
|
|
std::cout << perpPrev << std::endl;
|
|
|
|
|
throw 1;
|
|
|
|
|
}
|
|
|
|
|
if(perp.dot(perpPrev) < 0.0)
|
|
|
|
|
perp = -perp;
|
|
|
|
|
|
|
|
|
|
vertArray[newIndex] = points[j].pos + perp * 0.5f * points[j].width;
|
2025-08-23 13:28:01 +03:00
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(vertArray[newIndex]));
|
2025-07-06 20:43:53 +03:00
|
|
|
newIndex = (newIndex + 1) % 3;
|
|
|
|
|
if(passTriangleFunc(vertArray[0], vertArray[1], vertArray[2])) return;
|
|
|
|
|
|
|
|
|
|
vertArray[newIndex] = points[j].pos - perp * 0.5f * points[j].width;
|
2025-08-23 13:28:01 +03:00
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(vertArray[newIndex]));
|
2025-07-06 20:43:53 +03:00
|
|
|
newIndex = (newIndex + 1) % 3;
|
|
|
|
|
if(passTriangleFunc(vertArray[0], vertArray[1], vertArray[2])) return;
|
|
|
|
|
|
|
|
|
|
perpPrev = perp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2f rectDir = points[pointsEnd - 1].pos - points[pointsEnd].pos;
|
|
|
|
|
rectDir.normalize();
|
|
|
|
|
Vector2f perp = perpendicular_vec2(rectDir);
|
|
|
|
|
if(perp.dot(perpPrev) < 0.0)
|
|
|
|
|
perp = -perp;
|
|
|
|
|
vertArray[newIndex] = points[pointsEnd].pos + perp * points[pointsEnd].width * 0.5;
|
2025-08-23 13:28:01 +03:00
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(vertArray[newIndex]));
|
2025-07-06 20:43:53 +03:00
|
|
|
newIndex = (newIndex + 1) % 3;
|
|
|
|
|
if(passTriangleFunc(vertArray[0], vertArray[1], vertArray[2])) return;
|
|
|
|
|
|
|
|
|
|
vertArray[newIndex] = points[pointsEnd].pos - perp * points[pointsEnd].width * 0.5;
|
2025-08-23 13:28:01 +03:00
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(vertArray[newIndex]));
|
2025-07-06 20:43:53 +03:00
|
|
|
newIndex = (newIndex + 1) % 3;
|
|
|
|
|
if(passTriangleFunc(vertArray[0], vertArray[1], vertArray[2])) return;
|
|
|
|
|
|
|
|
|
|
// Render wedge
|
|
|
|
|
if(pointsEnd != points.size() - 1) {
|
|
|
|
|
// Find 4 points involved in intersection
|
|
|
|
|
Vector2f currentP1 = points[pointsEnd].pos - perp * points[pointsEnd].width * 0.5;
|
|
|
|
|
Vector2f currentP2 = points[pointsEnd].pos + perp * points[pointsEnd].width * 0.5;
|
|
|
|
|
Vector2f nextRectDir = points[pointsEnd + 1].pos - points[pointsEnd].pos;
|
|
|
|
|
nextRectDir.normalize();
|
|
|
|
|
Vector2f nextPerp = perpendicular_vec2(nextRectDir);
|
|
|
|
|
Vector2f nextP1 = points[pointsEnd].pos - nextPerp * points[pointsEnd].width * 0.5;
|
|
|
|
|
Vector2f nextP2 = points[pointsEnd].pos + nextPerp * points[pointsEnd].width * 0.5;
|
|
|
|
|
|
|
|
|
|
// Find center of wedge
|
|
|
|
|
Vector2f wedgeCenter = line_line_intersection(currentP1, currentP2, nextP1, nextP2);
|
|
|
|
|
|
|
|
|
|
Vector2f wedgeP1 = ((currentP1 - currentP2).dot(nextRectDir)) <= 0.0 ? currentP1 : currentP2;
|
|
|
|
|
Vector2f wedgeP2 = ((nextP1 - nextP2).dot(rectDir)) <= 0.0 ? nextP1 : nextP2;
|
2025-08-23 13:28:01 +03:00
|
|
|
bool isTopWedge = wedgeP1 == convert_vec2<Vector2f>(topPoints.back());
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
float arcRadius;
|
|
|
|
|
|
|
|
|
|
std::vector<Vector2f> arcPoints;
|
|
|
|
|
|
|
|
|
|
if(wedgeCenter.x() == std::numeric_limits<float>::max()) {
|
|
|
|
|
wedgeCenter = (currentP1 + currentP2) * 0.5;
|
|
|
|
|
wedgeP1 = currentP1;
|
|
|
|
|
wedgeP2 = currentP2;
|
|
|
|
|
arcRadius = points[pointsEnd].width * 0.5;
|
|
|
|
|
arcPoints = arc_vec(wedgeCenter, (wedgeP1 - wedgeCenter).normalized(), (wedgeP2 - wedgeCenter).normalized(), (-rectDir).eval(), arcRadius, ARC_SMOOTHNESS);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
arcRadius = (wedgeCenter - wedgeP1).norm();
|
|
|
|
|
arcPoints = arc_vec(wedgeCenter, (wedgeP1 - wedgeCenter).normalized(), (wedgeP2 - wedgeCenter).normalized(), lerp_vec((-rectDir).eval(), (-nextRectDir).eval(), 0.5).normalized(), arcRadius, ARC_SMOOTHNESS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arcPoints.emplace(arcPoints.begin(), wedgeP1);
|
|
|
|
|
arcPoints.emplace_back(wedgeP2);
|
2025-08-23 13:28:01 +03:00
|
|
|
for(size_t i = 0; i < arcPoints.size() - 1; i++) {
|
2025-07-06 20:43:53 +03:00
|
|
|
if(passTriangleFunc(wedgeCenter, arcPoints[i], arcPoints[i + 1])) return;
|
2025-08-23 13:28:01 +03:00
|
|
|
if(isTopWedge)
|
|
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(arcPoints[i]));
|
|
|
|
|
else
|
|
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(arcPoints[i]));
|
|
|
|
|
}
|
|
|
|
|
if(isTopWedge)
|
|
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(arcPoints.back()));
|
|
|
|
|
else
|
|
|
|
|
bottomPoints.emplace_back(convert_vec2<SkPoint>(arcPoints.back()));
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
2025-07-29 14:11:36 +03:00
|
|
|
else if(d->hasRoundCaps) { // It's the last point, cap it off
|
2025-07-06 20:43:53 +03:00
|
|
|
Vector2f arcDirStart = (convert_vec2<Vector2f>(vertArray[(newIndex + 1) % 3]) - points.back().pos).normalized();
|
|
|
|
|
Vector2f arcDirEnd = (convert_vec2<Vector2f>(vertArray[(newIndex + 2) % 3]) - points.back().pos).normalized();
|
|
|
|
|
Vector2f arcDirCenter = -rectDir;
|
|
|
|
|
std::vector<Vector2f> arcPoints = arc_vec<float>(points.back().pos, arcDirStart, arcDirEnd, arcDirCenter, points.back().width * 0.5f, ARC_SMOOTHNESS);
|
|
|
|
|
arcPoints.emplace(arcPoints.begin(), convert_vec2<Vector2f>(vertArray[(newIndex + 1) % 3]));
|
|
|
|
|
arcPoints.emplace_back(convert_vec2<Vector2f>(vertArray[(newIndex + 2) % 3]));
|
2025-08-23 13:28:01 +03:00
|
|
|
for(size_t i = 0; i < arcPoints.size() - 1; i++) {
|
2025-07-06 20:43:53 +03:00
|
|
|
if(passTriangleFunc(points.back().pos, arcPoints[i], arcPoints[i + 1])) return;
|
2025-08-23 13:28:01 +03:00
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(arcPoints[i]));
|
|
|
|
|
}
|
|
|
|
|
topPoints.emplace_back(convert_vec2<SkPoint>(arcPoints.back()));
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-23 13:28:01 +03:00
|
|
|
|
|
|
|
|
if(bPath && !topPoints.empty()) {
|
|
|
|
|
topPoints.insert(topPoints.begin(), bottomPoints.rbegin(), bottomPoints.rend());
|
2025-08-25 20:34:02 +03:00
|
|
|
bPath->addPoly(topPoints.data(), topPoints.size(), false);
|
2025-08-23 13:28:01 +03:00
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:25:28 +03:00
|
|
|
void DrawBrushStroke::create_collider() {
|
2025-07-06 20:43:53 +03:00
|
|
|
using namespace SCollision;
|
2025-07-29 14:11:36 +03:00
|
|
|
if(d->points.empty())
|
2025-07-14 17:00:28 +03:00
|
|
|
return;
|
2025-07-06 20:43:53 +03:00
|
|
|
ColliderCollection<float> strokeObjects;
|
2025-07-29 14:11:36 +03:00
|
|
|
std::vector<DrawBrushStrokePoint> points = smooth_points(0, d->points.size() - 1, DEFAULT_SMOOTHNESS);
|
2025-07-06 20:43:53 +03:00
|
|
|
create_triangles([&](Vector2f a, Vector2f b, Vector2f c) {
|
|
|
|
|
strokeObjects.triangle.emplace_back(a, b, c);
|
|
|
|
|
return false;
|
2025-08-23 13:28:01 +03:00
|
|
|
}, points, 0, nullptr);
|
2025-07-06 20:43:53 +03:00
|
|
|
strokeObjects.recalculate_bounds();
|
2025-07-28 17:07:39 +03:00
|
|
|
|
|
|
|
|
SCollision::BVHContainer<float> collisionTree;
|
|
|
|
|
|
2025-07-31 09:34:17 +03:00
|
|
|
collisionTree.calculate_bvh_recursive(strokeObjects, AABB_PRECHECK_BVH_LEVELS + 1);
|
2025-07-06 20:43:53 +03:00
|
|
|
precheckAABBLevels.clear();
|
2025-07-31 09:34:17 +03:00
|
|
|
add_precheck_aabb_level(AABB_PRECHECK_BVH_LEVELS, collisionTree.children);
|
2025-07-28 14:25:28 +03:00
|
|
|
|
2025-07-28 17:07:39 +03:00
|
|
|
bounds = collisionTree.objects.bounds;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawBrushStroke::add_precheck_aabb_level(size_t level, const std::vector<SCollision::BVHContainer<float>>& levelArray) {
|
|
|
|
|
for(const auto& a : levelArray) {
|
2025-07-31 09:34:17 +03:00
|
|
|
if(level == 0 || a.children.empty())
|
2025-07-06 20:43:53 +03:00
|
|
|
precheckAABBLevels.emplace_back(a.objects.bounds);
|
|
|
|
|
else
|
2025-07-31 09:34:17 +03:00
|
|
|
add_precheck_aabb_level(level - 1, a.children);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawBrushStroke::update(DrawingProgram& drawP) {
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:25:28 +03:00
|
|
|
bool DrawBrushStroke::collides_within_coords(const SCollision::ColliderCollection<float>& checkAgainst) {
|
2025-07-28 17:07:39 +03:00
|
|
|
if(!SCollision::collide(checkAgainst, bounds))
|
2025-07-06 20:43:53 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool deepPrecheck = false;
|
|
|
|
|
for(auto& aabb : precheckAABBLevels) {
|
|
|
|
|
if(SCollision::collide(checkAgainst, aabb)) {
|
|
|
|
|
deepPrecheck = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!deepPrecheck && !precheckAABBLevels.empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool toRet = false;
|
2025-07-29 14:11:36 +03:00
|
|
|
std::vector<DrawBrushStrokePoint> points = smooth_points(0, d->points.size() - 1, DEFAULT_SMOOTHNESS);
|
2025-07-06 20:43:53 +03:00
|
|
|
create_triangles([&](Vector2f a, Vector2f b, Vector2f c) {
|
|
|
|
|
toRet = SCollision::collide(checkAgainst, SCollision::Triangle<float>{a, b, c});
|
|
|
|
|
return toRet;
|
2025-08-23 13:28:01 +03:00
|
|
|
}, points, 0, nullptr);
|
2025-07-06 20:43:53 +03:00
|
|
|
return toRet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SCollision::AABB<float> DrawBrushStroke::get_obj_coord_bounds() const {
|
2025-07-28 17:07:39 +03:00
|
|
|
return bounds;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|