/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library is
 * distributed  in the  hope  that  it  will  be useful,  but WITHOUT ANY
 * WARRANTY;  without even the   implied  warranty of MERCHANTABILITY  or
 * FITNESS FOR A PARTICULAR PURPOSE.   See the GNU Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

//   -*- C++ -*-
/*****************************************************************************
 *
 * Class: FlFSetTriangle --- Fuzzy set mit triangle function
 *
 *****************************************************************************/

#include "FlFSetTriangle.h"


/*
 * Contructors
 */

FlFSetTriangle::FlFSetTriangle()
    : FlFSetTrapez(0, 0, 0, 0, 0, 0)
{}


FlFSetTriangle::FlFSetTriangle(double min, double max, double xm,
			       double xalpha, double xbeta )
    : FlFSetTrapez(min, max, xm, xm, xalpha, xbeta)
{}


FlFSetTriangle::FlFSetTriangle(double xm,
			       double xalpha, double xbeta )
    : FlFSetTrapez(xm, xm, xalpha, xbeta)
{}


FlFSetTriangle::FlFSetTriangle(FlStringR xname,
			       double min, double max, double xm,
			       double xalpha, double xbeta )
    : FlFSetTrapez(xname, min, max, xm, xm, xalpha, xbeta)
{}


FlFSetTriangle::FlFSetTriangle(FlStringR xname, double xm,
			       double xalpha, double xbeta )
    : FlFSetTrapez(xname, xm, xm, xalpha, xbeta)
{}



/*
 * Initialize
 */
void FlFSetTriangle::init(FlStringR xname, 
			double xm, double xalpha, double xbeta,
			double min, double max                 )
{
    FlFSetTrapez::init(xname, xm, xm, xalpha, xbeta, min, max);
}



/*
 * Compute membership value for x - optimized version for triangle
 */
double FlFSetTriangle::get_membership(double x) const
{
   double dy = ymax - ymin;

   // Triangle: m1 == m2 !
   if(x < (m1 - alpha) || x > (m2 + beta))
   {
	   return ymin;
   }
   if(x < m1)
   {
	   return ymin + dy*(x - (m1 - alpha))/alpha;
   }
   return ymin + dy*(m2 + beta - x)/beta; 
}



/***** Default I/O member function for FuzzyLib classes **************************/

// FlNormal output
void FlFSetTriangle::print(ostream &strm) const
{
    strm << "set triangle " << name()
	 << "(" << m1 << ", " << alpha << ", " << beta << ");";
}

// Debug output
void FlFSetTriangle::dump(ostream &strm) const
{
    strm << "FlFSetTriangle { $Revision: 1.5 $" << std::endl;
    print(strm);
    strm << "}" << std::endl;
}
