Function:ns2qvas

From S2PLOT

(Difference between revisions)
Jump to: navigation, search
Revision as of 02:00, 7 January 2009
S2plot admin (Talk | contribs)

← Previous diff
Current revision
S2plot admin (Talk | contribs)

Line 14: Line 14:
<table> <table>
<tr><td>[[Function:ns2svas | ns2svas]]</td><td>Set alpha scaling mode. </td></tr> <tr><td>[[Function:ns2svas | ns2svas]]</td><td>Set alpha scaling mode. </td></tr>
 +<tr><td>[[Function:ns2sevas | ns2sevas]]</td><td>Explicitly set alpha scaling values. </td></tr>
<tr><td>[[Function:ns2cvr | ns2cvr ]]</td><td>Create a volume rendering object. </td></tr> <tr><td>[[Function:ns2cvr | ns2cvr ]]</td><td>Create a volume rendering object. </td></tr>
<tr><td>[[Function:ns2cvra| ns2cvra ]]</td><td>Render volume of objects. </td></tr> <tr><td>[[Function:ns2cvra| ns2cvra ]]</td><td>Render volume of objects. </td></tr>
Line 21: Line 22:
<code><pre> <code><pre>
-</pre></code>+#include <stdio.h>
 +#include <stdlib.h>
 +#include <math.h>
 +#include <time.h>
 +#include "s2plot.h"
-[[S2PLOT:Function List | Back]] to S2PLOT function list.+/* Global variables */
 +int vid; /* ID for volume render object */
 +float tr[12]; /* Transformation matrix */
 +float ***volume; /* The data cube */
 +int nx, ny, nz; /* Data grid dimensions */
 +float ***initVolume(int nx, int ny, int nz)
 +/* Allocate memory and initialise a data cube */
 +{
 + float ***volume;
 + int i, j, k;
 + volume = (float ***)malloc(nx * sizeof(float **));
 + if (volume == NULL) {
 + fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float **)));
 + exit(-1);
 + }
 + for (i=0;i<nx;i++) {
 + volume[i] = (float **)malloc(ny * sizeof(float *));
 + if (volume[i] == NULL) {
 + fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float *)));
 + exit(-1);
 + }
 + for (j=0;j<ny;j++) {
 + volume[i][j] = (float *)malloc(nz * sizeof(float));
 + if (volume[i][j] == NULL) {
 + fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float)));
 + exit(-1);
 + }
 + for (k=0;k<nz;k++) {
 + volume[i][j][k] = 0.0; /* Initialise */
 + }
 + }
 + }
