ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
Revision: 2.7
Committed: Tue Aug 5 21:45:05 2014 UTC (10 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +4 -1 lines
Log Message:
Added missing _O_BINARY setting for Windows

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.7 static const char RCSid[] = "$Id: rmatrix.c,v 2.6 2014/08/02 17:10:43 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * General matrix operations.
6     */
7    
8     #include <stdio.h>
9     #include <stdlib.h>
10     #include <string.h>
11     #include <fcntl.h>
12     #include "resolu.h"
13     #include "rmatrix.h"
14    
15 greg 2.6 static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
16 greg 2.1
17     /* Allocate a nr x nc matrix with n components */
18     RMATRIX *
19     rmx_alloc(int nr, int nc, int n)
20     {
21     RMATRIX *dnew;
22    
23     if ((nr <= 0) | (nc <= 0) | (n <= 0))
24     return(NULL);
25     dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) +
26     sizeof(dnew->mtx[0])*(n*nr*nc));
27     if (dnew == NULL)
28     return(NULL);
29     dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
30 greg 2.6 dnew->dtype = DTdouble;
31 greg 2.5 dnew->info = NULL;
32 greg 2.1 return(dnew);
33     }
34    
35 greg 2.6 /* Free a RMATRIX array */
36     void
37     rmx_free(RMATRIX *rm)
38     {
39     if (!rm) return;
40     if (rm->info)
41     free(rm->info);
42     free(rm);
43     }
44    
45     /* Resolve data type based on two input types (returns 0 for mismatch) */
46     int
47     rmx_newtype(int dtyp1, int dtyp2)
48     {
49     if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2)
50     return(0);
51     if (dtyp1 < dtyp2)
52     return(dtyp1);
53     return(dtyp2);
54     }
55    
56 greg 2.5 /* Append header information associated with matrix data */
57     int
58     rmx_addinfo(RMATRIX *rm, const char *info)
59     {
60     if (!info || !*info)
61     return(0);
62     if (!rm->info)
63     rm->info = (char *)malloc(strlen(info)+1);
64     else
65     rm->info = (char *)realloc(rm->info,
66     strlen(rm->info)+strlen(info)+1);
67     if (!rm->info)
68     return(0);
69     strcat(rm->info, info);
70     return(1);
71     }
72    
73 greg 2.1 static int
74     get_dminfo(char *s, void *p)
75     {
76 greg 2.6 RMATRIX *ip = (RMATRIX *)p;
77 greg 2.5 char fmt[64];
78 greg 2.1 int i;
79    
80 greg 2.6 if (headidval(fmt, s))
81     return(0);
82 greg 2.1 if (!strncmp(s, "NCOMP=", 6)) {
83     ip->ncomp = atoi(s+6);
84     return(0);
85     }
86     if (!strncmp(s, "NROWS=", 6)) {
87     ip->nrows = atoi(s+6);
88     return(0);
89     }
90     if (!strncmp(s, "NCOLS=", 6)) {
91     ip->ncols = atoi(s+6);
92     return(0);
93     }
94 greg 2.5 if (!formatval(fmt, s)) {
95 greg 2.6 rmx_addinfo(ip, s);
96 greg 2.1 return(0);
97 greg 2.5 }
98 greg 2.1 for (i = 1; i < DTend; i++)
99     if (!strcmp(fmt, cm_fmt_id[i])) {
100     ip->dtype = i;
101     return(0);
102     }
103     return(-1);
104     }
105    
106     static int
107     rmx_load_ascii(RMATRIX *rm, FILE *fp)
108     {
109     int i, j, k;
110     #ifdef _WIN32
111     _setmode(fileno(fp), _O_TEXT);
112     #endif
113     for (i = 0; i < rm->nrows; i++)
114     for (j = 0; j < rm->ncols; j++)
115     for (k = 0; k < rm->ncomp; k++)
116     if (fscanf(fp, "%lf", &rmx_lval(rm,i,j,k)) != 1)
117     return(0);
118     return(1);
119     }
120    
121     static int
122     rmx_load_float(RMATRIX *rm, FILE *fp)
123     {
124     int i, j, k;
125     float val[100];
126    
127     if (rm->ncomp > 100) {
128     fputs("Unsupported # components in rmx_load_float()\n", stderr);
129     exit(1);
130     }
131     for (i = 0; i < rm->nrows; i++)
132     for (j = 0; j < rm->ncols; j++) {
133     if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
134     return(0);
135     for (k = rm->ncomp; k--; )
136     rmx_lval(rm,i,j,k) = val[k];
137     }
138     return(1);
139     }
140    
141     static int
142     rmx_load_double(RMATRIX *rm, FILE *fp)
143     {
144     int i, j, k;
145     double val[100];
146    
147     if (rm->ncomp > 100) {
148     fputs("Unsupported # components in rmx_load_double()\n", stderr);
149     exit(1);
150     }
151     for (i = 0; i < rm->nrows; i++)
152     for (j = 0; j < rm->ncols; j++) {
153     if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
154     return(0);
155     for (k = rm->ncomp; k--; )
156     rmx_lval(rm,i,j,k) = val[k];
157     }
158     return(1);
159     }
160    
161     static int
162     rmx_load_rgbe(RMATRIX *rm, FILE *fp)
163     {
164     COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
165     int i, j;
166    
167     if (scan == NULL)
168     return(0);
169     for (i = 0; i < rm->nrows; i++) {
170     if (freadscan(scan, rm->ncols, fp) < 0) {
171     free(scan);
172     return(0);
173     }
174     for (j = rm->ncols; j--; ) {
175     rmx_lval(rm,i,j,0) = colval(scan[j],RED);
176     rmx_lval(rm,i,j,1) = colval(scan[j],GRN);
177     rmx_lval(rm,i,j,2) = colval(scan[j],BLU);
178     }
179     }
180     free(scan);
181     return(1);
182     }
183    
184     /* Load matrix from supported file type */
185     RMATRIX *
186     rmx_load(const char *fname)
187     {
188     FILE *fp = stdin;
189 greg 2.6 RMATRIX dinfo;
190 greg 2.1 RMATRIX *dnew;
191    
192     if (fname == NULL) { /* reading from stdin? */
193     fname = "<stdin>";
194 greg 2.7 #ifdef _WIN32
195     _setmode(fileno(stdin), _O_BINARY);
196     #endif
197 greg 2.1 } else {
198     const char *sp = fname; /* check suffix */
199     while (*sp)
200     ++sp;
201     while (sp > fname && sp[-1] != '.')
202     --sp;
203     if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */
204     CMATRIX *cm = cm_loadBTDF((char *)fname);
205     if (cm == NULL)
206     return(NULL);
207     dnew = rmx_from_cmatrix(cm);
208     cm_free(cm);
209     return(dnew);
210     }
211     /* else open it ourselves */
212     if ((fp = fopen(fname, "rb")) == NULL)
213     return(NULL);
214     }
215     #ifdef getc_unlocked
216     flockfile(fp);
217     #endif
218     dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0;
219 greg 2.6 dinfo.dtype = DTascii; /* assumed w/o FORMAT */
220     dinfo.info = NULL;
221 greg 2.3 if (getheader(fp, get_dminfo, &dinfo) < 0) {
222 greg 2.1 fclose(fp);
223     return(NULL);
224     }
225 greg 2.4 if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) {
226 greg 2.1 if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) {
227     fclose(fp);
228     return(NULL);
229     }
230 greg 2.4 if (dinfo.ncomp <= 0)
231     dinfo.ncomp = 3;
232     else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) &&
233     dinfo.ncomp != 3) {
234     fclose(fp);
235     return(NULL);
236     }
237 greg 2.1 }
238     dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp);
239     if (dnew == NULL) {
240     fclose(fp);
241     return(NULL);
242     }
243 greg 2.6 dnew->info = dinfo.info;
244 greg 2.1 switch (dinfo.dtype) {
245     case DTascii:
246     if (!rmx_load_ascii(dnew, fp))
247     goto loaderr;
248     break;
249     case DTfloat:
250     if (!rmx_load_float(dnew, fp))
251     goto loaderr;
252 greg 2.6 dnew->dtype = DTfloat;
253 greg 2.1 break;
254     case DTdouble:
255     if (!rmx_load_double(dnew, fp))
256     goto loaderr;
257 greg 2.6 dnew->dtype = DTdouble;
258 greg 2.1 break;
259     case DTrgbe:
260     case DTxyze:
261     if (!rmx_load_rgbe(dnew, fp))
262     goto loaderr;
263 greg 2.6 dnew->dtype = dinfo.dtype;
264 greg 2.1 break;
265     default:
266     goto loaderr;
267     }
268     if (fp != stdin)
269     fclose(fp);
270     return(dnew);
271     loaderr: /* should report error? */
272     fclose(fp);
273     rmx_free(dnew);
274     return(NULL);
275     }
276    
277     static int
278     rmx_write_ascii(const RMATRIX *rm, FILE *fp)
279     {
280     int i, j, k;
281     #ifdef _WIN32
282     _setmode(fileno(fp), _O_TEXT);
283     #endif
284     for (i = 0; i < rm->nrows; i++) {
285     for (j = 0; j < rm->ncols; j++) {
286     for (k = 0; k < rm->ncomp; k++)
287     fprintf(fp, " %.15e", rmx_lval(rm,i,j,k));
288     fputc('\t', fp);
289     }
290     fputc('\n', fp);
291     }
292     return(1);
293     }
294    
295     static int
296     rmx_write_float(const RMATRIX *rm, FILE *fp)
297     {
298     int i, j, k;
299     float val[100];
300    
301     if (rm->ncomp > 100) {
302     fputs("Unsupported # components in rmx_write_float()\n", stderr);
303     exit(1);
304     }
305     for (i = 0; i < rm->nrows; i++)
306     for (j = 0; j < rm->ncols; j++) {
307     for (k = rm->ncomp; k--; )
308     val[k] = (float)rmx_lval(rm,i,j,k);
309     if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
310     return(0);
311     }
312     return(1);
313     }
314    
315     static int
316     rmx_write_double(const RMATRIX *rm, FILE *fp)
317     {
318     int i, j, k;
319     double val[100];
320    
321     if (rm->ncomp > 100) {
322     fputs("Unsupported # components in rmx_write_double()\n", stderr);
323     exit(1);
324     }
325     for (i = 0; i < rm->nrows; i++)
326     for (j = 0; j < rm->ncols; j++) {
327     for (k = rm->ncomp; k--; )
328     val[k] = rmx_lval(rm,i,j,k);
329     if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
330     return(0);
331     }
332     return(1);
333     }
334    
335     static int
336     rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
337     {
338     COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
339     int i, j;
340    
341     if (scan == NULL)
342     return(0);
343     for (i = 0; i < rm->nrows; i++) {
344     for (j = rm->ncols; j--; )
345     setcolor(scan[j], rmx_lval(rm,i,j,0),
346     rmx_lval(rm,i,j,1),
347     rmx_lval(rm,i,j,2) );
348     if (fwritescan(scan, rm->ncols, fp) < 0) {
349     free(scan);
350     return(0);
351     }
352     }
353     free(scan);
354     return(1);
355     }
356    
357 greg 2.6 /* Write matrix to file type indicated by dtype */
358 greg 2.1 long
359     rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
360     {
361     RMATRIX *mydm = NULL;
362     int ok = 1;
363    
364     if ((rm == NULL) | (fp == NULL))
365     return(0);
366     /* complete header */
367 greg 2.5 if (rm->info)
368     fputs(rm->info, fp);
369 greg 2.6 if (dtype == DTfromHeader)
370     dtype = rm->dtype;
371     else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
372     dtype = DTxyze;
373     else if ((dtype = DTxyze) & (rm->dtype == DTrgbe))
374     dtype = DTrgbe;
375 greg 2.1 if ((dtype != DTrgbe) & (dtype != DTxyze)) {
376     fprintf(fp, "NROWS=%d\n", rm->nrows);
377     fprintf(fp, "NCOLS=%d\n", rm->ncols);
378     fprintf(fp, "NCOMP=%d\n", rm->ncomp);
379     } else if (rm->ncomp != 3) { /* wrong # components? */
380     double cmtx[3];
381     if (rm->ncomp != 1) /* only convert grayscale */
382     return(0);
383     cmtx[0] = cmtx[1] = cmtx[2] = 1;
384     mydm = rmx_transform(rm, 3, cmtx);
385     if (mydm == NULL)
386     return(0);
387     rm = mydm;
388     }
389     fputformat((char *)cm_fmt_id[dtype], fp);
390     fputc('\n', fp);
391     switch (dtype) { /* write data */
392     case DTascii:
393     ok = rmx_write_ascii(rm, fp);
394     break;
395     case DTfloat:
396     ok = rmx_write_float(rm, fp);
397     break;
398     case DTdouble:
399     ok = rmx_write_double(rm, fp);
400     break;
401     case DTrgbe:
402     case DTxyze:
403     fprtresolu(rm->ncols, rm->nrows, fp);
404     ok = rmx_write_rgbe(rm, fp);
405     break;
406     default:
407     return(0);
408     }
409     ok &= (fflush(fp) == 0);
410     rmx_free(mydm);
411     return(ftell(fp) * ok); /* return # bytes written */
412     }
413    
414     /* Allocate and assign square identity matrix with n components */
415     RMATRIX *
416     rmx_identity(const int dim, const int n)
417     {
418     RMATRIX *rid = rmx_alloc(dim, dim, n);
419     int i;
420    
421     if (rid == NULL)
422     return(NULL);
423     memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
424     for (i = dim; i--; )
425     rmx_lval(rid,i,i,0) = 1;
426     for (i = n; --i; )
427     memcpy(rid->mtx+i*(dim*dim), rid->mtx,
428     sizeof(rid->mtx[0])*dim*dim);
429     return(rid);
430     }
431    
432     /* Duplicate the given matrix */
433     RMATRIX *
434     rmx_copy(const RMATRIX *rm)
435     {
436     RMATRIX *dnew;
437    
438     if (rm == NULL)
439     return(NULL);
440     dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
441     if (dnew == NULL)
442     return(NULL);
443 greg 2.5 rmx_addinfo(dnew, rm->info);
444 greg 2.6 dnew->dtype = rm->dtype;
445 greg 2.1 memcpy(dnew->mtx, rm->mtx,
446     sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
447     return(dnew);
448     }
449    
450 greg 2.2 /* Allocate and assign transposed matrix */
451     RMATRIX *
452     rmx_transpose(const RMATRIX *rm)
453 greg 2.1 {
454 greg 2.2 RMATRIX *dnew;
455 greg 2.1 int i, j, k;
456    
457     if (rm == NULL)
458     return(0);
459 greg 2.2 dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
460     if (dnew == NULL)
461     return(NULL);
462 greg 2.5 if (rm->info) {
463     rmx_addinfo(dnew, rm->info);
464     rmx_addinfo(dnew, "Transposed rows and columns\n");
465     }
466 greg 2.6 dnew->dtype = rm->dtype;
467 greg 2.2 for (i = dnew->nrows; i--; )
468     for (j = dnew->ncols; j--; )
469     for (k = dnew->ncomp; k--; )
470     rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k);
471     return(dnew);
472 greg 2.1 }
473    
474     /* Multiply (concatenate) two matrices and allocate the result */
475     RMATRIX *
476     rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
477     {
478     RMATRIX *mres;
479     int i, j, k, h;
480    
481     if ((m1 == NULL) | (m2 == NULL) ||
482     (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
483     return(NULL);
484     mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
485     if (mres == NULL)
486     return(NULL);
487 greg 2.6 i = rmx_newtype(m1->dtype, m2->dtype);
488     if (i)
489     mres->dtype = i;
490     else
491     rmx_addinfo(mres, rmx_mismatch_warn);
492 greg 2.1 for (i = mres->nrows; i--; )
493     for (j = mres->ncols; j--; )
494     for (h = m1->ncols; h--; ) {
495     long double d = 0;
496     for (k = mres->ncomp; k--; )
497     d += (long double)rmx_lval(m1,i,h,k) *
498     (long double)rmx_lval(m2,h,j,k);
499     rmx_lval(mres,i,j,k) = (double)d;
500     }
501     return(mres);
502     }
503    
504     /* Sum second matrix into first, applying scale factor beforehand */
505     int
506     rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[])
507     {
508     double *mysf = NULL;
509     int i, j, k;
510    
511     if ((msum == NULL) | (madd == NULL) ||
512     (msum->nrows != madd->nrows) |
513     (msum->ncols != madd->ncols) |
514     (msum->ncomp != madd->ncomp))
515     return(0);
516     if (sf == NULL) {
517     mysf = (double *)malloc(sizeof(double)*msum->ncomp);
518     if (mysf == NULL)
519     return(0);
520     for (k = msum->ncomp; k--; )
521     mysf[k] = 1;
522     sf = mysf;
523     }
524 greg 2.6 i = rmx_newtype(msum->dtype, madd->dtype);
525     if (i)
526     msum->dtype = i;
527     else
528     rmx_addinfo(msum, rmx_mismatch_warn);
529 greg 2.1 for (i = msum->nrows; i--; )
530     for (j = msum->ncols; j--; )
531     for (k = msum->ncomp; k--; )
532     rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k);
533    
534     free(mysf);
535     return(1);
536     }
537    
538     /* Scale the given matrix by the indicated scalar component vector */
539     int
540     rmx_scale(RMATRIX *rm, const double sf[])
541     {
542     int i, j, k;
543    
544     if ((rm == NULL) | (sf == NULL))
545     return(0);
546     for (i = rm->nrows; i--; )
547     for (j = rm->ncols; j--; )
548     for (k = rm->ncomp; k--; )
549     rmx_lval(rm,i,j,k) *= sf[k];
550    
551     return(1);
552     }
553    
554     /* Allocate new matrix and apply component transformation */
555     RMATRIX *
556     rmx_transform(const RMATRIX *msrc, int n, const double cmat[])
557     {
558     int i, j, ks, kd;
559     RMATRIX *dnew;
560    
561     if ((msrc == NULL) | (n <= 0) | (cmat == NULL))
562     return(NULL);
563     dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
564     if (dnew == NULL)
565     return(NULL);
566 greg 2.6 dnew->dtype = msrc->dtype;
567 greg 2.1 for (i = dnew->nrows; i--; )
568     for (j = dnew->ncols; j--; )
569     for (kd = dnew->ncomp; kd--; ) {
570     double d = 0;
571     for (ks = msrc->ncomp; ks--; )
572     d += cmat[kd*msrc->ncomp + ks] * rmx_lval(msrc,i,j,ks);
573     rmx_lval(dnew,i,j,kd) = d;
574     }
575     return(dnew);
576     }
577    
578     /* Convert a color matrix to newly allocated RMATRIX buffer */
579     RMATRIX *
580     rmx_from_cmatrix(const CMATRIX *cm)
581     {
582     int i, j;
583     RMATRIX *dnew;
584    
585     if (cm == NULL)
586     return(NULL);
587     dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
588     if (dnew == NULL)
589     return(NULL);
590 greg 2.6 dnew->dtype = DTfloat;
591 greg 2.1 for (i = dnew->nrows; i--; )
592     for (j = dnew->ncols; j--; ) {
593     const COLORV *cv = cm_lval(cm,i,j);
594     rmx_lval(dnew,i,j,0) = cv[0];
595     rmx_lval(dnew,i,j,1) = cv[1];
596     rmx_lval(dnew,i,j,2) = cv[2];
597     }
598     return(dnew);
599     }
600    
601     /* Convert general matrix to newly allocated CMATRIX buffer */
602     CMATRIX *
603     cm_from_rmatrix(const RMATRIX *rm)
604     {
605     int i, j;
606     CMATRIX *cnew;
607    
608     if (rm == NULL || rm->ncomp != 3)
609     return(NULL);
610     cnew = cm_alloc(rm->nrows, rm->ncols);
611     if (cnew == NULL)
612     return(NULL);
613     for (i = cnew->nrows; i--; )
614     for (j = cnew->ncols; j--; ) {
615     COLORV *cv = cm_lval(cnew,i,j);
616     cv[0] = (COLORV)rmx_lval(rm,i,j,0);
617     cv[1] = (COLORV)rmx_lval(rm,i,j,1);
618     cv[2] = (COLORV)rmx_lval(rm,i,j,2);
619     }
620     return(cnew);
621     }