2 * $Id: CMP_math.c 10456 2007-04-04 13:58:12Z jesterking $
4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #include "../CMP_util.h"
33 /* **************** SCALAR MATH ******************** */
34 static bNodeSocketType cmp_node_math_in
[]= {
35 { SOCK_VALUE
, 1, "Value", 0.5f
, 0.5f
, 0.5f
, 1.0f
, -10000.0f
, 10000.0f
},
36 { SOCK_VALUE
, 1, "Value", 0.5f
, 0.5f
, 0.5f
, 1.0f
, -10000.0f
, 10000.0f
},
40 static bNodeSocketType cmp_node_math_out
[]= {
41 { SOCK_VALUE
, 0, "Value", 0.0f
, 0.0f
, 0.0f
, 1.0f
, 0.0f
, 1.0f
},
45 static void do_math(bNode
*node
, float *out
, float *in
, float *in2
)
50 out
[0]= in
[0] + in2
[0];
52 case 1: /* Subtract */
53 out
[0]= in
[0] - in2
[0];
55 case 2: /* Multiply */
56 out
[0]= in
[0] * in2
[0];
60 if(in
[1]==0) /* We don't want to divide by zero. */
63 out
[0]= in
[0] / in2
[0];
75 case 7: /* Arc-Sine */
77 /* Can't do the impossible... */
78 if(in
[0] <= 1 && in
[0] >= -1 )
84 case 8: /* Arc-Cosine */
86 /* Can't do the impossible... */
87 if( in
[0] <= 1 && in
[0] >= -1 )
93 case 9: /* Arc-Tangent */
98 /* Don't want any imaginary numbers... */
100 out
[0]= pow(in
[0], in2
[0]);
105 case 11: /* Logarithm */
107 /* Don't want any imaginary numbers... */
108 if( in
[0] > 0 && in2
[0] > 0 )
109 out
[0]= log(in
[0]) / log(in2
[0]);
114 case 12: /* Minimum */
122 case 13: /* Maximum */
132 out
[0]= (int)(in
[0] + 0.5f
);
138 static void node_composit_exec_math(void *data
, bNode
*node
, bNodeStack
**in
, bNodeStack
**out
)
140 CompBuf
*cbuf
=in
[0]->data
;
141 CompBuf
*cbuf2
=in
[1]->data
;
144 /* check for inputs and outputs for early out*/
145 if(out
[0]->hasoutput
==0) return;
147 /* no image-color operation */
148 if(in
[0]->data
==NULL
&& in
[1]->data
==NULL
) {
149 do_math(node
, out
[0]->vec
, in
[0]->vec
, in
[1]->vec
);
153 /*create output based on first input */
155 stackbuf
=alloc_compbuf(cbuf
->x
, cbuf
->y
, CB_VAL
, 1);
157 /* and if it doesn't exist use the second input since we
158 know that one of them must exist at this point*/
160 stackbuf
=alloc_compbuf(cbuf2
->x
, cbuf2
->y
, CB_VAL
, 1);
163 /* operate in case there's valid size */
164 composit2_pixel_processor(node
, stackbuf
, in
[0]->data
, in
[0]->vec
, in
[1]->data
, in
[1]->vec
, do_math
, CB_VAL
, CB_VAL
);
165 out
[0]->data
= stackbuf
;
168 bNodeType cmp_node_math
= {
169 /* *next,*prev */ NULL
, NULL
,
170 /* type code */ CMP_NODE_MATH
,
172 /* width+range */ 120, 110, 160,
173 /* class+opts */ NODE_CLASS_CONVERTOR
, NODE_OPTIONS
,
174 /* input sock */ cmp_node_math_in
,
175 /* output sock */ cmp_node_math_out
,
177 /* execfunc */ node_composit_exec_math
,
180 /* freestoragefunc */ NULL
,
181 /* copystoragefunc */ NULL
,