; $Id: davegr2d__define.pro,v 1.17 2006/08/31 17:57:38 azuah Exp $
;###############################################################################
;
; CLASS NAME:
;   DaveGr2D
;
; PURPOSE:
;   The DaveGr2D class extends the DaveGr class to better handle
;   plotting of 2D data ie area and surface plots.
;
; CATEGORY:
;   Data Browser
;
; SUPERCLASSES:
;   DaveGr
;
; PUBLIC METHODS:
;   Display
;   Get
;   ReserMiscGrAttribs
;   Set
;
; AUTHOR:
;   Richard Tumanjong Azuah
;   NIST Center for Neutron Research
;   100 Bureau Drive, Gaithersburg, MD 20899
;   United States
;   azuah@nist.gov; (301) 9755604
;   Dec, 2002
;
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;###############################################################################



;===============================================================================
; DaveGr2D::Cleanup
;
; PURPOSE:
;   Cleanup method for objects of this class
;
; PARAMETERS:
;
pro DaveGr2D::cleanup

if (ptr_valid(self.plotAttribPtr)) then heap_free, self.plotAttribPtr
if (ptr_valid(self.dPlotAttribPtr)) then heap_free, self.dPlotAttribPtr

obj_destroy,self.palette

; call base class cleanup method
self->DaveGr::cleanup 

end
;-------------------------------------------------------------------------------



;===============================================================================
; DaveGr2D::initContour
;
; PURPOSE:
;   Private method used to initialize default class variables for a contour plot.
;
; PARAMETERS:
;
pro DaveGr2D::initContour

; handle plotType and plot attributes

paStr = {contourStr, $          ; contour attributes structure.
         fillFlag:1, $          ; fill flag to off(0)/on(1)
         nLevels:5, $           ; number of equally spaced contour levels
         shadeFlag:0, $          ; 0=>flat, 1=>Gouraud shading
         cLabelFlag:0, $         ; should contour labels be shown 0=>No, 1=>Yes 
         cLabelInt:1, $         ; show labels every cLabelStep contour
         cLabelDist:0.4, $      ; space (as % of contour len) between labels on same contour
         cLinestyle:0, $        ; linestyle for contours
         cThick:1.0 , $           ; line tickness for contours
         downhillFlag:0 $       ; hide=>0 or show=>1 downhill direction
        }

paStr.fillFlag = 1
paStr.nLevels = 20
paStr.shadeFlag = 0

paStr.cLabelFlag = 0 
paStr.cLabelInt = 2
paStr.cLabelDist = 0.7
paStr.cLineStyle = 0
paStr.cThick = 1.0
paStr.downhillFlag = 0

self.plotType = 'CONTOUR'
self.plotAttribPtr = ptr_new(paStr)
self.dPlotAttribPtr = ptr_new(paStr)

end
;-------------------------------------------------------------------------------



;===============================================================================
; DaveGr2D::initImage
;
; PURPOSE:
;   Private method used to initialize default class variables for an image plot.
;
; PARAMETERS:
;
pro DaveGr2D::initImage

paStr = {imageStr, $            ; image attributes structure.
         interpFlag:1b $      ; 1=>bilinear; 0=> nearest neighbor interpolation
        }

self.plotType = 'IMAGE'
self.plotAttribPtr = ptr_new(paStr)
self.dPlotAttribPtr = ptr_new(paStr)

end
;-------------------------------------------------------------------------------



;===============================================================================
; DaveGr2D::initImage
;
; PURPOSE:
;   Private method used to initialize default class variables for a surface plot.
;
; PARAMETERS:
;
pro DaveGr2D::initSurface

paStr = {surfaceStr, $          ; surface attributes structure.
         shadeFlag:1b, $        ; flat (0) or Gouraud (1) for styles[2,6].
         vertShadeFlag:1b, $    ; Use vertical/elevation color shading
         skirtFlag:0b, $        ; should a skirt be included? no (0), yes (1)
         skirtValue:0.0, $      ; z-value of skirt
         style:2b, $            ; 0=points,1=Wire,2=Filled,3=RuledXZ,4=RuledYZ,5=Lego,6=LegoFilled
         bottomColor:[0,0,0], $ ; the color to draw the bottom surface
         bottomFlag:0b, $       ; Should bottom be drawn using the specified bottom color
         hiddenLineFlag:1b, $   ; 0=disallow, 1=allow hidden line removal for surface
         ambLightInt:0.5, $     ; ambient light source intensity
         ambLightFlag:1, $      ; ambient light flag; 0/1 => Off/On
         posLightInt:0.5, $     ; positional light source intensity
         posLightLoc:[-1,0,1], $ ; positional light location
         posLightDir:[0,0,0], $ ; positional light direction
         posLightFlag:1 $       ; 0/1 => Off/On
        }

paStr.bottomColor = self.bgColor
self.plotType = 'SURFACE'
paStr.posLightDir = -(paStr.posLightLoc)
self.plotAttribPtr = ptr_new(paStr)
self.dPlotAttribPtr = ptr_new(paStr)

end
;-------------------------------------------------------------------------------



