# SpatialLocationCalculator

SpatialLocationCalculator node calculates the spatial coordinates of the ROI (region-of-interest) based on the `depth` map from
the `inputDepth`. By default, it will take the median of the depth values in the ROI and remove the ones out of range.

If you link ImgDetections to `inputDetections`, the node also calculates the spatial coordinates of each detection and sends them
as SpatialImgDetections on `outputDetections`.

ImgDetections that you send from the host must have the transformation of the image that their coordinates refer to
(`setTransformation()`). Use the transformation of the depth frame only when the detection coordinates are in depth image
coordinates. Without a transformation, the node does not send spatial detections. On RVC4, the node stops and the device
connection is lost.

You can also calculate spatial coordinates on host side, [demo
here](https://github.com/luxonis/oak-examples/tree/main/depth-measurement/calc-spatial-on-host). The demo also has logic similar
to the one performed on the device (`utils/measure_distance.py` file).

![Colorized depth frame with a ROI box and its spatial X, Y and Z coordinates](https://docs.luxonis.com/static/images/software-v3/depthai/depthai-components/nodes/colorized-depth-frame-with-a-roi-box-and-its-spatial-x-y.webp)

## How to place it

#### Python

```python

pipeline = dai.Pipeline()
spatialCalc = pipeline.create(dai.node.SpatialLocationCalculator)
```

#### C++

```cpp

dai::Pipeline pipeline;
auto spatialCalc = pipeline.create<dai::node::SpatialLocationCalculator>();
```

## Inputs and Outputs

## Usage

#### Python

```python

pipeline = dai.Pipeline()
spatialCalc = pipeline.create(dai.node.SpatialLocationCalculator)
spatialCalc.inputConfig.setWaitForMessage(False)

# Set initial config
config = dai.SpatialLocationCalculatorConfigData()
config.depthThresholds.lowerThreshold = 100
config.depthThresholds.upperThreshold = 10000

topLeft = dai.Point2f(0.4, 0.4)
bottomRight = dai.Point2f(0.6, 0.6)
config.roi = dai.Rect(topLeft, bottomRight)

spatialCalc.initialConfig.addROI(config)

# You can later send configs from the host (input queue) / Script node to the inputConfig
```

#### C++

```cpp

dai::Pipeline pipeline;
auto spatialCalc = pipeline.create<dai::node::SpatialLocationCalculator>();
spatialCalc->inputConfig.setWaitForMessage(false);

// Set initial config
dai::SpatialLocationCalculatorConfigData config;
config.depthThresholds.lowerThreshold = 100;
config.depthThresholds.upperThreshold = 10000;

dai::Point2f topLeft(0.4f, 0.4f);
dai::Point2f bottomRight(0.6f, 0.6f);
config.roi = dai::Rect(topLeft, bottomRight);

spatialCalc->initialConfig->addROI(config);

// You can later send configs from the host (input queue) / scripting node to the inputConfig
```

## Examples of functionality

 * [Spatial location
   calculator](https://docs.luxonis.com/software-v3/depthai/examples/spatial_location_calculator/spatial_location_calculator.md)

## Spatial coordinate system

OAK camera uses the RDF (Right-Down-Forward) coordinate system for all spatial coordinates.

![Spatial detections of cups with XYZ coordinates](https://docs.luxonis.com/static/images/software/depthai/nodes/spatial_detection_rdf_coordinates.webp)

Middle of the frame is 0,0 in terms of X,Y coordinates. If you go down, Y will increase, and if you go right, X will increase.

## Reference

### dai::node::SpatialLocationCalculator

Kind: class

SpatialLocationCalculator node. Calculates the spatial locations of detected objects based on the input depth map. Spatial
location calculations can be additionally refined by using a segmentation mask. If keypoints are provided, the spatial location is
calculated around each keypoint.

#### constexpr static const char * NAME

Kind: variable

#### std::shared_ptr< SpatialLocationCalculatorConfig > initialConfig

Kind: variable

Initial config to use when calculating spatial location data.

#### Input inputConfig

Kind: variable

Input SpatialLocationCalculatorConfig message with ability to modify parameters in runtime. Default queue is non-blocking with
size 4.

#### Input inputDetections

Kind: variable

Input messages on which spatial location will be calculated. Possible datatypes are ImgDetections or Keypoints.

#### Input inputDepth

Kind: variable

Input message with depth data used to retrieve spatial information about detected object. Default queue is non-blocking with size
4.

#### Output out

Kind: variable

Outputs SpatialLocationCalculatorData message that carries spatial locations for each additional ROI that is specified in the
config.

#### Output outputDetections

Kind: variable

Outputs SpatialImgDetections message that carries spatial locations along with original input data.

#### Output passthroughDepth

Kind: variable

Passthrough message on which the calculation was performed. Suitable for when input queue is set to non-blocking behavior.

#### SpatialLocationCalculator()

Kind: function

#### inline SpatialLocationCalculator(std::unique_ptr< Properties > props)

Kind: function

#### void setRunOnHost(bool runOnHost)

Kind: function

Specify whether to run on host or device By default, the node will run on device.

#### bool runOnHost()

Kind: function

Check if the node is set to run on host

#### void run()

Kind: function

#### inline DeviceNodeCRTP()

Kind: function

#### inline DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### inline DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### inline DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### inline DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
