00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030
00031 #include "fatal.h"
00032 #include "common.h"
00033
00034 #include "expr_types.h"
00035 #include "eval.h"
00036
00037 #include "splaytree_types.h"
00038 #include "splaytree.h"
00039
00040 #include "param_types.h"
00041 #include "param.h"
00042
00043 #include "per_pixel_eqn.h"
00044 #include "per_pixel_eqn_types.h"
00045
00046 #include "engine_vars.h"
00047
00048
00049 extern preset_t * active_preset;
00050
00051 extern int mesh_i;
00052 extern int mesh_j;
00053
00054
00055
00056
00057 inline void evalPerPixelEqn(per_pixel_eqn_t * per_pixel_eqn) {
00058
00059 double ** param_matrix = NULL;
00060 gen_expr_t * eqn_ptr = NULL;
00061 int x,y;
00062
00063 eqn_ptr = per_pixel_eqn->gen_expr;
00064 if (per_pixel_eqn->param->matrix == NULL) {
00065 if (PER_PIXEL_EQN_DEBUG) printf("evalPerPixelEqn: [begin initializing matrix] (index = %d) (name = %s)\n",
00066 per_pixel_eqn->index, per_pixel_eqn->param->name);
00067
00068 param_matrix = per_pixel_eqn->param->matrix = (double**)malloc(gx*sizeof(double*));
00069
00070 for(x = 0; x < gx; x++)
00071 param_matrix[x] = (double *)malloc(gy * sizeof(double));
00072
00073 for (x = 0; x < gx; x++)
00074 for (y = 0; y < gy; y++)
00075 param_matrix[x][y] = 0.0;
00076
00077 if (per_pixel_eqn->param->name == NULL)
00078 printf("null parameter?\n");
00079
00080
00081 }
00082 else
00083 param_matrix = (double**)per_pixel_eqn->param->matrix;
00084
00085 if (eqn_ptr == NULL)
00086 printf("something is seriously wrong...\n");
00087 for (mesh_i = 0; mesh_i < gx; mesh_i++) {
00088 for (mesh_j = 0; mesh_j < gy; mesh_j++) {
00089 param_matrix[mesh_i][mesh_j] = eval_gen_expr(eqn_ptr);
00090 }
00091 }
00092
00093
00094
00095
00096 per_pixel_eqn->param->matrix_flag = 1;
00097 }
00098
00099 inline void evalPerPixelEqns() {
00100
00101
00102 splay_traverse(evalPerPixelEqn, active_preset->per_pixel_eqn_tree);
00103
00104
00105 mesh_i = mesh_j = -1;
00106
00107 }
00108
00109
00110
00111 int add_per_pixel_eqn(char * name, gen_expr_t * gen_expr, preset_t * preset) {
00112
00113 per_pixel_eqn_t * per_pixel_eqn;
00114 int index;
00115 param_t * param = NULL;
00116
00117
00118 if (preset == NULL)
00119 return FAILURE;
00120 if (gen_expr == NULL)
00121 return FAILURE;
00122 if (name == NULL)
00123 return FAILURE;
00124
00125 if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: per pixel equation (name = \"%s\")\n", name);
00126
00127 if (!strncmp(name, "dx", strlen("dx")))
00128 preset->per_pixel_flag[DX_OP] = TRUE;
00129 else if (!strncmp(name, "dy", strlen("dy")))
00130 preset->per_pixel_flag[DY_OP] = TRUE;
00131 else if (!strncmp(name, "cx", strlen("cx")))
00132 preset->per_pixel_flag[CX_OP] = TRUE;
00133 else if (!strncmp(name, "cy", strlen("cy")))
00134 preset->per_pixel_flag[CX_OP] = TRUE;
00135 else if (!strncmp(name, "zoom", strlen("zoom")))
00136 preset->per_pixel_flag[ZOOM_OP] = TRUE;
00137 else if (!strncmp(name, "zoomexp", strlen("zoomexp")))
00138 preset->per_pixel_flag[ZOOMEXP_OP] = TRUE;
00139 else if (!strncmp(name, "rot", strlen("rot")))
00140 preset->per_pixel_flag[ROT_OP] = TRUE;
00141 else if (!strncmp(name, "sx", strlen("sx")))
00142 preset->per_pixel_flag[SX_OP] = TRUE;
00143 else if (!strncmp(name, "sy", strlen("sy")))
00144 preset->per_pixel_flag[SY_OP] = TRUE;
00145
00146
00147
00148
00149 if ((param = find_param(name, preset, TRUE)) == NULL) {
00150 if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to allocate a new parameter!\n");
00151 return FAILURE;
00152
00153 }
00154
00155
00156
00157
00158
00159 index = splay_size(preset->per_pixel_eqn_tree);
00160
00161
00162 if ((per_pixel_eqn = new_per_pixel_eqn(index, param, gen_expr)) == NULL) {
00163 if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to create new per pixel equation!\n");
00164 return FAILURE;
00165
00166 }
00167
00168 if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: new equation (index = %d) (param = \"%s\")\n",
00169 per_pixel_eqn->index, per_pixel_eqn->param->name);
00170
00171 if (splay_insert(per_pixel_eqn, &per_pixel_eqn->index, preset->per_pixel_eqn_tree) < 0) {
00172 free_per_pixel_eqn(per_pixel_eqn);
00173 printf("failed to add per pixel eqn!\n");
00174 return FAILURE;
00175 }
00176
00177
00178 return SUCCESS;
00179 }
00180
00181 per_pixel_eqn_t * new_per_pixel_eqn(int index, param_t * param, gen_expr_t * gen_expr) {
00182
00183 per_pixel_eqn_t * per_pixel_eqn;
00184
00185 if (index < 0)
00186 return NULL;
00187 if (param == NULL)
00188 return NULL;
00189 if (gen_expr == NULL)
00190 return NULL;
00191
00192 if ((per_pixel_eqn = (per_pixel_eqn_t*)malloc(sizeof(per_pixel_eqn_t))) == NULL)
00193 return NULL;
00194
00195
00196 per_pixel_eqn->index = index;
00197 per_pixel_eqn->param = param;
00198 per_pixel_eqn->gen_expr = gen_expr;
00199
00200 return per_pixel_eqn;
00201 }
00202
00203
00204 void free_per_pixel_eqn(per_pixel_eqn_t * per_pixel_eqn) {
00205
00206 if (per_pixel_eqn == NULL)
00207 return;
00208
00209 free_gen_expr(per_pixel_eqn->gen_expr);
00210
00211 free(per_pixel_eqn);
00212
00213 return;
00214 }
00215
00216 inline int isPerPixelEqn(int op) {
00217
00218 return active_preset->per_pixel_flag[op];
00219
00220 }
00221
00222 inline int resetPerPixelEqnFlags(preset_t * preset) {
00223 int i;
00224
00225 for (i = 0; i < NUM_OPS;i++)
00226 preset->per_pixel_flag[i] = FALSE;
00227
00228 return SUCCESS;
00229 }