Skip to content

Feature/algorithm development - #54

Open
MJ-Parson wants to merge 41 commits into
mainfrom
feature/algorithm-development
Open

MJ-Parson wants to merge 41 commits into
mainfrom
feature/algorithm-development

Conversation

@MJ-Parson

Copy link
Copy Markdown

Describe your changes

Adding algorithm development into the main branch. this encapsulates svg and hpgl generation. PDF will be added, as well as a visual requirement in later prs.

Issue/Story ticket number and link

#22
Issue 22: 2D shape Packing Algorithm

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have fulfilled all acceptance criteria.
  • All Tasks have been completed OR notes have been made on the ticket.
  • The solution builds.
  • 100% of tests pass with 80% code coverage from unit tests.
  • Documentation has been updated.

MJ-Parson and others added 30 commits October 1, 2026 14:26
…interpret all stickers as continuous, connecting around the outline.
…raphical or easy representation for him tues.
… but that would be cool. moving to proper algo rewrite.
…s refined through me getting better svg models honestly

@or8655 or8655 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I apologize for how long its taken me to get this review done. I walked through it with Claude Code and apart from the comments I made in the comment I have some more comments/suggestions below:

  • Include a README giving a quick overview of the algorithm, how each version works, and how to run them alongside their tests. Include all your dependencies in a requirements.txt.
  • Is algo_doc.md up to date? It can be a living document where you have a changelog at the top so its easy to tell when the last time it was updated was.
  • You should add a function that generates both a PDF and HPGL file from the same sticker placement. Basically a function one can hit run on and see the entire workflow completed. And that needs error handling too. For example, an HPGL file succeeds but PDF fails.
  • Claude found this while reviewing the branch. The new test in tests/test_svg_input.py: test_svg_y_axis_is_flipped_to_point_up confirms it by currently failing. SVG y-values increase going down, but the rest of the app (packer, HPGL, PDF) treats y as increasing going up. load_sticker_svg copies the SVG coordinates without converting them, so every sticker comes out of app/svg/loader.py mirrored top-to-bottom. Claude recommends adding from shapely.affinity import scale to loader.py and changing line 175 to return scale(Polygon(geometry.exterior), xfact=1, yfact=-1, origin="center") so the loader flips the sticker when it returns.
  • The current packing algorithms don't ingest the size of a sticker. They need to be modified to accept a sticker size (height and width) and ensure they pack using that sticker size.

assert "PU10,0;" in content
assert "PD10,5,0,5,0,0;" in content
assert "PU;" in content
assert "SP0;" in content

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A closed sticker outline would have to return to 10,0 since that's where you started (line 19). I think you're missing that in this test.

commands.append(f"PU{start_x},{start_y};")

#lower pen
remaining_points = points[1:-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need all the remaining points for the plotter to return to its starting point. By returning -1 we'd be missing the final edge of the sticker. Is there something I'm missing?

@MJ-Parson MJ-Parson Nov 15, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for array indexing, -1 is the last point in the array. it's not just reporting the int -1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The [1:-1] means that we are returning a slice of the original array from the second element to the second to last element. The -1 means we do not include the final element of the array. Why did you chose to not include the final element of the array?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is incorrect, like I had said. It slices the second element to the last element of the array. What are you saying?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last element in the array is the starting point. It will already include the last element by nature of starting there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the sticker-business/packing-algorithm/ should be the only home for the algorithm. Is there a need to keep the original root level algorithm folder?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there's not, but it has some of my important code and pieces and I wanted to ensure it stuck around for a little. I'll remove it when I can.

_, bottom, right, top = candidate.bounds
score = (top,right,bottom, x,y, angle)
candidates.append((score, candidate))
if candidates:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, we ignore any stickers that don't make it on to the sheet. But if our algorithm occasionally fails to fit a sticker on a sheet, the algorithm needs to raise an Error so the frontend notifies the user.
I recommend giving the algorithm a pass focused on Error handling so failures or unexpected behaviors while running the algorithm can notify the frontend when integration happens.
I see this change requiring system-level considerations. If you think this decision merits team input, we can try to set something up with the team before Tuesday.

while y < max_y:
x = min_x
while x < max_x:
for sticker in stickers:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this supposed to be remaining and not stickers.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's stickers. This is double checking placements of all possible stickers in that set. We're also not really worrying about greedy rn anyway, this is just the first pass algo. I'll revisit during break.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case the remaining variable in line 27 is unused.


FIXTURES = Path(__file__).parent / "fixtures"

def test_svg_loads_as_one_nonempty_sticker():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another function on line 67 with the same name.

@@ -0,0 +1,28 @@
[build-system]
requires = ["setuptools>=75"]
build-backed = "setuptools.build_meta"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be backend and not backed?

]

[tool.pytest.ini_options]
testpaths = ["test"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: the testing folder is named tests

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch!

from shapely.affinity import rotate, translate
from app.models.sticker import Sticker

PAGE_SIZE = (5.5*inch, 8.5*inch)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a global constant that the packer, PDF and HPGL generators all use. Then we can also use it to validate the output of the algorithm actually fits on a sheet. It should by design, but its not a bad idea to do a check on the output.

def save_hpgl(
placements,
output,
units_per_unit=1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this variable so the input and output units are obvious at a glance.

We convert from SVG units to HPGL points. The SVG units are also converted into PDF points in the PDF generator to map the sticker on to the sheet. We need the conversion of units between SVG, PDF and HPGL to be explicit. A PDF unit is 1/72 in and an HPGL unit is 1/1016 in. These values should be stored as a global constants alongside the default size of PDF half sheet, so we can easily convert between the units.

The unit_per_unit field should then become HPGL units / PDF units by default.

HPGL_UNITS_PER_INCH = 1016
PDF_POINTS_PER_INCH = 72

@or8655

or8655 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

also, remember to follow our git commit message style in future commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants