Next: , Up: Other classes   [Contents][Index]


10.1 Define new kind of plot (mglBase class)

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 mreal 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, mreal x, mreal y, mreal z, mreal 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, mreal x, mreal y, mreal z, mreal 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 = mglchr(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.

  mreal c=gr->NextColor(pal), d;

Second one for border. I use black color (call gr->AddTexture('k')) if second color is not specified.

  mreal 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.

    mreal 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, mreal x, mreal y, mreal z, mreal 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 = mglchr(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.

  mreal c=gr->GetC(ss,r);

Second one for border.

  mreal 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;
    mreal 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, mreal r, const char *stl="", const char *opt="")
  { circle(p.x,p.y,p.z, r, stl, opt); }
  inline void CircleCS(mglPoint p, mreal 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, mreal x, mreal y, mreal z, mreal r, const char *stl, const char *opt);
void circle_cs(HMGL gr, mreal x, mreal y, mreal z, mreal r, const char *stl, const char *opt);
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------
class MyGraph : public mglGraph
{
public:
  inline void CircleCF(mglPoint p, mreal r, const char *stl="", const char *opt="")
  { circle(p.x,p.y,p.z, r, stl, opt); }
  inline void CircleCS(mglPoint p, mreal r, const char *stl="", const char *opt="")
  { circle_cs(p.x,p.y,p.z, r, stl, opt); }
};
//---------------------------------------------------------
void circle(HMGL gr, mreal x, mreal y, mreal z, mreal 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 = mglchr(stl,'@');
  long pal=0;
  char mk=gr->SetPenPal(stl,&pal);
  mreal c=gr->NextColor(pal), d;
  mreal 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;
    mreal 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, mreal x, mreal y, mreal z, mreal 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 = mglchr(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);
  mreal c=gr->GetC(ss,r);
  mreal 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;
    mreal 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);
}

Next: , Up: Other classes   [Contents][Index]