feat: detect 3D cube shapes in neural network diagrams - #63
Open
ruanchenbin3 wants to merge 1 commit into
Open
Conversation
Add detection of 3D cube projections commonly found in neural network architecture diagrams for representing tensors/feature maps. Changes: - Add cube to DRAWIO_STYLES (using isoCube shape) - Add cube to VECTOR_TYPES - Add _detect_cube_from_elements: merges 3 adjacent rectangles/ parallelograms into a single cube element - Add _is_cube_triplet: heuristic check for cube topology - Hook cube detection into the processing pipeline
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds support for identifying and rendering 3D “cube” (feature-map/tensor) shapes by introducing a cube style and a post-processing pass that heuristically merges three adjacent face elements into a single cube element.
Changes:
- Added DrawIO style mapping and vectorization support for a new
cubeshape type. - Added a post-processing step in
process()to merge detected cube face triplets. - Implemented
_detect_cube_from_elements()and_is_cube_triplet()heuristics for cube detection/merging.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1609
to
+1621
| """检测3个相邻矩形/平行四边形是否构成3D立方体投影。 | ||
|
|
||
| 神经网络结构图中,3D tensor/feature map通常表示为3个相邻面的平行投影: | ||
| - 一个正面矩形(大) | ||
| - 一个顶面平行四边形(梯形) | ||
| - 一个侧面平行四边形 | ||
|
|
||
| 如果3个元素满足: | ||
| 1. 每个都是矩形/平行四边形 | ||
| 2. 两两共享一条边 | ||
| 3. 相对位置构成透视投影 | ||
| → 合并为一个cube元素。 | ||
| """"" |
Comment on lines
+1627
to
+1635
| # 收集所有矩形/平行四边形元素 | ||
| rects = [] | ||
| for elem in elements: | ||
| shape = elem.bbox or elem.shape_type | ||
| if not elem.bbox: | ||
| continue | ||
| b = elem.bbox | ||
| if hasattr(b, '__iter__'): | ||
| rects.append((elem, list(b))) |
Comment on lines
+1640
to
+1649
| # 暴力搜索:检查每3个元素是否构成cube | ||
| for i in range(len(rects)): | ||
| if i in merged: | ||
| continue | ||
| for j in range(i+1, len(rects)): | ||
| if j in merged: | ||
| continue | ||
| for k in range(j+1, len(rects)): | ||
| if k in merged: | ||
| continue |
| if len(elements) < 3: | ||
| return elements | ||
|
|
||
| import numpy as np |
Comment on lines
+1707
to
+1710
| # 至少两对相邻(距离==0) | ||
| adjacents = sum(1 for d in [d12, d13, d23] if d < 5) | ||
| if adjacents < 2: | ||
| return False |
Comment on lines
+1720
to
+1722
| # 最大面不应超过最小面的10倍 | ||
| if min_a > 0 and max_a / min_a > 10: | ||
| return False |
Comment on lines
+1417
to
+1419
| # 检测3D立方体(3个相邻矩形合并为cube) | ||
| if hasattr(self, '_detect_cube_from_elements') and context.elements: | ||
| context.elements = self._detect_cube_from_elements(context.elements) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds detection of 3D cube projections commonly found in neural network architecture diagrams for representing tensors/feature maps.
Changes
cubetoDRAWIO_STYLES(usingisoCubeshape)cubetoVECTOR_TYPES_detect_cube_from_elements(): merges 3 adjacent rectangles/ parallelograms into a single cube element using geometric heuristics_is_cube_triplet(): validates that 3 elements form a valid 3D cube projectionTesting
Tested on synthetic images with cube patterns. The heuristic checks:
Related
Closes the issue where 3D cube shapes were not recognized as editable shapes in neural network diagrams.