|
Blender
V2.59
|
00001 /* 00002 * $Id: CMP_math.c 37335 2011-06-09 13:46:34Z campbellbarton $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2006 Blender Foundation. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00035 #include "../CMP_util.h" 00036 00037 /* **************** SCALAR MATH ******************** */ 00038 static bNodeSocketType cmp_node_math_in[]= { 00039 { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f}, 00040 { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f}, 00041 { -1, 0, "" } 00042 }; 00043 00044 static bNodeSocketType cmp_node_math_out[]= { 00045 { SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 00046 { -1, 0, "" } 00047 }; 00048 00049 static void do_math(bNode *node, float *out, float *in, float *in2) 00050 { 00051 switch(node->custom1) 00052 { 00053 case 0: /* Add */ 00054 out[0]= in[0] + in2[0]; 00055 break; 00056 case 1: /* Subtract */ 00057 out[0]= in[0] - in2[0]; 00058 break; 00059 case 2: /* Multiply */ 00060 out[0]= in[0] * in2[0]; 00061 break; 00062 case 3: /* Divide */ 00063 { 00064 if(in2[0]==0) /* We don't want to divide by zero. */ 00065 out[0]= 0.0; 00066 else 00067 out[0]= in[0] / in2[0]; 00068 } 00069 break; 00070 case 4: /* Sine */ 00071 out[0]= sin(in[0]); 00072 break; 00073 case 5: /* Cosine */ 00074 out[0]= cos(in[0]); 00075 break; 00076 case 6: /* Tangent */ 00077 out[0]= tan(in[0]); 00078 break; 00079 case 7: /* Arc-Sine */ 00080 { 00081 /* Can't do the impossible... */ 00082 if(in[0] <= 1 && in[0] >= -1 ) 00083 out[0]= asin(in[0]); 00084 else 00085 out[0]= 0.0; 00086 } 00087 break; 00088 case 8: /* Arc-Cosine */ 00089 { 00090 /* Can't do the impossible... */ 00091 if( in[0] <= 1 && in[0] >= -1 ) 00092 out[0]= acos(in[0]); 00093 else 00094 out[0]= 0.0; 00095 } 00096 break; 00097 case 9: /* Arc-Tangent */ 00098 out[0]= atan(in[0]); 00099 break; 00100 case 10: /* Power */ 00101 { 00102 /* Only raise negative numbers by full integers */ 00103 if( in[0] >= 0 ) { 00104 out[0]= pow(in[0], in2[0]); 00105 } else { 00106 float y_mod_1 = fmod(in2[0], 1); 00107 /* if input value is not nearly an integer, fall back to zero, nicer than straight rounding */ 00108 if (y_mod_1 > 0.999 || y_mod_1 < 0.001) { 00109 out[0]= pow(in[0], floor(in2[0] + 0.5)); 00110 } else { 00111 out[0] = 0.0; 00112 } 00113 } 00114 } 00115 break; 00116 case 11: /* Logarithm */ 00117 { 00118 /* Don't want any imaginary numbers... */ 00119 if( in[0] > 0 && in2[0] > 0 ) 00120 out[0]= log(in[0]) / log(in2[0]); 00121 else 00122 out[0]= 0.0; 00123 } 00124 break; 00125 case 12: /* Minimum */ 00126 { 00127 if( in[0] < in2[0] ) 00128 out[0]= in[0]; 00129 else 00130 out[0]= in2[0]; 00131 } 00132 break; 00133 case 13: /* Maximum */ 00134 { 00135 if( in[0] > in2[0] ) 00136 out[0]= in[0]; 00137 else 00138 out[0]= in2[0]; 00139 } 00140 break; 00141 case 14: /* Round */ 00142 { 00143 /* round by the second value */ 00144 if( in2[0] != 0.0f ) 00145 out[0]= floorf(in[0] / in2[0] + 0.5f) * in2[0]; 00146 else 00147 out[0]= floorf(in[0] + 0.5f); 00148 00149 } 00150 break; 00151 case 15: /* Less Than */ 00152 { 00153 if( in[0] < in2[0] ) 00154 out[0]= 1.0f; 00155 else 00156 out[0]= 0.0f; 00157 } 00158 break; 00159 case 16: /* Greater Than */ 00160 { 00161 if( in[0] > in2[0] ) 00162 out[0]= 1.0f; 00163 else 00164 out[0]= 0.0f; 00165 } 00166 break; 00167 } 00168 } 00169 00170 static void node_composit_exec_math(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out) 00171 { 00172 CompBuf *cbuf=in[0]->data; 00173 CompBuf *cbuf2=in[1]->data; 00174 CompBuf *stackbuf; 00175 00176 /* check for inputs and outputs for early out*/ 00177 if(out[0]->hasoutput==0) return; 00178 00179 /* no image-color operation */ 00180 if(in[0]->data==NULL && in[1]->data==NULL) { 00181 do_math(node, out[0]->vec, in[0]->vec, in[1]->vec); 00182 return; 00183 } 00184 00185 /*create output based on first input */ 00186 if(cbuf) { 00187 stackbuf=alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1); 00188 } 00189 /* and if it doesn't exist use the second input since we 00190 know that one of them must exist at this point*/ 00191 else { 00192 stackbuf=alloc_compbuf(cbuf2->x, cbuf2->y, CB_VAL, 1); 00193 } 00194 00195 /* operate in case there's valid size */ 00196 composit2_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, in[1]->data, in[1]->vec, do_math, CB_VAL, CB_VAL); 00197 out[0]->data= stackbuf; 00198 } 00199 00200 void register_node_type_cmp_math(ListBase *lb) 00201 { 00202 static bNodeType ntype; 00203 00204 node_type_base(&ntype, CMP_NODE_MATH, "Math", NODE_CLASS_CONVERTOR, NODE_OPTIONS, 00205 cmp_node_math_in, cmp_node_math_out); 00206 node_type_size(&ntype, 120, 110, 160); 00207 node_type_label(&ntype, node_math_label); 00208 node_type_exec(&ntype, node_composit_exec_math); 00209 00210 nodeRegisterType(lb, &ntype); 00211 } 00212 00213 00214 00215