ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
(Generate patch)

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.1 by greg, Sat May 31 05:02:37 2014 UTC vs.
Revision 2.25 by greg, Mon Aug 28 15:59:46 2017 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   #include <stdlib.h>
10   #include <string.h>
11   #include <fcntl.h>
12 + #include <errno.h>
13 + #include "rtio.h"
14 + #include "platform.h"
15   #include "resolu.h"
16 + #include "paths.h"
17   #include "rmatrix.h"
18  
19 < typedef struct {
16 <        int     nrows, ncols, ncomp;
17 <        int     dtype;
18 < } DMINFO;
19 > static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
20  
21   /* Allocate a nr x nc matrix with n components */
22   RMATRIX *
# Line 30 | Line 31 | rmx_alloc(int nr, int nc, int n)
31          if (dnew == NULL)
32                  return(NULL);
33          dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
34 +        dnew->dtype = DTdouble;
35 +        dnew->info = NULL;
36          return(dnew);
37   }
38  
39 + /* Free a RMATRIX array */
40 + void
41 + rmx_free(RMATRIX *rm)
42 + {
43 +        if (!rm) return;
44 +        if (rm->info)
45 +                free(rm->info);
46 +        free(rm);
47 + }
48 +
49 + /* Resolve data type based on two input types (returns 0 for mismatch) */
50 + int
51 + rmx_newtype(int dtyp1, int dtyp2)
52 + {
53 +        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
54 +                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
55 +                        && dtyp1 != dtyp2)
56 +                return(0);
57 +        if (dtyp1 < dtyp2)
58 +                return(dtyp1);
59 +        return(dtyp2);
60 + }
61 +
62 + /* Append header information associated with matrix data */
63 + int
64 + rmx_addinfo(RMATRIX *rm, const char *info)
65 + {
66 +        if (!info || !*info)
67 +                return(0);
68 +        if (!rm->info) {
69 +                rm->info = (char *)malloc(strlen(info)+1);
70 +                if (rm->info) rm->info[0] = '\0';
71 +        } else
72 +                rm->info = (char *)realloc(rm->info,
73 +                                strlen(rm->info)+strlen(info)+1);
74 +        if (!rm->info)
75 +                return(0);
76 +        strcat(rm->info, info);
77 +        return(1);
78 + }
79 +
80   static int
81   get_dminfo(char *s, void *p)
82   {
83 <        DMINFO  *ip = (DMINFO *)p;
84 <        char    fmt[32];
83 >        RMATRIX *ip = (RMATRIX *)p;
84 >        char    fmt[64];
85          int     i;
86  
87 +        if (headidval(fmt, s))
88 +                return(0);
89          if (!strncmp(s, "NCOMP=", 6)) {
90                  ip->ncomp = atoi(s+6);
91                  return(0);
# Line 52 | Line 98 | get_dminfo(char *s, void *p)
98                  ip->ncols = atoi(s+6);
99                  return(0);
100          }
101 <        if (!formatval(fmt, s))
101 >        if (!formatval(fmt, s)) {
102 >                rmx_addinfo(ip, s);
103                  return(0);
104 +        }
105          for (i = 1; i < DTend; i++)
106                  if (!strcmp(fmt, cm_fmt_id[i])) {
107                          ip->dtype = i;
# Line 66 | Line 114 | static int
114   rmx_load_ascii(RMATRIX *rm, FILE *fp)
115   {
116          int     i, j, k;
117 < #ifdef _WIN32
70 <        _setmode(fileno(fp), _O_TEXT);
71 < #endif
117 >
118          for (i = 0; i < rm->nrows; i++)
119              for (j = 0; j < rm->ncols; j++)
120                  for (k = 0; k < rm->ncomp; k++)
# Line 89 | Line 135 | rmx_load_float(RMATRIX *rm, FILE *fp)
135          }
136          for (i = 0; i < rm->nrows; i++)
137              for (j = 0; j < rm->ncols; j++) {
138 <                if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
138 >                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
139                      return(0);
140                  for (k = rm->ncomp; k--; )
141                       rmx_lval(rm,i,j,k) = val[k];
# Line 109 | Line 155 | rmx_load_double(RMATRIX *rm, FILE *fp)
155          }
156          for (i = 0; i < rm->nrows; i++)
157              for (j = 0; j < rm->ncols; j++) {
158 <                if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
158 >                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
159                      return(0);
160                  for (k = rm->ncomp; k--; )
161                       rmx_lval(rm,i,j,k) = val[k];
# Line 142 | Line 188 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
188  
189   /* Load matrix from supported file type */
190   RMATRIX *
191 < rmx_load(const char *fname)
191 > rmx_load(const char *inspec)
192   {
193          FILE            *fp = stdin;
194 <        DMINFO          dinfo;
194 >        RMATRIX         dinfo;
195          RMATRIX         *dnew;
196  
197 <        if (fname == NULL) {                    /* reading from stdin? */
198 <                fname = "<stdin>";
197 >        if (inspec == NULL) {                   /* reading from stdin? */
198 >                inspec = "<stdin>";
199 >                SET_FILE_BINARY(stdin);
200 >        } else if (inspec[0] == '!') {
201 >                if ((fp = popen(inspec+1, "r")) == NULL)
202 >                        return(NULL);
203 >                SET_FILE_BINARY(stdin);
204          } else {
205 <                const char      *sp = fname;    /* check suffix */
205 >                const char      *sp = inspec;   /* check suffix */
206                  while (*sp)
207                          ++sp;
208 <                while (sp > fname && sp[-1] != '.')
208 >                while (sp > inspec && sp[-1] != '.')
209                          --sp;
210                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
211 <                        CMATRIX *cm = cm_loadBTDF((char *)fname);
211 >                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
212                          if (cm == NULL)
213                                  return(NULL);
214                          dnew = rmx_from_cmatrix(cm);
215                          cm_free(cm);
216 +                        dnew->dtype = DTascii;
217                          return(dnew);
218                  }
219                                                  /* else open it ourselves */
220 <                if ((fp = fopen(fname, "rb")) == NULL)
220 >                if ((fp = fopen(inspec, "rb")) == NULL)
221                          return(NULL);
222          }
223   #ifdef getc_unlocked
224          flockfile(fp);
225   #endif
226          dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0;
227 <        dinfo.dtype = DTascii;
228 <        if (getheader(fp, &get_dminfo, &dinfo) < 0) {
227 >        dinfo.dtype = DTascii;                  /* assumed w/o FORMAT */
228 >        dinfo.info = NULL;
229 >        if (getheader(fp, get_dminfo, &dinfo) < 0) {
230                  fclose(fp);
231                  return(NULL);
232          }
233 <        if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze)) {
233 >        if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) {
234                  if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) {
235                          fclose(fp);
236                          return(NULL);
237                  }
238 <                dinfo.ncomp = 3;
238 >                if (dinfo.ncomp <= 0)
239 >                        dinfo.ncomp = 3;
240 >                else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) &&
241 >                                dinfo.ncomp != 3) {
242 >                        fclose(fp);
243 >                        return(NULL);
244 >                }
245          }
246          dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp);
247          if (dnew == NULL) {
248                  fclose(fp);
249                  return(NULL);
250          }
251 +        dnew->info = dinfo.info;
252          switch (dinfo.dtype) {
253          case DTascii:
254 +                SET_FILE_TEXT(stdin);
255                  if (!rmx_load_ascii(dnew, fp))
256                          goto loaderr;
257 +                dnew->dtype = DTascii;          /* should leave double? */
258                  break;
259          case DTfloat:
260                  if (!rmx_load_float(dnew, fp))
261                          goto loaderr;
262 +                dnew->dtype = DTfloat;
263                  break;
264          case DTdouble:
265                  if (!rmx_load_double(dnew, fp))
266                          goto loaderr;
267 +                dnew->dtype = DTdouble;
268                  break;
269          case DTrgbe:
270          case DTxyze:
271                  if (!rmx_load_rgbe(dnew, fp))
272                          goto loaderr;
273 +                dnew->dtype = dinfo.dtype;
274                  break;
275          default:
276                  goto loaderr;
277          }
278 <        if (fp != stdin)
279 <                fclose(fp);
278 >        if (fp != stdin) {
279 >                if (inspec[0] == '!')
280 >                        pclose(fp);
281 >                else
282 >                        fclose(fp);
283 >        }
284 > #ifdef getc_unlocked
285 >        else
286 >                funlockfile(fp);
287 > #endif
288          return(dnew);
289   loaderr:                                        /* should report error? */
290 <        fclose(fp);
290 >        if (inspec[0] == '!')
291 >                pclose(fp);
292 >        else
293 >                fclose(fp);
294          rmx_free(dnew);
295          return(NULL);
296   }
# Line 223 | Line 299 | static int
299   rmx_write_ascii(const RMATRIX *rm, FILE *fp)
300   {
301          int     i, j, k;
302 < #ifdef _WIN32
227 <        _setmode(fileno(fp), _O_TEXT);
228 < #endif
302 >
303          for (i = 0; i < rm->nrows; i++) {
304              for (j = 0; j < rm->ncols; j++) {
305                  for (k = 0; k < rm->ncomp; k++)
# Line 251 | Line 325 | rmx_write_float(const RMATRIX *rm, FILE *fp)
325              for (j = 0; j < rm->ncols; j++) {
326                  for (k = rm->ncomp; k--; )
327                      val[k] = (float)rmx_lval(rm,i,j,k);
328 <                if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
328 >                if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
329                          return(0);
330              }
331          return(1);
# Line 271 | Line 345 | rmx_write_double(const RMATRIX *rm, FILE *fp)
345              for (j = 0; j < rm->ncols; j++) {
346                  for (k = rm->ncomp; k--; )
347                      val[k] = rmx_lval(rm,i,j,k);
348 <                if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
348 >                if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
349                          return(0);
350              }
351          return(1);
# Line 299 | Line 373 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
373          return(1);
374   }
375  
376 < /* Write matrix to file type indicated by dt */
377 < long
376 > /* Write matrix to file type indicated by dtype */
377 > int
378   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
379   {
380          RMATRIX *mydm = NULL;
# Line 308 | Line 382 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
382  
383          if ((rm == NULL) | (fp == NULL))
384                  return(0);
385 + #ifdef getc_unlocked
386 +        flockfile(fp);
387 + #endif
388                                                  /* complete header */
389 +        if (rm->info)
390 +                fputs(rm->info, fp);
391 +        if (dtype == DTfromHeader)
392 +                dtype = rm->dtype;
393 +        else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
394 +                dtype = DTxyze;
395 +        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
396 +                dtype = DTrgbe;
397          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
398                  fprintf(fp, "NROWS=%d\n", rm->nrows);
399                  fprintf(fp, "NCOLS=%d\n", rm->ncols);
# Line 344 | Line 429 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
429                  return(0);
430          }
431          ok &= (fflush(fp) == 0);
432 + #ifdef getc_unlocked
433 +        funlockfile(fp);
434 + #endif
435          rmx_free(mydm);
436 <        return(ftell(fp) * ok);         /* return # bytes written */
436 >        return(ok);
437   }
438  
439   /* Allocate and assign square identity matrix with n components */
# Line 353 | Line 441 | RMATRIX *
441   rmx_identity(const int dim, const int n)
442   {
443          RMATRIX *rid = rmx_alloc(dim, dim, n);
444 <        int     i;
444 >        int     i, k;
445  
446          if (rid == NULL)
447                  return(NULL);
448 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
448 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
449          for (i = dim; i--; )
450 <                rmx_lval(rid,i,i,0) = 1;
451 <        for (i = n; --i; )
364 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
365 <                                sizeof(rid->mtx[0])*dim*dim);
450 >            for (k = n; k--; )
451 >                rmx_lval(rid,i,i,k) = 1;
452          return(rid);
453   }
454  
# Line 377 | Line 463 | rmx_copy(const RMATRIX *rm)
463          dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
464          if (dnew == NULL)
465                  return(NULL);
466 +        rmx_addinfo(dnew, rm->info);
467 +        dnew->dtype = rm->dtype;
468          memcpy(dnew->mtx, rm->mtx,
469                  sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
470          return(dnew);
471   }
472  
473 < /* Swap rows and columns in the given matrix in situ */
474 < int
475 < rmx_transpose(RMATRIX *rm)
473 > /* Allocate and assign transposed matrix */
474 > RMATRIX *
475 > rmx_transpose(const RMATRIX *rm)
476   {
477 <        RMATRIX dswap;
477 >        RMATRIX *dnew;
478          int     i, j, k;
479  
480          if (rm == NULL)
481                  return(0);
482 <        dswap.nrows = rm->ncols;
483 <        dswap.ncols = rm->nrows;
484 <        dswap.ncomp = rm->ncomp;
485 <        for (i = 1; i < rm->nrows; i++)
486 <            for (j = 0; j < i; j++)
487 <                for (k = rm->ncomp; k--; ) {
488 <                        double  *opp = rm->mtx + rmx_indx(&dswap,j,i,k);
489 <                        double  d = *opp;
490 <                        *opp = rmx_lval(rm,i,j,k);
491 <                        rmx_lval(rm,i,j,k) = d;
492 <                }
493 <        rm->nrows = dswap.nrows;
494 <        rm->ncols = dswap.ncols;
407 <        return(1);
482 >        dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
483 >        if (dnew == NULL)
484 >                return(NULL);
485 >        if (rm->info) {
486 >                rmx_addinfo(dnew, rm->info);
487 >                rmx_addinfo(dnew, "Transposed rows and columns\n");
488 >        }
489 >        dnew->dtype = rm->dtype;
490 >        for (i = dnew->nrows; i--; )
491 >            for (j = dnew->ncols; j--; )
492 >                for (k = dnew->ncomp; k--; )
493 >                        rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k);
494 >        return(dnew);
495   }
496  
497   /* Multiply (concatenate) two matrices and allocate the result */
# Line 420 | Line 507 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
507          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
508          if (mres == NULL)
509                  return(NULL);
510 +        i = rmx_newtype(m1->dtype, m2->dtype);
511 +        if (i)
512 +                mres->dtype = i;
513 +        else
514 +                rmx_addinfo(mres, rmx_mismatch_warn);
515          for (i = mres->nrows; i--; )
516              for (j = mres->ncols; j--; )
517 <                for (h = m1->ncols; h--; ) {
517 >                for (k = mres->ncomp; k--; ) {
518                      long double d = 0;
519 <                    for (k = mres->ncomp; k--; )
520 <                        d += (long double)rmx_lval(m1,i,h,k) *
429 <                                (long double)rmx_lval(m2,h,j,k);
519 >                    for (h = m1->ncols; h--; )
520 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
521                      rmx_lval(mres,i,j,k) = (double)d;
522                  }
523          return(mres);
524   }
525  
526 + /* Element-wise multiplication (or division) of m2 into m1 */
527 + int
528 + rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide)
529 + {
530 +        int     zeroDivides = 0;
531 +        int     i, j, k;
532 +
533 +        if ((m1 == NULL) | (m2 == NULL) ||
534 +                        (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
535 +                return(0);
536 +        if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
537 +                return(0);
538 +        i = rmx_newtype(m1->dtype, m2->dtype);
539 +        if (i)
540 +                m1->dtype = i;
541 +        else
542 +                rmx_addinfo(m1, rmx_mismatch_warn);
543 +        for (i = m1->nrows; i--; )
544 +            for (j = m1->ncols; j--; )
545 +                if (divide) {
546 +                    double      d;
547 +                    if (m2->ncomp == 1) {
548 +                        d = rmx_lval(m2,i,j,0);
549 +                        if (d == 0) {
550 +                            ++zeroDivides;
551 +                            for (k = m1->ncomp; k--; )
552 +                                rmx_lval(m1,i,j,k) = 0;
553 +                        } else {
554 +                            d = 1./d;
555 +                            for (k = m1->ncomp; k--; )
556 +                                rmx_lval(m1,i,j,k) *= d;
557 +                        }
558 +                    } else
559 +                        for (k = m1->ncomp; k--; ) {
560 +                            d = rmx_lval(m2,i,j,k);
561 +                            if (d == 0) {
562 +                                ++zeroDivides;
563 +                                rmx_lval(m1,i,j,k) = 0;
564 +                            } else
565 +                                rmx_lval(m1,i,j,k) /= d;
566 +                        }
567 +                } else {
568 +                    if (m2->ncomp == 1) {
569 +                        const double    d = rmx_lval(m2,i,j,0);
570 +                        for (k = m1->ncomp; k--; )
571 +                            rmx_lval(m1,i,j,k) *= d;
572 +                    } else
573 +                        for (k = m1->ncomp; k--; )
574 +                            rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k);
575 +                }
576 +        if (zeroDivides) {
577 +                fputs("Divide by zero in rmx_elemult()\n", stderr);
578 +                errno = ERANGE;
579 +        }
580 +        return(1);
581 + }
582 +
583   /* Sum second matrix into first, applying scale factor beforehand */
584   int
585   rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[])
# Line 452 | Line 600 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
600                          mysf[k] = 1;
601                  sf = mysf;
602          }
603 +        i = rmx_newtype(msum->dtype, madd->dtype);
604 +        if (i)
605 +                msum->dtype = i;
606 +        else
607 +                rmx_addinfo(msum, rmx_mismatch_warn);
608          for (i = msum->nrows; i--; )
609              for (j = msum->ncols; j--; )
610                  for (k = msum->ncomp; k--; )
# Line 489 | Line 642 | rmx_transform(const RMATRIX *msrc, int n, const double
642          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
643          if (dnew == NULL)
644                  return(NULL);
645 +        dnew->dtype = msrc->dtype;
646          for (i = dnew->nrows; i--; )
647              for (j = dnew->ncols; j--; )
648                  for (kd = dnew->ncomp; kd--; ) {
# Line 512 | Line 666 | rmx_from_cmatrix(const CMATRIX *cm)
666          dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
667          if (dnew == NULL)
668                  return(NULL);
669 +        dnew->dtype = DTfloat;
670          for (i = dnew->nrows; i--; )
671              for (j = dnew->ncols; j--; ) {
672                  const COLORV    *cv = cm_lval(cm,i,j);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines