# C API: Picking

Which avatar, which body part or item, and which held prop is under a screen point or along a ray. On the CPU: each avatar's bounding sphere, then boxes per mesh and per run of 64 triangles made from the posed joints, then the triangles, skinned as `sona_pose_vertices` skins them (the scalar maths, so the same bits on every machine), back faces culled and the colour texture's alpha test applied as the draw does. A sprite avatar is met on its quad and the point on the picture goes on into the mesh at the baked turn. On the GPU (Vulkan, GL, GLES): an id buffer of the avatars the ray meets, one pixel, then the hit finished on the CPU on the mesh found. Call between `sona_update` calls, one thread at a time per ctx. The guide, with the cost measured over 1,000 avatars: [Picking](../../guides/picking.md).

`sona_pick_options` (56 bytes, 52 with 4-byte pointers): `{size, flags, max_distance, ignore_count, ignore, reserved[8]}`. Flags: `SONA_PICK_HIDDEN`, `SONA_PICK_CULLED`, `SONA_PICK_BACK_FACES`, `SONA_PICK_NO_ALPHA`, `SONA_PICK_NO_PROPS`, `SONA_PICK_BOUNDS`, `SONA_PICK_SPRITES_AS_MESH`. `sona_pick_hit` (176 bytes, 172): `{size, source, flags, avatar_id, avatar, part, section, mask, item[37], mesh, prop, triangle, joint, region, lod, distance, point[3], normal[3], bary[2], uv[2], reserved[8]}`; `source` is `SONA_PICK_SOURCE_MESH`, `_SPRITE` or `_BOUNDS`, `part` is `SONA_PICK_BODY`, `_ITEM` or `_PROP`, `flags` holds `SONA_PICK_HIT_BACK`, `_FACE` and `_GPU`, `region` is a `sona_body_region` (`SONA_REGION_HEAD` .. `SONA_REGION_RIGHT_FOOT`).

## sona_pick_defaults

