| 1 |
greg |
2.22 |
/* RCSid $Id: rmatrix.h,v 2.21 2024/05/19 15:32:24 greg Exp $ */
|
| 2 |
greg |
2.1 |
/*
|
| 3 |
|
|
* Header file for general matrix routines.
|
| 4 |
|
|
*/
|
| 5 |
|
|
|
| 6 |
|
|
#ifndef _RAD_RMATRIX_H_
|
| 7 |
|
|
#define _RAD_RMATRIX_H_
|
| 8 |
|
|
|
| 9 |
|
|
#include "cmatrix.h"
|
| 10 |
|
|
|
| 11 |
|
|
#ifdef __cplusplus
|
| 12 |
|
|
extern "C" {
|
| 13 |
|
|
#endif
|
| 14 |
|
|
|
| 15 |
greg |
2.13 |
/* Preferred BSDF component:
|
| 16 |
greg |
2.19 |
none, transmission, reflection front (normal side), reflection back */
|
| 17 |
|
|
typedef enum {RMPnone=-1, RMPtrans=0, RMPreflF, RMPreflB} RMPref;
|
| 18 |
greg |
2.13 |
|
| 19 |
greg |
2.21 |
/* Private flags:
|
| 20 |
|
|
need to swap input, need to free/unmap memory */
|
| 21 |
|
|
#define RMF_SWAPIN 1
|
| 22 |
|
|
#define RMF_OURMEM 2
|
| 23 |
|
|
|
| 24 |
greg |
2.22 |
#define DTrmx_native DTdouble /* in-core data type */
|
| 25 |
|
|
|
| 26 |
greg |
2.14 |
/* General [row][col][cmp] component matrix */
|
| 27 |
greg |
2.1 |
typedef struct {
|
| 28 |
greg |
2.14 |
char *info;
|
| 29 |
|
|
void *mapped;
|
| 30 |
|
|
double *mtx;
|
| 31 |
|
|
COLOR cexp;
|
| 32 |
greg |
2.16 |
float wlpart[4];
|
| 33 |
greg |
2.11 |
int nrows, ncols;
|
| 34 |
|
|
short ncomp;
|
| 35 |
|
|
uby8 dtype;
|
| 36 |
greg |
2.21 |
uby8 pflags;
|
| 37 |
greg |
2.1 |
} RMATRIX;
|
| 38 |
|
|
|
| 39 |
greg |
2.15 |
#define rmx_lval(rm,r,c) ((rm)->mtx + (rm)->ncomp*((c)+(size_t)(rm)->ncols*(r)))
|
| 40 |
greg |
2.17 |
#define rmx_val rmx_lval
|
| 41 |
greg |
2.1 |
|
| 42 |
greg |
2.21 |
#define rmx_array_size(rm) (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp)
|
| 43 |
|
|
#define rmx_mapped_size(rm) ((char *)(rm)->mtx + rmx_array_size(rm) - (char *)(rm)->mapped)
|
| 44 |
|
|
|
| 45 |
greg |
2.14 |
/* Initialize a RMATRIX struct but don't allocate array space */
|
| 46 |
|
|
extern RMATRIX *rmx_new(int nr, int nc, int n);
|
| 47 |
|
|
|
| 48 |
|
|
/* Prepare a RMATRIX for writing (allocate array if needed) */
|
| 49 |
|
|
extern int rmx_prepare(RMATRIX *rm);
|
| 50 |
|
|
|
| 51 |
|
|
/* Call rmx_new() and rmx_prepare() */
|
| 52 |
greg |
2.1 |
extern RMATRIX *rmx_alloc(int nr, int nc, int n);
|
| 53 |
|
|
|
| 54 |
greg |
2.19 |
/* Clear state by freeing info and matrix data */
|
| 55 |
|
|
extern void rmx_reset(RMATRIX *rm);
|
| 56 |
|
|
|
| 57 |
|
|
/* Free an RMATRIX struct and data */
|
| 58 |
greg |
2.4 |
extern void rmx_free(RMATRIX *rm);
|
| 59 |
|
|
|
| 60 |
|
|
/* Resolve data type based on two input types (returns 0 for mismatch) */
|
| 61 |
|
|
extern int rmx_newtype(int dtyp1, int dtyp2);
|
| 62 |
|
|
|
| 63 |
greg |
2.17 |
/* Read matrix header from input stream (cannot be XML) */
|
| 64 |
|
|
extern int rmx_load_header(RMATRIX *rm, FILE *fp);
|
| 65 |
|
|
|
| 66 |
greg |
2.18 |
/* Load next row as double (cannot be XML) */
|
| 67 |
|
|
extern int rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp);
|
| 68 |
|
|
|
| 69 |
greg |
2.17 |
/* Allocate & load post-header data from stream given type set in rm->dtype */
|
| 70 |
|
|
extern int rmx_load_data(RMATRIX *rm, FILE *fp);
|
| 71 |
|
|
|
| 72 |
greg |
2.7 |
/* Load matrix from supported file type (NULL for stdin, '!' with command) */
|
| 73 |
greg |
2.13 |
extern RMATRIX *rmx_load(const char *inspec, RMPref rmp);
|
| 74 |
greg |
2.1 |
|
| 75 |
greg |
2.3 |
/* Append header information associated with matrix data */
|
| 76 |
|
|
extern int rmx_addinfo(RMATRIX *rm, const char *info);
|
| 77 |
|
|
|
| 78 |
greg |
2.19 |
/* Finish writing header data with resolution and format, returning type used */
|
| 79 |
|
|
extern int rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp);
|
| 80 |
|
|
|
| 81 |
|
|
/* Write out matrix data (usually by row) */
|
| 82 |
|
|
extern int rmx_write_data(const double *dp, int nc, int len,
|
| 83 |
|
|
int dtype, FILE *fp);
|
| 84 |
|
|
|
| 85 |
|
|
/* Write matrix using file format indicated by dtype */
|
| 86 |
greg |
2.6 |
extern int rmx_write(const RMATRIX *rm, int dtype, FILE *fp);
|
| 87 |
greg |
2.1 |
|
| 88 |
|
|
/* Allocate and assign square identity matrix with n components */
|
| 89 |
|
|
extern RMATRIX *rmx_identity(int dim, int n);
|
| 90 |
|
|
|
| 91 |
|
|
/* Duplicate the given matrix */
|
| 92 |
|
|
extern RMATRIX *rmx_copy(const RMATRIX *rm);
|
| 93 |
|
|
|
| 94 |
greg |
2.20 |
/* Replace data in first matrix with data from second */
|
| 95 |
|
|
extern int rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa);
|
| 96 |
|
|
|
| 97 |
greg |
2.2 |
/* Allocate and assign transposed matrix */
|
| 98 |
|
|
extern RMATRIX *rmx_transpose(const RMATRIX *rm);
|
| 99 |
greg |
2.1 |
|
| 100 |
|
|
/* Multiply (concatenate) two matrices and allocate the result */
|
| 101 |
|
|
extern RMATRIX *rmx_multiply(const RMATRIX *m1, const RMATRIX *m2);
|
| 102 |
|
|
|
| 103 |
greg |
2.8 |
/* Element-wise multiplication (or division) of m2 into m1 */
|
| 104 |
|
|
extern int rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide);
|
| 105 |
|
|
|
| 106 |
greg |
2.1 |
/* Sum second matrix into first, applying scale factor beforehand */
|
| 107 |
|
|
extern int rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]);
|
| 108 |
|
|
|
| 109 |
|
|
/* Scale the given matrix by the indicated scalar component vector */
|
| 110 |
|
|
extern int rmx_scale(RMATRIX *rm, const double sf[]);
|
| 111 |
|
|
|
| 112 |
|
|
/* Allocate new matrix and apply component transformation */
|
| 113 |
|
|
extern RMATRIX *rmx_transform(const RMATRIX *msrc, int n, const double cmat[]);
|
| 114 |
|
|
|
| 115 |
|
|
/* Convert a color matrix to newly allocated RMATRIX buffer */
|
| 116 |
|
|
extern RMATRIX *rmx_from_cmatrix(const CMATRIX *cm);
|
| 117 |
|
|
|
| 118 |
|
|
/* Convert general matrix to newly allocated CMATRIX buffer */
|
| 119 |
|
|
extern CMATRIX *cm_from_rmatrix(const RMATRIX *rm);
|
| 120 |
|
|
|
| 121 |
|
|
#ifdef __cplusplus
|
| 122 |
|
|
}
|
| 123 |
|
|
#endif
|
| 124 |
|
|
#endif /* _RAD_RMATRIX_H_ */
|