Skip to content

Commit

Permalink
Update README example 2
Browse files Browse the repository at this point in the history
  • Loading branch information
janbridley committed Oct 15, 2024
1 parent d4b3b66 commit 3a261c6
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,48 +87,63 @@ For finer control over the viewport and settings, use the following built-in met
OpenGL-style perspective and view matrixes.

```python
from coxeter.families import ArchimedeanFamily

import svg3d
from svg3d import get_lookat_matrix, get_projection_matrix

# Define the vertices and faces of a cube
vertices = np.array(
[[-1, -1, -1],
[-1, -1, 1],
[-1, 1, -1],
[-1, 1, 1],
[ 1, -1, -1],
[ 1, -1, 1],
[ 1, 1, -1],
[ 1, 1, 1]]
)

def generate_svg(filename, poly):
pos_object = [0.0, 0.0, 0.0] # "at" position
pos_camera = [40, 40, 120] # "eye" position
vec_up = [0.0, 1.0, 0.0] # "up" vector of camera. This is the default value.

z_near, z_far = 1.0, 200.0
aspect = 1.0 # Aspect ratio of the view cone
fov_y = 1.0 # Opening angle of the view cone. fov_x is equal to fov_y * aspect

look_at = get_lookat_matrix(pos_object, pos_camera, vec_up=vec_up)
projection = get_projection_matrix(
z_near=z_near, z_far=z_far, fov_y=fov_y, aspect=aspect
)

# A "scene" is a list of Mesh objects, which can be easily generated from Coxeter!
scene = [svg3d.Mesh.from_poly(poly, style=style)]

view = svg3d.View.from_look_at_and_projection(
look_at=look_at,
projection=projection,
scene=scene,
)
faces = [
[0, 2, 6, 4],
[0, 4, 5, 1],
[4, 6, 7, 5],
[0, 1, 3, 2],
[2, 3, 7, 6],
[1, 5, 7, 3]
]

svg3d.Engine([view]).render(filename)
# Set up our rendering style - transparent white gives a nice wireframe appearance
style = {
"fill": "#FFFFFF",
"fill_opacity": "0.75",
"stroke": "black",
"stroke_linejoin": "round",
"stroke_width": "0.005",
}

pos_object = [0.0, 0.0, 0.0] # "at" position
pos_camera = [40, 40, 120] # "eye" position
vec_up = [0.0, 1.0, 0.0] # "up" vector of camera. This is the default value.

z_near, z_far = 1.0, 200.0
aspect = 1.0 # Aspect ratio of the view cone
fov_y = 2.0 # Opening angle of the view cone. fov_x is equal to fov_y * aspect

look_at = get_lookat_matrix(pos_object, pos_camera, vec_up=vec_up)
projection = get_projection_matrix(
z_near=z_near, z_far=z_far, fov_y=fov_y, aspect=aspect
)

# A "scene" is a list of Mesh objects, which can be easily generated from raw data
scene = [svg3d.Mesh.from_vertices_and_faces(vertices, faces, style=style)]

style = dict(
fill="#00B2A6",
fill_opacity="0.85",
stroke="black",
stroke_linejoin="round",
stroke_width="0.005",
view = svg3d.View.from_look_at_and_projection(
look_at=look_at,
projection=projection,
scene=scene,
)

truncated_cube = ArchimedeanFamily.get_shape("Truncated Cube")
generate_svg(filename="truncated_cube.svg", poly=truncated_cube)
svg3d.Engine([view]).render("cube-wireframe.svg")

```

Running the code above generates the following image, using a simple dot-product
Expand Down

0 comments on commit 3a261c6

Please sign in to comment.