# Unigine::BoundSphere Class

Header: | #include <UnigineBounds.h> |

This class serves to construct the bounding sphere in single precision coordinates.

Instances of this class are deleted automatically when it is necessary.

In case of double precision coordinates, the bounding sphere should be constructed by using the **WorldBoundSphere** class.
It includes the same functions as the *BoundSphere* class, but its functions deal with the double precision coordinates.

To support both single and double precision builds, you can use the

*WorldBoundSphere*class only. The engine will automatically substitute it with the*BoundSphere*if it is required.## BoundSphere Class

### Members

## BoundSphere
(
)
*#*

Constructor. Creates an empty bounding sphere.## BoundSphere
(
const vec3 & center, float radius
)
*#*

Initialization by the center and radius of the bounding sphere.### Arguments

*const vec3 &***center**- The bounding sphere center.*float***radius**- The bounding sphere radius.

## BoundSphere
(
const BoundSphere & bs
)
*#*

Initialization by the bounding sphere.### Arguments

*const BoundSphere &***bs**- The bounding sphere.

## BoundSphere
(
const BoundSphere & bs, const mat4 & transform
)
*#*

Initialization by the bounding sphere and setting the given transformation matrix to the new bounding sphere.### Arguments

*const BoundSphere &***bs**- The bounding sphere.*const mat4 &***transform**- Transformation matrix.

## explicit BoundSphere
(
const BoundBox & bb
)
*#*

Initialization by the bounding box.### Arguments

*const BoundBox &***bb**- The bounding box.

## BoundSphere
(
const vec3 * points, int num_points, int optimal
)
*#*

Initialization by the vector of points.### Arguments

*const vec3 ****points**- Vector of points.*int***num_points**- Number of points in the vector.*int***optimal**- Flag defining if the optimal sphere should be used. If**0**, the sphere will be expanded for including all the given points.

## void set
(
const vec3 & center, float radius
)
*#*

Sets the bounding sphere by its center and radius.### Arguments

*const vec3 &***center**- The bounding sphere center.*float***radius**- The bounding sphere radius.

## void set
(
const BoundSphere & bs
)
*#*

Sets the bounding sphere.### Arguments

*const BoundSphere &***bs**- The bounding sphere.

## void set
(
const BoundBox & bb
)
*#*

Sets the bounding sphere by the bounding box.### Arguments

*const BoundBox &***bb**- The bounding box.

## const vec3 & getCenter
(
)
*#* const

Returns the bounding sphere center.### Return value

The bounding sphere center.## float getRadius
(
)
*#* const

Returns the bounding sphere radius.### Return value

The bounding sphere radius.## void setTransform
(
const mat4 & transform
)
*#*

Sets the given transformation matrix to bounding sphere.### Arguments

*const mat4 &***transform**- Transformation matrix.

## void setTransform
(
const dmat4 & transform
)
*#*

Sets the given transformation matrix to bounding sphere.### Arguments

*const dmat4 &***transform**- Transformation matrix.

## int isValid
(
)
*#* const

Checks the bounding sphere status.### Return value

**1**if the bounding sphere radius has a positive value; otherwise,

**0**.

## void clear
(
)
*#*

Clears the bounding sphere.## void expand
(
const BoundSphere & bs
)
*#*

Expands the current bounding sphere for including given bounding sphere.### Arguments

*const BoundSphere &***bs**- Bounding sphere to be included.

## void expand
(
const BoundBox & bb
)
*#*

Expands the current bounding sphere for including given bounding box.### Arguments

*const BoundBox &***bb**- Bounding box to be included.

## void expand
(
const vec3 & point
)
*#*

Expands the current bounding sphere for including given point.### Arguments

*const vec3 &***point**- Point coordinates to be included.

## void expand
(
const vec3 * points, int num_points
)
*#*

Expands the current bounding sphere for including all points in the vector.### Arguments

*const vec3 ****points**- Vector of points.*int***num_points**- Number of points.

## void expandRadius
(
const vec3 & point
)
*#*

Expands the radius of the bounding sphere.
```
float r = length(center - point);
if (center.w < r)
radius = r;
```

### Arguments

*const vec3 &***point**- Point coordinates the end point of radius.

## void expandRadius
(
const BoundSphere & bs
)
*#*

Expands the radius of the bounding sphere by using the radius of the given bounding sphere.
```
float r = length(bs.center - center) + bs.radius;
if (center.w < r)
radius = r;
```

### Arguments

*const BoundSphere &***bs**- Bounding sphere.

