Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 67 additions & 20 deletions asset-preprocessor/src/parse_shapefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::hash::Hash;

use geo::Simplify;
use itertools::Itertools;
Expand All @@ -12,20 +13,49 @@ use renderer_types::*;

use crate::math::*;

struct VertexBuffer {
buffer: Vec<(Of32, Of32)>,
dict: HashMap<(Of32, Of32), usize>,
struct AreaCodeBuffer {
area_code_to_internal_code: HashMap<u32, u16>,
}

impl VertexBuffer {
impl AreaCodeBuffer {
fn new() -> Self {
Self {
area_code_to_internal_code: Default::default(),
}
}

fn insert(&mut self, area_code: u32) -> u16 {
match self.area_code_to_internal_code.get(&area_code) {
Some(index) => *index,
None => {
let index = self.area_code_to_internal_code.len();
self.area_code_to_internal_code
.insert(area_code, index as u16);
index as u16
}
}
}

fn into_buffer(self) -> HashMap<u32, u16> {
self.area_code_to_internal_code
}
}


struct VertexBuffer<T: Hash + Eq + Clone + Copy> {
buffer: Vec<T>,
dict: HashMap<T, usize>,
}

impl<T: Hash + Eq + Clone + Copy> VertexBuffer<T> {
fn new() -> Self {
Self {
buffer: Default::default(),
dict: Default::default(),
}
}

fn insert(&mut self, v: (Of32, Of32)) -> usize {
fn insert(&mut self, v: T) -> usize {
match self.dict.get(&v) {
Some(index) => *index,
None => {
Expand All @@ -37,8 +67,8 @@ impl VertexBuffer {
}
}

fn into_buffer(self) -> Vec<(f32, f32)> {
self.buffer.into_iter().map(|(x, y)| (x.0, y.0)).collect()
fn into_buffer(self) -> Vec<T> {
self.buffer
}
}

Expand Down Expand Up @@ -206,17 +236,20 @@ pub fn read(
) -> (
HashMap<codes::Area, BoundingBox<GeoDegree>>, // area_bounding_box
HashMap<codes::Area, Vertex<GeoDegree>>, // area_centers
Vec<(f32, f32)>, // vertex_buffer
Vec<(f32, f32, u16)>, // map_vertex_buffer
Vec<(f32, f32)>, // line_vertex_buffer
Vec<u32>, // map_indices
Vec<Vec<u32>>, // area_lines
Vec<Vec<u32>>, // pref_lines
Vec<(f32, usize)>, // scale_level_map
HashMap<codes::Area, u16>, // area_code_to_internal_code
) {
let shapefile = Shapefile::new(
"../assets/shapefile/earthquake_detailed/earthquake_detailed_simplified.shp",
"../assets/shapefile/earthquake_detailed/earthquake_detailed_simplified.dbf",
);
let mut vertex_buffer = VertexBuffer::new();
let mut map_vertex_buffer = VertexBuffer::new();
let mut area_code_buffer = AreaCodeBuffer::new();

// @Siro_256 にゃ~っ…! (ΦωΦ)

Expand All @@ -229,13 +262,23 @@ pub fn read(
.map(|area_rings| (area_rings.area_code, area_rings.bounding_box))
.collect();

let map_indices = shapefile
.entries
.iter()
.flat_map(|area_rings| &area_rings.rings)
.flat_map(|r| r.triangulate())
.map(|p| vertex_buffer.insert(p.into()) as u32)
.collect();
let mut map_indices = vec![];

for entry in &shapefile.entries {
let area_code = entry.area_code;
let internal_code = area_code_buffer.insert(area_code);

map_indices.extend(
entry
.rings
.iter()
.flat_map(|ring| ring.triangulate())
.map(|p| {
let p = Into::<(Of32, Of32)>::into(p);
map_vertex_buffer.insert((p.0, p.1, internal_code)) as u32
}),
);
}

let references = PointReferences::tally_of(&shapefile, area_code__pref_code);

Expand Down Expand Up @@ -308,8 +351,10 @@ pub fn read(
(100.0_f32.powf(0.24), 0.117),
];

let area_lines = gen_lod(&mut vertex_buffer, &lod_details, &area_lines);
let pref_lines = gen_lod(&mut vertex_buffer, &lod_details, &pref_lines);
let mut line_vertex_buffer = VertexBuffer::new();

let area_lines = gen_lod(&mut line_vertex_buffer, &lod_details, &area_lines);
let pref_lines = gen_lod(&mut line_vertex_buffer, &lod_details, &pref_lines);

let scale_level_map = lod_details
.into_iter()
Expand All @@ -324,11 +369,13 @@ pub fn read(
(
area_bounding_box,
area_centers,
vertex_buffer.into_buffer(),
map_vertex_buffer.into_buffer().into_iter().map(|(x, y, code)| (x.0, y.0, code)).collect(),
line_vertex_buffer.into_buffer().into_iter().map(|(x, y)| (x.0, y.0)).collect(),
map_indices,
area_lines,
pref_lines,
scale_level_map,
area_code_buffer.into_buffer(),
)
}
}
Expand Down Expand Up @@ -372,7 +419,7 @@ fn cut_rings(rings: &[&Ring], cut_points: &[Point]) -> Vec<Line> {
}

fn gen_lod(
vertex_buffer: &mut VertexBuffer,
vertex_buffer: &mut VertexBuffer<(Of32, Of32)>,
lod_details: &[(f32, f64)],
base_lines: &[&Line],
) -> Vec<Vec<u32>> {
Expand Down
17 changes: 14 additions & 3 deletions renderer-assets/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ fn main() {
) = station_codes_parser::read(&s);

#[allow(non_snake_case)]
let (area_code__bbox, area_code__centers, vertices, indices, area_lines, pref_lines, scale_level_map) =
parse_shapefile::read(&area_code__pref_code);
let (
area_code__bbox,
area_code__centers,
map_vertices,
line_vertices,
indices,
area_lines,
pref_lines,
scale_level_map,
area_code_to_internal_code,
) = parse_shapefile::read(&area_code__pref_code);

// <AreaCode, (StationIndex, (BBox))>
let areas: HashMap<u32, (usize, (f32, f32, f32, f32))> = area_code__bbox
Expand Down Expand Up @@ -55,8 +64,10 @@ fn main() {
const_declaration!(INTENSITY_STATION_POSITIONS = intensity_station_minimized),
const_declaration!(AREAS = areas),
const_declaration!(STATION_CODES = station_code__index),
const_declaration!(VERTICES = vertices),
const_declaration!(MAP_VERTICES = map_vertices),
const_declaration!(LINE_VERTICES = line_vertices),
const_declaration!(MAP_TRIANGLES = indices),
const_declaration!(AREA_CODE_TO_INTERNAL_CODE = area_code_to_internal_code),
const_declaration!(AREA_LINES = area_lines),
const_declaration!(PREF_LINES = pref_lines),
const_declaration!(SCALE_LEVEL_MAP = scale_level_map),
Expand Down
15 changes: 13 additions & 2 deletions renderer-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use renderer_types::*;
pub struct QueryInterface;

pub struct Geometries {
pub vertices: &'static [(f32, f32)],
pub map_vertices: &'static [(f32, f32, u16)],
pub map_triangles: &'static [u32],
pub line_vertices: &'static [(f32, f32)],
pub area_lines: &'static [&'static [u32]],
pub pref_lines: &'static [&'static [u32]],
}
Expand All @@ -22,8 +23,9 @@ pub struct LakeGeometries {
impl QueryInterface {
pub fn geometries() -> Geometries {
Geometries {
vertices: VERTICES,
map_vertices: MAP_VERTICES,
map_triangles: MAP_TRIANGLES,
line_vertices: LINE_VERTICES,
area_lines: AREA_LINES,
pref_lines: PREF_LINES,
}
Expand All @@ -35,6 +37,15 @@ impl QueryInterface {
indices: LAKE_INDICES,
}
}

pub fn area_code_to_internal_code(area_code: codes::Area) -> Option<u16> {
AREA_CODE_TO_INTERNAL_CODE.get(&area_code).copied()
}

pub fn area_code_count() -> usize {
AREA_CODE_TO_INTERNAL_CODE.len()
}

pub fn query_bounding_box_by_area(area_code: codes::Area) -> Option<BoundingBox<GeoDegree>> {
Some(BoundingBox::from_tuple::<GeoDegree>(
AREAS.get(&area_code)?.1,
Expand Down
6 changes: 3 additions & 3 deletions renderer/src/worker/drawer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn draw<F: ?Sized + Facade, S: ?Sized + Surface>(frame_context: &FrameContex
.map
.draw(
frame_context.surface.borrow_mut().deref_mut(),
&resources.buffer.vertex,
&resources.buffer.map_vertex,
&resources.buffer.map,
&MapUniform {
aspect_ratio,
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn draw<F: ?Sized + Facade, S: ?Sized + Surface>(frame_context: &FrameContex
.border_line
.draw(
frame_context.surface.borrow_mut().deref_mut(),
&resources.buffer.vertex,
&resources.buffer.line_vertex,
resources.buffer.get_area_line_by_scale(scale).unwrap(),
&BorderLineUniform {
dimension,
Expand All @@ -73,7 +73,7 @@ pub fn draw<F: ?Sized + Facade, S: ?Sized + Surface>(frame_context: &FrameContex
.border_line
.draw(
frame_context.surface.borrow_mut().deref_mut(),
&resources.buffer.vertex,
&resources.buffer.line_vertex,
resources.buffer.get_pref_line_by_scale(scale).unwrap(),
&BorderLineUniform {
dimension,
Expand Down
22 changes: 17 additions & 5 deletions renderer/src/worker/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl Resources<'_> {

#[derive(Debug)]
pub struct Buffer {
pub vertex: VertexBuffer<MapVertex>,
pub map_vertex: VertexBuffer<MapVertex>,
pub line_vertex: VertexBuffer<MapVertex>,
area_line: Vec<IndexBuffer<u32>>,
pref_line: Vec<IndexBuffer<u32>>,
pub map: IndexBuffer<u32>,
Expand All @@ -43,15 +44,25 @@ impl Buffer {
fn load<F: ?Sized + Facade>(facade: &F) -> Self {
let geom = renderer_assets::QueryInterface::geometries();

let vertices: Vec<_> = geom
.vertices
let map_vertices: Vec<_> = geom
.map_vertices
.iter()
.map(|v| MapVertex {
position: Vertex::<GeoDegree>::from((v.0, v.1)).to_slice()
})
.collect();

let map_vertex = VertexBuffer::new(facade, &map_vertices).unwrap();

let line_vertices: Vec<_> = geom
.line_vertices
.iter()
.map(|v| MapVertex {
position: Vertex::<GeoDegree>::from(*v).to_slice()
})
.collect();

let vertex = VertexBuffer::new(facade, &vertices).unwrap();
let line_vertex = VertexBuffer::new(facade, &line_vertices).unwrap();

let map =
IndexBuffer::new(facade, PrimitiveType::TrianglesList, geom.map_triangles).unwrap();
Expand All @@ -69,7 +80,8 @@ impl Buffer {
.collect();

Buffer {
vertex,
map_vertex,
line_vertex,
map,
area_line,
pref_line,
Expand Down
Loading