//*****************************************
//******** PC Configurator V1.00 **********
//*** (C) - 2001 B.Bowling/A. Grippo ******
//** All derivatives from this software ***
//**  are required to keep this header ****
//*****************************************

static char *rcsId() { return "$Id$"; }

#include "stdafx.h"
#include "megatune.h"
#include "msDatabase.h"
#include "veconst.h"
#include "Dconplot.h"

#ifdef _DEBUG
#  define new DEBUG_NEW
#  undef THIS_FILE
   static char    THIS_FILE[] = __FILE__;
#endif

extern msDatabase mdb;

//------------------------------------------------------------------------------
// Dconplot dialog

Dconplot::Dconplot(CWnd * pParent /*=NULL*/)
 : CDialog(Dconplot::IDD, pParent)
{
   //{{AFX_DATA_INIT(Dconplot)
   // NOTE: the ClassWizard will add member initialization here
   //}}AFX_DATA_INIT
}

//------------------------------------------------------------------------------

void Dconplot::DoDataExchange(CDataExchange * pDX)
{
 CDialog::DoDataExchange(pDX);
   //{{AFX_DATA_MAP(Dconplot)
   // NOTE: the ClassWizard will add DDX and DDV calls here
   //}}AFX_DATA_MAP
}

//------------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(Dconplot, CDialog)
   //{{AFX_MSG_MAP(Dconplot)
ON_WM_PAINT()
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//------------------------------------------------------------------------------
// Dconplot message handlers

BOOL Dconplot::OnInitDialog()
{
   CDialog::OnInitDialog();

/* CPen pborder(PS_SOLID,10,RGB(0,0,0));

   CClientDC dc(this);
   dc.SelectObject(&pborder);
   dc.MoveTo(10,10);
   dc.LineTo(110,110);
 */

   int i, j, ive = 0;

   for (j = 0; j < 8; j++) {
      for (i = 0; i < 8; i++) {
         mdb.vePlotVals[i][j] = mdb.Const(Dve+ive);
         ive++;
      }
   }

   for (i = 0; i < 8; i++) {
      mdb.kpaPlotVals[i] = mdb.Const(Dkparangeve+i);
      if (mdb.alphaN() == 1) mdb.kpaPlotVals[i] = mdb.pctFromTps(BYTE(mdb.kpaPlotVals[i]));
      mdb.rpmPlotVals[i] = mdb.Const(Drpmrangeve+i);
   }

   return TRUE;
}

//------------------------------------------------------------------------------

