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.3 by greg, Tue Jul 8 16:39:41 2014 UTC vs.
Revision 2.17 by greg, Wed Jul 22 04:47:51 2015 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   #include <string.h>
11   #include <fcntl.h>
12   #include "resolu.h"
13 + #include "rtprocess.h"
14   #include "rmatrix.h"
15  
16 < typedef struct {
16 <        int     nrows, ncols, ncomp;
17 <        int     dtype;
18 < } DMINFO;
16 > static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
17  
18   /* Allocate a nr x nc matrix with n components */
19   RMATRIX *
# Line 30 | Line 28 | rmx_alloc(int nr, int nc, int n)
28          if (dnew == NULL)
29                  return(NULL);
30          dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
31 +        dnew->dtype = DTdouble;
32 +        dnew->info = NULL;
33          return(dnew);
34   }
35  
36 + /* Free a RMATRIX array */
37 + void
38 + rmx_free(RMATRIX *rm)
39 + {
40 +        if (!rm) return;
41 +        if (rm->info)
42 +                free(rm->info);
43 +        free(rm);
44 + }
45 +
46 + /* Resolve data type based on two input types (returns 0 for mismatch) */
47 + int
48 + rmx_newtype(int dtyp1, int dtyp2)
49 + {
50 +        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
51 +                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
52 +                        && dtyp1 != dtyp2)
53 +                return(0);
54 +        if (dtyp1 < dtyp2)
55 +                return(dtyp1);
56 +        return(dtyp2);
57 + }
58 +
59 + /* Append header information associated with matrix data */
60 + int
61 + rmx_addinfo(RMATRIX *rm, const char *info)
62 + {
63 +        if (!info || !*info)
64 +                return(0);
65 +        if (!rm->info) {
66 +                rm->info = (char *)malloc(strlen(info)+1);
67 +                if (rm->info) rm->info[0] = '\0';
68 +        } else
69 +                rm->info = (char *)realloc(rm->info,
70 +                                strlen(rm->info)+strlen(info)+1);
71 +        if (!rm->info)
72 +                return(0);
73 +        strcat(rm->info, info);
74 +        return(1);
75 + }
76 +
77   static int
78   get_dminfo(char *s, void *p)
79   {
80 <        DMINFO  *ip = (DMINFO *)p;
81 <        char    fmt[32];
80 >        RMATRIX *ip = (RMATRIX *)p;
81 >        char    fmt[64];
82          int     i;
83  
84 +        if (headidval(fmt, s))
85 +                return(0);
86          if (!strncmp(s, "NCOMP=", 6)) {
87                  ip->ncomp = atoi(s+6);
88                  return(0);
# Line 52 | Line 95 | get_dminfo(char *s, void *p)
95                  ip->ncols = atoi(s+6);
96                  return(0);
97          }
98 <        if (!formatval(fmt, s))
98 >        if (!formatval(fmt, s)) {
99 >                rmx_addinfo(ip, s);
100                  return(0);
101 +        }
102          for (i = 1; i < DTend; i++)
103                  if (!strcmp(fmt, cm_fmt_id[i])) {
104                          ip->dtype = i;
# Line 66 | Line 111 | static int
111   rmx_load_ascii(RMATRIX *rm, FILE *fp)
112   {
113          int     i, j, k;
114 < #ifdef _WIN32
70 <        _setmode(fileno(fp), _O_TEXT);
71 < #endif
114 >
115          for (i = 0; i < rm->nrows; i++)
116              for (j = 0; j < rm->ncols; j++)
117                  for (k = 0; k < rm->ncomp; k++)
# Line 142 | Line 185 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
185  
186   /* Load matrix from supported file type */
187   RMATRIX *
188 < rmx_load(const char *fname)
188 > rmx_load(const char *inspec)
189   {
190          FILE            *fp = stdin;
191 <        DMINFO          dinfo;
191 >        RMATRIX         dinfo;
192          RMATRIX         *dnew;
193  
194 <        if (fname == NULL) {                    /* reading from stdin? */
195 <                fname = "<stdin>";
194 >        if (inspec == NULL) {                   /* reading from stdin? */
195 >                inspec = "<stdin>";
196 > #ifdef _WIN32
197 >                _setmode(fileno(stdin), _O_BINARY);
198 > #endif
199 >        } else if (inspec[0] == '!') {
200 >                if ((fp = popen(inspec+1, "r")) == NULL)
201 >                        return(NULL);
202 > #ifdef _WIN32
203 >                _setmode(fileno(fp), _O_BINARY);
204 > #endif
205          } else {
206 <                const char      *sp = fname;    /* check suffix */
206 >                const char      *sp = inspec;   /* check suffix */
207                  while (*sp)
208                          ++sp;
209 <                while (sp > fname && sp[-1] != '.')
209 >                while (sp > inspec && sp[-1] != '.')
210                          --sp;
211                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
212 <                        CMATRIX *cm = cm_loadBTDF((char *)fname);
212 >                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
213                          if (cm == NULL)
214                                  return(NULL);
215                          dnew = rmx_from_cmatrix(cm);
# Line 165 | Line 217 | rmx_load(const char *fname)
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;
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 + #ifdef _WIN32
255 +                _setmode(fileno(fp), _O_TEXT);
256 + #endif
257                  if (!rmx_load_ascii(dnew, fp))
258                          goto loaderr;
259 +                dnew->dtype = DTascii;          /* should leave double? */
260                  break;
261          case DTfloat:
262                  if (!rmx_load_float(dnew, fp))
263                          goto loaderr;
264 +                dnew->dtype = DTfloat;
265                  break;
266          case DTdouble:
267                  if (!rmx_load_double(dnew, fp))
268                          goto loaderr;
269 +                dnew->dtype = DTdouble;
270                  break;
271          case DTrgbe:
272          case DTxyze:
273                  if (!rmx_load_rgbe(dnew, fp))
274                          goto loaderr;
275 +                dnew->dtype = dinfo.dtype;
276                  break;
277          default:
278                  goto loaderr;
279          }
280 <        if (fp != stdin)
281 <                fclose(fp);
280 >        if (fp != stdin) {
281 >                if (inspec[0] == '!')
282 >                        pclose(fp);
283 >                else
284 >                        fclose(fp);
285 >        }
286 > #ifdef getc_unlocked
287 >        else
288 >                funlockfile(fp);
289 > #endif
290          return(dnew);
291   loaderr:                                        /* should report error? */
292 <        fclose(fp);
292 >        if (inspec[0] == '!')
293 >                pclose(fp);
294 >        else
295 >                fclose(fp);
296          rmx_free(dnew);
297          return(NULL);
298   }
# Line 223 | Line 301 | static int
301   rmx_write_ascii(const RMATRIX *rm, FILE *fp)
302   {
303          int     i, j, k;
304 < #ifdef _WIN32
227 <        _setmode(fileno(fp), _O_TEXT);
228 < #endif
304 >
305          for (i = 0; i < rm->nrows; i++) {
306              for (j = 0; j < rm->ncols; j++) {
307                  for (k = 0; k < rm->ncomp; k++)
# Line 299 | Line 375 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
375          return(1);
376   }
377  
378 < /* Write matrix to file type indicated by dt */
379 < long
378 > /* Write matrix to file type indicated by dtype */
379 > int
380   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
381   {
382          RMATRIX *mydm = NULL;
# Line 308 | Line 384 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
384  
385          if ((rm == NULL) | (fp == NULL))
386                  return(0);
387 + #ifdef getc_unlocked
388 +        flockfile(fp);
389 + #endif
390                                                  /* complete header */
391 +        if (rm->info)
392 +                fputs(rm->info, fp);
393 +        if (dtype == DTfromHeader)
394 +                dtype = rm->dtype;
395 +        else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
396 +                dtype = DTxyze;
397 +        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
398 +                dtype = DTrgbe;
399          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
400                  fprintf(fp, "NROWS=%d\n", rm->nrows);
401                  fprintf(fp, "NCOLS=%d\n", rm->ncols);
# Line 344 | Line 431 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
431                  return(0);
432          }
433          ok &= (fflush(fp) == 0);
434 + #ifdef getc_unlocked
435 +        funlockfile(fp);
436 + #endif
437          rmx_free(mydm);
438 <        return(ftell(fp) * ok);         /* return # bytes written */
438 >        return(ok);
439   }
440  
441   /* Allocate and assign square identity matrix with n components */
# Line 353 | Line 443 | RMATRIX *
443   rmx_identity(const int dim, const int n)
444   {
445          RMATRIX *rid = rmx_alloc(dim, dim, n);
446 <        int     i;
446 >        int     i, k;
447  
448          if (rid == NULL)
449                  return(NULL);
450 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
450 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
451          for (i = dim; i--; )
452 <                rmx_lval(rid,i,i,0) = 1;
453 <        for (i = n; --i; )
364 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
365 <                                sizeof(rid->mtx[0])*dim*dim);
452 >            for (k = n; k--; )
453 >                rmx_lval(rid,i,i,k) = 1;
454          return(rid);
455   }
456  
# Line 377 | Line 465 | rmx_copy(const RMATRIX *rm)
465          dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
466          if (dnew == NULL)
467                  return(NULL);
468 +        rmx_addinfo(dnew, rm->info);
469 +        dnew->dtype = rm->dtype;
470          memcpy(dnew->mtx, rm->mtx,
471                  sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
472          return(dnew);
# Line 394 | Line 484 | rmx_transpose(const RMATRIX *rm)
484          dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
485          if (dnew == NULL)
486                  return(NULL);
487 +        if (rm->info) {
488 +                rmx_addinfo(dnew, rm->info);
489 +                rmx_addinfo(dnew, "Transposed rows and columns\n");
490 +        }
491 +        dnew->dtype = rm->dtype;
492          for (i = dnew->nrows; i--; )
493              for (j = dnew->ncols; j--; )
494                  for (k = dnew->ncomp; k--; )
# Line 414 | Line 509 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
509          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
510          if (mres == NULL)
511                  return(NULL);
512 +        i = rmx_newtype(m1->dtype, m2->dtype);
513 +        if (i)
514 +                mres->dtype = i;
515 +        else
516 +                rmx_addinfo(mres, rmx_mismatch_warn);
517          for (i = mres->nrows; i--; )
518              for (j = mres->ncols; j--; )
519 <                for (h = m1->ncols; h--; ) {
519 >                for (k = mres->ncomp; k--; ) {
520                      long double d = 0;
521 <                    for (k = mres->ncomp; k--; )
522 <                        d += (long double)rmx_lval(m1,i,h,k) *
423 <                                (long double)rmx_lval(m2,h,j,k);
521 >                    for (h = m1->ncols; h--; )
522 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
523                      rmx_lval(mres,i,j,k) = (double)d;
524                  }
525          return(mres);
# Line 446 | Line 545 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
545                          mysf[k] = 1;
546                  sf = mysf;
547          }
548 +        i = rmx_newtype(msum->dtype, madd->dtype);
549 +        if (i)
550 +                msum->dtype = i;
551 +        else
552 +                rmx_addinfo(msum, rmx_mismatch_warn);
553          for (i = msum->nrows; i--; )
554              for (j = msum->ncols; j--; )
555                  for (k = msum->ncomp; k--; )
# Line 483 | Line 587 | rmx_transform(const RMATRIX *msrc, int n, const double
587          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
588          if (dnew == NULL)
589                  return(NULL);
590 +        dnew->dtype = msrc->dtype;
591          for (i = dnew->nrows; i--; )
592              for (j = dnew->ncols; j--; )
593                  for (kd = dnew->ncomp; kd--; ) {
# Line 506 | Line 611 | rmx_from_cmatrix(const CMATRIX *cm)
611          dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
612          if (dnew == NULL)
613                  return(NULL);
614 +        dnew->dtype = DTfloat;
615          for (i = dnew->nrows; i--; )
616              for (j = dnew->ncols; j--; ) {
617                  const COLORV    *cv = cm_lval(cm,i,j);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines