# Operators

## Casting

pgSphere provides some casting operators. So, you can transform an object to another data type. A cast is done using a `CAST(x AS typename)`, `x::typename` or `typename(x)` construct.

## Castings

casting argument | type target | returns
`spoint` | `scircle` | circle with center position `spoint` and radius 0.0
`spoint` | `sellipse` | an ellipse at position `spoint` and radius 0.0
`spoint` | `sline` | a line with length 0.0 at position `spoint`
`scircle` | `sellipse` | the `scircle` as `sellipse`
`sline` | `strans` | the Euler transformation of `sline`
`sellipse` | `scircle` | the bounding circle of `sellipse`
`sellipse` | `strans` | the Euler transformation of `sellipse`

### Cast a spherical point as a circle

    sql> SELECT CAST ( spoint '(10d,20d)' AS scircle );
          scircle
    --------------------
     <(10d , 20d) , 0d>
    (1 row)

## Equality

All data types of pgSphere have equality operators. The equality operator is as in `SQL` `=`. Furthermore, there are two valid negators to indicate that two objects are not equal: `!=` and `<>`.

### Equality of two spherical points

    sql> SELECT spoint '(10d,20d)' = spoint '(370d,20d)' ;
     test
    ------
     t
    (1 row)

## Contain and overlap

On the sphere, an equality relationship is rarely used. There are frequently questions like Is object `a` contained by object `b`? or Does object `a` overlap object `b`? pgSphere supports such queries using binary operators returning true or false:

## Contain and overlap operators

operator | operator returns true, if
<@ or @ (deprecated, not for smoc) | the left object is contained by the right object
@> or ˜ (deprecated, not for smoc) | the left object contains the right object
!<@ or !@ (deprecated, not for smoc) | the left object is not contained by the right object
!@> or !˜ (deprecated, not for smoc) | the left object does not contain the right object
&& | the objects overlap each other
!&& | the objects do not overlap each other

An overlap or contain operator does not exist for all combinations of data types. For instance, scircle <@ `spoint` is useless because a spherical point can never contain a spherical circle.

### Is the left circle contained by the right circle?

    sql> SELECT scircle '<(0d,20d),2d>' <@ scircle '<(355d,20d),10d>' AS test ;
    test
    ------
     t
    (1 row)

### Are the circles overlapping?

    sql> SELECT scircle '<(0d,20d),2d>' && scircle '<(199d,-10d),10d>' AS test ;
     test
    ------
     f
    (1 row)

## Crossing of lines

Another binary relationship is crossing. pgSphere supports only crossing of lines. The correlative operator is named `#`.

### Are the lines crossed?

    sql> SELECT sline '(0d,0d,0d),10d' # sline '(90d,5d,5d,XYZ),10d' AS test ;
     test
    ------
     t
    (1 row)

## Distance

The binary distance operator `<->` is a non-boolean operator returning the distance between two objects in radians. Currently, pgSphere supports only distances between points, circles, and between point and circle. If the objects are overlapping, the distance operator returns zero (0.0).

### Distance between two circles

    sql> SELECT 180 * ( scircle '<(0d,20d),2d>' <-> scircle '<(0d,40d),2d>' )
            / pi() AS dist ;
     dist
    ------
     16
    (1 row)

## Length and circumference

The length/circumference operator `@-@` is a non-boolean unary operator returning the circumference or length of an object. In the current implementation, pgSphere supports only circumferences of circles, polygons, and boxes. It supports lengths of lines and paths too. Instead of using the operator, you can use the functions `circum(object)` or `length(object)`.

### Circumference of a circle

    sql> SELECT 180 * ( @-@ scircle '<(0d,20d),30d>' )/ pi() AS circ ;
     circ
    ------
     180
    (1 row)

### Length of a line

    sql> SELECT 180 * ( @-@ sline '(0d,0d,0d),30d' )/ pi() AS length ;
     length
    --------
     30
    (1 row)

## Center

The center operator `@@` is a non-boolean unary operator returning the center of an object. In the current implementation of pgSphere, only centers of circles and ellipses are supported. Instead of using the operator, you can use the function `center(object)`.

### Center of a circle

    sql> SELECT @@ scircle '<(0d,20d),30d>';

## Change the direction

The unary operator `-` changes the direction of `sline` or `spath` objects. You can use it with an Euler transformation object in the figurative sense, too (op.trans).

### Swap begin and end of a `sline`

    sql> SELECT - sline (spoint '(0d,0d)', spoint '(10d,0d)');

## Turn the path of a line

The unary operator `!` turns the path of `sline` objects, but preserves begin and end of the spherical line. The length of returned line will be 360° minus the line length of operator's argument.

The operator `!` returns `NULL`, if the length of `sline` argument is 0, because the path of returned `sline` is undefined.

### Return length and check if north pole on `sline`s

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT length ( sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) ) *
            180.0 / pi() AS length;
     length
    --------
         10
    (1 row)

    sql> SELECT spoint '(0d,90d)' @
            sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) AS test;
     test
    ------
     f
    (1 row)

    sql> SELECT length ( ! sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) ) *
            180.0 / pi() AS length;
     length
    --------
        350
    (1 row)

    sql> SELECT spoint '(0d,90d)' @
            ! sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) AS test;
     test
    ------
     t
    (1 row)

## Transformation

As in a plane, translations and rotations are needed to do object or coordinate transformations. With pgSphere, it is done using Euler transformations (`strans`). On a sphere, there aren't real translations. All movements on a sphere are rotations around axes.

The general syntax for a transformation is always:

    object operator euler

where operators are `+` for a usual transformation, `-` for an inverse transformation. You can transform any object having a pgSphere data type, except the data type `sbox`.

### Transformation of a point

Rotate a spherical point counterclockwise, first 90° around the `x`-axis, second 90° around the `z`-axis, and last 40.5° around the `x`-axis.

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT spoint '(30d,0d)' + strans '90d, 90d, 40.5d, XZX AS spoint';
       spoint
    ---------------
     (90d , 70.5d)
    (1 row)

You can use the `+` and `-` operator as unary operators for transformations, too. `+`strans`` just returns the transformation itself, `-`strans`` returns the inverse transformation.

### An inverse transformation

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT - strans '20d, 50d, 80d, XYZ' AS inverted;
          inverted
    -----------------------
     280d, 310d, 340d, ZYX
    (1 row)

## `smoc` operators

A unique feature of MOCs compared to the other pgSphere objects is that their unions and intersections are again MOCs. Hence, smocs support two special operators which both yield `smoc` as output:

Union:

    smoc | smoc

Intersection:

    smoc & smoc