## void expandRadius
(
const BoundBox & bb
)
*#*

Expands the radius of the bounding sphere by using the max and min points of the given bounding box. It uses the expandRadius method.
```
const vec3 &min = bb.getMin();
const vec3 &max = bb.getMax();
expandRadius(vec3(min.x, min.y, min.z));
expandRadius(vec3(max.x, min.y, min.z));
expandRadius(vec3(min.x, max.y, min.z));
expandRadius(vec3(max.x, max.y, min.z));
expandRadius(vec3(min.x, min.y, max.z));
expandRadius(vec3(max.x, min.y, max.z));
expandRadius(vec3(min.x, max.y, max.z));
expandRadius(vec3(max.x, max.y, max.z));
```

### Arguments

*const BoundBox &***bb**- Bounding box.

## void expandRadius
(
const vec3 * points, int num_points
)
*#*

Expands the radius of the current bounding sphere for including all points in the vector.### Arguments

*const vec3 ****points**- Vector of points.*int***num_points**- Number of points.

## int inside
(
const vec3 & point
)
*#*

Checks if the given point is inside the current bounding sphere.### Arguments

*const vec3 &***point**- Point.

### Return value

**1**if the point is inside the bounding sphere; otherwise,

**0**.

## int insideValid
(
const vec3 & point
)
*#*

Checks if the given point is inside the current bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const vec3 &***point**- Point.

### Return value

**1**if the given point is inside the bounding sphere; otherwise,

**0**.

## int inside
(
const vec3 & point, float radius
)
*#* const

Checks if the sphere is inside the bounding sphere.### Arguments

*const vec3 &***point**- The coordinates of the center of the sphere .*float***radius**- The sphere radius.

### Return value

**1**if the sphere is inside the bounding sphere; otherwise,

**0**.

## int insideValid
(
const vec3 & point, float radius
)
*#*

Checks if the given sphere is inside the current bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const vec3 &***point**- Cente of the sphere.*float***radius**- Radius of the sphere.

### Return value

**1**if the sphere is inside the bounding sphere; otherwise,

**0**.

## int inside
(
const vec3 & min, const vec3 & max
)
*#* const

Checks if the box is inside the bounding sphere.### Arguments

*const vec3 &***min**- The box minimum coordinates.*const vec3 &***max**- The box maximum coordinates.

### Return value

**1**if the box is inside the bounding sphere; otherwise,

**0**.

## int insideValid
(
const vec3 & min, const vec3 & max
)
*#*

Checks if the box is inside the bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const vec3 &***min**- The box minimum coordinates.*const vec3 &***max**- The box maximum coordinates.

### Return value

**1**if the box is inside the bounding sphere; otherwise,

**0**.

## int inside
(
const BoundSphere & bs
)
*#* const

Checks if the bounding sphere is inside the bounding sphere.### Arguments

*const BoundSphere &***bs**- The bounding sphere.

### Return value

**1**if the bounding sphere is inside the bounding sphere; otherwise,

**0**.

## int insideValid
(
const BoundSphere & bs
)
*#*

Checks if the bounding sphere is inside the bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const BoundSphere &***bs**- The bounding sphere.

### Return value

**1**if the bounding sphere is inside the bounding sphere; otherwise,

**0**.

## int inside
(
const BoundBox & bb
)
*#* const

Checks if the bounding box is inside the bounding sphere.### Arguments

*const BoundBox &***bb**- The bounding box.

### Return value

**1**if the bounding box is inside the bounding sphere; otherwise,

**0**.

## int insideValid
(
const BoundBox & bb
)
*#*

Checks if the bounding box is inside the bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const BoundBox &***bb**- The bounding box.

### Return value

**1**if the bounding box is inside the bounding sphere; otherwise,

**0**.

## int insideAll
(
const BoundBox & bb
)
*#*

Checks if the whole given bounding box is inside the current bounding sphere.### Arguments

*const BoundBox &***bb**- Bounding box.

### Return value

**1**if the whole bounding box is inside the bounding sphere; otherwise,

**0**.

## int insideAllValid
(
const BoundBox & bb
)
*#*

Checks if the whole given bounding box is inside the current bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const BoundBox &***bb**- Bounding box.

### Return value

**1**if the whole bounding box is inside the bounding sphere; otherwise,

**0**.

## int insideAll
(
const BoundSphere & bs
)
*#*

Checks if the whole given bounding sphere is inside the current bounding sphere.### Arguments

*const BoundSphere &***bs**- Bounding sphere.

### Return value