void Dconplot::OnPaint()
{
   CPaintDC       dc(this);     // device context for painting

   CRect r;
   GetWindowRect(&r);
   ScreenToClient(&r);

   int BORDERX1 = r.left   + 10;
   int BORDERY1 = r.top    + 30;
   int BORDERX2 = r.right  - 10;
   int BORDERY2 = r.bottom - 45;
   int CPX1     = BORDERX1 + 30;
   int CPY1     = BORDERY1 + 30;
   int CPX2     = BORDERX2 - 30;
   int CPY2     = BORDERY2 - 45;

   CPen           pborder(PS_SOLID, 2, RGB(0, 0, 0));
   CPen           pgrid(PS_SOLID, 1, RGB(0, 0, 0));
   double         sloperpm, slopekpa, f1, f2, rpm, kpa, tmp;
   int            irpm, ikpa, ik;

   CString        hhh;
   LPTSTR         pp;


   CPen          *oldpen = dc.SelectObject(&pborder);

   dc.Rectangle(BORDERX1, BORDERY1, BORDERX2, BORDERY2);
   //dc.MoveTo(10,10);
   //dc.LineTo(110,110);

   dc.Rectangle(CPX1, CPY1, CPX2, CPY2);

   // set up color table
   COLORREF       vecolors[256];

   for (int ii = 0; ii < 256; ii++) {
      int            ired, iblue, igreen;

      //  ired = int(5.1 * (ii - 70));
      ired = int(6.375 * (ii - 80));
      ired = int(8.500 * (ii - 70));
      if (ired < 0) ired = 0;
      if (ired > 255) ired = 255;

      iblue = int(-6.375 * (ii - 80));
      iblue = int(-8.500 * (ii - 70));
      if (iblue < 0) iblue = 0;
      if (iblue > 255) iblue = 255;

      if (ii < 80) {
         igreen = int(6.375 * (ii - 40));
         igreen = int(8.500 * (ii - 40));
      }
      else {
         igreen = int(-6.375 * (ii - 120));
         igreen = int(-8.500 * (ii - 100));
      }
      if (igreen < 0) igreen = 0;
      if (igreen > 255) igreen = 255;

      vecolors[ii] = RGB(ired, igreen, iblue);
   }

   sloperpm = (mdb.rpmPlotVals[7] - mdb.rpmPlotVals[0]) / (CPX2 - CPX1);
   slopekpa = (mdb.kpaPlotVals[7] - mdb.kpaPlotVals[0]) / (CPY2 - CPY1);

   for (int i = CPX1 + 1; i < CPX2 - 1; i++) {
      irpm = 0;
      rpm = sloperpm * (i - CPX1) + mdb.rpmPlotVals[0];
      for (irpm = 7; irpm > -1; irpm--) {
         if (rpm > mdb.rpmPlotVals[irpm]) break;
         /*{
            irpm = ik;
            goto GT1;
            } */
      }

      for (int j = CPY1 + 1; j < CPY2 - 1; j++) {
         kpa = slopekpa * (j - CPY1) + mdb.kpaPlotVals[0];
         for (ikpa = 7; ikpa > -1; ikpa--) {
            if (kpa > mdb.kpaPlotVals[ikpa]) break;
         }

         f1 = (rpm - mdb.rpmPlotVals[irpm]) / (mdb.rpmPlotVals[irpm + 1] - mdb.rpmPlotVals[irpm]);
         f2 = (kpa - mdb.kpaPlotVals[ikpa]) / (mdb.kpaPlotVals[ikpa + 1] - mdb.kpaPlotVals[ikpa]);
         tmp = (1.0 - f1) * (1.0 - f2) * mdb.vePlotVals[irpm][ikpa]
            + f1 * (1.0 - f2) * mdb.vePlotVals[irpm + 1][ikpa]
            + f2 * (1.0 - f1) * mdb.vePlotVals[irpm][ikpa + 1]
            + f1 * f2 * mdb.vePlotVals[irpm + 1][ikpa + 1];

         //tmp = mdb.vePlotVals[irpm][ikpa];

         ik = (int) tmp;
         if (ik > 255) ik = 255;
         dc.SetPixel(i, j, vecolors[ik]);
      }
   }

   CFont  MyFont, RotFont;

   MyFont.CreateFont(12,
                     0,
                     0,
                     0,
                     400,
                     FALSE,
                     FALSE,
                     0,
                     ANSI_CHARSET,
                     OUT_DEFAULT_PRECIS,
                     CLIP_DEFAULT_PRECIS,
                     DEFAULT_QUALITY,
                     DEFAULT_PITCH | FF_SWISS,
                     "Arial");

   RotFont.CreateFont(12,
                      0,
                      900,
                      0,
                      400,
                      FALSE,
                      FALSE,
                      0,
                      ANSI_CHARSET,
                      OUT_DEFAULT_PRECIS,
                      CLIP_DEFAULT_PRECIS,
                      DEFAULT_QUALITY,
                      DEFAULT_PITCH | FF_SWISS,
                      "Arial");

   CFont         *pOldFont = dc.SelectObject(&MyFont);

   dc.SelectObject(&pgrid);

   for (i = 0; i < 8; i++) {
      f1 = (1.0 / sloperpm) * (mdb.rpmPlotVals[i] - mdb.rpmPlotVals[0]) + CPX1;
      dc.MoveTo((int) f1, CPY1);
      dc.LineTo((int) f1, CPY2 + 6);


      pp = hhh.GetBuffer(10); sprintf(pp, "%3.0f", mdb.rpmPlotVals[i]); hhh.ReleaseBuffer();
      dc.TextOut((int) f1 - 10, CPY2 + 10, hhh);

      f1 = (1.0 / slopekpa) * (mdb.kpaPlotVals[i] - mdb.kpaPlotVals[0]) + CPY1;
      dc.MoveTo(CPX1 - 6, (int) f1);
      dc.LineTo(CPX2, (int) f1);

      pp = hhh.GetBuffer(10); sprintf(pp, "%3.0f", mdb.kpaPlotVals[i]); hhh.ReleaseBuffer();
      dc.TextOut(CPX1 - 20, (int) f1 - 5, hhh);
   }

   pp = hhh.GetBuffer(10); sprintf(pp, "RPM"); hhh.ReleaseBuffer();
   dc.TextOut(CPX1 + 120, CPY2 + 20, hhh);

   pp = hhh.GetBuffer(40); sprintf(pp, "Contour Plot Of Volumetric Efficiency"); hhh.ReleaseBuffer();
   dc.TextOut(CPX1 + 60, CPY1 - 20, hhh);

   dc.SelectObject(&RotFont);
   pp = hhh.GetBuffer(13); sprintf(pp, "KiloPascals"); hhh.ReleaseBuffer();
   dc.TextOut(CPX1 - 55, CPY2 - 100, hhh);

   dc.SelectObject(pOldFont);

   // Do not call CDialog::OnPaint() for painting messages
}

//------------------------------------------------------------------------------

