Skip to content

Commit

Permalink
Remove grid cache, added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
tebben committed Jan 18, 2024
1 parent e567827 commit 28ea5cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
39 changes: 30 additions & 9 deletions ctod/core/cog/processor/cog_processor_quantized_mesh_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,30 @@
from ctod.core.cog.processor.grid import generate_grid
from ctod.core.normals import calculate_normals


class CogProcessorQuantizedMeshGrid(CogProcessor):
"""A CogProcessor for a grid based mesh.
- Create grid with 2d vertices and triangles
- Sample height data from COG data and make vertices 3d
- Calculate normals
ToDo: Grids can be stored in a cache, however generating a grid takes 0.7ms on average
"""

def __init__(self):
self.grid_cache = {}
self.ellipsoid: Ellipsoid = WGS84

def process(self, cog_request: CogRequest):
def process(self, cog_request: CogRequest) -> tuple:
"""Process a CogRequest and return the vertices, triangles and normals.
Args:
cog_request (CogRequest): The CogRequest to process.
Returns:
tuple: Generated vertices, triangles and normals
"""

vertices2d, triangles = self.get_grid(25, 25)
height_data_indices = np.floor(vertices2d).astype(int)

Expand All @@ -31,7 +48,17 @@ def process(self, cog_request: CogRequest):

return (vertices_new, triangles_new, normals)

def get_grid(self, num_rows, num_cols):
def get_grid(self, num_rows: int, num_cols: int) -> tuple:
"""Generate a grid of vertices and triangles
Args:
num_rows (int): Amount of rows to generate
num_cols (int): Amount of columns to generate
Returns:
tuple: vertices, triangles
"""

width, height = 255, 255

if num_rows > height:
Expand All @@ -40,12 +67,6 @@ def get_grid(self, num_rows, num_cols):
if num_cols > width:
num_cols = width

if (num_rows, num_cols) in self.grid_cache:
return self.grid_cache[(num_rows, num_cols)]

generated_grid = generate_grid(width, height, num_rows, num_cols)

# Save the generated grid to the cache
self.grid_cache[(num_rows, num_cols)] = generated_grid

return generated_grid
16 changes: 14 additions & 2 deletions ctod/core/cog/processor/grid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import numpy as np

def generate_grid(width, height, num_rows, num_cols):
# Create the grid using NumPy
def generate_grid(width: int, height: int, num_rows: int, num_cols: int) -> tuple:
"""Generate a grid of vertices and triangles
Args:
width (int): Width of the grid
height (int): Height of the grid
num_rows (int): Number of rows to generate
num_cols (int): Number of columns to generate
Returns:
tuple: vertices, triangles
"""

# Create the grid using NumPy
grid_x, grid_y = np.meshgrid(np.linspace(0, width, num_cols + 1), np.linspace(0, height, num_rows + 1))

# Stack the x and y coordinates into a single array
Expand Down

0 comments on commit 28ea5cb

Please sign in to comment.