[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVclSurfacePlotter

(C++ VCL) SurfacePlotter

The TChart Component does not support a surface plot. Therefore I post this class to draw surface plots.

//---------------------------------------------------------------------------
//#Include guard
  1. ifndef UnitSurfacePlotterSingleH
  2. define UnitSurfacePlotterSingleH
//--------------------------------------------------------------------------- // Surface plotter // C++ VCL class written by Bilderbikkel // Feel free to copy it, use it and credit me //--------------------------------------------------------------------------- //STL headers
  1. include <vector>
  2. include <algorithm>
  3. include <cassert>
//Boost headers
  1. include <boost/scoped_ptr.hpp>
//VCL headers
  1. include <Graphics.hpp>
  2. include <ExtCtrls.hpp>
//--------------------------------------------------------------------------- struct SurfacePlotter { SurfacePlotter() : mImage(new Extctrls::TImage(0)) { mImage->AutoSize = true; mImage->Visible = false; mImage->Stretch = false; assert(FileExists("1x1.bmp")); mImage->Picture->LoadFromFile("1x1.bmp"); } //Sets the image of the surface plot to the values of the vector //The doubles can be in any range void setSurfaceGrey(const std::vector<std::vector<double> >& surface) { //Get the size const int maxx = surface.size(); const int maxy = surface[0].size(); //Resize Image to the correct size mImage->Picture->Bitmap->Width = maxx; mImage->Picture->Bitmap->Height = maxy; //Minimum and maximum are not given, so these need to be calculated double minVal = *(std::min_element(surface[0].begin(),surface[0].end())); double maxVal = *(std::max_element(surface[0].begin(),surface[0].end())); for (int x=1; x!=maxx; ++x) { const double localMinVal = *(std::min_element(surface[x].begin(),surface[x].end())); const double localMaxVal = *(std::max_element(surface[x].begin(),surface[x].end())); if (localMinVal < minVal) minVal = localMinVal; if (localMaxVal > maxVal) maxVal = localMaxVal; } //Draw the pixels for (int y=0; y!=maxy; ++y) { unsigned char * line = static_cast<unsigned char *>(mImage->Picture->Bitmap->ScanLine[y]); for (int x=0; x!=maxx; ++x) { const double greyValueDouble = (surface[x][y] - minVal) / (maxVal - minVal); assert(greyValueDouble >= 0.0 && greyValueDouble <= 1.0); const char greyValue = greyValueDouble * 255.0; line[x*3+0] = greyValue; //Blue line[x*3+1] = greyValue; //Green line[x*3+2] = greyValue; //Red } } } //Sets the image of the surface plot to the values of the vector //Assumes that the chars are in the range [0,255] (a char's range) //If the chars are in a shorter range, they will NOT be rescaled to [0,255] void setSurfaceGrey(const std::vector<std::vector<char> >& surface) { const int maxx = surface.size(); const int maxy = surface[0].size(); mImage->Picture->Bitmap->Width = maxx; mImage->Picture->Bitmap->Height = maxy; for (int y=0; y!=maxy; ++y) { unsigned char * line = static_cast<unsigned char *>(mImage->Picture->Bitmap->ScanLine[y]); for (int x=0; x!=maxx; ++x) { const char greyValue = surface[x][y]; line[x*3+0] = greyValue; //Blue line[x*3+1] = greyValue; //Green line[x*3+2] = greyValue; //Red } } } //Draws the surface plot to the canvas given void draw(const int& x, const int& y, TCanvas * canvas) const { canvas->Draw(x,y,mImage->Picture->Graphic); } private: boost::scoped_ptr<TImage> mImage; };
  1. endif


Usage example:
void __fastcall TForm1::Button1Click(TObject*)
{
  const int maxx = 256;
  const int maxy = 128;
  std::vector<std::vector<double> > test(
    maxx,std::vector<double>(maxy,0.0)); //Black
  for (int y=0; y!=maxy; ++y)
  {
    for (int x=0; x!=maxx; ++x)
    {
      test[x][y] 
        = std::cos(x / 10.0) 
        + std::sin(y / 15.0) 
        + std::cos((x+y)/20.0);
    }
  }
  SurfacePlotter s;
  s.setSurfaceGrey(test);
  s.draw(0,0,Canvas);
}


Code links



last edited (November 18, 2006) by bilderbikkel, Number of views: 2329, Current Rev: 8 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.