;+ ; NAME: ; wedge_bounding_box ; PURPOSE: ; Calculate boundary and bounding box for a wedge specified as two points ; in polar coordinates ; CATEGORY: ; gen/idl/toolbox ; CALLING SEQUENCE: FUNCTION wedge_bounding_box, p_box, $ center = center , $ wedge = wedge , $ degrees = degrees , $ dimension = dimension ; INPUTS: ; p_box array[2,2]; type: float ; limiting values in phase angle and radius of the wedge ; in the form [[angle1,radius1],[angle2,radius2]]. ; Angle1 and angle2 are in radians between [-!pi,+!pi]. ; The wedge runs counterclockwise from 'angle1' to 'angle2' ; over less than 180 degrees: either angle2 > angle1 with ; angle2-angle1 < !pi or angle2 < angle1 with ; angle+2*!pi-angle1 < !pi. Always radius1 < radius2. ; OPTIONAL INPUT PARAMETERS: ; center=center ; array[2]; type: float ; center in same units as the radius in p_box. ; This defines the center for the polar coordinates specified in p_box. ; dimension=dimension ; array[2]; type: integer ; size of full image. If present, these are used to modify b_box so ; that it contains valid indices into an array with these dimension ; /degrees if set units of all angles are in degrees (default: radians) ; OUTPUTS: ; Result array[2,2]; type: integer ; corners of the smallest square in the image that completely encloses ; the wedge in pixel coordinates in the form [[x1,y1],[x2,y2]. ; Always x1 <= x2, y1 <= y2. ; OPTIONAL OUTPUT PARAMETERS: ; wedge array[2,n]; type: float ; boundary of wedge as a closed array (first=last point) ; in rectangular (pixel) coordinates. ; INCLUDE: @compile_opt.pro ; On error, return to caller ;+ ; CALLS: ; InitVar, ToRadians ; PROCEDURE: ; The bounding box usually refers to a subsection of a 2D image: ; img[b_box[0,0]:b_box[1,0],b_box[0,1]:b_box[1,1]] ; However, there is no guarantee that b_box lies inside the image, i.e. ; b_box[0,0] or b_box[0,1] might be negative, b_box[1,0] and b_box[1,1] ; might be larger than the image dimensions. Set the 'dimension' keyword to ; force the indices in b_box to be valid indices into the full image array. ; MODIFICATION HISTORY: ; FEB-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ; Extracted from qimage_cw_wedge.pro for use outside widget ;- InitVar, center, [0.0,0.0] rpm = ToRadians(degrees=degrees) pi2 = !dpi/2/rpm twopi = 2*!dpi/rpm n = 101 one = replicate(1,n) p = p_box[0]+(p_box[2]+twopi*fix(p_box[2] LT p_box[0])-p_box[0])*findgen(n)/(n-1) wedge = [ [ p,reverse(p),p[0]], [ p_box[1]*one,p_box[3]*one,p_box[1]] ] ; x,y coordinates of wedge wedge = center#replicate(1,2*n+1)+cv_coord(from_polar=transpose(wedge), /to_rect, degrees=degrees) ; Calculate the quadrant (1,2,3,4) quadrant = 1+fix(( (p_box[[0,2]]+twopi) mod twopi)/pi2) IF quadrant[1] LT quadrant[0] THEN quadrant[1] = quadrant[1]+4 x = p_box[[1,3]]#cos(p_box[[0,2]]*rpm) y = p_box[[1,3]]#sin(p_box[[0,2]]*rpm) b_box = [ [min(x), min(y)], [max(x), max(y)] ] r = p_box[3] ; Outer radius CASE quadrant[0] OF 1: BEGIN IF quadrant[1] GE 2 THEN b_box[3] = b_box[3] > ( r) IF quadrant[1] GE 3 THEN b_box[0] = b_box[0] < (-r) IF quadrant[1] GE 4 THEN b_box[1] = b_box[1] < (-r) END 2: BEGIN IF quadrant[1] GE 3 THEN b_box[0] = b_box[0] < (-r) IF quadrant[1] GE 4 THEN b_box[1] = b_box[1] < (-r) IF quadrant[1] GE 5 THEN b_box[2] = b_box[2] < ( r) END 3: BEGIN IF quadrant[1] GE 4 THEN b_box[1] = b_box[1] < (-r) IF quadrant[1] GE 5 THEN b_box[2] = b_box[2] > ( r) IF quadrant[1] GE 6 THEN b_box[3] = b_box[3] > ( r) END 4: BEGIN IF quadrant[1] GE 5 THEN b_box[2] = b_box[2] > ( r) IF quadrant[1] GE 6 THEN b_box[3] = b_box[3] > ( r) IF quadrant[1] GE 7 THEN b_box[0] = b_box[0] < (-r) END ENDCASE b_box = center#[1,1]+b_box ; Center is floating point b_box = [[floor(b_box[0:1])],[ceil(b_box[2:3])]] IF IsType(dimension, /defined) THEN BEGIN b_box = b_box + (-(b_box[0:1] < 0)#[1,1] ) b_box = b_box - ((b_box[2:3]-dimension+1) > 0)#[1,1] b_box = (b_box > 0) ENDIF RETURN, b_box & END