17 #include <SeExpr2/Expression.h>
22 using namespace SeExpr2;
34 void setX(
double x_input) {
x.val = x_input; }
42 void eval(
double* result) { result[0] = val; }
44 void eval(
const char** result) {}
52 if (name ==
"x")
return &
x;
57 int main(
int argc,
char* argv[]) {
66 GrapherExpr
expr(exprStr);
68 if (!expr.isValid()) {
69 std::cerr <<
"expression failed " << expr.parseError() << std::endl;
71 }
else if (!expr.returnType().isFP(1)) {
73 << expr.returnType().toString() << std::endl;
77 double xmin = -10, xmax = 10, ymin = -10, ymax = 10;
79 char* buffer =
new char[w * h];
80 memset(buffer, (
int)
' ', w * h);
83 int j_zero = (-ymin) / (ymax - ymin) * h;
84 if (j_zero >= 0 && j_zero < h) {
85 for (
int i = 0; i < w; i++) {
86 buffer[i + j_zero * w] =
'-';
90 int i_zero = (-xmin) / (xmax - xmin) * w;
91 if (i_zero >= 0 && i_zero < w) {
92 for (
int j = 0; j < h; j++) {
93 buffer[i_zero + j * w] =
'|';
98 const int samplesPerPixel = 10;
100 for (
int i = 0; i < w; i++) {
101 for (
int sample = 0; sample < samplesPerPixel; sample++) {
104 double x = double(dx + i) / double(w) * (xmax - xmin) + xmin;
107 const double* val = expr.evalFP();
113 int j = (y - ymin) / (ymax - ymin) * h;
115 if (j >= 0 && j < h) buffer[i + j * w] =
'#';
120 for (
int j = h - 1; j >= 0; j--) {
121 for (
int i = 0; i < w; i++) {
122 std::cout << buffer[i + j * w];
124 std::cout << std::endl;
ExprType & Varying()
Mutate this into a varying lifetime.
</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")
std::string toString() const
Stringify the type into a printable string.
virtual void eval(ArgHandle args)
</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
const double one_over_samples_per_pixel
</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[])
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
abstract class for implementing variable references
This is the same as the prman cellnoise function< br ></div >< br > float< b > float y< br > float< b > float y