diff --git a/content/Quadtree.org b/content/Quadtree.org index 4c07050..04ae1b6 100644 --- a/content/Quadtree.org +++ b/content/Quadtree.org @@ -12,6 +12,8 @@ Several things can be created with this; ** Resources [[http://ericandrewlewis.github.io/how-a-quadtree-works/][Visualize a Quadtree]] [[http://donar.umiacs.umd.edu/quadtree/][Academic Interactive Demo]] +[[https://codepen.io/_bm/pen/ExPBMrW][Codepen (Js example)]] +[[https://gist.github.com/patricksurry/6478178][D3.js Example]] ** Notes // exclude node if point is farther away than best distance in either axis if (x < x1 - best.d || x > x2 + best.d || y < y1 - best.d || y > y2 + best.d) { diff --git a/content/Triangulation.org b/content/Triangulation.org index 1b57909..5089bba 100644 --- a/content/Triangulation.org +++ b/content/Triangulation.org @@ -3,5 +3,10 @@ * Triangulation Generate triangles from points ** Convex Hull Jarvis March (Gift wrapping) :algorithm: +[[file:~/Development/gaming-pads/convexhull-jarvismarch.py][Python Code]] ** Resources +https://www.youtube.com/watch?v=Vu84lmMzP2o +https://www.habrador.com/tutorials/math/8-convex-hull/ +https://www.youtube.com/watch?v=B2AJoQSZf4M +https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf ** Notes diff --git a/triangulation.py b/convexhull-jarvismarch.py similarity index 99% rename from triangulation.py rename to convexhull-jarvismarch.py index 5d7bd60..537cbef 100644 --- a/triangulation.py +++ b/convexhull-jarvismarch.py @@ -65,7 +65,6 @@ def player_input(w: World): w.dragging_vert.x = collision.point.x w.dragging_vert.z = collision.point.z if RL.is_mouse_button_released(0): - print('no') w.dragging_vert = None def update(w: World): diff --git a/starter.py b/starter.py index 313d9e5..cc2eae0 100644 --- a/starter.py +++ b/starter.py @@ -1,13 +1,23 @@ import pyray as RL -from pyray import (Rectangle as Rect, Vector2 as Vec2, Vector3 as Vec3, Camera3D) +from pyray import (Rectangle as Rect, Vector2 as Vec2, Vector3 as Vec3, Camera3D, BoundingBox) import math import pdb import random from typing import Optional, Tuple, List from dataclasses import dataclass, field -screen_width = 1280 -screen_height = 1024 +def dump(struct): + s = f"{RL.ffi.typeof(struct)}: (".replace('', '') + for field in dir(struct): + data = struct.__getattribute__(field) + if str(data).startswith(" World: - pass + cam = Camera3D(Vec3(0, 10, 10), Vec3(0, 0, 0), Vec3(0, 1, 0), 45, RL.CAMERA_PERSPECTIVE) + return World(cam) def player_input(w: World): pass @@ -32,9 +43,7 @@ def draw_2d(w: World): RL.init_window(screen_width, screen_height, "Starter"); RL.set_target_fps(60) -init() -cam = Camera3D(Vec3(0, 0, 10), RL.vector3_zero(), Vec3(0, 1, 0), 60, 1) -w = World(cam) +w = init() while not RL.window_should_close(): player_input(w) update(w) diff --git a/triangulation-earclipping.py b/triangulation-earclipping.py new file mode 100644 index 0000000..d6e208a --- /dev/null +++ b/triangulation-earclipping.py @@ -0,0 +1,172 @@ +import pyray as RL +from pyray import ( + ffi, + Rectangle as Rect, + Vector2 as Vec2, + Vector3 as Vec3, + Camera3D, + BoundingBox, + Mesh, + Model +) +import math +import pdb +import random +from typing import Optional, Tuple, List +from dataclasses import dataclass, field + +def dump(struct): + s = f"{RL.ffi.typeof(struct)}: (".replace('', '') + for field in dir(struct): + data = struct.__getattribute__(field) + if str(data).startswith(" 0: + left_most = v2 + if left_most == convex_hull_points[0]: + break + convex_hull_points.append(left_most) + return convex_hull_points + +def init() -> World: + cam = Camera3D(Vec3(0, 10, 10), Vec3(0, 0, 0), Vec3(0, 1, 0), 45, RL.CAMERA_PERSPECTIVE) + half_grid = grid_slices * grid_spacing * 0.5 + floor_bb = BoundingBox(Vec3(-half_grid, -0.01, -half_grid), Vec3(half_grid, 0.01, half_grid)) + w = World(cam, floor_bb) + num_starting_verts = 10 + radius = 4 + w.vertices = [ + Vec3(math.cos(math.pi * 2 / num_starting_verts * i) * radius, + 0, + math.sin(math.pi * 2 / num_starting_verts * i) * radius) + for i in range(num_starting_verts) + ] + + vs = get_convex_hull(w.vertices) + def get_points(v: Vec3): + return v.x, v.y, v.z + vertices = ffi.new("float[]", len(vs) * 3 * 3) + # for i,v in enumerate([0,0,0 1,0,2, 2,0,0, 1,0,2, 3,0,2, 2,0,0]): + for i,v in enumerate(vs): + if i < 2: + continue + for j,vv in enumerate([*get_points(vs[0]), *get_points(vs[i - 1]), *get_points(vs[i])]): + vertices[(i-2)*9+j] = vv + + texcoords = ffi.new("float[]", 0) + # for i,c in enumerate([0, 0, 0.5, 1, 1, 0]): + # texcoords[i] = c + normals = ffi.new("float[]", 0) + # for i,c in enumerate([0, 1, 0, 0, 1, 0, 0, 1, 0]): + # normals[i] = c + texcoords2 = ffi.new("float[]", 0) + # for i in range(6): + # texcoords[i] = 0 + + # mesh = Mesh(6, 2, vertices, texcoords, texcoords2, normals) + mesh = Mesh(len(vs) * 3, len(vs), vertices, texcoords, texcoords2, normals) + RL.upload_mesh(mesh, False) + w.mesh = mesh + w.model = RL.load_model_from_mesh(mesh) + return w + +def player_input(w: World): + if RL.is_key_pressed(RL.KEY_SPACE): + w.rotate_cam = not w.rotate_cam + if RL.is_key_pressed(RL.KEY_W): + w.draw_wireframe = not w.draw_wireframe + +def update(w: World): + pass + # mesh = Mesh( + # vertexCount = len(w.vertices), + # triangleCount = len(w.vertices) - 2, + # vertices = w.vertices + # ) + # new_mesh = RL.gen_ + +def draw_3d(w: World): + RL.draw_grid(grid_slices, grid_spacing) + RL.draw_bounding_box(w.floor_bb, RL.GREEN) + for vert in w.vertices: + RL.draw_sphere(vert, vertex_radius, RL.GREEN) + if w.draw_wireframe[0]: + RL.rl_enable_wire_mode() + else: + RL.rl_disable_wire_mode() + RL.draw_model(w.model, RL.vector3_zero(), 1, RL.GREEN) + +def draw_2d(w: World): + val = RL.gui_check_box(Rect(10, 30, 50, 50), "Draw Wireframe", w.draw_wireframe) + if val != 0: + print(val) + if val: + print('hello') + w.draw_wireframe = True + +RL.init_window(screen_width, screen_height, "Starter"); +RL.set_target_fps(60) + +w = init() +while not RL.window_should_close(): + player_input(w) + update(w) + + # Drawing + if w.rotate_cam: + RL.update_camera(w.cam, RL.CameraMode.CAMERA_ORBITAL) + RL.begin_drawing() + RL.clear_background(RL.WHITE) + + RL.begin_mode_3d(w.cam) + draw_3d(w) + RL.end_mode_3d() + + draw_2d(w) + + RL.end_drawing() + w.frame_count += 1 +