def ang_to_quat(offsets): """Convert cartesian angle offsets and rotation into quaternions. Each offset contains two angles specifying the distance from the Z axis in orthogonal directions (called "X" and "Y"). The third angle is the rotation about the Z axis. A quaternion is computed that first rotates about the Z axis and then rotates this axis to the specified X/Y angle location. Args: offsets (list of arrays): Each item of the list has 3 elements for the X / Y angle offsets in radians and the rotation in radians about the Z axis. Returns: (list): List of quaternions, one for each item in the input list. """ out = list() zaxis = np.array([0, 0, 1], dtype=np.float64) for off in offsets: angrot = qa.rotation(zaxis, off[2]) wx = np.sin(off[0]) wy = np.sin(off[1]) wz = np.sqrt(1.0 - (wx * wx + wy * wy)) wdir = np.array([wx, wy, wz]) posrot = qa.from_vectors(zaxis, wdir) out.append(qa.mult(posrot, angrot)) return out
def test_fromvectors(self): axis = np.array([0,0,1]) angle = np.radians(30) v1 = np.array([1, 0, 0]) v2 = np.array([np.cos(angle), np.sin(angle), 0]) np.testing.assert_array_almost_equal(qarray.from_vectors(v1, v2), np.array([0, 0, np.sin(np.radians(15)), np.cos(np.radians(15))]))
def triangle(npos, width, rotate=None): """Compute positions in an equilateral triangle layout. Args: npos (int): The number of positions packed onto wafer=3 width (float): distance between tubes in degrees rotate (array, optional): Optional array of rotation angles in degrees to apply to each position. Returns: (array): Array of quaternions for the positions. """ zaxis = np.array([0, 0, 1], dtype=np.float64) sixty = np.pi / 3.0 thirty = np.pi / 6.0 rtthree = np.sqrt(3.0) rtthreebytwo = 0.5 * rtthree tubedist = width * np.pi / 180.0 result = np.zeros((npos, 4), dtype=np.float64) posangarr = np.array([sixty * 3.0 + thirty, -thirty, thirty * 3.0]) for pos in range(npos): posang = posangarr[pos] posdist = tubedist / rtthree posx = np.sin(posdist) * np.cos(posang) posy = np.sin(posdist) * np.sin(posang) posz = np.cos(posdist) posdir = np.array([posx, posy, posz], dtype=np.float64) norm = np.sqrt(np.dot(posdir, posdir)) posdir /= norm posrot = qa.from_vectors(zaxis, posdir) if rotate is None: result[pos] = posrot else: prerot = qa.rotation(zaxis, rotate[pos] * np.pi / 180.0) result[pos] = qa.mult(posrot, prerot) return result
def rhombus_layout(npos, width, rotate=None): """Compute positions in a hexagon layout. This particular rhombus geometry is essentially a third of a hexagon. In other words the aspect ratio of the rhombus is constrained to have the long dimension be sqrt(3) times the short dimension. The rhombus is projected on the sphere and centered on the Z axis. The X axis is along the short direction. The Y axis is along the longer direction. For example:: O Y ^ O O | O O O | O O O O +--> X O O O O O O Each position is numbered 0..npos-1. The first position is at the "top", and then the positions are numbered moving downward and left to right. The extent of the rhombus is directly specified by the width parameter which is the angular extent along the X direction. Args: npos (int): The number of positions in the rhombus. width (float): The angle (in degrees) subtended by the width along the X axis. rotate (array, optional): Optional array of rotation angles in degrees to apply to each position. Returns: (array): Array of quaternions for the positions. """ zaxis = np.array([0, 0, 1], dtype=np.float64) rtthree = np.sqrt(3.0) angwidth = width * np.pi / 180.0 dim = rhomb_dim(npos) # find the angular packing size of one detector posdiam = angwidth / (dim - 1) result = np.zeros((npos, 4), dtype=np.float64) for pos in range(npos): posrow, poscol = rhomb_row_col(npos, pos) rowang = 0.5 * rtthree * ((dim - 1) - posrow) * posdiam relrow = posrow if posrow >= dim: relrow = (2 * dim - 2) - posrow colang = (float(poscol) - float(relrow) / 2.0) * posdiam distang = np.sqrt(rowang**2 + colang**2) zang = np.cos(distang) posdir = np.array([colang, rowang, zang], dtype=np.float64) norm = np.sqrt(np.dot(posdir, posdir)) posdir /= norm posrot = qa.from_vectors(zaxis, posdir) if rotate is None: result[pos] = posrot else: prerot = qa.rotation(zaxis, rotate[pos] * np.pi / 180.0) result[pos] = qa.mult(posrot, prerot) return result
def hex_layout(npos, width, rotate=None): """Compute positions in a hexagon layout. Place the given number of positions in a hexagonal layout projected on the sphere and centered at z axis. The width specifies the angular extent from vertex to vertex along the "X" axis. For example:: Y ^ O O O | O O O O | O O + O O +--> X O O O O O O O Each position is numbered 0..npos-1. The first position is at the center, and then the positions are numbered moving outward in rings. Args: npos (int): The number of positions packed onto wafer. width (float): The angle (in degrees) subtended by the width along the X axis. rotate (array, optional): Optional array of rotation angles in degrees to apply to each position. Returns: (array): Array of quaternions for the positions. """ zaxis = np.array([0, 0, 1], dtype=np.float64) nullquat = np.array([0, 0, 0, 1], dtype=np.float64) sixty = np.pi / 3.0 thirty = np.pi / 6.0 rtthree = np.sqrt(3.0) rtthreebytwo = 0.5 * rtthree angdiameter = width * np.pi / 180.0 # find the angular packing size of one detector nrings = hex_nring(npos) posdiam = angdiameter / (2 * nrings - 2) result = np.zeros((npos, 4), dtype=np.float64) for pos in range(npos): if pos == 0: # center position has no offset posrot = nullquat else: # Not at the center, find ring for this position test = pos - 1 ring = 1 while (test - 6 * ring) >= 0: test -= 6 * ring ring += 1 sectors = int(test / ring) sectorsteps = np.mod(test, ring) # Convert angular steps around the ring into the angle and distance # in polar coordinates. Each "sector" of 60 degrees is essentially # an equilateral triangle, and each step is equally spaced along # the edge opposite the vertex: # # O # O O (step 2) # O O (step 1) # X O O O (step 0) # # For a given ring, "R" (center is R=0), there are R steps along # the sector edge. The line from the origin to the opposite edge # that bisects this triangle has length R*sqrt(3)/2. For each # equally-spaced step, we use the right triangle formed with this # bisection line to compute the angle and radius within this # sector. # The distance from the origin to the midpoint of the opposite # side. midline = rtthreebytwo * float(ring) # the distance along the opposite edge from the midpoint (positive # or negative) edgedist = float(sectorsteps) - 0.5 * float(ring) # the angle relative to the midpoint line (positive or negative) relang = np.arctan2(edgedist, midline) # total angle is based on number of sectors we have and the angle # within the final sector. posang = sectors * sixty + thirty + relang posdist = rtthreebytwo * posdiam * float(ring) / np.cos(relang) posx = np.sin(posdist) * np.cos(posang) posy = np.sin(posdist) * np.sin(posang) posz = np.cos(posdist) posdir = np.array([posx, posy, posz], dtype=np.float64) norm = np.sqrt(np.dot(posdir, posdir)) posdir /= norm posrot = qa.from_vectors(zaxis, posdir) if rotate is None: result[pos] = posrot else: prerot = qa.rotation(zaxis, rotate[pos] * np.pi / 180.0) result[pos] = qa.mult(posrot, prerot) return result