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.24 by greg, Tue Aug 30 15:11:22 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines