[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppVclSurfacePlotter
Usage example:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppVclSurfacePlotter
(C++ VCL) SurfacePlotter
The TChart Component does not support a surface plot. Therefore I post this class to draw surface plots.//--------------------------------------------------------------------------- //#Include guard//--------------------------------------------------------------------------- // Surface plotter // C++ VCL class written by Bilderbikkel // Feel free to copy it, use it and credit me //--------------------------------------------------------------------------- //STL headers
- ifndef UnitSurfacePlotterSingleH
- define UnitSurfacePlotterSingleH
//Boost headers
- include <vector>
- include <algorithm>
- include <cassert>
//VCL headers
- include <boost/scoped_ptr.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; };
- include <Graphics.hpp>
- include <ExtCtrls.hpp>
- 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
- #include
- #include guard
- assert
- boost
- char
- class
- boost::scoped_ptr
- const
- for
- include
- include guard
- int
- min_element
- max_element
- ScanLine
- std
- std::min_element
- std::max_element
- scoped_ptr
- static_cast
- struct
- TImage
- TObject
- unsigned
- vector
- void
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
