OptrisOptrisDocumentation

Documentation/OTC SDK/Overviewv12.0

Region Shapes

Building one, composing several into a boolean tree, moving it while the camera streams, and what travels with it into a file and onto the screen. What the shape then measures is Region Statistics.

The region chapter is four pages, each answering one question, and the worked examples are a fifth: Region Examples.

Page Question
Regions What is a region, what kinds are there, and what do its coordinates mean?
Region Shapes How do I define, compose and edit the shape?
Apertures and Spots What exactly is a spot looking at, and where does its emissivity apply?
Region Statistics What comes back, and how do I read it per frame?

On this page

Creating a region

The everyday constructors take the terms an operator thinks in. None of them requires a matrix.

using optris::sdk::Region;

Region::Rect(centre, size, rotation_rad = 0);
Region::Circle(centre, radius);
Region::Ellipse(centre, radii, rotation_rad = 0);
Region::Line(a, b);
Region::Spot(at, aperture = {});
Region::Polygon(points);
Region::Spline(points);        // a curve through the points
Region::ClosedSpline(points);  // the same curve, closed into an area
Region::Ring(centre, outer_radius, inner_radius);

Curves

Region::Spline() is the curved sibling of Region::Line(), and Region::ClosedSpline() of Region::Polygon(). Both build the one Primitive::kSpline, a centripetal Catmull-Rom curve that passes through every control point it is given; drag a point and the curve goes there.

Open or closed is in the control points, and it changes what the region measures. The curve is closed when its last control point equals its first exactly, which is what ClosedSpline() arranges for you:

Contains() RegionSample::position
open: a path, no area the pixels it runs through, rasterised on the frame's pixel lattice exactly as a line is arc length from the first control point, samples ordered by it
closed: an area the pixels it encloses, by the same even-odd rule a polygon uses quiet NaN: the samples are interior pixels, which have no position on the outline

So an open spline is what to draw along a weld or a pipe and chart as a profile, and a closed one is what to draw around a part. There is deliberately no second primitive for the closed case: the geometry tree already composes areas, so a curved area with a hole is ClosedSpline(outer).Subtracted(ClosedSpline(inner)) rather than a new shape.

A spline through collinear control points measures exactly the pixels the line through them does: the same set, not a similar one. It uses LineTolerance() and the same rasteriser, so there is no second definition of "which pixels a path crosses" to drift from the first.

Two things to know before building one by hand. SplinePolyline() gives the points to draw; the control points are the handles, and joining them up draws the control polygon instead of the curve. And a spline carries at most kMaxSplinePoints (64) control points, unlike a polygon, because each one costs kSplineSpanSegments edge tests per pixel rather than one; adding points also improves accuracy, so more points is the right answer to a curve that needs more detail rather than a workaround.

Any two of them can be combined, non-mutatingly, into a third:

auto a = Region::Rect(centre, size);
auto b = Region::Circle(centre, radius);

auto both    = a.United(b);        // the area in either
auto overlap = a.Intersected(b);   // the area in both
auto cut     = a.Subtracted(b);    // a, with b removed

Region::FromDrag() builds one from two opposite corners in any order, which is what a mouse drag produces:

auto region = Region::FromDrag(optris::sdk::Primitive::kRect,
                               FromPixels(drag_start, w, h),
                               FromPixels(cursor, w, h));

Dragging up-and-left gives the same region as dragging down-and-right. Primitive::kPoint produces a Region::Spot() at the cursor, and Primitive::kPolygon and Primitive::kSpline produce the drag's bounding rectangle. A drag cannot describe either, and a rectangle is a more useful answer to a UI than nothing. Build them with Region::Polygon(), Region::Spline() or Region::ClosedSpline() once the points are known.

Regions().Add() returns the region's id, or 0 if the geometry is invalid: an empty geometry, a polygon with fewer than three vertices, a spline with fewer than two control points or more than kMaxSplinePoints, a closed spline with fewer than three distinct points, or a boolean tree past the depth or node cap.

Editing live

Edit through Imager::Regions().Edit(). The callback runs under the same lock the frame thread takes to measure, so editing while frames are arriving is safe.

imager->Regions().Edit(id, [&](optris::sdk::Region &r) {
  r.SetCentre(FromPixels(cursor, w, h));
});