-__NOTOC__+ return volume;
-__NOEDITSECTION__+}
 +void cb(double *t, int *kc)
 +/* A dynamic callback */
 +{
 + ns2svas(*kc%3); /* Set the alpha scaling mode */
 + int qvas = ns2qvas(); /* Query the alpha scaling mode */
 + char string[255];
 + sprintf(string,"Alpha scaling mode: %d. Press <space> to toggle mode",
 + qvas);
 + s2lab("","","",string);
-Back to [[Function:Template | template]] page.+ ds2dvr(vid, 1); /* Draw the volume render object */
 +}
 + 
 +int main(int argc, char *argv[])
 +{
 + int i; /* Loop variable */
 + float dmin, dmax, amin, amax; /* Min/max values */
 + char trans; /* Transparency mode */
 + float x1 = -1.0, x2 = +1.0; /* Data range along axes */
 + float y1 = -1.0, y2 = +1.0;
 + float z1 = -1.0, z2 = +1.0;
 + int vi, vj, vk; /* Temporary variables */
 + float dx, dy, dz; /* Temporary variables */
 + COLOUR amb = {0.8, 0.8, 0.8}; /* Ambient light */
 + 
 + srand48((long)time(NULL)); /* Seed random numbers */
 + 
 + nx = 40; ny = 40; nz = 10; /* Dimensions of data cube */
 + volume = initVolume(nx, ny, nz); /* Allocate memory */
 + 
 + for (i=0;i<12;i++) { /* Set-up transfrom matrix */
 + tr[i] = 0.0;
 + }
 + tr[ 0] = x1; /* Mapping from data cube */
 + tr[ 1] = (x2-x1)/(float)(nx-1.0); /* to physical coordinates */
 + tr[ 4] = y1;
 + tr[ 6] = (y2-y1)/(float)(ny-1.0);
 + tr[ 8] = z1;
 + tr[11] = (z2-z1)/(float)(nz-1.0);
 + 
 + dmin = 0.0; /* Mininum data value in volume to plot */
 + dmax = 2.0; /* Maximum data value in volume to plot */
 + amin = 0.0; /* Minimum alpha channel value */
 + amax = 0.8; /* Maximum alpha channel value */
 + trans = 't'; /* Transparency type */
 + 
 + dx = tr[1]*0.5; /* Offsets for window - voxels are pixel */
 + dy = tr[6]*0.5; /* centred */
 + dz = tr[11]*0.5;
 + 
 + s2opend("/S2MONO",argc,argv); /* Open the display */
 + s2swin(x1-dx,x2+dx, y1-dy,y2+dy, z1-dz,z2+dz); /* Set window coords */
 + s2svp(-1,1,-1.5,1.5,-0.25,0.25);
 + s2box("BCDE",0,0,"BCDE",0,0,"BCDE",0,0); /* Draw coord box */
 + 
 + long N = 5000;
 + 
 + for (i=0;i<N;i++) { /* Create N random (x,y,z) values */
 + vi = (int)(drand48()*(nx));
 + vj = (int)(drand48()*(ny));
 + vk = (int)(drand48()*(nz));
 + 
 + volume[vi][vj][vk]+=1.0-drand48()*drand48();
 + /* Give a value to volume */
 + }
 + 
 + s2scir(1000,2000); /* Set colour range */
 + s2icm("mgreen",1000,2000); /* Install colour map */
 + vid = ns2cvr(volume, nx, ny, nz, 0, nx-1, 0, ny-1, 0, nz-1,
 + tr, trans, dmin, dmax, amin, amax);
 + /* Create the volume render object */
 + 
 + cs2scb(cb); /* Install a dynamic callback */
 + 
 + ss2srm(SHADE_FLAT); /* Set shading type to FLAT */
 + ss2sl(amb, 0, NULL, NULL, 0); /* Ambient lighting only */
 + 
 + ss2sas(1); /* Start the cube spinning */
 + 
 + s2show(1); /* Open the s2plot window */
 + 
 + return 1;
 +}
 +</pre></code>
 + 
 +[[S2PLOT:Function List | Back]] to S2PLOT function list.
 + 
 +__NOTOC__
 +__NOEDITSECTION__

Current revision

ns2qvas

Query alpha scaling mode.

Prototype

int ns2qvas(void);

Description

Query alpha scaling mode for volume rendering, and normal scaling mode for isosurfaces: 0 => none, 1 => scale by volume dimensions, 2 => scale by viewport dimensions.

See Also

ns2svasSet alpha scaling mode.
ns2sevasExplicitly set alpha scaling values.
ns2cvr Create a volume rendering object.
ns2cvra Render volume of objects.

Code Example

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "s2plot.h"

/* Global variables */
int vid;                                /* ID for volume render object */
float tr[12];                           /* Transformation matrix */
float ***volume;                        /* The data cube */
int nx, ny, nz;                         /* Data grid dimensions */

float ***initVolume(int nx, int ny, int nz)
/* Allocate memory and initialise a data cube */
{
   float ***volume;
   int i, j, k;

   volume = (float ***)malloc(nx * sizeof(float **));
   if (volume == NULL) {
      fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float **)));
      exit(-1);
   }
   for (i=0;i<nx;i++) {
      volume[i] = (float **)malloc(ny * sizeof(float *));
      if (volume[i] == NULL) {
         fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float *)));
         exit(-1);
      }
      for (j=0;j<ny;j++) {
         volume[i][j] = (float *)malloc(nz * sizeof(float));
         if (volume[i][j] == NULL) {
            fprintf(stderr,"Failed to allocate %ld bytes\n",(long)(nx*sizeof(float)));
            exit(-1);
         }
         for (k=0;k<nz;k++) {
            volume[i][j][k] = 0.0;              /* Initialise */
         }
      }
   }

   return volume;
}

