37 std::vector<std::pair<int, int> >
ranges;
50 if (args.
nargs() >= 2) {
64 std::vector<std::pair<int, int> >
ranges;
78 double* out = &args.
outFp;
81 int num = args.
nargs();
83 for (
int k = 2; k < num; k++) val += args.
inFp<1>(k)[0];
85 for (
int k = 0; k < 3; k++) out[k] = val;
96 std::vector<std::pair<int, int> >
ranges;
110 double* out = &args.
outFp;
113 int num = args.
nargs();
115 for (
int k = 1; k < num; k++) val += (args.
inFp<3>(k)[0] + args.
inFp<3>(k)[1] + args.
inFp<3>(k)[2]);
117 for (
int k = 0; k < 3; k++) out[k] = val;
130 using namespace SeExpr2;
145 void eval(
double* result) { result[0] = val; }
147 void eval(
const char** result) { assert(
false); }
155 void eval(
double* result) {
156 for (
int k = 0; k < 3; k++) result[k] = val[k];
159 void eval(
const char** reuslt) {}
163 mutable std::map<std::string, Var> vars;
164 mutable std::map<std::string, VecVar> vecvars;
169 std::map<std::string, Var>::iterator i = vars.find(name);
170 if (i != vars.end())
return &i->second;
173 std::map<std::string, VecVar>::iterator i = vecvars.find(name);
174 if (i != vecvars.end())
return &i->second;
182 int main(
int argc,
char* argv[]) {
184 std::cerr <<
"Usage: " << argv[0] <<
" <image file> <width> <height> <exprFile>" << std::endl;
193 const char* imageFile = argv[1];
194 const char* exprFile = argv[4];
195 int width = atoi(argv[2]), height = atoi(argv[3]);
196 if (width < 0 || height < 0) {
197 std::cerr <<
"invalid width/height" << std::endl;
201 std::ifstream istream(exprFile);
203 std::cerr <<
"Cannot read file " << exprFile << std::endl;
206 std::string exprStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
207 ImageSynthExpr
expr(exprStr);
210 expr.vars[
"u"] = ImageSynthExpr::Var(0.);
211 expr.vars[
"v"] = ImageSynthExpr::Var(0.);
212 expr.vars[
"w"] = ImageSynthExpr::Var(width);
213 expr.vars[
"h"] = ImageSynthExpr::Var(height);
215 expr.vars[
"faceId"] = ImageSynthExpr::Var(0.);
216 expr.vecvars[
"P"] = ImageSynthExpr::VecVar();
217 expr.vecvars[
"Cs"] = ImageSynthExpr::VecVar();
218 expr.vecvars[
"Ci"] = ImageSynthExpr::VecVar();
221 bool valid = expr.isValid();
223 std::cerr <<
"Invalid expression " << std::endl;
224 std::cerr << expr.parseError() << std::endl;
233 std::cerr <<
"Evaluating expresion...from " << exprFile << std::endl;
234 unsigned char* image =
new unsigned char[width * height * 4];
235 double one_over_width = 1. / width, one_over_height = 1. / height;
236 double& u = expr.vars[
"u"].val;
237 double& v = expr.vars[
"v"].val;
239 double& faceId = expr.vars[
"faceId"].val;
244 unsigned char* pixel = image;
248 for (
int row = 0; row < height; row++) {
249 for (
int col = 0; col < width; col++) {
250 u = one_over_width * (col + .5);
251 v = one_over_height * (row + .5);
253 faceId = floor(u * 5);
264 const double* result = expr.evalFP();
267 pixel[0] =
clamp(result[0] * 256.);
268 pixel[1] =
clamp(result[1] * 256.);
269 pixel[2] =
clamp(result[2] * 256.);
277 std::cerr <<
"Writing image..." << imageFile << std::endl;
278 FILE* fp = fopen(imageFile,
"wb");
285 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
286 info_ptr = png_create_info_struct(png_ptr);
287 png_init_io(png_ptr, fp);
288 int color_type = PNG_COLOR_TYPE_RGBA;
289 png_set_IHDR(png_ptr,
296 PNG_COMPRESSION_TYPE_DEFAULT,
297 PNG_FILTER_TYPE_DEFAULT);
298 const unsigned char* ptrs[height];
299 for (
int i = 0; i < height; i++) {
300 ptrs[i] = &image[width * i * 4];
302 png_set_rows(png_ptr, info_ptr, (png_byte**)ptrs);
303 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
static const char * map_docstring
static const char * rand_docstring
std::vector< std::pair< int, int > > ranges
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
ExprType & Varying()
Mutate this into a varying lifetime.
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
</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")
SeExpr2::TriplanarFuncX triplanar
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
Function Definition, used in parse tree and func table.
virtual void eval(ArgHandle args)
std::vector< std::pair< int, int > > ranges
Vec< double, d, true > inFp(int i)
static void define(const char *name, ExprFunc f, const char *docString)
static const char * triplanar_docstring
double max(double x, double y)
Node that calls a function.
std::vector< std::pair< int, int > > ranges
virtual void eval(ArgHandle args)
bool checkArg(int argIndex, ExprType type, ExprVarEnvBuilder &envBuilder)
double min(double x, double y)
base class for custom instance data
</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
int numChildren() const
Number of children.
virtual void eval(ArgHandle args)
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
double clamp(double x, double lo, double hi)
virtual ExprType prep(ExprFuncNode *node, bool wantScalar, ExprVarEnvBuilder &envBuilder) const
</pre > Once we have this we need an instance to store our variable and provide a reference to that We make it because resolveVar() is const .One does not need to store a variable reference in a given expression.In fact
int main(int argc, char *argv[])
virtual void eval(ArgHandle args)
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
abstract class for implementing variable references
ExprType & Error()
Mutate this into an error type.
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const
virtual ~TriplanarFuncX()
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...