1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#include "expr.h"
#include "interpreter.h"
#include <geogram/basic/string.h>
extern "C" {
#include <lauxlib.h>
#include <lualib.h>
}
namespace GEO {
namespace String {
std::string to_string(Expr* e) {
return e->atomic() ? e->to_string() : "(" + e->to_string() + ")";
}
}
/**************************************************************************/
Expr::~Expr() { }
std::string Expr::name() const {
std::string result;
// Try to find this variable in Lua's global scope,
// if found, use its name.
lua_State* L = Interpreter::instance()->lua_state();
lua_pushglobaltable(L);
int index = lua_gettop(L);
lua_pushnil(L);
while(lua_next(L,index) != 0) {
if(lua_isexpr(L,-1) && lua_toexpr(L,-1) == this) {
result = std::string(lua_tostring(L,-2));
}
lua_pop(L,1);
}
// If not found in Lua's global scope, generate a unique ID
// from this Expr's address.
if(result.length() == 0) {
result = "expr@" + String::to_string(this);
}
return result;
}
/**************************************************************************/
Expr::Type Constant::type() const {
return SCALAR;
}
index_t Constant::dim() const {
return 1;
}
std::string Constant::to_string() const {
return String::to_string(value());
}
bool Constant::atomic() const {
return true;
}
/**************************************************************************/
Expr::Type Variable::type() const {
return type_;
}
bool Variable::atomic() const {
return true;
}
/**************************************************************************/
index_t ScalarVar::dim() const {
return 1;
}
std::string ScalarVar::to_string() const {
// return name() + ":scalar";
return name();
}
/**************************************************************************/
index_t VectorVar::default_dim_ = 3;
index_t VectorVar::dim() const {
return dim_ == index_t(-1) ? default_dim_ : dim_;
}
std::string VectorVar::to_string() const {
std::string result = name();
/*
result += ":vec";
if(dim_ != index_t(-1)) {
result += ("[" + String::to_string(dim_) + "]");
}
*/
return result;
}
/**************************************************************************/
std::string VectorComponent::to_string() const {
return String::to_string(vector()) + "[" + String::to_string(component_) + "]";
}
bool VectorComponent::atomic() const {
return true;
}
Expr::Type VectorComponent::type() const {
return SCALAR;
}
index_t VectorComponent::dim() const {
return 1;
}
/**************************************************************************/
index_t SignVar::dim() const {
return 0;
}
std::string SignVar::to_string() const {
return name() + ":sign";
}
/**************************************************************************/
Expr::Type Sum::type() const {
return type_;
}
index_t Sum::dim() const {
return dim_;
}
std::string Sum::to_string() const {
std::string result;
for(index_t i=0; i<nb_terms(); ++i) {
result += String::to_string(ith_term(i));
if(i != nb_terms()-1) {
result += " + ";
}
}
return result;
}
void Sum::add_term(Expr* term) {
{
Sum* sum = dynamic_cast<Sum*>(term);
if(sum != nullptr) {
for(index_t i=0; i<sum->nb_terms(); ++i) {
add_term(sum->ith_term(i));
}
return;
}
}
{
Constant* co1 = dynamic_cast<Constant*>(term);
if(co1 != nullptr) {
if(co1->value() == 0.0) {
return;
}
for(index_t i=0; i<nb_terms(); ++i) {
Constant* co2 = dynamic_cast<Constant*>(ith_term(i));
if(co2 != nullptr) {
terms_[i] = new Constant(co1->value() + co2->value());
return;
}
}
}
}
terms_.push_back(term);
}
bool Sum::atomic() const {
return false;
}
/**************************************************************************/
Expr::Type Product::type() const {
return type_;
}
index_t Product::dim() const {
return dim_;
}
std::string Product::to_string() const {
std::string result;
for(index_t i=0; i<nb_factors(); ++i) {
result += String::to_string(ith_factor(i));
if(i != nb_factors()-1) {
result += " * ";
}
}
return result;
}
void Product::mult_factor(Expr* factor) {
{
for(index_t i=0; i<nb_factors(); ++i) {
if(factor == ith_factor(i)) {
factors_[i] = new Pow(factor,2);
return;
}
Pow* pow = dynamic_cast<Pow*>(ith_factor(i));
if(pow != nullptr && pow->arg() == factor) {
factors_[i] = new Pow(factor,pow->exponent()+1);
return;
}
}
}
{
Product* prod = dynamic_cast<Product*>(factor);
if(prod != nullptr) {
for(index_t i=0; i<prod->nb_factors(); ++i) {
mult_factor(prod->ith_factor(i));
}
return;
}
}
{
Constant* co1 = dynamic_cast<Constant*>(factor);
if(co1 != nullptr) {
if(co1->value() == 1.0) {
return;
}
for(index_t i=0; i<nb_factors(); ++i) {
Constant* co2 = dynamic_cast<Constant*>(ith_factor(i));
if(co2 != nullptr) {
factors_[i] = new Constant(co1->value() * co2->value());
return;
}
}
}
}
factors_.push_back(factor);
}
bool Product::atomic() const {
return false;
}
/**************************************************************************/
Expr::Type Pow::type() const {
return type_;
}
index_t Pow::dim() const {
return dim_;
}
std::string Pow::to_string() const {
return String::to_string(arg()) + "^" + String::to_string(exponent());
}
bool Pow::atomic() const {
return false;
}
/**************************************************************************/
}
|