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.8 by greg, Wed Aug 27 13:33:47 2014 UTC vs.
Revision 2.18 by greg, Thu Aug 27 04:07:05 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   static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
# Line 46 | Line 47 | rmx_free(RMATRIX *rm)
47   int
48   rmx_newtype(int dtyp1, int dtyp2)
49   {
50 <        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2)
50 >        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
51 >                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
52 >                        && dtyp1 != dtyp2)
53                  return(0);
54          if (dtyp1 < dtyp2)
55                  return(dtyp1);
# Line 108 | Line 111 | static int
111   rmx_load_ascii(RMATRIX *rm, FILE *fp)
112   {
113          int     i, j, k;
114 < #ifdef _WIN32
112 <        _setmode(fileno(fp), _O_TEXT);
113 < #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 184 | 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          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);
216                          cm_free(cm);
217 +                        dnew->dtype = DTascii;
218                          return(dnew);
219                  }
220                                                  /* else open it ourselves */
221 <                if ((fp = fopen(fname, "rb")) == NULL)
221 >                if ((fp = fopen(inspec, "rb")) == NULL)
222                          return(NULL);
223          }
224   #ifdef getc_unlocked
# Line 244 | Line 252 | rmx_load(const char *fname)
252          dnew->info = dinfo.info;
253          switch (dinfo.dtype) {
254          case DTascii:
255 + #ifdef _WIN32
256 +                _setmode(fileno(fp), _O_TEXT);
257 + #endif
258                  if (!rmx_load_ascii(dnew, fp))
259                          goto loaderr;
260 +                dnew->dtype = DTascii;          /* should leave double? */
261                  break;
262          case DTfloat:
263                  if (!rmx_load_float(dnew, fp))
# Line 266 | Line 278 | rmx_load(const char *fname)
278          default:
279                  goto loaderr;
280          }
281 <        if (fp != stdin)
282 <                fclose(fp);
281 >        if (fp != stdin) {
282 >                if (inspec[0] == '!')
283 >                        pclose(fp);
284 >                else
285 >                        fclose(fp);
286 >        }
287 > #ifdef getc_unlocked
288 >        else
289 >                funlockfile(fp);
290 > #endif
291          return(dnew);
292   loaderr:                                        /* should report error? */
293 <        fclose(fp);
293 >        if (inspec[0] == '!')
294 >                pclose(fp);
295 >        else
296 >                fclose(fp);
297          rmx_free(dnew);
298          return(NULL);
299   }
# Line 279 | Line 302 | static int
302   rmx_write_ascii(const RMATRIX *rm, FILE *fp)
303   {
304          int     i, j, k;
305 < #ifdef _WIN32
283 <        _setmode(fileno(fp), _O_TEXT);
284 < #endif
305 >
306          for (i = 0; i < rm->nrows; i++) {
307              for (j = 0; j < rm->ncols; j++) {
308                  for (k = 0; k < rm->ncomp; k++)
# Line 356 | Line 377 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
377   }
378  
379   /* Write matrix to file type indicated by dtype */
380 < long
380 > int
381   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
382   {
383          RMATRIX *mydm = NULL;
# Line 364 | Line 385 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
385  
386          if ((rm == NULL) | (fp == NULL))
387                  return(0);
388 + #ifdef getc_unlocked
389 +        flockfile(fp);
390 + #endif
391                                                  /* complete header */
392          if (rm->info)
393                  fputs(rm->info, fp);
# Line 371 | Line 395 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
395                  dtype = rm->dtype;
396          else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
397                  dtype = DTxyze;
398 <        else if ((dtype = DTxyze) & (rm->dtype == DTrgbe))
398 >        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
399                  dtype = DTrgbe;
400          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
401                  fprintf(fp, "NROWS=%d\n", rm->nrows);
# Line 408 | Line 432 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
432                  return(0);
433          }
434          ok &= (fflush(fp) == 0);
435 + #ifdef getc_unlocked
436 +        funlockfile(fp);
437 + #endif
438          rmx_free(mydm);
439 <        return(ftell(fp) * ok);         /* return # bytes written */
439 >        return(ok);
440   }
441  
442   /* Allocate and assign square identity matrix with n components */
# Line 417 | Line 444 | RMATRIX *
444   rmx_identity(const int dim, const int n)
445   {
446          RMATRIX *rid = rmx_alloc(dim, dim, n);
447 <        int     i;
447 >        int     i, k;
448  
449          if (rid == NULL)
450                  return(NULL);
451 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
451 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
452          for (i = dim; i--; )
453 <                rmx_lval(rid,i,i,0) = 1;
454 <        for (i = n; --i; )
428 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
429 <                                sizeof(rid->mtx[0])*dim*dim);
453 >            for (k = n; k--; )
454 >                rmx_lval(rid,i,i,k) = 1;
455          return(rid);
456   }
457  
# Line 495 | Line 520 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
520                  for (k = mres->ncomp; k--; ) {
521                      long double d = 0;
522                      for (h = m1->ncols; h--; )
523 <                        d += (long double)rmx_lval(m1,i,h,k) *
499 <                                (long double)rmx_lval(m2,h,j,k);
523 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
524                      rmx_lval(mres,i,j,k) = (double)d;
525                  }
526          return(mres);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines