Skip to content

Optimize parsing in primitive.py#37

Merged
chenzhaiyu merged 2 commits intomainfrom
codex/issue-26-primitive-optimization
Mar 12, 2026
Merged

Optimize parsing in primitive.py#37
chenzhaiyu merged 2 commits intomainfrom
codex/issue-26-primitive-optimization

Conversation

@chenzhaiyu
Copy link
Copy Markdown
Owner

Performance difference

Benchmarks were run on the provided .vg fixtures using default VertexGroup processing, reporting mean runtime over 15 runs.

Dataset Before After Speedup
test_points.vg 0.013251s 0.011740s 11.41%
test_church.vg 0.043811s 0.030116s 31.26%

Validation data

Behavioral equivalence (before vs after) was validated on all provided fixtures:

  • tutorials/data/test_points.vg
  • tutorials/data/test_church.vg
  • tutorials/data/test_points.bvg
  • tutorials/data/test_church.bvg

Closes #26

Copilot AI review requested due to automatic review settings March 12, 2026 16:13
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81efa3e68e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread abspy/primitive.py Outdated
eig_vec = pca.components_
points_trans = pca.transform(points)
# equivalent to pca.transform(points) but avoids repeated input validation
points_trans = points @ eig_vec.T - (pca.mean_ @ eig_vec.T)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Center points before projection to avoid cancellation

Computing points_trans as points @ eig_vec.T - (pca.mean_ @ eig_vec.T) is algebraically equivalent to pca.transform(points), but it is numerically less stable because it subtracts two potentially large projected values; with large absolute coordinates and small local spread (common in geospatial scans), this can lose precision and distort point_min/point_max, producing inaccurate OBBs. Using (points - pca.mean_) @ eig_vec.T keeps the optimization while preserving the stable centering order.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR focuses on improving runtime performance in abspy/primitive.py by reducing Python-level overhead during vertex-group parsing and plane fitting, aligning with the broader goal of speeding up VertexGroup processing.

Changes:

  • Optimizes point extraction in get_points() by slicing once instead of accumulating lines.
  • Optimizes primitive/parameter line discovery and parameter parsing in get_primitives().
  • Speeds up PCA-based OBB computation in fit_plane() by avoiding pca.transform() input validation overhead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread abspy/primitive.py Outdated
Comment on lines 252 to 253
param = np.fromstring(parameters[i][18:-1], sep=' ')
aabb = self._points_bound(self.points)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Parsing group parameters via parameters[i][18:-1] drops the final character. For .bvg inputs (where lines come from split('\n')) and for any vgroup_ascii without a trailing newline, this will silently truncate the last parameter (e.g., 0.12 -> 0.1), yielding incorrect plane parameters. Prefer extracting the substring after the group_parameters: prefix and stripping whitespace/newlines instead of fixed slicing.

Copilot uses AI. Check for mistakes.
Comment thread abspy/primitive.py Outdated
Comment on lines 261 to 262
param = np.fromstring(parameters[i][18:-1], sep=' ')
_, obb = self.fit_plane(points, mode='PCA')
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Same issue as the empty-group branch: parameters[i][18:-1] truncates the last character of the last numeric token when the line does not end with a newline (notably .bvg parsing). This can change d (and any other last value) and lead to incorrect planes/bounds. Use prefix-based extraction plus .strip() rather than positional slicing.

Copilot uses AI. Check for mistakes.
Comment thread abspy/primitive.py
Comment on lines +403 to 405
# equivalent to pca.transform(points) but avoids repeated input validation
points_trans = points @ eig_vec.T - (pca.mean_ @ eig_vec.T)
point_min = np.amin(points_trans, axis=0)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

fit_plane now computes points_trans with points @ eig_vec.T ..., which requires points to be a NumPy array supporting @. Previously pca.transform(points) accepted any array-like input (lists, tuples) via scikit-learn validation. Since fit_plane is a documented public API, consider converting points = np.asarray(points) (or falling back to pca.transform) to avoid an unintended API regression.

Copilot uses AI. Check for mistakes.
@chenzhaiyu chenzhaiyu merged commit 8f749f4 into main Mar 12, 2026
4 checks passed
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.

parallelize some parts of code

2 participants