/************************************************************************
 * filterwidth.h - Some handy macros for filter size estimation
 *                 for antialiasing of shaders.
 *
 * Entropy and BMRT are:
 * (c) Copyright 2001 Exluna, Inc.  Portions (c) 1990-2000 Larry Gritz.
 * All rights reserved.
 *
 * $Revision: 1.5 $    $Date: 2001/06/26 01:01:19 $
 *
 ************************************************************************/



#ifndef FILTERWIDTH_H
#define FILTERWIDTH_H 1

/* Define metrics for estimating filter widths, if none has already
 * been defined.  This is crucial for antialiasing.
 */
#ifndef MINFILTWIDTH
#  define MINFILTWIDTH 1.0e-6
#endif


/* The filterwidth function returns the approximate amount that a
 * value changes from pixel to adjacent pixel.  
 */
float filterwidth (float x)
{
    return max (abs(Du(x)*du) + abs(Dv(x)*dv), MINFILTWIDTH);
}

float filterwidth (point x)
{
    return max (sqrt(area(x)), MINFILTWIDTH);
}

float filterwidth (vector x)
{
    return max (sqrt(area(x)), MINFILTWIDTH);
}

float filterwidth (normal x)
{
    return max (sqrt(area(x)), MINFILTWIDTH);
}




/* Back compatibility: we used to have filterwidthp for points.  Now
 * there's just filterwidth.
 */
#define filterwidthp filterwidth



/* Given a function g, its known average g_avg, its high frequency
 * feature size, and a filter width, fade to the average as we approach
 * the Nyquist limit.
 */
#define fadeout(g,g_avg,featuresize,fwidth) \
        mix (g, g_avg, smoothstep(.2,.6,fwidth/featuresize))


#endif /* FILTERWIDTH_H */
