| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
There are only 5 end-user classes: mglGraph (see MathGL core), mglWindow and mglGLUT (see Widget classes), mglData (see Data processing), mglParse (see MGL scripts). Exactly these classes I recommend to use in most of user programs. All methods in all of these classes are inline and have exact C/Fortran analogue functions. This give compiler independent binary libraries for MathGL.
However, sometimes you may need to extend MathGL by writing yours own plotting functions or handling yours own data structures. In these cases you may need to use low-level API. This chapter describes it.
The internal structure of MathGL is rather complicated. There are C++ classes mglBase, mglCanvas, ... for drawing primitives and positioning the plot (blue ones in the figure). There is a layer of C functions, which include interface for most important methods of these classes. Also most of plotting functions are implemented as C functions. After it, there are “inline” front-end classes which are created for user convenience (yellow ones in the figure). Also there are 2 classes which are widgets for FLTK and Qt libraries (green ones in the figure).
Below I show how this internal classes can be used.
| 8.0.1 Define new kind of plot (mglBase class) | ||
| 8.0.2 User defined types (mglDataA class) | ||
| 8.1 mglColor class | ||
| 8.2 mglPoint class | ||
| 8.3 mglFont class |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Basically most of new kinds of plot can be created using just MathGL primitives (see Primitives). However the usage of mglBase methods can give you higher speed of drawing and better control of plot settings.
All plotting functions should use a pointer to mglBase class (or HMGL type in C functions) due to compatibility issues. Exactly such type of pointers are used in front-end classes (mglGraph, mglWindow) and in widgets (QMathGL, Fl_MathGL).
MathGL tries to remember all vertexes and all primitives and plot creation stage, and to use them for making final picture by demand. Basically for making plot, you need to add vertexes by AddPnt() function, which return index for new vertex, and call one of primitive drawing function (like mark_plot(), arrow_plot(), line_plot(), trig_plot(), quad_plot(), text_plot()), using vertex indexes as argument(s). AddPnt() function use 2 float numbers for color specification. First one is positioning in textures – integer part is texture index, fractional part is relative coordinate in the texture. Second number is like a transparency of plot (or second coordinate in the 2D texture).
I don’t want to put here detailed description of mglBase class. It was rather well documented in mgl2/base.h file. I just show and example of its usage on the base of circle drawing.
First, we should prototype new function circle() as C function.
#ifdef __cplusplus
extern "C" {
#endif
void circle(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt);
#ifdef __cplusplus
}
#endif
This is done for generating compiler independent binary. Because only C-functions have standard naming mechanism, the same for any compilers.
Now, we create a C++ file and put the code of function. I’ll write it line by line and try to comment all important points.
void circle(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt)
{
First, we need to check all input arguments and send warnings if something is wrong. In our case it is negative value of r argument. We just send warning, since it is not critical situation – other plot still can be drawn.
if(r<=0) { gr->SetWarn(mglWarnNeg,"Circle"); return; }
Next step is creating a group. Group keep some general setting for plot (like options) and useful for export in 3d files.
static int cgid=1; gr->StartGroup("Circle",cgid++);
Now let apply options. Options are rather useful things, generally, which allow one easily redefine axis range(s), transparency and other settings (see Command options).
gr->SaveState(opt);
I use global setting for determining the number of points in circle approximation. Note, that user can change MeshNum by options easily.
const int n = gr->MeshNum>1?gr->MeshNum : 41;
Let try to determine plot specific flags. MathGL functions expect that most of flags will be sent in string. In our case it is symbol ‘@’ which set to draw filled circle instead of border only (last will be default). Note, you have to handle NULL as string pointer.
bool fill = stl && strchr(stl,'@');
Now, time for coloring. I use palette mechanism because circle have few colors: one for filling and another for border. SetPenPal() function parse input string and write resulting texture index in pal. Function return the character for marker, which can be specified in string str. Marker will be plotted at the center of circle. I’ll show on next sample how you can use color schemes (smooth colors) too.
long pal=0; char mk=gr->SetPenPal(stl,&pal);
Next step, is determining colors for filling and for border. First one for filling.
float c=gr->NextColor(pal), d;
Second one for border. I use black color (call gr->AddTexture('k')) if second color is not specified.
float k=(gr->GetNumPal(pal)>1)?gr->NextColor(pal):gr->AddTexture('k');
If user want draw only border (fill=false) then I use first color for border.
if(!fill) k=c;
Now we should reserve space for vertexes. This functions need n for border, n+1 for filling and 1 for marker. So, maximal number of vertexes is 2*n+2. Note, that such reservation is not required for normal work but can sufficiently speed up the plotting.
gr->Reserve(2*n+2);
We’ve done with setup and ready to start drawing. First, we need to add vertex(es). Let define NAN as normals, since I don’t want handle lighting for this plot,
mglPoint q(NAN,NAN);
and start adding vertexes. First one for central point of filling. I use -1 if I don’t need this point. The arguments of AddPnt() function is: mglPoint(x,y,z) – coordinate of vertex, c – vertex color, q – normal at vertex, -1 – vertex transparency (-1 for default), 3 bitwise flag which show that coordinates will be scaled (0x1) and will not be cutted (0x2).
long n0,n1,n2,m1,m2,i; n0 = fill ? gr->AddPnt(mglPoint(x,y,z),c,q,-1,3):-1;
Similar for marker, but we use different color k.
n2 = mk ? gr->AddPnt(mglPoint(x,y,z),k,q,-1,3):-1;
Draw marker.
if(mk) gr->mark_plot(n2,mk);
Time for drawing circle itself. I use -1 for m1, n1 as sign that primitives shouldn’t be drawn for first point i=0.
for(i=0,m1=n1=-1;i<n;i++)
{
Each function should check Stop variable and return if it is non-zero. It is done for interrupting drawing for system which don’t support multi-threading.
if(gr->Stop) return;
Let find coordinates of vertex.
float t = i*2*M_PI/(n-1.);
mglPoint p(x+r*cos(t), y+r*sin(t), z);
Save previous vertex and add next one
n2 = n1; n1 = gr->AddPnt(p,c,q,-1,3);
and copy it for border but with different color. Such copying is much faster than adding new vertex using AddPnt().
m2 = m1; m1 = gr->CopyNtoC(n1,k);
Now draw triangle for filling internal part
if(fill) gr->trig_plot(n0,n1,n2);
and draw line for border.
gr->line_plot(m1,m2); }
Drawing is done. Let close group and return.
gr->EndGroup(); }
Another sample I want to show is exactly the same function but with smooth coloring using color scheme. So, I’ll add comments only in the place of difference.
void circle_cs(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt)
{
In this case let allow negative radius too. Formally it is not the problem for plotting (formulas the same) and this allow us to handle all color range.
//if(r<=0) { gr->SetWarn(mglWarnNeg,"Circle"); return; }
static int cgid=1; gr->StartGroup("CircleCS",cgid++);
gr->SaveState(opt);
const int n = gr->MeshNum>1?gr->MeshNum : 41;
bool fill = stl && strchr(stl,'@');
Here is main difference. We need to create texture for color scheme specified by user
long ss = gr->AddTexture(stl);
But we need also get marker and color for it (if filling is enabled). Let suppose that marker and color is specified after ‘:’. This is standard delimiter which stop color scheme entering. So, just lets find it and use for setting pen.
const char *pen=0; if(stl) pen = strchr(stl,':'); if(pen) pen++;
The substring is placed in pen and it will be used as line style.
long pal=0; char mk=gr->SetPenPal(pen,&pal);
Next step, is determining colors for filling and for border. First one for filling.
float c=gr->GetC(ss,r);
Second one for border.
float k=gr->NextColor(pal);
The rest part is the same as in previous function.
if(!fill) k=c;
gr->Reserve(2*n+2);
mglPoint q(NAN,NAN);
long n0,n1,n2,m1,m2,i;
n0 = fill ? gr->AddPnt(mglPoint(x,y,z),c,q,-1,3):-1;
n2 = mk ? gr->AddPnt(mglPoint(x,y,z),k,q,-1,3):-1;
if(mk) gr->mark_plot(n2,mk);
for(i=0,m1=n1=-1;i<n;i++)
{
if(gr->Stop) return;
float t = i*2*M_PI/(n-1.);
mglPoint p(x+r*cos(t), y+r*sin(t), z);
n2 = n1; n1 = gr->AddPnt(p,c,q,-1,3);
m2 = m1; m1 = gr->CopyNtoC(n1,k);
if(fill) gr->trig_plot(n0,n1,n2);
gr->line_plot(m1,m2);
}
gr->EndGroup();
}
The last thing which we can do is derive our own class with new plotting functions. Good idea is to derive it from mglGraph (if you don’t need extended window), or from mglWindow (if you need to extend window). So, in our case it will be
class MyGraph : public mglGraph
{
public:
inline void Circle(mglPoint p, float r, const char *stl="", const char *opt="")
{ circle(p.x,p.y,p.z, r, stl, opt); }
inline void CircleCS(mglPoint p, float r, const char *stl="", const char *opt="")
{ circle_cs(p.x,p.y,p.z, r, stl, opt); }
};
Note, that I use inline modifier for using the same binary code with different compilers.
So, the complete sample will be
#include <mgl2/mgl.h>
//---------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
void circle(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt);
void circle_cs(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt);
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------
class MyGraph : public mglGraph
{
public:
inline void CircleCF(mglPoint p, float r, const char *stl="", const char *opt="")
{ circle(p.x,p.y,p.z, r, stl, opt); }
inline void CircleCS(mglPoint p, float r, const char *stl="", const char *opt="")
{ circle_cs(p.x,p.y,p.z, r, stl, opt); }
};
//---------------------------------------------------------
void circle(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt)
{
if(r<=0) { gr->SetWarn(mglWarnNeg,"Circle"); return; }
static int cgid=1; gr->StartGroup("Circle",cgid++);
gr->SaveState(opt);
const int n = gr->MeshNum>1?gr->MeshNum : 41;
bool fill = stl && strchr(stl,'@');
long pal=0;
char mk=gr->SetPenPal(stl,&pal);
float c=gr->NextColor(pal), d;
float k=(gr->GetNumPal(pal)>1)?gr->NextColor(pal):gr->AddTexture('k');
if(!fill) k=c;
gr->Reserve(2*n+2);
mglPoint q(NAN,NAN);
long n0,n1,n2,m1,m2,i;
n0 = fill ? gr->AddPnt(mglPoint(x,y,z),c,q,-1,3):-1;
n2 = mk ? gr->AddPnt(mglPoint(x,y,z),k,q,-1,3):-1;
if(mk) gr->mark_plot(n2,mk);
for(i=0,m1=n1=-1;i<n;i++)
{
if(gr->Stop) return;
float t = i*2*M_PI/(n-1.);
mglPoint p(x+r*cos(t), y+r*sin(t), z);
n2 = n1; n1 = gr->AddPnt(p,c,q,-1,3);
m2 = m1; m1 = gr->CopyNtoC(n1,k);
if(fill) gr->trig_plot(n0,n1,n2);
gr->line_plot(m1,m2);
}
gr->EndGroup();
}
//---------------------------------------------------------
void circle_cs(HMGL gr, float x, float y, float z, float r, const char *stl, const char *opt)
{
static int cgid=1; gr->StartGroup("CircleCS",cgid++);
gr->SaveState(opt);
const int n = gr->MeshNum>1?gr->MeshNum : 41;
bool fill = stl && strchr(stl,'@');
long ss = gr->AddTexture(stl);
const char *pen=0;
if(stl) pen = strchr(stl,':');
if(pen) pen++;
long pal=0;
char mk=gr->SetPenPal(pen,&pal);
float c=gr->GetC(ss,r);
float k=gr->NextColor(pal);
if(!fill) k=c;
gr->Reserve(2*n+2);
mglPoint q(NAN,NAN);
long n0,n1,n2,m1,m2,i;
n0 = fill ? gr->AddPnt(mglPoint(x,y,z),c,q,-1,3):-1;
n2 = mk ? gr->AddPnt(mglPoint(x,y,z),k,q,-1,3):-1;
if(mk) gr->mark_plot(n2,mk);
for(i=0,m1=n1=-1;i<n;i++)
{
if(gr->Stop) return;
float t = i*2*M_PI/(n-1.);
mglPoint p(x+r*cos(t), y+r*sin(t), z);
n2 = n1; n1 = gr->AddPnt(p,c,q,-1,3);
m2 = m1; m1 = gr->CopyNtoC(n1,k);
if(fill) gr->trig_plot(n0,n1,n2);
gr->line_plot(m1,m2);
}
gr->EndGroup();
}
//---------------------------------------------------------
int main()
{
MyGraph gr;
gr.Box();
// first let draw circles with fixed colors
for(int i=0;i<10;i++)
gr.CircleCF(mglPoint(2*mgl_rnd()-1, 2*mgl_rnd()-1), mgl_rnd());
// now let draw circles with color scheme
for(int i=0;i<10;i++)
gr.CircleCS(mglPoint(2*mgl_rnd()-1, 2*mgl_rnd()-1), 2*mgl_rnd()-1);
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
mglData class have abstract predecessor class mglDataA. Exactly the pointers to mglDataA instances are used in all plotting functions and some of data processing functions. This was done for taking possibility to define yours own class, which will handle yours own data (for example, complex numbers, or differently organized data). And this new class will be almost the same as mglData for plotting purposes.
However, the most of data processing functions will be slower as if you used mglData instance. This is more or less understandable – I don’t know how data in yours particular class will be organized, and couldn’t optimize the these functions generally.
There are few virtual functions which must be provided in derived classes. This functions give:
GetNx, GetNy, GetNz),
v, dvx, dvy, dvz),
Maximal, Minimal) – you can use provided functions (like mgl_data_max and mgl_data_min), but yours own realization can be more efficient,
vthr) – you need this only if you want using MathGL’s data processing functions.
Let me, for example define class mglComplex which will handle complex number and draw its amplitude or phase, depending on flag use_abs:
#include <complex>
#include <mgl2/mgl.h>
#define dual std::complex<double>
class mglComplex : public mglDataA
{
public:
long nx; ///< number of points in 1st dimensions ('x' dimension)
long ny; ///< number of points in 2nd dimensions ('y' dimension)
long nz; ///< number of points in 3d dimensions ('z' dimension)
dual *a; ///< data array
bool use_abs; ///< flag to use abs() or arg()
inline mglComplex(long xx=1,long yy=1,long zz=1)
{ a=0; use_abs=true; Create(xx,yy,zz); }
virtual ~mglComplex() { if(a) delete []a; }
/// Get sizes
inline long GetNx() const { return nx; }
inline long GetNy() const { return ny; }
inline long GetNz() const { return nz; }
/// Create or recreate the array with specified size and fill it by zero
inline void Create(long mx,long my=1,long mz=1)
{ nx=mx; ny=my; nz=mz; if(a) delete []a;
a = new dual[nx*ny*nz]; }
/// Get maximal value of the data
inline float Maximal() const { return mgl_data_max(this); }
/// Get minimal value of the data
inline float Minimal() const { return mgl_data_min(this); }
protected:
inline mreal v(long i,long j=0,long k=0) const
{ return use_abs ? abs(a[i+nx*(j+ny*k)]) : arg(a[i+nx*(j+ny*k)]); }
inline mreal vthr(long i) const
{ return use_abs ? abs(a[i]) : arg(a[i]); }
inline mreal dvx(long i,long j=0,long k=0) const
{ register long i0=i+nx*(j+ny*k);
std::complex<double> res=i>0? (i<nx-1? (a[i0+1]-a[i0-1])/2.:a[i0]-a[i0-1]) : a[i0+1]-a[i0];
return use_abs? abs(res) : arg(res); }
inline mreal dvy(long i,long j=0,long k=0) const
{ register long i0=i+nx*(j+ny*k);
std::complex<double> res=j>0? (j<ny-1? (a[i0+nx]-a[i0-nx])/2.:a[i0]-a[i0-nx]) : a[i0+nx]-a[i0];
return use_abs? abs(res) : arg(res); }
inline mreal dvz(long i,long j=0,long k=0) const
{ register long i0=i+nx*(j+ny*k), n=nx*ny;
std::complex<double> res=k>0? (k<nz-1? (a[i0+n]-a[i0-n])/2.:a[i0]-a[i0-n]) : a[i0+n]-a[i0];
return use_abs? abs(res) : arg(res); }
};
int main()
{
mglComplex dat(20);
for(long i=0;i<20;i++)
dat.a[i] = 3*exp(-0.05*(i-10)*(i-10))*dual(cos(M_PI*i*0.3), sin(M_PI*i*0.3));
mglGraph gr;
gr.SetRange('y', -M_PI, M_PI); gr.Box();
gr.Plot(dat,"r","legend 'abs'");
dat.use_abs=false;
gr.Plot(dat,"b","legend 'arg'");
gr.Legend();
gr.WritePNG("complex.png");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by Autobuild on July 16, 2012 using texi2html 1.82.