kicad2ems.core.view3d

The Draw page's 3D view as numbers, with no Qt or OpenGL (so it is tested
where there is no display): pyqtgraph's GLViewWidget camera as matrices, the
ray under a pixel and the pixel of a point, camera presets and framing, the
drag of a point along one axis (the gizmo's, and X / Y / Z while dragging),
a clip plane through the meshes, a solid's outline and the working level's
grid.

    cam = dict(center=(x, y, z), distance=d, elevation=e, azimuth=a, fov=60,
               ortho=False)             # ortho: an orthographic camera
    m = mvp(cam, w, h)                  # 4x4: model mm -> clip space
    o, d = ray(m, w, h, px, py)         # the ray under pixel (px, py)
    px, py = project(m, w, h, p)
    xy, front = project_many(m, w, h, pts)      # (n, 2) pixels, in front
    keys = box_pick(meshes, m, w, h, (x0, y0, x1, y1), crossing)
                                        # the meshes inside a pixel box (or
                                        #  touching it: crossing)
    v, f = arrow_cone(base, axis, length, radius)   # a gizmo's arrowhead
    cam.update(preset('top', bounds, fov, w / h))  # bounds (x0, y0, z0, x1,
    cam.update(frame(bounds, cam, w / h))           #  y1, z1)
    s = along_axis(o, d, at, axis)      # how far along axis (0, 1, 2) from
                                        # `at` the ray passes nearest
    key, t = pick_boxed(meshes, boxes, o, d)    # core.shapes.pick, the
                                                # meshes' boxes tried first
    v, f = clip_mesh(v, f, axis, value, keep='below')
    g = clip_outline(polygon, axis, value, keep='below')
    v, f = prism_fast(polygon, z0, z1)  # core.shapes.prism, a convex one
                                        # quicker
    lines = prism_edges(polygon, z0, z1)        # (2k, 3) segment ends
    spec = grid_spec((x0, y0, x1, y1), step)    # centre, size, spacing

Camera angles are pyqtgraph's (degrees): elevation above the xy plane,
azimuth from +x anticlockwise to where the camera stands; fov the horizontal
field of view. An orthographic camera (ortho) sees, at every depth, what the
perspective one sees at its centre: 2 distance tan(fov / 2) across, so the
wheel's zoom (the distance) and the pan work alike in both.
kicad2ems.core.view3d.along_axis(origin, direction, at, axis)[source]
How far (mm, signed) along model axis `axis` (0 x, 1 y, 2 z) from
point `at` the line through `at` comes nearest the ray: where a drag
constrained to that axis puts the point. None when the ray runs along
the axis.
kicad2ems.core.view3d.arrow_cone(base, axis, length, radius, n=16)[source]
A cone (vertices, faces wound outward) from `base` along model axis
`axis` (0 x, 1 y, 2 z) `length` long and `radius` round at its base: a
gizmo's arrowhead.
kicad2ems.core.view3d.box_pick(meshes, m, width, height, box, crossing=False)[source]
The keys of the meshes {key: (vertices, faces)} wholly inside the
pixel box (x0, y0, x1, y1) as drawn, or (crossing) touching it (the
outline of its corners on the screen), as the plan's box selects; a mesh
with a corner behind the camera is never wholly inside. m: mvp.
kicad2ems.core.view3d.clip_mesh(v, f, axis, value, keep='below')[source]
Triangles (vertices v (n, 3), faces f (m, 3)) cut by the plane
model[axis] = value, the side `keep` ('below' or 'above') kept: (v, f),
a triangle across the plane cut along it (no cap).
kicad2ems.core.view3d.clip_outline(g, axis, value, keep='below')[source]
Polygon g with what is past x = value (axis 0) or y = value (axis 1)
taken off, or None when nothing is left (a prism of it is the clipped
solid, capped).
kicad2ems.core.view3d.clip_range(z0, z1, value, keep='below')[source]
A solid's heights z0..z1 clipped at z = value, or None (all cut).
kicad2ems.core.view3d.frame(bounds, cam, aspect, margin=1.1)[source]
dict(center, distance) that fit box `bounds` (x0, y0, z0, x1, y1, z1)
in the view from where `cam` looks (its elevation, azimuth and fov, and
the view's width / height `aspect`; ortho: orthographic): the box's eight
corners, seen from its centre, inside each axis's own field (`margin` to
spare). A bounding sphere in the narrower field put a flat board in a
wide view 2-3x too far (sma_launch from the top: 38 % of the height and
20 % of the width).
kicad2ems.core.view3d.grid_spec(bounds, step, max_lines=120)[source]
A grid over bounds (x0, y0, x1, y1) on the multiples of `step` (made
coarser by 2, 5, 10... until it has at most max_lines lines a way):
dict(center=(x, y), size=(sx, sy), spacing).
kicad2ems.core.view3d.mvp(cam, width, height)[source]
projection @ view of camera `cam` (center, distance, elevation,
azimuth, fov; ortho: orthographic) on a view width x height pixels.
kicad2ems.core.view3d.ortho_matrix(distance, fov, width, height, region=None)[source]
The orthographic projection (its centre plane the perspective's at
`distance`: 2 distance tan(fov / 2) across) over the whole view, or the
pixel region (x, y, w, h) of it (from the bottom left, as pyqtgraph's
picking asks); its depth ORTHO_DEPTH either side of the camera.
kicad2ems.core.view3d.ortho_pixel(distance, fov, width)[source]
An orthographic view's pixel, mm (the same at every depth).
kicad2ems.core.view3d.pick_boxed(meshes, boxes, origin, direction)[source]
core.shapes.pick ((key, t): the mesh of {key: (vertices, faces)} the
ray meets first) with each mesh's box (boxes {key: (x0, y0, z0, x1, y1,
z1)}) tried first: the meshes are tried nearest box first, those whose
boxes it misses never (300 vias: one or two triangle tests a pick, not
300).
kicad2ems.core.view3d.pixel_to_segment(m, width, height, a, b, x, y)[source]
How many pixels pixel (x, y) is from segment a-b (model points) as
drawn, and where along it (0-1).
kicad2ems.core.view3d.preset(name, bounds, fov=60.0, aspect=1.0, ortho=False)[source]
The camera of PRESETS[name] (top, front, side, iso) fitting `bounds`
(ortho: an orthographic camera): dict(center, distance, elevation,
azimuth).
kicad2ems.core.view3d.prism_edges(g, z0, z1)[source]
The edges of polygon g extruded from z0 to z1, as line segments
((2k, 3) points in pairs): its rings top and bottom, and an upright at
every corner where the outline turns (not along a circle's facets).
kicad2ems.core.view3d.prism_fast(g, z0, z1)[source]
core.shapes.prism (polygon g extruded from z0 to z1: vertices, faces,
wound outward), its caps a fan from the first corner when g is convex
with no holes (a via, a pad, a box: shapely's triangulation took three
quarters of a 300-via 3D view's time), its corners shared by the faces;
anything else, and a multipolygon part by part, is prism's.
kicad2ems.core.view3d.project(m, width, height, p)[source]
The pixel (x, y) of model point p; m: mvp.
kicad2ems.core.view3d.project_many(m, width, height, pts)[source]
The pixels (n, 2) of model points pts (n, 3), and whether each is in
front of the camera (n,); m: mvp.
kicad2ems.core.view3d.projection_matrix(distance, fov, width, height)[source]
The perspective (GLViewWidget.projectionMatrix over the whole view:
near clip distance / 1000, far distance x 1000).
kicad2ems.core.view3d.ray(m, width, height, x, y)[source]
(origin, unit direction) of the ray under pixel (x, y) (from the top
left), model mm; m: mvp.
kicad2ems.core.view3d.ray_boxes(origin, direction, boxes)[source]
How far along the ray (t >= 0) it enters each box of (n, 6) boxes
(x0, y0, z0, x1, y1, z1): (n,), inf where it misses (the slab test).
kicad2ems.core.view3d.view_matrix(center, distance, elevation, azimuth)[source]
The camera's view matrix (pyqtgraph's GLViewWidget.viewMatrix, the
euler rotation).