Field Layout & Native Interop
This page describes how field layout data is loaded and how QuestNav interfaces with its native C/C++ libraries.
Field Layout
Choosing a Layout
The layout is chosen in the AprilTag tab of the web interface (Field Layout). The app ships two:
| File | Field |
|---|---|
2026-rebuilt-welded.json | 2026 REBUILT, welded field (default) |
2025-reefscape-welded.json | 2025 REEFSCAPE, welded field |
The app reads the selected layout once, at startup (QuestNav.cs calls LoadJsonFromFileAsync with the configured
file name). A layout change takes effect after a restart; the web interface shows a banner when one is needed.
Tag IDs that are not in the loaded layout are ignored by the detector, and pose resets outside the layout's field bounds (plus 0.25 m) are logged as warnings.
Custom Layouts
Use Manage Custom Layouts... in the AprilTag tab, or the field-layout endpoints of the web API, to upload, download, rename and delete your own layouts, such as a practice field.
- Storage. Custom layouts are stored in
persistentDataPath/apriltag/fieldlayouts-custom/on the headset. - Lookup order. The custom folder is checked before the bundled one.
- File names.
- Allowed characters: letters, digits,
.,_and-. - Length: at most 64 characters before
.json. - First character: must not be
.. - No
..anywhere in the name. - Extension:
.jsonis added when missing to a name you upload or rename to. The web API's{name}URLs need the full file name, such aspractice-field.json. - Bundled names: a custom layout cannot use one (compared case-insensitively), so it never shadows a bundled layout.
- Allowed characters: letters, digits,
- Content. The upload must:
- parse as a WPILib
AprilTagFieldLayout - contain at least one tag
- give every tag a pose with finite numbers
- have no duplicate IDs
- parse as a WPILib
- Size limit: 1 MB.
- Deleting the selected layout. The configuration falls back to
2026-rebuilt-welded.json. - Renaming the selected layout. The configuration follows the new name.
If the selected file can't be loaded at startup, the error is logged and the detector has no tags to look for.
JSON Format
The layout file follows the WPILib field layout format. Each tag entry contains:
{
"ID": 1,
"pose": {
"translation": { "x": 16.697198, "y": 0.655294, "z": 1.4859 },
"rotation": {
"quaternion": { "W": 0.4539905, "X": 0.0, "Y": 0.0, "Z": 0.8910065 }
}
}
}
- Translation: Tag center position in meters, in FRC field coordinates (origin at blue alliance corner)
- Rotation: Tag orientation as a quaternion in FRC coordinates (the tag's normal direction)
Tag Size
The physical tag size (the black square of the tag36h11 family) is a constant, QuestNavConstants.AprilTag.TAG_SIZE_METERS (0.1651 m / 6.5 in). The default constructor uses it:
new AprilTagFieldLayout() // standard FRC tags
new AprilTagFieldLayout(0.2032) // overload, for different-sized tags (meters)
The size is not carried in the layout JSON — the WPILib schema has only tags and field — so a practice field printed with different-sized tags must pass an explicit size to the overload.
3D Corner Computation
AprilTagFieldLayout.GetTagCorners() computes the four 3D corner positions of each tag in field coordinates. From the tag center pose, corners are offset in the tag's local frame:
| Corner | Tag-local offset |
|---|---|
| Bottom-Right | (0, -halfSize, -halfSize) |
| Bottom-Left | (0, +halfSize, -halfSize) |
| Upper-Left | (0, +halfSize, +halfSize) |
| Upper-Right | (0, -halfSize, +halfSize) |
where halfSize = tagSize / 2 = 0.08255m.
Each offset is a Transform3d applied to the tag's field pose via tagPose.Plus(cornerTransform), producing corners in FRC field coordinates.
The corner ordering (BR, BL, UL, UR) is intentionally non-standard. The Meta Quest passthrough camera produces a mirrored image, causing the AprilTag detector to return 2D corners in clockwise order. The 3D corners must match this order for correct 2D-3D correspondences. Do not reorder them to match the standard counter-clockwise AprilTag convention.
Native Library Architecture
QuestNav uses three native C/C++ libraries accessed via P/Invoke ([DllImport]). All use CallingConvention.Cdecl.
Library Overview
| Library | Source | Purpose | Platform Files |
|---|---|---|---|
| libapriltag | University of Michigan | Tag detection in grayscale images | libapriltag.so (Quest), apriltag.dll (Editor) |
| libposelib | PoseLib project | Multi-tag PnP pose estimation | libposelib.so (Quest), poselib.dll (Editor) |
| libntcore | WPILib | NetworkTables 4 protocol | libntcore.so + deps (Quest), ntcore.dll + deps (Editor) |
Interop Pattern
All native interop follows a consistent pattern:
- Native struct mirrors with
[StructLayout(LayoutKind.Sequential)]match C struct memory layout exactly *Nativesclasses contain all[DllImport]declarations as static methods- Managed wrappers hold unsafe pointers to native memory and implement
IDisposablefor cleanup
AprilTag Detection (libapriltag)
Key native functions:
| Function | Purpose |
|---|---|
apriltag_detector_create() | Create a detector instance |
apriltag_detector_add_family_bits() | Register the tag36h11 family |
apriltag_detector_detect() | Run detection on a grayscale image |
tag36h11_create() / destroy() | Tag family lifecycle |
image_u8_create() / destroy() | Grayscale image buffer lifecycle |
The zarray_get and zarray_size functions from the C library are re-implemented in C# by directly reading native struct memory, because the C originals are static inline and cannot be P/Invoked.
PoseLib Solver (libposelib)
A single function:
poselib_estimate_absolute_pose_simple(
points2D[], // Flat array of 2D image coordinates
points3D[], // Flat array of 3D field coordinates
numPoints, // Number of correspondences
cameraModelId, // POSELIB_CAMERA_PINHOLE (0)
cameraParams[], // [fx, fy, cx, cy]
imageWidth,
imageHeight,
maxReprojError, // 12 pixels (hardcoded)
&qw, &qx, &qy, &qz, // Output quaternion
&tx, &ty, &tz, // Output translation
&numInliers // Output inlier count
)
Camera intrinsics come from PassthroughCameraAccess.Intrinsics, which holds static values in sensor pixels:
- Focal length:
Intrinsics.FocalLength(fx, fy) - Principal point:
Intrinsics.PrincipalPoint(cx, cy) - Sensor size:
Intrinsics.SensorResolution
Detections are in pixels of the captured frame (CurrentResolution), which the SDK produces by centre-cropping the sensor to the frame's aspect ratio and scaling the crop. ImageIntrinsics.TryFromSensor applies the same crop and scale to fx, fy, cx and cy, and CurrentResolution is passed as the image size. AprilTagManager recomputes them whenever CurrentResolution changes.
Memory Management
| Resource | Strategy |
|---|---|
ImageU8 | Static cached buffer; reallocated only on resolution change. Returned wrapper does not own the handle. |
AprilTagDetectionResults | IDisposable. Wraps the zarray_t* returned by Detect() and frees it and every detection in it. The coroutine disposes it with using each frame. |
AprilTagDetection | Non-owning view into its AprilTagDetectionResults. Not disposable; throws ObjectDisposedException once its results are disposed. |
AprilTagDetector | IDisposable. Owns the families added to it. Destroys the native detector first (which unregisters the families), then disposes the families. |
AprilTagFamily | IDisposable with a Disposed flag guard (safe for double-dispose). |
An AprilTagDetection is valid only until its AprilTagDetectionResults is disposed. Do not hold references to detection objects across frames.
WPILib Geometry Library
QuestNav includes a C# port of WPILib's Java geometry classes in the QuestNav.QuestNav.Geometry namespace. These provide double-precision 3D math operations.
| Class | Description |
|---|---|
Pose3d | Translation3d + Rotation3d. JSON-serializable. Converts to/from protobuf. |
Rotation3d | Quaternion-backed rotation. Constructs from matrix, axis-angle, or Euler angles. |
Translation3d | 3D vector with norm, distance, and rotation operations. |
Transform3d | Relative transform between two poses. |
Quaternion | Double-precision quaternion. Not UnityEngine.Quaternion. |
Field2d | Field dimensions container. |
QuestNav.QuestNav.Geometry.Quaternion and UnityEngine.Quaternion are different types. The geometry quaternion uses double precision and (W, X, Y, Z) constructor order. Files that need both types add using Quaternion = UnityEngine.Quaternion; at the top. The double namespace (QuestNav.QuestNav.Geometry) is intentional — do not "fix" it.