RegAlgebra: Boolean Algebra on Spatial Regions

Summary

This document describes the boolean arithmetic defined for region expressions.

Description

When defining a region, several shapes can be combined using boolean operations. The boolean operators are (in order of precedence):
  Symbol        Operator                Associativity
  ------        --------                -------------
  !             not                     right to left
  &             and                     left to right
  ^             exclusive or            left to right
  |             inclusive or            left to right

For example, to create a mask consisting of a large circle with a smaller box removed, one can use the and and not operators:
  $ regdisp snr.ev "circle(512 512 5) & \!circle(512,512,2)"
  regions: circle(512 512 5) & !circle(512,512,2)
  .............
  .............
  ....11111....
  ...1111111...
  ..111111111..
  ..111...111..
  ..111...111..
  ..111...111..
  ..111111111..
  ...1111111...
  ....11111....
  .............
  .............
A three-quarter circle can be defined as:
  $ regdisp snr.ev "circle(512,512,5) & \!pie(512,512,270,360)"
  regions: circle(512,512,5) & !pie(512,512,270,360)
  .............
  .............
  ....11111....
  ...1111111...
  ..111111111..
  ..111111111..
  ..11111......
  ..11111......
  ..11111......
  ...1111......
  ....111......
  .............
  .............
Two non-intersecting ellipses can be made into the same region:
  $ regdisp snr.ev "ellipse(522,522,5,10,90) | ellipse(512,512,10,5,0)"
  regions: ellipse(522,522,5,10,90) | ellipse(512,512,10,5,0)
  .................................
  .................................
  ................11111111111......
  ..............111111111111111....
  ............1111111111111111111..
  ............1111111111111111111..
  ............1111111111111111111..
  ............1111111111111111111..
  ............1111111111111111111..
  ..............111111111111111....
  ................11111111111......
  .................................
  ......11111111111................
  ....111111111111111..............
  ..1111111111111111111............
  ..1111111111111111111............
  ..1111111111111111111............
  ..1111111111111111111............
  ..1111111111111111111............
  ....111111111111111..............
  ......11111111111................
  .................................
  .................................
You can use several boolean operations in a single region expression, to create arbitrarily complex regions. With the important exception below, you can apply the operators in any order, using parentheses if necessary to override the natural precedence of the operators.

NB: Using a panda shape is always much more efficient than explicitly specifying "pie & annulus", due to the ability of panda to place a limit on the number of pixels checked in the pie shape. If you are going to specify the intersection of pie and annulus, use panda instead.

As described in "help regexamples", the PIE slice goes to the edge of the field. To limit its scope, PIE usually is is combined with other shapes, such as circles and annuli, using boolean operations. In this context, it is worth noting that that there is a difference between -PIE and &!PIE. The former is a global exclude of all pixels in the PIE slice, while the latter is a local excludes of pixels affecting only the region(s) with which the PIE is combined. For example, the following region uses &!PIE as a local exclude of a single circle. Two other circles are also defined and are unaffected by the local exclude:

  $ cat foo
  circle(10,20,5)
  circle(20,20,5)&!pie(20,20,60,120)&!pie(20,20,240,300)
  circle(30,20,5)
  $ regdisp snr.fits @foo
  regions: @foo
  .................................
  .................................
  ....11111...............33333....
  ...1111111...22...22...3333333...
  ..111111111.222...222.333333333..
  ..111111111.2222.2222.333333333..
  ..111111111.222222222.333333333..
  ..111111111.2222.2222.333333333..
  ..111111111.222...222.333333333..
  ...1111111...22...22...3333333...
  ....11111...............33333....
  .................................
  .................................
Note that the two other regions are not affected by the &!PIE, which only affects the circle with which it is combined.

On the other hand, a -PIE is an global exclude that does affect other regions with which it overlaps:

  # two small circles inside a larger one
  # no excludes
  $ cat foo
  circle(20,15,3)
  circle(20,25,3)
  circle(20,20,10)
  $ regdisp snr.fits @foo
  regions: @foo
  .......................
  .......................
  .......333333333.......
  ......33333333333......
  ....333332222233333....
  ....333332222233333....
  ...33333322222333333...
  ..3333333222223333333..
  ..3333333222223333333..
  ..3333333333333333333..
  ..3333333333333333333..
  ..3333333333333333333..
  ..3333333333333333333..
  ..3333333333333333333..
  ..3333333111113333333..
  ..3333333111113333333..
  ...33333311111333333...
  ....333331111133333....
  ....333331111133333....
  ......33333333333......
  .......333333333.......
  .......................
  .......................

  # two small circles inside a larger one
  # with global pie excludes
  $ cat foo
  circle(20,15,3)
  circle(20,25,3)
  circle(20,20,10)
  -pie(20,20,60,120)
  -pie(20,20,240,300)
  $ regdisp snr.fits @foo
  regions: @foo
  .......................
  .......................
  ......3.........3......
  ....333.........333....
  ....3333.......3333....
  ...333333.....333333...
  ..3333333.....3333333..
  ..33333332...23333333..
  ..33333333...33333333..
  ..333333333.333333333..
  ..3333333333333333333..
  ..333333333.333333333..
  ..33333333...33333333..
  ..33333331...13333333..
  ..3333333.....3333333..
  ...333333.....333333...
  ....3333.......3333....
  ....333.........333....
  ......3.........3......
  .......................
  .......................
The two smaller circles are entirely contained within the two exclude PIE slices and therefore are excluded from the region.


Other pages of interest:
Last updated: August 7, 2015