38 std::vector<std::pair<int, int> >
ranges;
52 if (args.
nargs() >= 2) {
66 std::vector<std::pair<int, int> >
ranges;
81 double* out = &args.
outFp;
84 int num = args.
nargs();
86 for (
int k = 2; k < num; k++) val += args.
inFp<1>(k)[0];
88 for (
int k = 0; k < 3; k++) out[k] = val;
99 std::vector<std::pair<int, int> >
ranges;
114 double* out = &args.
outFp;
117 int num = args.
nargs();
119 for (
int k = 1; k < num; k++) val += (args.
inFp<3>(k)[0] + args.
inFp<3>(k)[1] + args.
inFp<3>(k)[2]);
121 for (
int k = 0; k < 3; k++) out[k] = val;
149 void eval(
double* result) { result[0] = val; }
151 void eval(
const char** result) { assert(
false); }
159 void eval(
double* result) {
160 for (
int k = 0; k < 3; k++) result[k] = val[k];
163 void eval(
const char** reuslt) {}
167 mutable std::map<std::string, Var> vars;
168 mutable std::map<std::string, VecVar> vecvars;
171 ExprVarRef* resolveVar(
const std::string& name)
const {
173 std::map<std::string, Var>::iterator i = vars.find(name);
174 if (i != vars.end())
return &i->second;
177 std::map<std::string, VecVar>::iterator i = vecvars.find(name);
178 if (i != vecvars.end())
return &i->second;
184double clamp(
double x) {
return std::max(0., std::min(255.,
x)); }
186int main(
int argc,
char* argv[]) {
188 std::cerr <<
"Usage: " << argv[0] <<
" <image file> <width> <height> <exprFile>" << std::endl;
197 const char* imageFile = argv[1];
198 const char* exprFile = argv[4];
199 int width = atoi(argv[2]), height = atoi(argv[3]);
200 if (width < 0 || height < 0) {
201 std::cerr <<
"invalid width/height" << std::endl;
205 std::ifstream istream(exprFile);
207 std::cerr <<
"Cannot read file " << exprFile << std::endl;
210 std::string exprStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
211 ImageSynthExpr
expr(exprStr);
214 expr.vars[
"u"] = ImageSynthExpr::Var(0.);
215 expr.vars[
"v"] = ImageSynthExpr::Var(0.);
216 expr.vars[
"w"] = ImageSynthExpr::Var(width);
217 expr.vars[
"h"] = ImageSynthExpr::Var(height);
219 expr.vars[
"faceId"] = ImageSynthExpr::Var(0.);
220 expr.vecvars[
"P"] = ImageSynthExpr::VecVar();
221 expr.vecvars[
"Cs"] = ImageSynthExpr::VecVar();
222 expr.vecvars[
"Ci"] = ImageSynthExpr::VecVar();
225 bool valid =
expr.isValid();
227 std::cerr <<
"Invalid expression " << std::endl;
228 std::cerr <<
expr.parseError() << std::endl;
237 std::cerr <<
"Evaluating expresion...from " << exprFile << std::endl;
238 unsigned char* image =
new unsigned char[width * height * 4];
239 double one_over_width = 1. / width, one_over_height = 1. / height;
240 double& u =
expr.vars[
"u"].val;
241 double& v =
expr.vars[
"v"].val;
243 double& faceId =
expr.vars[
"faceId"].val;
248 unsigned char* pixel = image;
252 for (
int row = 0; row < height; row++) {
253 for (
int col = 0; col < width; col++) {
254 u = one_over_width * (col + .5);
255 v = one_over_height * (row + .5);
257 faceId = floor(u * 5);
268 const double* result =
expr.evalFP();
271 pixel[0] =
clamp(result[0] * 256.);
272 pixel[1] =
clamp(result[1] * 256.);
273 pixel[2] =
clamp(result[2] * 256.);
281 std::cerr <<
"Writing image..." << imageFile << std::endl;
282 FILE* fp = fopen(imageFile,
"wb");
289 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
290 info_ptr = png_create_info_struct(png_ptr);
291 png_init_io(png_ptr, fp);
292 int color_type = PNG_COLOR_TYPE_RGBA;
293 png_set_IHDR(png_ptr,
300 PNG_COMPRESSION_TYPE_DEFAULT,
301 PNG_FILTER_TYPE_DEFAULT);
302 const unsigned char* ptrs[height];
303 for (
int i = 0; i < height; i++) {
304 ptrs[i] = &image[width * i * 4];
306 png_set_rows(png_ptr, info_ptr, (png_byte**)ptrs);
307 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
Node that calls a function.
bool checkArg(int argIndex, ExprType type, ExprVarEnvBuilder &envBuilder)
Vec< double, d, true > inFp(int i)
Function Definition, used in parse tree and func table.
static void define(const char *name, ExprFunc f, const char *docString)
int numChildren() const
Number of children.
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
ExprType & Error()
Mutate this into an error type.
ExprType & Varying()
Mutate this into a varying lifetime.
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
abstract class for implementing variable references
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
virtual void eval(ArgHandle args)
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
virtual void eval(ArgHandle args)
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
virtual void eval(ArgHandle args)
virtual ~TriplanarFuncX()
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
int main(int argc, char *argv[])
static const char * rand_docstring
static const char * triplanar_docstring
static const char * map_docstring
SeExpr2::TriplanarFuncX triplanar
base class for custom instance data
std::vector< std::pair< int, int > > ranges
std::vector< std::pair< int, int > > ranges
std::vector< std::pair< int, int > > ranges
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x