Only C++ has the callback. Every binding does the same thing as a read, a change and a write back, and the id and the signal slot survive that too, so no alarm is orphaned. What a binding does not get is the guarantee that the change lands between two frames rather than across them: the lock is held for the duration of the C++ callback, and for one call each in the read-modify-write.

Edit() returns false for an unknown id. The region keeps its id and its signal slot, so every alarm attached to it stays bound. There is no remove-and-re-add and nothing is rebuilt.

Available on Region: SetCentre, SetSize, SetRadius, SetRotation, Translate, Scale, and the matching Centre(), Size(), Rotation() accessors. All of them operate on the whole geometry, so a composite (a ring, say) moves and scales as one rigid body rather than coming apart.

Warning: The callback must not call back into the imager. It runs with the region lock held.

The transform, and when you need it

Transform() and SetTransform() expose the 3×3 matrix directly. Reach for them when you want something the friendly setters cannot express:

  • shear, which no centre/size/rotation triple describes;
  • animation, where emitting a new matrix per frame is the cheapest way to move a region and costs almost nothing to record;
  • an arbitrary transform you already have from elsewhere.
imager->Regions().Edit(id, [](optris::sdk::Region &r) {
  r.SetTransform(optris::sdk::Multiply(shear, r.Transform()));
});

LeafTransforms() and SetLeafTransforms() reach every shape in a composite, in tree order.

Centre(), Size() and Rotation() are exact for regions built by the constructors above and best-effort for a matrix you set yourself; HasShear() reports the difference. The friendly setters preserve only translation, rotation and scale, so applying one to a sheared region drops the shear.

Boolean composition

Regions compose with union, intersection and subtraction, nested to any depth within the caps. This is how shapes like rings are expressed, instead of adding a new entry to a shape enum for each request.

There are two levels. Region::United(), Region::Intersected() and Region::Subtracted() combine two regions and return a new one, which is what an application holds and what an editor needs:

// A plate with two bolt holes taken out of it.
auto region = plate.Subtracted(hole_a).Subtracted(hole_b);

They are non-mutating, and the result is the receiver reshaped: it keeps the receiver's name, style, emissivity, rules and children, and the operand contributes only its shape. The full merge rules, and why these are members rather than symmetric-looking free functions, are documented on optris::sdk::Region.

Underneath, Union(), Intersect() and Subtract() combine bare Composite trees, for callers building geometry directly:

using optris::sdk::Subtract;

auto geometry = Subtract(Subtract(MakeShape(...), hole_a), hole_b);

Region::Ring(centre, outer, inner) is the shorthand for the common case, and is exactly Circle(centre, outer).Subtracted(Circle(centre, inner)).

The tree is held as a flat node array with indices, so a region stays a plain value: copyable, comparable and serializable, with no heap nodes. Point-in-region walks it from the root; bounding boxes combine by box algebra, so a subtract never widens the area scanned and an intersect narrows it.

Limits: 16 nodes deep, 64 nodes total (kMaxGeometryDepth, kMaxGeometryNodes), checked at Regions().Add(), which returns 0 for a tree past them. Call IsValid(region.geometry) first when building one up in a loop. A region is a measuring area, not a CSG modeller, and an unbounded tree is an unbounded per-pixel cost on the frame thread.

Hierarchy

Region::children holds nested regions. A kHotSpot or kColdSpot child tracks the extreme within its parent's geometry and clips its aperture to that parent:

auto area = Region::Rect({0.3f, 0.3f}, {0.4f, 0.4f});
area.name = "Zone A";

auto hot = Region::Spot({0.0f, 0.0f}, aperture);
hot.kind = optris::sdk::RegionKind::kHotSpot;
hot.name = "Zone A hot";
area.children.push_back(std::move(hot));

const auto id = imager->Regions().Add(std::move(area));

Only C++ can nest before adding. Region::children is a C++ field with no ABI counterpart, so a binding adds the parent first and then nests onto the id it was given, which is AddChild below. The result on the camera is the same tree.

Children get ids of their own, so a rule can name one directly. Region::tracked_loc holds where a tracked spot currently sits, in region coordinates.

