home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
forum23.lzh
/
f23b
/
SOFTWARE
/
PDRAW
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-15
|
15KB
|
611 lines
/*** main.c ***/
#define MAIN
#include <stdio.h>
#include <strings.h>
#include <math.h>
#include "header.h"
segmptr segmhead; /* pointer to first segment */
segmptr segmtail; /* pointer to last segment */
pfileptr pfilehead;
pfileptr pfiletail;
/* some plot options */
char *term; /* terminal type */
char xlabel[MAXCHAR]; /* x-label */
char ylabel[MAXCHAR]; /* y-label */
char zlabel[MAXCHAR]; /* z-label */
char toplabel[MAXCHAR]; /* header label */
char printer[MAXCHAR]; /* printer type */
/* info for scaling and transformations */
xyzdata eyepos; /* position of the eye/camera */
xyzdata viewcenter; /* center of view */
xyzdata viewup; /* up direction of view */
box window; /* window dimensions relative to center */
box viewport; /* viewport in normalized device coordinates*/
matrix I; /* identity matrix */
matrix view_transfo; /* matrix for view transformation */
double ndc_width; /* width in ndc space */
double ndc_height; /* height in ndc space */
/* plotting options/parameters */
int grid,equalscale,postscript,printplot,noplot;
int xticks,yticks,zticks;
int line,linechange,marker,markerchange;
int linetype[MAXTYPE], markertype[MAXTYPE];
int hiddenline, quick_sort, nosort;
double scale;
/* plot boundaries */
double xmin, xmax, ymin, ymax, zmin, zmax;
double pxmin, pymin, pxmax, pymax;
main(argc,argv)
int argc;
char *argv[];
{
FILE *fopen(), *fp;
extern pfileptr pfilehead;
pfileptr P;
char c;
int format;
fprintf(stdout,"Pdraw V1.4 9/4/90\n");
initialize_data(); /* Initialize the variables */
fileopen(argc,argv); /* read the data and options */
initialize_view(); /* initialize the view parameters */
/* process each plotfile */
for (P=pfilehead; P!=NULL; P=P->next) {
open_file(P->plotfile,&fp,&format);
fileread(fp,format); /* read the plotfile */
read_view(0.0,0.0); /* read the view transformation */
draw_view(argc,argv); /* draw the picture */
clear_mem(); /* clear the memory */
/* keep on reading until the next blank line */
while ( (c=getc(fp)) != EOF && c!= '\n');
while (c!=EOF) {
if ( (c=getc(fp))!=EOF && c=='\n'){
/* more curves coming up */
fileread(fp,format); /* read the plotfile */
read_view(0.0,0.0); /* read the view transformation */
draw_view(argc,argv); /* draw the picture */
clear_mem(); /* clear the memory */
}
/* read the rest of the line */
while ( (c=getc(fp)) != EOF && c!= '\n');
}
fclose(fp);
}
/* read view is used thru every loop iteration to set the view
* parameters. draw_view() also calls read_view(). The points
* are transformed on a point-by-point basis just before they are
* plotted using transform_point(). The point-segm structure read
* in is unaffected by the subsequent matrix operations.
*/
fprintf(stdout,"Congratulations!!! You have been TERMINATED...\n");
}
/* clear the memory */
clear_mem()
{
void delete_node();
void delete_segm();
segmptr S;
extern segmptr segmhead,segmtail;
/* delete the nodes in each segment */
for (S=segmhead; S!=NULL; S=S->next)
while (S->head!=NULL) delete_node(S->tail,S);
/* now delete all the segments */
while (segmhead!=NULL) delete_segm(segmtail);
segmhead = NULL;
segmtail = NULL;
}
/* draw the view */
draw_view(argc,argv)
int argc;
char *argv[];
{
FILE *fopen(), *ips;
char *sprintf();
char c, command[MAXCHAR];
int PRINT = FALSE;
if (!noplot) {
if ( (strcmp("xterm",term)==0) ||
(strcmp("xterms",term)==0) ||
(strcmp("vt200",term)==0) ||
(strcmp("vt220",term)==0) ||
(strcmp("vs100s",term)==0) ) {
InitWindow(argc,argv);
RunOps();
}
}
/* Prepare for postscript output */
if (postscript == ON) {
if ((ips = fopen(PSFILE,"w")) == NULL) {
fprintf(stderr,"cat: can't open %s\n",PSFILE);
exit(1);
}
initps(ips);
axesps(ips);
plotps(ips);
endgrps(ips);
}
/* Get postscript printout */
if (postscript == ON && !printplot) {
fprintf(stdout,"Type return (or n/q) to ");
fprintf(stdout,"send the plot to the %s printer :",printer);
if ( (c=getc(stdin)) == '\n' || c == 'y' || c == 'Y' ) {
PRINT = ON;
} else if (c == 'q')
exit(1);
if (c != '\n') while ( (c=getc(stdin)) != '\n');
}
/* now send to the printer */
if ((printplot || PRINT) && postscript==ON) {
sprintf(command,"%s %s %s","lpr",printer,PSFILE);
system(command);
fprintf(stdout,"Postscript file has been ");
fprintf(stdout,"sent to the %s printer\n",printer);
}
if (strcmp("sun",term)==0) {
fprintf(stdout,"SUN plotting routines not implemented yet.\n");
}
}
/* initialize some data */
initialize_data()
{
char *getenv();
char *strcpy();
char *sprintf();
int i,j;
char *ptr;
/* Identity matrix */
for (i=0; i<N; i++)
for (j=0; j<N; j++)
if (i==j) I[i][j] = 1.0;
else I[i][j] = 0.0;
/* Terminal type */
term = getenv("TERM");
fprintf(stdout," Terminal Type = %s\n",term);
/* Labeling information */
strcpy(xlabel,"X-Axis");
strcpy(ylabel,"Y-Axis");
strcpy(zlabel,"Z-Axis");
strcpy(toplabel,"3D Line Plot");
/* plotting parameters/options */
grid = OFF;
equalscale = ON;
postscript = ON;
printplot = OFF;
noplot = OFF;
line = ON;
linechange = OFF;
marker = OFF;
markerchange = OFF;
hiddenline = OFF;
quick_sort = ON;
nosort = OFF;
for (i=0; i<MAXTYPE; i++) linetype[i] = i;
for (i=0; i<MAXTYPE; i++) markertype[i] = i;
scale = 1.0;
xticks = 2;
yticks = 2;
zticks = 2;
/* data */
segmhead = NULL;
segmtail = NULL;
/* printer type */
if ((ptr = getenv("PRINTER")) == NULL)
strcpy(printer,"-Plp550M");
else
sprintf(printer,"-P%s",ptr);
fprintf(stdout," Default Printer = %s\n",printer);
/* ndc parameters */
ndc_width = 1.0;
ndc_height = 1.0;
}
initialize_view()
{
extern double xmax, xmin, ymax, ymin, zmax, zmin;
double max;
/* viewing parameters */
if ((eyepos.x*eyepos.x + eyepos.y*eyepos.y + eyepos.z*eyepos.z) < SMALL) {
eyepos.x = 1.0;
eyepos.y = 1.5;
eyepos.z = 0.5;
}
viewcenter.x = 0.0;
viewcenter.y = 0.0;
viewcenter.z = 0.0;
viewup.x = 0.0;
viewup.y = 0.0;
viewup.z = 1.0;
max = -1.0e10;
if (xmax-xmin > max) max = xmax-xmin;
if (ymax-ymin > max) max = zmax-ymin;
if (zmax-zmin > max) max = zmax-zmin;
max = 1.3*max;
window.xl = -max;
window.xr = max;
window.yb = -max;
window.yt = max;
viewport.xl = 1.0*PSMIN;
viewport.xr = 1.0*PSMAX*scale;
viewport.yb = 1.0*PSMIN;
viewport.yt = 1.0*PSMAX*scale;
}
read_view(theta,phi)
double theta, phi;
{
xyzdata transform_point(), normalize_vector();
matrix vwscale, R, Rp, Rx, Ry, Rz, SC;
double dxsq, dysq, dzsq, l, r, v, angle, theta0, phi0;
xyzdata newpoint, neweye;
/* eye/center */
dxsq = (eyepos.x-viewcenter.x)*(eyepos.x-viewcenter.x);
dysq = (eyepos.y-viewcenter.y)*(eyepos.y-viewcenter.y);
dzsq = (eyepos.z-viewcenter.z)*(eyepos.z-viewcenter.z);
l = sqrt(dxsq + dysq + dzsq);
r = sqrt(dxsq + dysq);
if (l < SMALL) {
fprintf(stderr,"Error: Zero Length Vector - No View Specified\n");
exit(1);
}
phi0 = acos((eyepos.z-viewcenter.z)/l);
if (r > SMALL) theta0 = acos((eyepos.x-viewcenter.x)/r);
else theta0 = 0.0;
neweye.x = l*sin(phi0+phi)*cos(theta0+theta);
neweye.y = l*sin(phi0+phi)*sin(theta0+theta);
neweye.z = l*cos(phi0+phi);
/* eye/center */
dxsq = (neweye.x-viewcenter.x)*(neweye.x-viewcenter.x);
dysq = (neweye.y-viewcenter.y)*(neweye.y-viewcenter.y);
dzsq = (neweye.z-viewcenter.z)*(neweye.z-viewcenter.z);
l = sqrt(dxsq + dysq + dzsq);
v = sqrt( dysq + dzsq);
if (l==0) {
fprintf(stderr,"Error: Zero Length Vector - No View Specified\n");
exit(1);
}
/* Rotate around x by Theta */
if (v > SMALL) angle = acos((neweye.z-viewcenter.z)/v);
else angle = 0.0;
if (neweye.y < 0.0) angle = -1.0*angle;
rotate('x',angle,Rx);
/* Rotate around y by -Theta */
angle = -acos(v/l);
if (neweye.x < 0.0) angle = -1.0*angle;
rotate('y',angle,Ry);
/* Rotation Matrix */
mult_matrix(Rp,Rx,Ry); /* Rp = Rx * Ry */
/* Find the necessary matrix to rotate to the view up position */
if ( dxsq + dysq > SMALL) {
newpoint = transform_point(viewup,Rp);
newpoint.z = 0.0;
newpoint = normalize_vector(newpoint);
angle = -acos(newpoint.y); /* dot_product[(newpoint),(0,1,0)] */
if (newpoint.x>0.0)
angle = -1.0*angle; /* from cross_product with (0,1,0) */
} else {
angle = 0.0;
}
rotate('z',angle,Rz);
/* Rotation Matrix */
mult_matrix(R,Rp,Rz); /* R = R * Ry */
/* xyz scaling */
if (equalscale==OFF) {
assign_matrix(SC,I);
if ((xmax-xmin) > 1.0e-10) SC[0][0] = 1.0/(xmax-xmin);
if ((ymax-ymin) > 1.0e-10) SC[1][1] = 1.0/(ymax-ymin);
if ((zmax-zmin) > 1.0e-10) SC[2][2] = 1.0/(zmax-zmin);
/* multiply the rotation matrix by a scaling matrix */
mult_matrix(R,SC,R);
}
/* window - viewport scaling */
assign_matrix(vwscale,I);
find_window(R);
vwscale[0][0] = (viewport.xr - viewport.xl)/(window.xr-window.xl);
vwscale[1][1] = (viewport.yt - viewport.yb)/(window.yt-window.yb);
vwscale[3][0] = viewport.xl - vwscale[0][0]*window.xl;
vwscale[3][1] = viewport.yb - vwscale[1][1]*window.yb;
/* Apply scaling and transformation */
mult_matrix(view_transfo,R,vwscale);
}
find_window(R)
matrix R;
{
xyzdata transform_point();
xyzdata point;
double delx, dely;
int i,j,k;
window.xl = 1.0e10;
window.xr = -1.0e10;
window.yb = 1.0e10;
window.yt = -1.0e10;
for (i=0; i<2; i++)
for (j=0; j<2; j++)
for (k=0; k<2; k++) {
point.x = xmin + i*(xmax-xmin);
point.y = ymin + j*(ymax-ymin);
point.z = zmin + k*(zmax-zmin);
point = transform_point(point,R);
if (point.x < window.xl) window.xl = point.x;
if (point.x > window.xr) window.xr = point.x;
if (point.y < window.yb) window.yb = point.y;
if (point.y > window.yt) window.yt = point.y;
}
delx = window.xr - window.xl;
dely = window.yt - window.yb;
if (delx > dely) window.yt = window.yb + delx;
if (dely > delx){
window.xl = window.xl - 0.5*(dely-delx);
window.xr = window.xr + 0.5*(dely-delx);
}
}
apply_view()
{
extern segmptr segmhead;
double a[N];
int j;
segmptr S;
nodeptr Nd;
for (S=segmhead; S!=NULL; S=S->next)
for (Nd=S->head; Nd!=NULL; Nd=Nd->next) {
for (j=0; j<N; j++)
a[j] = Nd->x * view_transfo[0][j] +
Nd->y * view_transfo[1][j] +
Nd->z * view_transfo[2][j] +
view_transfo[3][j];
Nd->x = a[0]/a[3];
Nd->y = a[1]/a[3];
Nd->z = a[2]/a[3];
}
}
check_view_angles(theta0,phi0)
double *theta0, *phi0;
{
extern xyzdata eyepos, viewcenter;
double dx, dy, dz, l, r;
/* view vector */
dx = eyepos.x - viewcenter.x;
dy = eyepos.y - viewcenter.y;
dz = eyepos.z - viewcenter.z;
l = sqrt(dx*dx + dy*dy + dz*dz);
r = sqrt(dx*dx + dy*dy);
if (l < SMALL) {
fprintf(stderr,"Error: Zero Length Vector - No View Specified\n");
exit(1);
}
/* angle from z-axis */
*phi0 = acos(dz/l);
/* angle from x-axis */
*theta0 = 0.0;
if (r > SMALL) *theta0 = acos(dx/r);
}
xyzdata transform_point(point,A)
xyzdata point;
matrix A;
{
double a[N];
int j;
xyzdata newpoint;
for (j=0; j<N; j++)
a[j] = point.x * A[0][j] +
point.y * A[1][j] +
point.z * A[2][j] + A[3][j];
newpoint.x = a[0]/a[3];
newpoint.y = a[1]/a[3];
newpoint.z = a[2]/a[3];
return(newpoint);
}
xyzdata normalize_vector(vector)
xyzdata vector;
{
double dl;
xyzdata newvector;
dl = sqrt(vector.x*vector.x + vector.y*vector.y + vector.z*vector.z);
newvector.x = vector.x/dl;
newvector.y = vector.y/dl;
newvector.z = vector.z/dl;
return(newvector);
}
xyzdata midpoint(x1,y1,z1,x2,y2,z2,ratio)
double x1,y1,z1,x2,y2,z2,ratio;
{
xyzdata midpt, newpt;
xyzdata transform_point();
midpt.x = x1+ratio*(x2-x1);
midpt.y = y1+ratio*(y2-y1);
midpt.z = z1+ratio*(z2-z1);
newpt = transform_point(midpt,view_transfo);
return(newpt);
}
print_point(point)
xyzdata point;
{
fprintf(stdout,"x = %10.5f ", point.x);
fprintf(stdout,"y = %10.5f ", point.y);
fprintf(stdout,"z = %10.5f\n",point.z);
}
print_data()
{
extern segmptr segmhead;
segmptr S;
nodeptr Nd;
for (S=segmhead; S!=NULL; S=S->next)
for (Nd=S->head; Nd!=NULL; Nd=Nd->next) {
fprintf(stdout,"x = %10.5f ",Nd->x);
fprintf(stdout,"y = %10.5f ",Nd->y);
fprintf(stdout,"z = %10.5f\n",Nd->z);
}
}
/* matrix OPERATIONS */
print_matrix(A)
matrix A;
{
int i,j;
for (i=0; i<N; i++) {
fprintf(stdout,"\n");
for (j=0; j<N; j++)
fprintf(stdout,"%10.5f",A[i][j]);
}
fprintf(stdout,"\n");
}
assign_matrix(A,B)
matrix A,B;
{
int i,j;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
A[i][j] = B[i][j];
}
mult_matrix(A,B,C)
matrix A,B,C;
{
int i,j;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
A[i][j] = B[i][0]*C[0][j] + B[i][1]*C[1][j] +
B[i][2]*C[2][j] + B[i][3]*C[3][j];
}
scalematrix(sx,sy,sz,A)
double sx, sy, sz;
matrix A;
{
assign_matrix(A,I);
A[0][0] = sx;
A[1][1] = sy;
A[2][2] = sz;
}
mirror(axis,A)
char axis;
matrix A;
{
double sx, sy, sz;
sx = 1.0;
sy = 1.0;
sz = 1.0;
if (axis == 'x') sx = -1.0;
else if (axis == 'y') sy = -1.0;
else if (axis == 'z') sz = -1.0;
else {
fprintf(stderr,"Error: The character %c does not belong in mirror\n",axis);
exit(1);
}
scalematrix(sx,sy,sz,A);
}
rotate(axis,angle,A)
char axis;
double angle;
matrix A;
{
assign_matrix(A,I);
if (axis == 'z') {
A[0][0] = cos(angle);
A[0][1] = sin(angle);
A[1][0] = -sin(angle);
A[1][1] = cos(angle);
} else if (axis == 'y') {
A[0][0] = cos(angle);
A[0][2] = -sin(angle);
A[2][0] = sin(angle);
A[2][2] = cos(angle);
} else if (axis == 'x') {
A[1][1] = cos(angle);
A[1][2] = sin(angle);
A[2][1] = -sin(angle);
A[2][2] = cos(angle);
} else {
fprintf(stderr,"Error: The character %c does not belong in rotate\n",axis);
exit(1);
}
}
translate(tx,ty,tz,A)
double tx,ty,tz;
matrix A;
{
assign_matrix(A,I);
A[3][0] = tx;
A[3][1] = ty;
A[3][2] = tz;
}