**1**if the whole bounding sphere is inside the bounding sphere; otherwise,

**0**.

## int insideAllValid
(
const BoundSphere & bs
)
*#*

Checks if the whole given bounding sphere is inside the current bounding sphere.The method doesn't check if the bounding sphere is valid (has a positive radius).

### Arguments

*const BoundSphere &***bs**- Bounding sphere.

### Return value

**1**if the whole bounding sphere is inside the bounding sphere; otherwise,

**0**.

## BoundSphere & operator=
(
const BoundSphere & bs
)
*#*

Assignment operator for the bounding sphere.### Arguments

*const BoundSphere &***bs**- The bounding sphere.

## void set
(
const vec3 * points, int num_points, int optimal
)
*#*

Set the bounding sphere by a vector of points.### Arguments

*const vec3 ****points**- Vector of points.*int***num_points**- Number of points in the vector.*int***optimal**- Flag defining if the optimal sphere should be used. If**0**, the sphere will be expanded for including all the given points.

## void set
(
const BoundSphere & bs, const mat4 & transform
)
*#*

Set the bounding sphere by a bounding sphere with a transformation matrix taken into account.### Arguments

*const BoundSphere &***bs**- The bounding sphere.*const mat4 &***transform**- Transformation matrix.

## int compare
(
const BoundSphere & bs
)
*#*

Compares the bounding sphere with the given one according to the degree of precision equal to **1.0e-6f**.

### Arguments

*const BoundSphere &***bs**- The bounding sphere to compare with.

### Return value

**1**if the radii and the centers of both bounding spheres are equal; otherwise,

**0**.

## int operator==
(
const BoundSphere & bs
)
*#*

Compares the bounding sphere with the given one according to the degree of precision equal to **1.0e-6f**.

### Arguments

*const BoundSphere &***bs**- The bounding sphere to compare with.

### Return value

**1**if the radii and the centers of both bounding spheres are equal; otherwise,

**0**.

## int operator!=
(
const BoundSphere & bs
)
*#*

Bounding spheres not equal comparison operator.### Arguments

*const BoundSphere &***bs**- The bounding sphere to compare with.

### Return value

**1**if the radii and the centers of both bounding spheres are not equal; otherwise,

**0**.

## int rayIntersection
(
const vec3 & point, const vec3 & direction
)
*#*

Checks for an intersection of a ray with the current bounding sphere.### Arguments

*const vec3 &***point**- The starting point of the ray.*const vec3 &***direction**- The direction vector of the ray.

### Return value

**1**if an intersection has occured; otherwise,

**0**.

## int rayIntersectionValid
(
const vec3 & point, const vec3 & direction
)
*#*

Checks for an intersection of a ray with the current bounding sphere.This method doesn't check if the current bounding sphere is valid (has a positive radius).

### Arguments

*const vec3 &***point**- The starting point of the ray.*const vec3 &***direction**- The direction vector of the ray.

### Return value

**1**if an intersection has occured; otherwise,

**0**.

## int getIntersection
(
const vec3 & p0, const vec3 & p1
)
*#*

Checks for an intersection of a line with the current bounding sphere.### Arguments

*const vec3 &***p0**- The starting point of the line.*const vec3 &***p1**- The ending point of the line.

### Return value

**1**if an intersection has occured; otherwise,

**0**.

## int getIntersectionValid
(
const vec3 & p0, const vec3 & p1
)
*#*

Checks for an intersection of a line with the current bounding sphere.This method doesn't check if the current bounding sphere is valid (has a positive radius).

### Arguments

*const vec3 &***p0**- The starting point of the line.*const vec3 &***p1**- The ending point of the line.

### Return value

**1**if an intersection has occured; otherwise,

**0**.

## float distance
(
)
*#*

Returns the distance from the origin of coordinates to the closest point of the current bounding sphere.### Return value

Distance from the origin, in units.## float distanceValid
(
)
*#*

Returns the distance from the origin of coordinates to the closest point of the current bounding sphere.### Return value

Distance from the origin, in units.## float distance
(
const vec3 & point
)
*#*

Returns the distance from the given point to the closest point of the current bounding sphere.### Arguments

*const vec3 &***point**- Point

### Return value

Distance from the point, in units.## float distanceValid
(
const vec3 & point
)
*#*

Returns the distance from the given point to the closest point of the current bounding sphere.### Arguments

*const vec3 &***point**- Point

### Return value

Distance from the point, in units.Last update:
2019-12-25

Help improve this article

Was this article helpful?

(or select a word/phrase and press Ctrl+Enter)