Nesting a region onto one that is already on the camera is Regions().AddChild(parent_id, child), which returns the child's id or 0 if the parent is unknown or the child's geometry is invalid. A kHotSpot or kColdSpot child carries no geometry of its own (its position is wherever the extreme turns out to be) and is exempt from that check. Regions().Children(parent_id) lists the direct children, in the order they were added; it is empty both for a region with none and for an unknown id, so use Get() when the difference matters. A child is reachable by its own id through Get(), Edit(), Remove(), Samples(), Percentiles() and SignalSlot(), all of which search the whole tree.

Moving a region under a different parent is RegionStore::Reparent(id, new_parent_id) (0 means the top level), which keeps the region's id, and therefore its signal slot, its rules and its children. That matters: the alternative is to remove the region and call AddChild() with a copy, and AddChild() mints a fresh id, so every rule naming the old id is silently orphaned and nesting a measuring area quietly disables its alarms. Geometry is in frame coordinates rather than parent-relative, so a reparented region stays exactly where it was on the image. What legitimately changes is what a tracked kind measures, because a kHotSpot searches within its parent. Reparent() reports kOk, kNoSuchRegion, kNoSuchParent, kWouldCycle or kAlreadyThere and changes nothing on any result but the first; WouldCycle() answers the same question before a drag is offered rather than after it is made.

Note: Reparent() and WouldCycle() are on the bare RegionStore (optris/sdk/region.h), not on the RegionEngine that Imager::Regions() returns. There is no reparent on a live camera today.

Warning: Children do not appear in Regions().States(). A child's alarm already rolls up into its parent's RegionState, so listing it separately would count it twice in anything that sums the list. Enumerate children with Children() and read each one through Get().

Persistence

Imager::SaveConfig() and LoadConfig() write and restore the regions and their rules alongside the camera settings, the motion zones, the colour windows and the canvas placement.

imager->SaveConfig("setup.json");
// ... later, or on another machine ...
imager->LoadConfig("setup.json");

Loading replaces the regions and rules already on the camera; anything added since the file was written is discarded. Ids are reassigned on load, so a region's rules are re-attached to the id it actually receives. LoadConfig() returns a ConfigLoadReport saying which sections were applied, and a file that cannot work on this camera is refused before anything is changed. The same schema is written into a recording's metadata, so a recording carries the measuring areas it was made with, and a per-frame record repeats a region's matrices only on frames where it moved.

Rendering

Regions are drawn as overlays, coloured by Region::style and switched to the alarm colour while any of their rules is ringing. There are two ways to get them, and they answer different questions.

Draw them yourself, at your own resolution. Display().CompositeOverlays(rgba, w, h) paints the current outlines, markers and blob contours onto a BGRA8 buffer of any size. Primitives are built to fill the size given, so a display-resolution buffer keeps the overlays crisp when the image is magnified. It uses the GPU where there is a GPU backend and a CPU rasteriser where there is not, so it works on a simulated camera and on a machine with no GPU alike; it returns false only when there is nothing to draw.

Or have the SDK draw them into the delivered frame. Whether it does by default depends on where the picture is made. A camera whose picture is made on the graphics card (a GPU backend) draws them by default; Display().SetOverlayCompositing(false) stops it, and it leaves them out while an Orientation is set. A camera on the CPU pipeline, the simulated camera and a Player draw nothing until Display().SetOverlayCompositing(true), deliberately: until the CPU rasteriser existed that path drew nothing whatever the flag said, so those callers have been receiving frames without outlines, and switching it on by default would put outlines into their images at once, including the callers that composite their own at display resolution, which would then draw them twice. Overlays composited this way are at the frame's own resolution.

Display().GetOverlayCompositing() reads the setting back: true on a fresh imager or player, on every backend. It reports the setting, not the picture, so on the CPU pipeline, the simulated camera and a Player it reads true while no outlines are drawn until the setter has been called. Whether a saved picture has outlines in it is the snapshot's overlays field.

On the GPU pipeline there is a third path: Display().ConfigureSurface() renders colorized output and overlays straight onto a surface you own, at display resolution. Inline overlays are on there by default, and are switched off automatically while an Orientation is set (the overlay would otherwise be rotated away from the region), in which case CompositeOverlays() is the way to draw them.

A boolean tree draws as one outline per shape leaf, with leaves reached through an odd number of subtract branches drawn dashed. So a ring appears as two circles, the inner one dashed, rather than as an annulus. That is an approximation: exact CSG outline extraction is a contouring problem, the overlay is an operator aid, and the measurement itself always uses the real tree.