-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【WIP】feat: Add rectangle packing algorithm for texture placement #6
Draft
yud0uhu
wants to merge
1
commit into
main
Choose a base branch
from
feature/atlas-rectpack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod export; | ||
pub mod pack; | ||
pub mod place; | ||
pub mod rectpack; | ||
pub mod texture; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#[derive(Clone, Copy)] | ||
pub struct Rectangle { | ||
pub left: u32, | ||
pub top: u32, | ||
pub right: u32, | ||
pub bottom: u32, | ||
} | ||
|
||
impl Rectangle { | ||
pub fn width(&self) -> u32 { | ||
self.right - self.left | ||
} | ||
|
||
pub fn height(&self) -> u32 { | ||
self.bottom - self.top | ||
} | ||
|
||
pub fn fits(&self, img: &Image) -> bool { | ||
self.width() >= img.width && self.height() >= img.height | ||
} | ||
|
||
pub fn fits_perfectly(&self, img: &Image) -> bool { | ||
self.width() == img.width && self.height() == img.height | ||
} | ||
} | ||
|
||
pub struct Image { | ||
pub id: String, | ||
pub width: u32, | ||
pub height: u32, | ||
} | ||
|
||
pub struct Node { | ||
child: [Option<Box<Node>>; 2], | ||
rect: Rectangle, | ||
image_id: Option<i32>, | ||
} | ||
|
||
impl Node { | ||
pub fn new(rect: Rectangle) -> Self { | ||
Node { | ||
child: [None, None], | ||
rect, | ||
image_id: None, | ||
} | ||
} | ||
|
||
pub fn insert(&mut self, img: &Image) -> Option<&mut Node> { | ||
if let Some(ref mut child0) = self.child[0] { | ||
if let Some(new_node) = child0.insert(img) { | ||
return Some(new_node); | ||
} | ||
} else { | ||
if self.image_id.is_some() { | ||
return None; | ||
} | ||
|
||
if !self.rect.fits(img) { | ||
return None; | ||
} | ||
|
||
if self.rect.fits_perfectly(img) { | ||
// self.image_id = Some(img.id); | ||
return Some(self); | ||
} | ||
|
||
let child0_rect; | ||
let child1_rect; | ||
let dw = self.rect.width() - img.width; | ||
let dh = self.rect.height() - img.height; | ||
|
||
if dw > dh { | ||
child0_rect = Rectangle { | ||
left: self.rect.left, | ||
top: self.rect.top, | ||
right: self.rect.left + img.width - 1, | ||
bottom: self.rect.bottom, | ||
}; | ||
child1_rect = Rectangle { | ||
left: self.rect.left + img.width, | ||
top: self.rect.top, | ||
right: self.rect.right, | ||
bottom: self.rect.bottom, | ||
}; | ||
} else { | ||
child0_rect = Rectangle { | ||
left: self.rect.left, | ||
top: self.rect.top, | ||
right: self.rect.right, | ||
bottom: self.rect.top + img.height - 1, | ||
}; | ||
child1_rect = Rectangle { | ||
left: self.rect.left, | ||
top: self.rect.top + img.height, | ||
right: self.rect.right, | ||
bottom: self.rect.bottom, | ||
}; | ||
} | ||
|
||
self.child[0] = Some(Box::new(Node::new(child0_rect))); | ||
self.child[1] = Some(Box::new(Node::new(child1_rect))); | ||
|
||
if let Some(ref mut child0) = self.child[0] { | ||
return child0.insert(img); | ||
} | ||
} | ||
None | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node::insert
メソッドの改善点Node::insert
メソッドは、ノードに画像を挿入するためのロジックを含んでいますが、image_id
の設定がコメントアウトされています。画像が完璧にフィットする場合にimage_id
を設定する必要があります。