-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/separate world shape #336
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
Open
sjcl
wants to merge
5
commits into
master
Choose a base branch
from
feat/separate-world-shape
base: master
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0eff7cb
Add world preprocessor
sjcl b51f7bd
Separate world shapefile
sjcl a3c4e1b
Fix WEB_MERCATOR_MAX_LATITUD excessive precision
sjcl d5b3226
Update ci assets path to shapefiles_v2
sjcl 851c843
Rename saibunkuiki to saibunkuiki_line to make it clear used only for
sjcl 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 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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| #![allow(clippy::type_complexity)] | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use shapefile::{Shape, ShapeReader}; | ||
|
|
||
| use crate::math::*; | ||
|
|
||
| const WEB_MERCATOR_MAX_LATITUD: f32 = 85.051_13; | ||
|
|
||
| struct VertexBuffer { | ||
| buffer: Vec<(Of32, Of32)>, | ||
| dict: HashMap<(Of32, Of32), usize>, | ||
| } | ||
|
|
||
| impl VertexBuffer { | ||
| fn new() -> Self { | ||
| Self { | ||
| buffer: Default::default(), | ||
| dict: Default::default(), | ||
| } | ||
| } | ||
|
|
||
| fn insert(&mut self, v: (Of32, Of32)) -> usize { | ||
| match self.dict.get(&v) { | ||
| Some(index) => *index, | ||
| None => { | ||
| self.buffer.push(v); | ||
| let index = self.buffer.len() - 1; | ||
| self.dict.insert(v, index); | ||
| index | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn into_buffer(self) -> Vec<(f32, f32)> { | ||
| self.buffer.into_iter().map(|(x, y)| (x.0, y.0)).collect() | ||
| } | ||
| } | ||
|
|
||
| /// 極点での発散を避けるため、球面Web Mercatorで一般的に使用される打ち切り緯度 ±85.05113° を、レンダラーの描画上限として採用する。 | ||
| /// 経度はそのまま返す。 | ||
| fn clamp_mercator_latitude(point: Point) -> Point { | ||
| let latitude = point | ||
| .latitude | ||
| .0 | ||
| .clamp(-WEB_MERCATOR_MAX_LATITUD, WEB_MERCATOR_MAX_LATITUD); | ||
|
|
||
| Point::new(Of32::from(latitude), point.longitude) | ||
| } | ||
|
|
||
| struct WorldRings { | ||
| rings: Vec<Ring>, | ||
| } | ||
|
|
||
| impl WorldRings { | ||
| fn from_shape(shape: Shape) -> Self { | ||
| let Shape::Polygon(polygon) = shape else { | ||
| panic!("world shapefile contains a non-Polygon shape"); | ||
| }; | ||
|
|
||
| let rings: Vec<_> = polygon | ||
| .rings() | ||
| .iter() | ||
| .map(|ring| Ring::from(ring.points().to_vec())) | ||
| .collect(); | ||
|
|
||
| Self { rings } | ||
| } | ||
| } | ||
|
|
||
| struct Shapefile { | ||
| entries: Vec<WorldRings>, | ||
| } | ||
|
|
||
| impl Shapefile { | ||
| fn new() -> Self { | ||
| let shp_file = std::fs::File::open("../assets/shapefile/world/world.shp"); | ||
|
|
||
| let Ok(shp_file) = shp_file else { | ||
| panic!( | ||
| r#"EEWBot Renderer requirements is not satisfied. | ||
|
|
||
| World shape file is not found. | ||
| - assets/shapefile/world/world.shp | ||
|
|
||
| Please follow: | ||
| https://github.com/EEWBot/eew-renderer/wiki#shapefile-%E5%85%A5%E6%89%8B%E5%85%88"# | ||
| ) | ||
| }; | ||
|
|
||
| let mut shape_reader = ShapeReader::new(shp_file).unwrap(); | ||
|
|
||
| let entries = shape_reader | ||
| .iter_shapes() | ||
| .map(|shape| shape.expect("Failed to read a shape from world shapefile")) | ||
| .map(WorldRings::from_shape) | ||
| .collect(); | ||
|
|
||
| Self { entries } | ||
| } | ||
| } | ||
|
|
||
| pub fn read() -> ( | ||
| Vec<(f32, f32)>, // vertices | ||
| Vec<u32>, // indices | ||
| ) { | ||
| let shapefile = Shapefile::new(); | ||
|
|
||
| let mut vertex_buffer = VertexBuffer::new(); | ||
|
|
||
| let world_indices: Vec<u32> = shapefile | ||
| .entries | ||
| .iter() | ||
| .flat_map(|world_rings| &world_rings.rings) | ||
| .flat_map(|ring| ring.triangulate()) | ||
| .map(clamp_mercator_latitude) | ||
| .map(|point| { | ||
| u32::try_from(vertex_buffer.insert(point.into())) | ||
| .expect("world shapefile has too many vertices") | ||
| }) | ||
| .collect(); | ||
|
|
||
| let world_vertices = vertex_buffer.into_buffer(); | ||
|
|
||
| assert!( | ||
| !world_vertices.is_empty(), | ||
| "world shapefile produced no vertices" | ||
| ); | ||
| assert!( | ||
| !world_indices.is_empty(), | ||
| "world shapefile produced no indices" | ||
| ); | ||
|
|
||
| (world_vertices, world_indices) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| https://github.com/EEWBot/eew-renderer/wiki に従い、以下のファイルを作成する | ||
| - `assets/shapefile/world/world.shp` |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
本当にここであるべきかについて考えています。
traitで切り出したほうがきれい…?いやそんなことはないか。うーん謎。
まぁ妥当な気もしてきた。
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.
ふむー。謎です。