ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/contour.c
Revision: 1.1
Committed: Tue Mar 12 13:05:08 1991 UTC (33 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * contour.c - program to make contour plots, mappings from 3 to
9     * 2 dimenstions.
10     *
11     * 8/22/86
12     */
13    
14     #include "stdio.h"
15    
16     #include "ctype.h"
17    
18     #define MAXPTS 2048 /* maximum number of input points */
19    
20     typedef float DATAPT[3];
21    
22     DATAPT xyz[MAXPTS]; /* the input data */
23     int xyzsiz;
24    
25     double zmin = 1e20; /* z minimum */
26     double zmax = -1e20; /* z maximum */
27     int minset = 0; /* user set minimum? */
28     int maxset = 0; /* user set maximum? */
29    
30     int ncurves = 6; /* number of contours */
31    
32    
33     main(argc, argv)
34     int argc;
35     char *argv[];
36     {
37     extern double atof();
38     extern int xycmp();
39     FILE *fp;
40     int i;
41    
42     for (i = 1; i < argc && argv[i][0] == '-'; i++)
43     switch (argv[i][1]) {
44     case 'n':
45     ncurves = atoi(argv[++i]);
46     break;
47     case 'l':
48     zmin = atof(argv[++i]);
49     minset = 1;
50     break;
51     case 'u':
52     zmax = atof(argv[++i]);
53     maxset = 1;
54     break;
55     default:
56     fprintf(stderr, "%s: unknown option: %s\n",
57     argv[0], argv[i]);
58     exit(1);
59     }
60    
61     if (i == argc)
62     getdata(stdin);
63     else if (i < argc-1) {
64     fprintf(stderr, "%s: too many file names\n", argv[0]);
65     exit(1);
66     } else if ((fp = fopen(argv[i], "r")) == NULL) {
67     fprintf(stderr, "%s: file not found\n", argv[i]);
68     exit(1);
69     } else
70     getdata(fp);
71    
72     qsort(xyz, xyzsiz, sizeof(DATAPT), xycmp); /* sort data */
73    
74     for (i = 0; i < ncurves; i++) /* do contours */
75     contour(i);
76    
77     exit(0);
78     }
79    
80    
81     getdata(fp) /* read input data */
82     FILE *fp;
83     {
84     register int i;
85    
86     while (xyzsiz < MAXPTS) {
87     for (i = 0; i < 3; i++)
88     if (fscanf(fp, "%f", &xyz[xyzsiz][i]) != 1)
89     break;
90     if (i != 3)
91     break;
92     if (xyz[xyzsiz][2] < zmin)
93     if (minset)
94     continue;
95     else
96     zmin = xyz[xyzsiz][2];
97     if (xyz[xyzsiz][2] > zmax)
98     if (maxset)
99     continue;
100     else
101     zmax = xyz[xyzsiz][2];
102     xyzsiz++;
103     }
104     if (!feof(fp)) {
105     fprintf(stderr, "Error reading input data\n");
106     exit(1);
107     }
108     }
109    
110    
111     int
112     xycmp(p1, p2) /* -1 if p1 < p2, 0 if equal, 1 if p1 > p2 */
113     DATAPT p1, p2;
114     {
115     if (p1[0] > p2[0])
116     return(1);
117     if (p1[0] < p2[0])
118     return(-1);
119     if (p1[1] > p2[1])
120     return(1);
121     if (p1[1] < p2[1])
122     return(-1);
123     return(0);
124     }
125    
126    
127     contour(n) /* make contour n */
128     int n;
129     {
130     double z;
131    
132     z = (n+.5)*(zmax-zmin)/ncurves + zmin;
133     printf("%clabel=\"%f\";\n", n+'A', z);
134     printf("%cdata=\n", n+'A');
135     crossings(z);
136     printf(";\n");
137     }
138    
139    
140     crossings(z) /* find crossings for z */
141     double z;
142     {
143     register DATAPT *p0, *p1;
144    
145     p0 = p1 = xyz;
146     while (p0 < xyz+xyzsiz-2) {
147     while (p1 < xyz+xyzsiz-2) {
148     if (p0[0][0] < p1[0][0] && p0[0][1] <= p1[0][1])
149     break;
150     p1++;
151     }
152     if (p0[0][0] == p0[1][0])
153     cross(p0[0], p0[1], z);
154     if (p0[0][1] == p1[0][1])
155     cross(p0[0], p1[0], z);
156     p0++;
157     }
158     }
159    
160    
161     cross(p0, p1, z) /* mark crossing between p0 and p1 */
162     register DATAPT p0, p1;
163     double z;
164     {
165     if (p1[2] - p0[2] == 0.0)
166     return;
167     z = (z - p0[2])/(p1[2] - p0[2]);
168     if (z < 0.0 || z >= 1.0)
169     return;
170     printf("%e\t", p0[0]*(1.0 - z) + p1[0]*z);
171     printf("%e\n", p0[1]*(1.0 - z) + p1[1]*z);
172     }