Add normalize object coordinates

This commit is contained in:
Yousef Khadadeh 2026-05-28 16:55:58 +03:00
parent 54fb48cec7
commit afc672210c
7 changed files with 33 additions and 1 deletions

View file

@ -129,7 +129,7 @@ std::optional<SkPath> skpath_simplify_only_lines(const SkPath& skPath) {
if(!clipper.Execute(ClipType::Union, skPath.getFillType() == SkPathFillType::kEvenOdd ? FillRule::EvenOdd : FillRule::NonZero, solutionPaths))
return std::nullopt;
SimplifyPaths(solutionPaths, 0.01);
SimplifyPaths(solutionPaths, CLIPPER_SIMPLIFY_EPSILON);
SkPathBuilder newPath;
newPath.setFillType(skPath.getFillType());

View file

@ -78,6 +78,9 @@ bool CanvasComponent::can_erase_detail() const {
void CanvasComponent::simplify_paths() {
}
void CanvasComponent::normalize_object_coordinates(CoordSpaceHelper& coords) {
}
std::vector<CanvasComponentContainer*> CanvasComponent::attempt_split(DrawingProgram& drawP) const { return {}; }
CanvasComponentEraseDetailResult CanvasComponent::erase_detail(const SkPath& eraseAgainst) {

View file

@ -44,6 +44,7 @@ class CanvasComponent {
virtual std::vector<CanvasComponentContainer*> attempt_split(DrawingProgram& drawP) const;
virtual void simplify_paths();
virtual std::optional<Vector4f> get_stroke_color() const;
virtual void normalize_object_coordinates(CoordSpaceHelper& coords);
virtual void set_data_from(const CanvasComponent& other) = 0;
virtual std::unique_ptr<CanvasComponent> get_data_copy() const = 0;

View file

@ -104,6 +104,12 @@ void CanvasComponentContainer::load_file(cereal::PortableBinaryInputArchive& a,
compAllocator->comp->load_file(a, version);
compAllocator->comp->compContainer = this;
}
if(version < VersionNumber(0, 6, 0))
normalize_object_coordinates();
}
void CanvasComponentContainer::normalize_object_coordinates() {
get_comp().normalize_object_coordinates(coords);
}
void CanvasComponentContainer::draw(SkCanvas* canvas, const DrawData& drawData) const {

View file

@ -95,6 +95,7 @@ class CanvasComponentContainer {
bool collides_with_point(const CoordSpaceHelper& camCoords, const Vector2f& checkAgainstCam) const;
void send_comp_update(DrawingProgram& drawP, bool finalUpdate);
void scale_up(const WorldScalar& scaleUpAmount);
void normalize_object_coordinates();
std::weak_ptr<DrawingProgramCacheBVHNode> cacheParentBvhNode;
DrawingProgramLayerListItem* parentLayer = nullptr;

View file

@ -311,6 +311,26 @@ std::vector<CanvasComponentContainer*> MeshCanvasComponent::attempt_split(Drawin
return toRet;
}
void MeshCanvasComponent::normalize_object_coordinates(CoordSpaceHelper& coords) {
SkRect pathBounds = d.meshPath.getBounds();
SkPoint center = pathBounds.center();
float maxDimScaleFactor = 1.0 / (std::max(pathBounds.width(), pathBounds.height()) * 0.5f); // Scale must be uniform. This scale factor should force the maximum coordinate = 1 after translation
SkMatrix m = SkMatrix::I();
// Might not be in correct order. Translate to origin first, then scale to normalize coordinates
m.postTranslate(-center.x(), -center.y()).postScale(maxDimScaleFactor, maxDimScaleFactor);
std::optional<SkPath> tryTransformPath = d.meshPath.tryMakeTransform(m);
if(tryTransformPath) {
d.meshPath = tryTransformPath.value();
//for(auto& p : d.meshPath.points())
// std::cout << vec_pretty(convert_vec2<Vector2f>(p)) << std::endl;
WorldVec newCenter = WorldVec{center.x(), center.y()} * coords.inverseScale;
newCenter = rotate_world_coord(newCenter, coords.rotation);
coords.translate(newCenter);
coords.scale_about_double(coords.pos, maxDimScaleFactor);
}
//std::cout << vec_pretty(convert_vec2<Vector2f>(center)) << " " << coords.pos.x().display_int_str(3) << " " << coords.pos.y().display_int_str(3) << " " << coords.inverseScale << std::endl;
}
CanvasComponentEraseDetailResult MeshCanvasComponent::erase_detail(const SkPath& eraseAgainst) {
bool intersectsAABB = d.meshPath.getBounds().intersects(eraseAgainst.getBounds());
if(!intersectsAABB)

View file

@ -37,6 +37,7 @@ class MeshCanvasComponent : public CanvasComponent {
virtual std::optional<Vector4f> get_stroke_color() const override;
virtual void set_data_from(const CanvasComponent& other) override;
virtual void simplify_paths() override;
virtual void normalize_object_coordinates(CoordSpaceHelper& coords) override;
struct Data {
SkPath meshPath;