
;----------------------------------------------------------------------------------
; This is a utility routine to calculate the scaling vector
; required to position a vector of specified range at a
; specific position given in normalized coordinates. The
; scaling vector is given as a two-element array like this:
;
;   scalingVector = [translationFactor, scalingFactor]
;
; The scaling vector should be used with the [XYZ]COORD_CONV
; keywords of a graphics object or model. For example, if you
; wanted to scale an X axis into the data range of -0.5 to 0.5,
; you might type something like this:
;
;   xAxis->GetProperty, Range=xRange
;   xScale = Normalize(xRange, Position=[-0.5, 0.5])
;   xAxis, XCoord_Conv=xScale
FUNCTION Normalize, range, Position=position
	On_Error, 1
	IF N_Params() EQ 0 THEN Message, 'Please pass range vector as argument.'
	
	IF (N_Elements(position) EQ 0) THEN position = [0.0, 1.0] ELSE $
		position=Float(position)
	range = Float(range)
	
	scale = [((position[0]*range[1])-(position[1]*range[0])) / $
		(range[1]-range[0]), (position[1]-position[0])/(range[1]-range[0])]
	
	RETURN, scale
END



;----- MAIN PROGRAM ------
PRO line

	;Create widgets and objects
	wBaseTop = WIDGET_BASE(TITLE='Line')
	wDraw = WIDGET_DRAW(wBaseTop, XSIZE=300, YSIZE=300, GRAPHICS_LEVEL=2)

	WIDGET_CONTROL, wBaseTop, /REALIZE
	WIDGET_CONTROL, wDraw, GET_VALUE=oWindow
	
	oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT=[-1,-1,2,2])
	oModel = OBJ_NEW('IDLgrModel')
	oView->Add, oModel

	;First i put a line.
	coord = [ [-1,-1, 1], [1,1,1] ]
	polyline = [2, 0,1]
	oLine = OBJ_NEW('IDLgrPolyline', coord, POLYLINES=polyline, $
		DEPTH_TEST_FUNCTION=8)
	oModel->Add, oLine
	
	
	;Second i put an image

	;Read image 	
	READ_JPEG, FILEPATH('rose.jpg', SUBDIR=['examples','data']), img, /TRUE
	
	;Normalize dimensions (3 x X x Y)
	sizes = SIZE(img, /DIMENSIONS)
	xnorm = NORMALIZE([0, sizes[1]-1], Position=[-0.9,0.9])
	ynorm = NORMALIZE([0, sizes[2]-1], Position=[-0.9,0.9])
	
	oImage = OBJ_NEW('IDLgrImage', img, XCOORD_CONV=xnorm, YCOORD_CONV=ynorm)
	oModel->Add, oImage

	
	oWindow->Draw, oView
	
END










