PRO triangle_space, tr, xc, yc, trc, status, SPACE = space
; Description: This module calculates the triangle space coordinates and perimeter for each triangle
; in the set of triangles "tr" formed from a (subset) of the points with x and y
; coordinates "xc" and "yc", respectively. The default triangle space used is the "mixed"
; triangle space described in the paper by Pal & Bakos (Pal A. & Bakos G.A., 2006, PASP,
; 118, 1474). This triangle space has coordinates Tx and Ty defined by:
;
; Tx = p/a
; Ty = q/a
;
; where a, p and q are the lengths of the sides of the triangle in descending size order.
; Other triangle space coordinates, such as the "chiral" and "continuous" spaces
; described in the same paper, may be returned by specifying the relevant value of the
; SPACE keyword.
;
; Input Parameters:
;
; tr - INTEGER/LONG ARRAY - The set of Nt triangles for which the triangle space coordinates and
; perimeters are to be calculated. The array "tr" is a 3 by Nt array of
; numbers where each triplet of numbers represents the indices of the
; points corresponding to the three vertices of each triangle. The module
; will fail if there is at least one triplet of equal numbers (which
; represents in this case a single point, not a triangle or even a line)
; xc - FLOAT/DOUBLE VECTOR - A one-dimensional vector of x coordinates for the set of points which
; contain the vertices of the triangles specified by "tr". Note that the
; set of x coordinates must contain values for all the vertex indices
; specified in "tr".
; yc - FLOAT/DOUBLE VECTOR - A one-dimensional vector of y coordinates for the set of points which
; contain the vertices of the triangles specified by "tr". Note that the
; set of y coordinates must contain values for all the vertex indices
; specified in "tr".
;
; Output Parameters:
;
; trc - DOUBLE ARRAY - A 3 by Nt array of DOUBLES where the first and second elements in each
; triplet of numbers represent the x and y coordinates in the respective
; triangle space of each triangle. The third element in each triplet of
; numbers represents the perimeter of each triangle.
; status - INTEGER - If the module successfully calculated the triangle space coordinates and
; perimeters, then "status" is returned with a value of "1", otherwise it is
; returned with a value of "0".
;
; Keywords:
;
; If the keyword SPACE is set to the string "chiral", then triangle coordinates are calculated
; in the chiral triangle space as described in Pal & Bakos 2006. If the keyword SPACE is set to
; the string "continuous", then triangle coordinates are calculated in the continuous triangle
; space as described in Pal & Bakos 2006. If the keyword SPACE is set to any other value (or
; not set), then the triangle coordinates are calculated in the default mixed triangle space
; defined above.
;
; Author: Dan Bramich (dan.bramich@hotmail.co.uk)
;
; History:
;
; 24/04/2009 - Further module optimisation performed (dmb).
; 08/08/2008 - Module created (dmb).
;Check that "tr" contains values of the correct number type, and that it has the correct dimensions
;Check that "xc" and "yc" are one-dimensional vectors of the correct number type, and that they
;the same number of elements
;Check that the triangle list "tr" contains no negative indices
;Check that the maximum index specified in the triangle list "tr" has x and y coordinates specified
;in "xc" and "yc"
;If the SPACE keyword is set
;If the SPACE keyword is not set to either of "chiral" or "continuous"
;If the SPACE keyword is not set
;Create a distance array (N.B: Strangely, taking the "sqrt" inside the "for" loop, leads to
;very slightly better performance here!)
;Set up the triangle coordinates array
;If the triangle space is set to "mixed"
;Calculate the triangle space coordinates for the "mixed" triangle space and the perimeter
;for each triangle in the triangle list
;Set up some variables
;Let d1 be the distance between the vertices 1 and 2, d2 be the distance between vertices 1
;and 3, and d3 be the distance between vertices 2 and 3. If the distances in ascending order
;are d1, d2 and d3, then calculate the triangle space coordinates for the current triangle.
;If the distances in ascending order are d1, d3 and d2, or d2, d1 and d3
;If the distances in ascending order are d1, d3 and d2, then calculate the triangle space
;coordinates for the current triangle
;If the distances in ascending order are d2, d1 and d3, then calculate the triangle space
;coordinates for the current triangle
;If the distances in ascending order are d3, d1 and d2, or d2, d3 and d1
;If the distances in ascending order are d3, d1 and d2, then calculate the triangle space
;coordinates for the current triangle
;If the distances in ascending order are d2, d3 and d1, then calculate the triangle space
;coordinates for the current triangle
;If the distances in ascending order are d3, d2 and d1, then calculate the triangle space
;coordinates for the current triangle
;Calculate the perimeter of the current triangle
;If the triangle space is set to "chiral" or "continuous"
;Calculate the triangle space coordinates for the "chiral" triangle space and the perimeter
;for each triangle in the triangle list
;Set up some variables
;Let d1 be the distance between the vertices 1 and 2, d2 be the distance between vertices 1
;and 3, and d3 be the distance between vertices 2 and 3. If the distances in ascending order
;are d1, d2 and d3, then continue.
;Order the vertices based on their x coordinates
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the distances in ascending order are d1, d3 and d2, or d2, d1 and d3
;If the distances in ascending order are d1, d3 and d2
;Order the vertices based on their x coordinates
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the distances in ascending order are d2, d1 and d3
;Order the vertices based on their x coordinates
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the distances in ascending order are d3, d1 and d2, or d2, d3 and d1
;If the distances in ascending order are d3, d1 and d2
;Order the vertices based on their x coordinates
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the distances in ascending order are d2, d3 and d1
;If the longest side is not vertical, then calculate the triangle space coordinates for
;the current triangle
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the distances in ascending order are d3, d2 and d1
;If the longest side is not vertical, then calculate the triangle space coordinates for
;the current triangle
;If the longest side is vertical, then calculate the triangle space coordinates for the
;current triangle
;If the triangle space is set to "continuous", then continue to transform the "chiral"
;triangle space coordinates to "continuous" triangle space coordinates
;Flip the chiral triangle space coordinates along the line Tx + Ty = 1, where Tx
;and Ty are the triangle space coordinates
;Radially magnify the flipped chiral triangle space such that the line Tx + Ty = 1
;is mapped to Tx^2 + Ty^2 = 1, and the magnification factor is only a function of
;theta. Then apply an azimuthal slew by a factor of 4 to identify the Ty = 0, Tx > 0
;and Tx = 0, Ty > 0 edges of the triangle space.
;Save the triangle space coordinates
;Calculate the perimeter of the current triangle
;Set the status to 1