```c
void sona_pick_defaults(sona_pick_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_pick_options *` | The [`sona_pick_options`](../fields.md#sona_pick_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_pick_options x_pick_options;
memset(&x_pick_options, 0, sizeof x_pick_options);
x_pick_options.size = sizeof x_pick_options;
sona_pick_defaults(&x_pick_options);
```

`size` set, everything else 0: visible avatars in view, front faces, the alpha test, props, no distance limit. NULL does nothing.

## sona_view_ray

```c
sona_result sona_view_ray(const sona_view *v, double x, double y, float origin[3], float dir[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `v` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `x` | `double` | A pixel from the picture's left | - |
| `y` | `double` | A pixel from the picture's top | - |
| `origin` | `float [3]` | Receives the ray's origin | required |
| `dir` | `float [3]` | Receives the ray's direction | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_view x_view;
sona_view_defaults(&x_view);
sona_camera_preset(avatar, SONA_PRESET_FULL, &x_view);
float x_origin2[3];
memset(x_origin2, 0, sizeof x_origin2);
float x_dir3[3];
memset(x_dir3, 0, sizeof x_dir3);
sona_result result = sona_view_ray(&x_view, 128.0, 128.0, x_origin2, x_dir3);
if (result != SONA_OK) printf("sona_view_ray: %s\n", sona_result_name(result));
```

The ray through the point (x, y) of the view's picture, in pixels from its top left (a pixel's centre is + 0.5): `origin` on the near plane, `dir` of length 1. Pure. `SONA_E_ARGUMENT` for a view with no size or a NULL pointer.

## sona_pick_ray

```c
sona_result sona_pick_ray(sona_ctx *ctx, const float origin[3], const float dir[3], const sona_pick_options *o,
                          sona_pick_hit *hit);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `origin` | `const float [3]` | The ray's origin, world space | required |
| `dir` | `const float [3]` | The ray's direction, world space | required |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | `NULL` allowed (below) |
| `hit` | `sona_pick_hit *` | Receives what was hit | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
float x_origin[3];
memset(x_origin, 0, sizeof x_origin);
float x_dir2[3];
memset(x_dir2, 0, sizeof x_dir2);
sona_pick_options x_pick_options3;
memset(&x_pick_options3, 0, sizeof x_pick_options3);
x_pick_options3.size = sizeof x_pick_options3;
sona_pick_hit x_pick_hit4;
memset(&x_pick_hit4, 0, sizeof x_pick_hit4);
x_pick_hit4.size = sizeof x_pick_hit4;
sona_result result = sona_pick_ray(ctx, x_origin, x_dir2, &x_pick_options3, &x_pick_hit4);
if (result != SONA_OK) printf("sona_pick_ray: %s\n", sona_result_name(result));
```

The nearest hit. `SONA_OK` with `*hit` set, `SONA_E_NOT_FOUND` when nothing is hit (`*hit` then has `size` set and `mesh`, `prop` and `joint` at -1). `o` may be NULL. `dir` need not be unit length; `distance` is in metres along it. Sprites are met as the last `sona_update`'s view faces them. `SONA_E_ARGUMENT` for a zero or non-finite ray, a `size` too small, a negative `max_distance` or `ignore_count` without `ignore`.

## sona_pick_screen

```c
sona_result sona_pick_screen(sona_ctx *ctx, const sona_view *v, double x, double y, const sona_pick_options *o,
                             sona_pick_hit *hit);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `v` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `x` | `double` | A pixel from the picture's left | - |
| `y` | `double` | A pixel from the picture's top | - |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | required |
| `hit` | `sona_pick_hit *` | Receives what was hit | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_view x_view;
sona_view_defaults(&x_view);
sona_camera_preset(avatar, SONA_PRESET_FULL, &x_view);
sona_pick_options x_pick_options2;
memset(&x_pick_options2, 0, sizeof x_pick_options2);
x_pick_options2.size = sizeof x_pick_options2;
sona_pick_hit x_pick_hit3;
memset(&x_pick_hit3, 0, sizeof x_pick_hit3);
x_pick_hit3.size = sizeof x_pick_hit3;
sona_result result = sona_pick_screen(ctx, &x_view, 128.0, 128.0, &x_pick_options2, &x_pick_hit3);
if (result != SONA_OK) printf("sona_pick_screen: %s\n", sona_result_name(result));
```

`sona_view_ray` then `sona_pick_ray`, with the sprites facing this view.

## sona_pick_ray_all

```c
uint32_t sona_pick_ray_all(sona_ctx *ctx, const float origin[3], const float dir[3], const sona_pick_options *o,
                           sona_pick_hit *out, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `origin` | `const float [3]` | The ray's origin, world space | required |
| `dir` | `const float [3]` | The ray's direction, world space | required |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | required |
| `out` | `sona_pick_hit *` | Receives the [`sona_pick_hit`](../fields.md#sona_pick_hit) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example**:

```c
float x_origin[3];
memset(x_origin, 0, sizeof x_origin);
float x_dir2[3];
memset(x_dir2, 0, sizeof x_dir2);
sona_pick_options x_pick_options3;
memset(&x_pick_options3, 0, sizeof x_pick_options3);
x_pick_options3.size = sizeof x_pick_options3;
sona_pick_hit x_pick_hit4;
memset(&x_pick_hit4, 0, sizeof x_pick_hit4);
x_pick_hit4.size = sizeof x_pick_hit4;
uint32_t x_got = sona_pick_ray_all(ctx, x_origin, x_dir2, &x_pick_options3, &x_pick_hit4, 1);
printf("sona_pick_ray_all: %lld\n", (long long)x_got);
```

Every avatar the ray hits, each at its nearest hit, nearest first. Returns how many avatars it hits and writes up to `cap` of them (`cap` 0 and `out` NULL only count). 0 on a bad argument.

## sona_avatar_pick

```c
sona_result sona_avatar_pick(sona_avatar *a, const float origin[3], const float dir[3], const sona_pick_options *o,
                             sona_pick_hit *hit);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `origin` | `const float [3]` | The ray's origin, world space | required |
| `dir` | `const float [3]` | The ray's direction, world space | required |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | required |
| `hit` | `sona_pick_hit *` | Receives what was hit | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
float x_origin[3];
memset(x_origin, 0, sizeof x_origin);
float x_dir2[3];
memset(x_dir2, 0, sizeof x_dir2);
sona_pick_options x_pick_options3;
memset(&x_pick_options3, 0, sizeof x_pick_options3);
x_pick_options3.size = sizeof x_pick_options3;
sona_pick_hit x_pick_hit4;
memset(&x_pick_hit4, 0, sizeof x_pick_hit4);
x_pick_hit4.size = sizeof x_pick_hit4;
sona_result result = sona_avatar_pick(avatar, x_origin, x_dir2, &x_pick_options3, &x_pick_hit4);
if (result != SONA_OK) printf("sona_avatar_pick: %s\n", sona_result_name(result));
```

The ray against one avatar; its hidden and culled flags and `o->ignore` do not apply. `SONA_E_NOT_READY` before it is READY and posed, `SONA_E_DESTROYED` once removed.

## sona_region_name

```c
const char *sona_region_name(uint32_t region);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `region` | `uint32_t` | A body region index | - |

**Returns** `const char *`.

**Example**:

```c
const char * x_got = sona_region_name(1);
printf("sona_region_name: %s\n", x_got ? "a value" : "none");
```

`"none"`, `"head"`, `"neck"`, `"torso"`, `"hips"`, `"leftArm"`, `"rightArm"`, `"leftHand"`, `"rightHand"`, `"leftLeg"`, `"rightLeg"`, `"leftFoot"`, `"rightFoot"`; NULL past the last.

## sona_pick_gpu

```c
sona_result sona_pick_gpu(sona_renderer *r, const sona_view *v, double x, double y, const sona_pick_options *o,
                          sona_pick_hit *hit);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `v` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `x` | `double` | A pixel from the picture's left | - |
| `y` | `double` | A pixel from the picture's top | - |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | required |
| `hit` | `sona_pick_hit *` | Receives what was hit | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_view x_view;
sona_view_defaults(&x_view);
sona_camera_preset(avatar, SONA_PRESET_FULL, &x_view);
sona_pick_options x_pick_options2;
memset(&x_pick_options2, 0, sizeof x_pick_options2);
x_pick_options2.size = sizeof x_pick_options2;
sona_pick_hit x_pick_hit3;
memset(&x_pick_hit3, 0, sizeof x_pick_hit3);
x_pick_hit3.size = sizeof x_pick_hit3;
sona_result result = sona_pick_gpu(r, &x_view, 128.0, 128.0, &x_pick_options2, &x_pick_hit3);
if (result != SONA_OK) printf("sona_pick_gpu: %s\n", sona_result_name(result));
```

The pick through the id buffer: the avatars whose sphere the ray meets are drawn once more, each draw writing its index, into a one-pixel target of the pass's own under a projection of that pixel; the backend reads it back (on Vulkan this submits on the device's queue and waits, as `sona_snapshot` does) and the hit is finished on the CPU on the one mesh found (`SONA_PICK_HIT_GPU` set). Sprite avatars are picked on the CPU and the nearer hit wins. `SONA_E_UNSUPPORTED` on D3D11, D3D12, Metal and Xenos. With `SONA_PICK_BOUNDS` it is the CPU pick.

## sona_pick_gpu_ids

```c
sona_result sona_pick_gpu_ids(sona_renderer *r, const sona_view *v, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
                              const sona_pick_options *o, uint32_t *avatar_ids, uint16_t *meshes);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `v` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `x` | `uint32_t` | The region's left edge in pixels | - |
| `y` | `uint32_t` | The region's top edge in pixels | - |
| `w` | `uint32_t` | The region's width in pixels | - |
| `h` | `uint32_t` | The region's height in pixels | - |
| `o` | `const sona_pick_options *` | The options, a [`sona_pick_options`](../fields.md#sona_pick_options) | required |
| `avatar_ids` | `uint32_t *` | Receives the avatar id per pixel, `w * h` of them | required |
| `meshes` | `uint16_t *` | Receives the mesh index + 1 per pixel; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

The id buffer of the region (x, y, w, h) of the view's picture, at most 4096 x 4096 and inside the view: per pixel, row 0 at the top, the avatar id drawn there (0: none) and, when `meshes` is not NULL, its mesh index + 1. Every drawable avatar in the region is drawn, sprite avatars as their meshes in their held pose. For selection outlines and editors.
