KSeExpr 6.0.0.0
asciiGraph.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2// SPDX-License-Identifier: LicenseRef-Apache-2.0
3// SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4// SPDX-License-Identifier: GPL-3.0-or-later
5// SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
6// SPDX-License-Identifier: GPL-3.0-or-later
7
9#include <cassert>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14using namespace KSeExpr;
15
20class GrapherExpr : public Expression
21{
22public:
24 GrapherExpr(const std::string &expr)
25 : Expression(expr)
26 {
27 }
28
30 void setX(double x_input)
31 {
32 x.val = x_input;
33 }
34
35private:
37 struct SimpleVar : public ExprVarRef {
38 SimpleVar()
39 : ExprVarRef(ExprType().FP(1).Varying())
40 {
41 }
42
43 double val{0.0}; // independent variable
44 void eval(double *result) override
45 {
46 result[0] = val;
47 }
48
49 void eval(const char **) override
50 {
51 assert(false);
52 }
53 };
54
56 mutable SimpleVar x;
57
59 ExprVarRef *resolveVar(const std::string &name) const override
60 {
61 if (name == "x")
62 return &x;
63 return nullptr;
64 }
65};
66
67int main(int argc, char *argv[])
68{
69 std::string exprStr =
70 "\
71 $val=.5*PI*x;\
72 7*sin(val)/val \
73 ";
74 if (argc == 2) {
75 exprStr = argv[1];
76 }
77 GrapherExpr expr(exprStr);
78
79 if (!expr.isValid()) {
80 std::cerr << "expression failed " << expr.parseError() << std::endl;
81 exit(1);
82 } else if (!expr.returnType().isFP(1)) {
83 std::cerr << "Expected expression of type " << ExprType().FP(1).Varying().toString() << " got " << expr.returnType().toString() << std::endl;
84 exit(1);
85 }
86
87 double xmin = -10; // NOLINT readability-magic-numbers
88 double xmax = 10; // NOLINT readability-magic-numbers
89 double ymin = -10; // NOLINT readability-magic-numbers
90 double ymax = 10; // NOLINT readability-magic-numbers
91 int w = 60; // NOLINT readability-magic-numbers
92 int h = 30; // NOLINT readability-magic-numbers
93 auto buffer = std::vector<char>(w * h, ' ');
94
95 // draw x axis
96 int j_zero = static_cast<int>((-ymin) / (ymax - ymin) * h);
97 if (j_zero >= 0 && j_zero < h) {
98 for (int i = 0; i < w; i++) {
99 buffer[i + j_zero * w] = '-';
100 }
101 }
102 // draw y axis
103 int i_zero = static_cast<int>((-xmin) / (xmax - xmin) * w);
104 if (i_zero >= 0 && i_zero < w) {
105 for (int j = 0; j < h; j++) {
106 buffer[i_zero + j * w] = '|';
107 }
108 }
109
110 // evaluate the graph
111 const int samplesPerPixel = 10;
113 for (int i = 0; i < w; i++) {
114 for (int sample = 0; sample < samplesPerPixel; sample++) {
115 // transform from device to logical coordinatex
117 double x = double(dx + i) / double(w) * (xmax - xmin) + xmin;
118 // prep the expression engine for evaluation
119 expr.setX(x);
120 const double *val = expr.evalFP();
121 // evaluate and pull scalar value - currently does not work
122 // TODO: fix eval and then use actual call
123 // Vec3d val=0.0;//expr.evaluate();
124 double y = val[0];
125 // transform from logical to device coordinate
126 int j = static_cast<int>((y - ymin) / (ymax - ymin) * h);
127 // store to the buffer
128 if (j >= 0 && j < h)
129 buffer[i + j * w] = '#';
130 }
131 }
132
133 // draw the graph from the buffer
134 for (int j = h - 1; j >= 0; j--) {
135 for (int i = 0; i < w; i++) {
136 std::cout << buffer[i + j * w];
137 }
138 std::cout << std::endl;
139 }
140
141 return 0;
142}
int main(int argc, char *argv[])
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition ExprType.h:97
ExprType & Varying()
Mutate this into a varying lifetime.
Definition ExprType.h:134
std::string toString() const
Stringify the type into a printable string.
Definition ExprType.h:253
abstract class for implementing variable references
Definition Expression.h:36
virtual void eval(double *result)=0
returns this variable's value by setting result
main expression class
Definition Expression.h:67
virtual ExprVarRef * resolveVar(const std::string &) const
Definition Expression.h:201