ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
Revision: 2.12
Committed: Fri Jan 23 01:14:20 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +5 -7 lines
Log Message:
Fixed rmx_identity(), which was incorrect as first written but never used

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rmatrix.c,v 2.11 2014/12/16 20:21:46 greg Exp $";
3 #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 static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
16
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 dnew->dtype = DTdouble;
31 dnew->info = NULL;
32 return(dnew);
33 }
34
35 /* 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 /* 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 if (rm->info) rm->info[0] = '\0';
65 } else
66 rm->info = (char *)realloc(rm->info,
67 strlen(rm->info)+strlen(info)+1);
68 if (!rm->info)
69 return(0);
70 strcat(rm->info, info);
71 return(1);
72 }
73
74 static int
75 get_dminfo(char *s, void *p)
76 {
77 RMATRIX *ip = (RMATRIX *)p;
78 char fmt[64];
79 int i;
80
81 if (headidval(fmt, s))
82 return(0);
83 if (!strncmp(s, "NCOMP=", 6)) {
84 ip->ncomp = atoi(s+6);
85 return(0);
86 }
87 if (!strncmp(s, "NROWS=", 6)) {
88 ip->nrows = atoi(s+6);
89 return(0);
90 }
91 if (!strncmp(s, "NCOLS=", 6)) {
92 ip->ncols = atoi(s+6);
93 return(0);
94 }
95 if (!formatval(fmt, s)) {
96 rmx_addinfo(ip, s);
97 return(0);
98 }
99 for (i = 1; i < DTend; i++)
100 if (!strcmp(fmt, cm_fmt_id[i])) {
101 ip->dtype = i;
102 return(0);
103 }
104 return(-1);
105 }
106
107 static int
108 rmx_load_ascii(RMATRIX *rm, FILE *fp)
109 {
110 int i, j, k;
111 #ifdef _WIN32
112 _setmode(fileno(fp), _O_TEXT);
113 #endif
114 for (i = 0; i < rm->nrows; i++)
115 for (j = 0; j < rm->ncols; j++)
116 for (k = 0; k < rm->ncomp; k++)
117 if (fscanf(fp, "%lf", &rmx_lval(rm,i,j,k)) != 1)
118 return(0);
119 return(1);
120 }
121
122 static int
123 rmx_load_float(RMATRIX *rm, FILE *fp)
124 {
125 int i, j, k;
126 float val[100];
127
128 if (rm->ncomp > 100) {
129 fputs("Unsupported # components in rmx_load_float()\n", stderr);
130 exit(1);
131 }
132 for (i = 0; i < rm->nrows; i++)
133 for (j = 0; j < rm->ncols; j++) {
134 if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
135 return(0);
136 for (k = rm->ncomp; k--; )
137 rmx_lval(rm,i,j,k) = val[k];
138 }
139 return(1);
140 }
141
142 static int
143 rmx_load_double(RMATRIX *rm, FILE *fp)
144 {
145 int i, j, k;
146 double val[100];
147
148 if (rm->ncomp > 100) {
149 fputs("Unsupported # components in rmx_load_double()\n", stderr);
150 exit(1);
151 }
152 for (i = 0; i < rm->nrows; i++)
153 for (j = 0; j < rm->ncols; j++) {
154 if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
155 return(0);
156 for (k = rm->ncomp; k--; )
157 rmx_lval(rm,i,j,k) = val[k];
158 }
159 return(1);
160 }
161
162 static int
163 rmx_load_rgbe(RMATRIX *rm, FILE *fp)
164 {
165 COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
166 int i, j;
167
168 if (scan == NULL)
169 return(0);
170 for (i = 0; i < rm->nrows; i++) {
171 if (freadscan(scan, rm->ncols, fp) < 0) {
172 free(scan);
173 return(0);
174 }
175 for (j = rm->ncols; j--; ) {
176 rmx_lval(rm,i,j,0) = colval(scan[j],RED);
177 rmx_lval(rm,i,j,1) = colval(scan[j],GRN);
178 rmx_lval(rm,i,j,2) = colval(scan[j],BLU);
179 }
180 }
181 free(scan);
182 return(1);
183 }
184
185 /* Load matrix from supported file type */
186 RMATRIX *
187 rmx_load(const char *fname)
188 {
189 FILE *fp = stdin;
190 RMATRIX dinfo;
191 RMATRIX *dnew;
192
193 if (fname == NULL) { /* reading from stdin? */
194 fname = "<stdin>";
195 #ifdef _WIN32
196 _setmode(fileno(stdin), _O_BINARY);
197 #endif
198 } else {
199 const char *sp = fname; /* check suffix */
200 while (*sp)
201 ++sp;
202 while (sp > fname && sp[-1] != '.')
203 --sp;
204 if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */
205 CMATRIX *cm = cm_loadBTDF((char *)fname);
206 if (cm == NULL)
207 return(NULL);
208 dnew = rmx_from_cmatrix(cm);
209 cm_free(cm);
210 return(dnew);
211 }
212 /* else open it ourselves */
213 if ((fp = fopen(fname, "rb")) == NULL)
214 return(NULL);
215 }
216 #ifdef getc_unlocked
217 flockfile(fp);
218 #endif
219 dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0;
220 dinfo.dtype = DTascii; /* assumed w/o FORMAT */
221 dinfo.info = NULL;
222 if (getheader(fp, get_dminfo, &dinfo) < 0) {
223 fclose(fp);
224 return(NULL);
225 }
226 if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) {
227 if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) {
228 fclose(fp);
229 return(NULL);
230 }
231 if (dinfo.ncomp <= 0)
232 dinfo.ncomp = 3;
233 else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) &&
234 dinfo.ncomp != 3) {
235 fclose(fp);
236 return(NULL);
237 }
238 }
239 dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp);
240 if (dnew == NULL) {
241 fclose(fp);
242 return(NULL);
243 }
244 dnew->info = dinfo.info;
245 switch (dinfo.dtype) {
246 case DTascii:
247 if (!rmx_load_ascii(dnew, fp))
248 goto loaderr;
249 dnew->dtype = DTascii; /* should leave double? */
250 break;
251 case DTfloat:
252 if (!rmx_load_float(dnew, fp))
253 goto loaderr;
254 dnew->dtype = DTfloat;
255 break;
256 case DTdouble:
257 if (!rmx_load_double(dnew, fp))
258 goto loaderr;
259 dnew->dtype = DTdouble;
260 break;
261 case DTrgbe:
262 case DTxyze:
263 if (!rmx_load_rgbe(dnew, fp))
264 goto loaderr;
265 dnew->dtype = dinfo.dtype;
266 break;
267 default:
268 goto loaderr;
269 }
270 if (fp != stdin)
271 fclose(fp);
272 return(dnew);
273 loaderr: /* should report error? */
274 fclose(fp);
275 rmx_free(dnew);
276 return(NULL);
277 }
278
279 static int
280 rmx_write_ascii(const RMATRIX *rm, FILE *fp)
281 {
282 int i, j, k;
283 #ifdef _WIN32
284 _setmode(fileno(fp), _O_TEXT);
285 #endif
286 for (i = 0; i < rm->nrows; i++) {
287 for (j = 0; j < rm->ncols; j++) {
288 for (k = 0; k < rm->ncomp; k++)
289 fprintf(fp, " %.15e", rmx_lval(rm,i,j,k));
290 fputc('\t', fp);
291 }
292 fputc('\n', fp);
293 }
294 return(1);
295 }
296
297 static int
298 rmx_write_float(const RMATRIX *rm, FILE *fp)
299 {
300 int i, j, k;
301 float val[100];
302
303 if (rm->ncomp > 100) {
304 fputs("Unsupported # components in rmx_write_float()\n", stderr);
305 exit(1);
306 }
307 for (i = 0; i < rm->nrows; i++)
308 for (j = 0; j < rm->ncols; j++) {
309 for (k = rm->ncomp; k--; )
310 val[k] = (float)rmx_lval(rm,i,j,k);
311 if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
312 return(0);
313 }
314 return(1);
315 }
316
317 static int
318 rmx_write_double(const RMATRIX *rm, FILE *fp)
319 {
320 int i, j, k;
321 double val[100];
322
323 if (rm->ncomp > 100) {
324 fputs("Unsupported # components in rmx_write_double()\n", stderr);
325 exit(1);
326 }
327 for (i = 0; i < rm->nrows; i++)
328 for (j = 0; j < rm->ncols; j++) {
329 for (k = rm->ncomp; k--; )
330 val[k] = rmx_lval(rm,i,j,k);
331 if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
332 return(0);
333 }
334 return(1);
335 }
336
337 static int
338 rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
339 {
340 COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
341 int i, j;
342
343 if (scan == NULL)
344 return(0);
345 for (i = 0; i < rm->nrows; i++) {
346 for (j = rm->ncols; j--; )
347 setcolor(scan[j], rmx_lval(rm,i,j,0),
348 rmx_lval(rm,i,j,1),
349 rmx_lval(rm,i,j,2) );
350 if (fwritescan(scan, rm->ncols, fp) < 0) {
351 free(scan);
352 return(0);
353 }
354 }
355 free(scan);
356 return(1);
357 }
358
359 /* Write matrix to file type indicated by dtype */
360 int
361 rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
362 {
363 RMATRIX *mydm = NULL;
364 int ok = 1;
365
366 if ((rm == NULL) | (fp == NULL))
367 return(0);
368 /* complete header */
369 if (rm->info)
370 fputs(rm->info, fp);
371 if (dtype == DTfromHeader)
372 dtype = rm->dtype;
373 else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
374 dtype = DTxyze;
375 else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
376 dtype = DTrgbe;
377 if ((dtype != DTrgbe) & (dtype != DTxyze)) {
378 fprintf(fp, "NROWS=%d\n", rm->nrows);
379 fprintf(fp, "NCOLS=%d\n", rm->ncols);
380 fprintf(fp, "NCOMP=%d\n", rm->ncomp);
381 } else if (rm->ncomp != 3) { /* wrong # components? */
382 double cmtx[3];
383 if (rm->ncomp != 1) /* only convert grayscale */
384 return(0);
385 cmtx[0] = cmtx[1] = cmtx[2] = 1;
386 mydm = rmx_transform(rm, 3, cmtx);
387 if (mydm == NULL)
388 return(0);
389 rm = mydm;
390 }
391 fputformat((char *)cm_fmt_id[dtype], fp);
392 fputc('\n', fp);
393 switch (dtype) { /* write data */
394 case DTascii:
395 ok = rmx_write_ascii(rm, fp);
396 break;
397 case DTfloat:
398 ok = rmx_write_float(rm, fp);
399 break;
400 case DTdouble:
401 ok = rmx_write_double(rm, fp);
402 break;
403 case DTrgbe:
404 case DTxyze:
405 fprtresolu(rm->ncols, rm->nrows, fp);
406 ok = rmx_write_rgbe(rm, fp);
407 break;
408 default:
409 return(0);
410 }
411 ok &= (fflush(fp) == 0);
412 rmx_free(mydm);
413 return(ok);
414 }
415
416 /* Allocate and assign square identity matrix with n components */
417 RMATRIX *
418 rmx_identity(const int dim, const int n)
419 {
420 RMATRIX *rid = rmx_alloc(dim, dim, n);
421 int i, k;
422
423 if (rid == NULL)
424 return(NULL);
425 memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
426 for (i = dim; i--; )
427 for (k = n; k--; )
428 rmx_lval(rid,i,i,k) = 1;
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 rmx_addinfo(dnew, rm->info);
444 dnew->dtype = rm->dtype;
445 memcpy(dnew->mtx, rm->mtx,
446 sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
447 return(dnew);
448 }
449
450 /* Allocate and assign transposed matrix */
451 RMATRIX *
452 rmx_transpose(const RMATRIX *rm)
453 {
454 RMATRIX *dnew;
455 int i, j, k;
456
457 if (rm == NULL)
458 return(0);
459 dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
460 if (dnew == NULL)
461 return(NULL);
462 if (rm->info) {
463 rmx_addinfo(dnew, rm->info);
464 rmx_addinfo(dnew, "Transposed rows and columns\n");
465 }
466 dnew->dtype = rm->dtype;
467 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 }
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 i = rmx_newtype(m1->dtype, m2->dtype);
488 if (i)
489 mres->dtype = i;
490 else
491 rmx_addinfo(mres, rmx_mismatch_warn);
492 for (i = mres->nrows; i--; )
493 for (j = mres->ncols; j--; )
494 for (k = mres->ncomp; k--; ) {
495 long double d = 0;
496 for (h = m1->ncols; h--; )
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 i = rmx_newtype(msum->dtype, madd->dtype);
525 if (i)
526 msum->dtype = i;
527 else
528 rmx_addinfo(msum, rmx_mismatch_warn);
529 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 dnew->dtype = msrc->dtype;
567 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 dnew->dtype = DTfloat;
591 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 }