void cb(double *t, int *kc)
/* A dynamic callback */
{
   ns2svas(*kc%3);                      /* Set the alpha scaling mode */
   int qvas = ns2qvas();                /* Query the alpha scaling mode */
   char string[255];
   sprintf(string,"Alpha scaling mode: %d.  Press <space> to toggle mode",
                qvas);
   s2lab("","","",string);

   ds2dvr(vid, 1);                      /* Draw the volume render object */
}

int main(int argc, char *argv[])
{
   int i;                                       /* Loop variable */
   float dmin, dmax, amin, amax;                /* Min/max values */
   char trans;                                  /* Transparency mode */
   float x1 = -1.0, x2 = +1.0;                  /* Data range along axes */
   float y1 = -1.0, y2 = +1.0;
   float z1 = -1.0, z2 = +1.0;
   int vi, vj, vk;                              /* Temporary variables */
   float dx, dy, dz;                            /* Temporary variables */
   COLOUR amb = {0.8, 0.8, 0.8};                /* Ambient light */

   srand48((long)time(NULL));                   /* Seed random numbers */

   nx = 40; ny = 40; nz = 10;                   /* Dimensions of data cube */
   volume = initVolume(nx, ny, nz);             /* Allocate memory */

   for (i=0;i<12;i++) {                         /* Set-up transfrom matrix */
      tr[i] = 0.0;
   }
   tr[ 0]  = x1;                                /* Mapping from data cube */
   tr[ 1]  = (x2-x1)/(float)(nx-1.0);           /* to physical coordinates */
   tr[ 4]  = y1;
   tr[ 6]  = (y2-y1)/(float)(ny-1.0);
   tr[ 8]  = z1;
   tr[11]  = (z2-z1)/(float)(nz-1.0);

   dmin = 0.0;                  /* Mininum data value in volume to plot */
   dmax = 2.0;                  /* Maximum data value in volume to plot */
   amin = 0.0;                  /* Minimum alpha channel value */
   amax = 0.8;                  /* Maximum alpha channel value */
   trans = 't';                 /* Transparency type */

   dx = tr[1]*0.5;              /* Offsets for window - voxels are pixel */
   dy = tr[6]*0.5;              /*  centred */
   dz = tr[11]*0.5;

   s2opend("/S2MONO",argc,argv);                        /* Open the display */
   s2swin(x1-dx,x2+dx, y1-dy,y2+dy, z1-dz,z2+dz);       /* Set window coords */
   s2svp(-1,1,-1.5,1.5,-0.25,0.25);
   s2box("BCDE",0,0,"BCDE",0,0,"BCDE",0,0);             /* Draw coord box */

   long N = 5000;

   for (i=0;i<N;i++) {                  /* Create N random (x,y,z) values */
      vi = (int)(drand48()*(nx));
      vj = (int)(drand48()*(ny));
      vk = (int)(drand48()*(nz));

      volume[vi][vj][vk]+=1.0-drand48()*drand48();
                                        /* Give a value to volume */
   }

   s2scir(1000,2000);                   /* Set colour range */
   s2icm("mgreen",1000,2000);           /* Install colour map */
   vid = ns2cvr(volume, nx, ny, nz, 0, nx-1, 0, ny-1, 0, nz-1,
                tr, trans, dmin, dmax, amin, amax);
                                        /* Create the volume render object */

   cs2scb(cb);                          /* Install a dynamic callback */

   ss2srm(SHADE_FLAT);                  /* Set shading type to FLAT */
   ss2sl(amb, 0, NULL, NULL, 0);        /* Ambient lighting only */

   ss2sas(1);                           /* Start the cube spinning */

   s2show(1);                           /* Open the s2plot window */

   return 1;
}

Back to S2PLOT function list.


Personal tools