;===============================================================================
; DaveGr2D::Init
;
; PURPOSE:
;   Initialization method for objects of this class
;
; PARAMETERS:
;
; KEYWORDS:
;   plotType [in] - The plotType specifies how the data should be
;                     displayed. The options are 'contour', 'surface'
;                     and 'image'. The default is 'contour' if
;                     plotType keyword is not used.
;
;   yColor [in] - an [R,G,B] vector of byte values specifying the
;                 y-axis color.
;
;   yFont [in] - a string specifying a valid font family name for the
;                y-axis.
;
;   yFontSize [in] - an integer specifying a valid fontsize for the
;                    y-axis.
;
;   yLabel [in] - a string specifying the label for the y-axis.
;
;   yLineSize [in] - a float specifying a valid line thickness for
;                    drawing the y-axis.
;
;   _EXTRA [in] - allows keywords to be passed on to DaveGr::Init.
;
; RETURN VALUE:
;   0 => failure
;   1 => success.
;
function DaveGr2D::init, plotType=plotType, yColor=yColor,yFont=yFont,yLineSize=yLineSize, $
  yFontSize=yFontSize,yLabel=yLabel, _EXTRA=etc

; reset !error_state structure, ie clear all errors
message,/reset

; if a dataObj keyword was passed make sure that it's value
; is a DaveDat2D object
; then call base class initialization after
if (n_elements(etc) gt 0) then begin
    res = where(tag_names(etc) eq 'DATAREF', dataRef_isSet)
    if (dataRef_isSet gt 0) then begin
        dataRef = etc.dataRef
        if (vartype((etc.dataRef)[0]) ne 'DAVEDAT2D') then begin
            !error_state.msg = 'DaveGr2D::init: invalid object reference (dataRef) passed'
            !error_state.code = -1
            return, 0
        endif
    endif 
endif
if (self->DaveGr::init(_EXTRA=etc) ne 1) then return, 0 ; base class initialization

if (n_elements(yFont) gt 0) then begin
    if (vartype(yFont) eq 'STRING') then self.yFont = yFont
endif else self.yFont = 'HELVETICA'

if (n_elements(yFontSize) gt 0) then begin
    self.yFontSize = yFontSize
endif else self.yFontSize = 12.0

if (n_elements(yLineSize) gt 0) then begin
    self.yLineSize = yLineSize
endif else self.yLineSize = 1.0

if (n_elements(yLabel) gt 0) then begin
    if (vartype(yLabel) eq 'STRING') then self.yLabel = yLabel
endif else begin
    if (dataRef_isSet) then begin        
        if ((dataRef[0])->get(yLabel=yl,yUnits=yu,transpose=transFlag)) then begin
            self.yLabel = (strlen(yu) gt 0)? yl+' ('+yu+')' : yl
            self.transposeFlag = transFlag
        endif
    endif
endelse

if (n_elements(yColor) gt 0) then begin
    dim = size(yColor)
    if ( (dim[0] eq 1) and (dim[1] eq 3)) then self.yColor = yColor
endif else self.yColor = [0b,0b,0b]

; if (n_elements(yLabel) gt 0) then begin
;     if (vartype(yLabel) eq 'STRING') then self.yLabel = yLabel
; endif else if (n_elements(dataRef) gt 0) then begin
;     valid = ['DAVEDAT2D']
;     for i=0,n_elements(dataRef)-1 do begin
;         res = where(valid eq vartype(dataRef[i]), count)
;         if (count) then begin
;             if (i eq 0) then begin
;                 if ((dataRef[i])->get(yLabel=yl,yUnits=yu,transpose=transFlag)) then begin
;                     self.yLabel = (strlen(yu) gt 0)? yl+' ('+yu+')' : yl
;                     self.transposeFlag = transFlag
;                 endif
;             endif
;         endif
;     endfor
; endif

; init the color palette
self.palette = obj_new('IDLgrPalette')
self.colorIndex = 13
(self.palette)->loadct,self.colorIndex

; Set bkgd and foreground colors
self.bgColor = [255b,255b,255b]
self.fgColor = not self.bgColor

if (n_elements(plotType) le 0) then plotType='' 

case strupcase(plotType) of    
    'IMAGE': self->initImage
    'SURFACE': self->initSurface
    'CONTOUR': self->initContour
    else: self->initContour
endcase

; Initialize miscellaneous axes graph attributes
self->resetMiscGrAttribs

return, 1

end
;-------------------------------------------------------------------------------



;===============================================================================
; DaveGr2D__define
;
; PURPOSE:
;   Class structure definition procedure
;
; PARAMETERS:
;
; KEYWORDS:
;
pro DaveGr2D__define

; Structure to represent miscellaneous axis graphic attributes and settings
void = {miscGrAttribs, $
        minimum:0.0, $          ; minimum value to be displayed
        maximum:0.0, $          ; maximum value to be displayed
        tickInterval:0.0, $     ; major tick interval
        useCustomFlag:0b , $    ; flag indicates if above limits should be used
        scaleFactor:0.0, $      ; a scale factor to be applied to axis
        logFlag:0, $            ; flag: 0==>linear; 1==>log10
        decimalPlaces:0b, $     ; number of decimal places to display
        tickDir:0b, $           ; tic direction: 0==>inwards; 1==>outwards
        tickLen:0.0, $          ; tick length as a percentage of the axis
        minorTicks:0b $         ; number of minor ticks to display
        }

void = {DaveGr2D, $
        plotType:'CONTOUR', $   ; type of 2D plot: 'contour','surface' or 'image'
        plotAttribPtr:ptr_new(), $ ; ptr to a struct defining attributes of the 2D plot
        dPlotAttribPtr:ptr_new(), $ ; default (fall back) value for the attributes
        colorIndex:0, $         ; color index in use
        palette:obj_new(), $    ;  color palette
        fgColor:[0b,0b,0b], $   ; foreground color - eg used for contours
        yLabel:'', $
        yColor:[0b,0b,0b], $
        yFont:'', $
        yFontSize:12.0, $
        yLineSize:1.0, $
        yMiscAttribs:{miscGrAttribs}, $ ;
        transposeFlag:0b, $
        inherits DaveGr $       ; 
       }

end

