ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.h
Revision: 2.21
Committed: Sun May 19 15:32:24 2024 UTC (26 hours, 29 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.20: +10 -2 lines
Log Message:
feat: Added facility for app-managed matrix memory

File Contents

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