Docs
Guides

Group objects into a compound object

A whole made of parts that stay Scene Objects of their own; move or turn the whole and its parts go with it, edit a part and only the part changes.

Some things are made of parts you still want to reach one by one: a bench and its legs, a pump skid and its valves, a stand and the screens on it. A compound object keeps both. The whole is a Scene Object with the geometry group: it draws nothing itself and carries the name, the place and the heading of the thing. Each part is a Scene Object of its own, drawn as itself, that names the whole in partOf.

compound.ts
import { createScene, partsOfWhole } from "@klorad/api/world";

const scene = createScene({ coordinateSystem: { origin: { lat: 40.62637, lon: 22.94838 } } });

// The whole: a group draws nothing itself; its parts draw it.
scene.add({ id: "bench", name: "Bench", position: { east: 10, north: 0 }, geometry: { kind: "group" } });

// Each part is a Scene Object of its own that names its whole.
scene.add({ id: "seat", name: "Seat", position: { east: 10, north: 0, up: 0.45 }, geometry: { kind: "box", width: 2, depth: 0.5, height: 0.06 }, partOf: "bench" });
scene.add({ id: "leg-west", name: "West leg", position: { east: 9.1, north: 0 }, geometry: { kind: "box", width: 0.08, depth: 0.45, height: 0.45 }, partOf: "bench" });
scene.add({ id: "leg-east", name: "East leg", position: { east: 10.9, north: 0 }, geometry: { kind: "box", width: 0.08, depth: 0.45, height: 0.45 }, partOf: "bench" });

// A part is edited on its own: only the seat changes colour.
scene.update("seat", { color: "#8a5a2b" });

// Moving or turning the whole carries every part with it, about the whole's position.
scene.update("bench", { position: { east: 20, north: 5 }, orientation: { heading: 90 } });

const parts = partsOfWhole(scene.objects(), "bench").map((o) => o.name); // ["Seat", "West leg", "East leg"]

// Removing the whole removes its parts.
scene.remove("bench");

export { parts };

The rules

  • Only a group holds parts. A part that names an object that is not in the scene, or is not a group, is refused, and so is a whole put inside one of its own parts.
  • Moving or turning the whole carries every part with it: each keeps its offset from the whole, turned by the change of heading, and turns with it. The pitch and roll of the whole do not tilt its parts, and its scale does not resize them.
  • A part is edited on its own: moving, turning, renaming or describing it changes nothing else.
  • Removing the whole removes its parts. partOf: null takes a part out of its whole first, so it stays.
  • A whole may be a part of another whole; the rules apply at each level.

The same rules hold in a stored scene: a commit that moves or removes a whole brings the update or the removal of its parts with it, in the same revision. expandCompound gives you that batch yourself, and partsOfWhole lists the parts of a whole, the parts of its parts included.

A model carries the same way the objects it draws: each object with feature geometry that names it in of, such as an element of a building linked to a sensor. Moving or turning the model moves them to where it now has them, and removing it removes them. carries(object) says whether an object takes others with it, and carriedBy(objects, id) lists them, a whole's parts and a model's features alike.

In React

A SceneObject takes partOf like any other field. Declare the whole before its parts:

<SceneObject id="bench" name="Bench" position={{ east: 0, north: 0 }} geometry={{ kind: "group" }} />
<SceneObject id="seat" name="Seat" position={{ east: 0, north: 0, up: 0.45 }} geometry={{ kind: "box", width: 3, depth: 0.6, height: 0.08 }} partOf="bench" />

What the readers do

Every reader draws the parts as the objects they are and draws nothing for the whole. In three.js the whole still holds its place, so it can be selected and framed; on a plan and on a map it has no shape.

Try it in your browserExample: Move a whole, or only its partA bench made of its seat and legs: turn the bench and every part turns with it, change a part and only the part changes.

Something wrong or unclear? Tell us.