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