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

Comparing ray/src/util/rmtxop.c (file contents):
Revision 2.1 by greg, Sat May 31 05:02:37 2014 UTC vs.
Revision 2.12 by greg, Mon Aug 27 23:03:05 2018 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7  
8   #include <stdio.h>
9   #include <stdlib.h>
10 + #include <errno.h>
11   #include "rtio.h"
12   #include "resolu.h"
13   #include "rmatrix.h"
14 + #include "platform.h"
15  
16   #define MAXCOMP         50              /* #components we support */
17  
# Line 18 | Line 20 | typedef struct {
20          int             nsf;                    /* number of scalars */
21          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
22          int             clen;                   /* number of coefficients */
23 <        int             transpose;              /* do transpose? (<0 first) */
23 >        int             transpose;              /* do transpose? */
24          int             op;                     /* '*' or '+' */
25   } ROPERAT;                              /* matrix operation */
26  
25 int     outfmt = DTascii;               /* output format */
27   int     verbose = 0;                    /* verbose reporting? */
28  
29   static void
30   op_default(ROPERAT *op)
31   {
32          memset(op, 0, sizeof(ROPERAT));
33 <        op->op = '*';
33 >        op->op = '.';
34   }
35  
36   static RMATRIX *
37   operate(RMATRIX *mleft, ROPERAT *op, const char *fname)
38   {
39          RMATRIX *mright = rmx_load(fname);
40 +        RMATRIX *mtmp;
41          int     i;
42  
43          if (fname == NULL)
# Line 45 | Line 47 | operate(RMATRIX *mleft, ROPERAT *op, const char *fname
47                  fputs(": cannot load matrix\n", stderr);
48                  return(NULL);
49          }
48        if (op->transpose < 0) {        /* transpose first? */
49                if (!rmx_transpose(mright)) {
50                        fputs(fname, stderr);
51                        fputs(": transpose failed\n", stderr);
52                        rmx_free(mright);
53                        return(NULL);
54                }
55                if (verbose) {
56                        fputs(fname, stderr);
57                        fputs(": transposed rows and columns\n", stderr);
58                }
59        }
50          if (op->nsf > 0) {              /* apply scalar(s) */
51                  if (op->clen > 0) {
52                          fputs("Options -s and -c are exclusive\n", stderr);
# Line 88 | Line 78 | operate(RMATRIX *mleft, ROPERAT *op, const char *fname
78                  }
79          }
80          if (op->clen > 0) {             /* apply transform */
91                RMATRIX *mtmp;
81                  if (op->clen % mright->ncomp) {
82                          fprintf(stderr, "%s: -c must have N x %d coefficients\n",
83                                          fname, mright->ncomp);
# Line 107 | Line 96 | operate(RMATRIX *mleft, ROPERAT *op, const char *fname
96                  rmx_free(mright);
97                  mright = mtmp;
98          }
99 <        if (op->transpose > 0) {        /* transpose after? */
100 <                if (!rmx_transpose(mright)) {
99 >        if (op->transpose) {            /* transpose matrix? */
100 >                mtmp = rmx_transpose(mright);
101 >                if (mtmp == NULL) {
102                          fputs(fname, stderr);
103                          fputs(": transpose failed\n", stderr);
104                          rmx_free(mright);
# Line 118 | Line 108 | operate(RMATRIX *mleft, ROPERAT *op, const char *fname
108                          fputs(fname, stderr);
109                          fputs(": transposed rows and columns\n", stderr);
110                  }
111 +                rmx_free(mright);
112 +                mright = mtmp;
113          }
114          if (mleft == NULL)              /* just one matrix */
115                  return(mright);
116 <        if (op->op == '*') {            /* concatenate */
116 >        if (op->op == '.') {            /* concatenate */
117                  RMATRIX *mres = rmx_multiply(mleft, mright);
118                  if (mres == NULL) {
119                          fputs(fname, stderr);
# Line 152 | Line 144 | operate(RMATRIX *mleft, ROPERAT *op, const char *fname
144                          fputs(": added in matrix\n", stderr);
145                  }
146                  rmx_free(mright);
147 +        } else if ((op->op == '*') | (op->op == '/')) {
148 +                const char *    tnam = (op->op == '/') ?
149 +                                        "division" : "multiplication";
150 +                errno = 0;
151 +                if (!rmx_elemult(mleft, mright, (op->op == '/'))) {
152 +                        fprintf(stderr, "%s: element-wise %s failed\n",
153 +                                        fname, tnam);
154 +                        rmx_free(mright);
155 +                        return(NULL);
156 +                }
157 +                if (errno)
158 +                        fprintf(stderr,
159 +                                "%s: warning - error during element-wise %s\n",
160 +                                        fname, tnam);
161 +                else if (verbose)
162 +                        fprintf(stderr, "%s: element-wise %s\n", fname, tnam);
163 +                rmx_free(mright);
164          } else {
165                  fprintf(stderr, "%s: unknown operation '%c'\n", fname, op->op);
166                  rmx_free(mright);
# Line 174 | Line 183 | get_factors(double da[], int n, char *av[])
183   int
184   main(int argc, char *argv[])
185   {
186 +        int     outfmt = DTfromHeader;
187          RMATRIX *mres = NULL;
188          ROPERAT op;
179        long    nbw;
189          int     i;
190                                          /* initialize */
191          op_default(&op);
192                                          /* get options and arguments */
193          for (i = 1; i < argc; i++)
194 <                if (argv[i][0] == '+' && !argv[i][1]) {
195 <                        op.op = '+';
194 >                if (argv[i][0] && !argv[i][1] &&
195 >                                strchr("+*/", argv[i][0]) != NULL) {
196 >                        op.op = argv[i][0];
197                  } else if (argv[i][0] != '-' || !argv[i][1]) {
198                          char    *fname = NULL;  /* load matrix */
199                          if (argv[i][0] != '-')
# Line 220 | Line 230 | main(int argc, char *argv[])
230                                  }
231                                  break;
232                          case 't':
233 <                                if (!op.nsf & !op.clen)
224 <                                        op.transpose = -1;
225 <                                else
226 <                                        op.transpose = 1;
233 >                                op.transpose = 1;
234                                  break;
235                          case 's':
236                                  if (n > MAXCOMP) n = MAXCOMP;
# Line 244 | Line 251 | main(int argc, char *argv[])
251          if (mres == NULL)               /* check that we got something */
252                  goto userr;
253                                          /* write result to stdout */
254 +        if (outfmt == DTfromHeader)
255 +                outfmt = mres->dtype;
256 +        if (outfmt != DTascii)
257 +                SET_FILE_BINARY(stdout);
258          newheader("RADIANCE", stdout);
259          printargs(argc, argv, stdout);
260 <        nbw = rmx_write(mres, outfmt, stdout);
250 <        /* rmx_free(mres); mres = NULL; */
251 <        if (nbw <= 0) {
260 >        if (!rmx_write(mres, outfmt, stdout)) {
261                  fprintf(stderr, "%s: error writing result matrix\n", argv[0]);
262                  return(1);
263          }
264 <        if (verbose)
256 <                fprintf(stderr, "%s: %ld bytes written\n", argv[0], nbw);
264 >        /* rmx_free(mres); mres = NULL; */
265          return(0);
266   userr:
267          fprintf(stderr,
268 <        "Usage: %s [-v][-f[adfc][-t][-s sf ..][-c ce ..] m1 [+] .. > mres\n",
268 >        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [+*/] .. > mres\n",
269                          argv[0]);
270          return(1);
271   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines