alaCarte Maps
Renderer for OpenStreetMap tiles
function_operation_node.cpp
Go to the documentation of this file.
1 
23 #include "utils/random.hpp"
24 
25 #include "general/geo_object.hpp"
26 
28 
31 
32 
33 
34 namespace eval {
35 
42  : operation(op::Nothing)
43 {
44  this->enclosed.push_back(enclosed);
45 }
46 
57  : operation(operation)
58  , enclosed(enclosed)
59 {
60  assert(enclosed.size());
61 }
62 
63 
65 {
66  string first = enclosed.front()->eval(obj);
67 
68  switch(operation)
69  {
70  case op::Str:
71  case op::Nothing:
72  return first;
73 
74  // Canonical
75  case op::Boolean:
76  {
77  bool b;
78  Conv<bool>(first, &b);
79  return ToString(b);
80  }
81  return first;
82  case op::Num:
83  {
84  float f;
85  if(Conv(first, &f))
86  {
87  return first;
88  }
89  return "";
90  }
91 
92  // Mathematics
93  case op::Sqrt:
94  {
95  float f;
96  if(Conv(first, &f))
97  {
98  return ToString(sqrt(f));
99  }
100  return "";
101  }
102  case op::Int:
103  {
104  float f;
105  if(Conv(first, &f))
106  {
107  return ToString((int)floor(f));
108  }
109  return "";
110  }
111  case op::Not:
112  {
113  bool b;
114  Conv(first, &b);
115  return ToString(!b);
116  }
117  //case op::Metric:
118  //case op::ZMetric:
119 
120 
121  case op::Tag:
122  {
123  MaybeCachedString first_uncached(first);
124  auto it = obj->getTags().find(first_uncached, boost::hash<MaybeCachedString>(), CachedComparator());
125  if(it != obj->getTags().cend())
126  {
127  return it->second.str();
128  }else{
129  return "";
130  }
131  }
132  case op::Cond:
133  {
134  if(enclosed.size() != 3)
135  return "";
136 
137  bool condition;
138  Conv<bool>(first, &condition);
139 
140  if(condition)
141  {
142  return enclosed[1]->eval(obj);
143  }else{
144  return enclosed[2]->eval(obj);
145  }
146  }
147 
148  case op::Colgen:
149  {
150  Color base(0xFFFFFFFF);
151  Color top(0xFFFFFFFF);
152  if(enclosed.size() > 3)
153  return "";
154 
155  // 0 = direct set
156  // 1 = base multiplication
157  // 2 = between set
158  int operation = 0;
159 
160  if(enclosed.size() >= 2)
161  {
162  if(Conv(enclosed[1]->eval(obj), &base))
163  {
164  ++operation;
165 
166  if(enclosed.size() >= 3)
167  {
168  if(Conv(enclosed[2]->eval(obj), &top))
169  ++operation;
170  }
171  }
172  }
173 
174  Color result;
175  Random rand(boost::hash_value(first));
176 
177  Color randColor((uint8)rand.rand(255), (uint8)rand.rand(255), (uint8)rand.rand(255), (uint8)rand.rand(255));
178 
179 
180 
181  switch (operation)
182  {
183  case 0:
184  result = randColor;
185  result.a = 0.3f;
186  break;
187  case 1:
188  result = base * randColor;
189  result.a = base.a;
190  break;
191  case 2:
192  result = base + randColor * (top - base).max(Color(uint32(0x00000000)));
193  break;
194  default:
195  return "";
196  }
197 
198  std::ostringstream os;
199  uint32 col = result;
200  os << '#' << std::hex << std::setw(6) << std::setfill('0') << (col & 0xFFFFFF);
201  os << std::hex << std::setw(2) << std::setfill('0') << (col >> 24);
202 
203  return os.str();
204  }
205  default:
206  assert(false);
207  break;
208  }
209 
210  assert(!"Unknown operation executed");
211  return "";
212 }
213 
214 
215 
216 
217 
218 }
std::uint8_t uint8
Definition: settings.hpp:112
Definition: random.hpp:8
std::uint32_t uint32
Definition: settings.hpp:116
TESTABLE const DataMap< CachedString, CachedString > & getTags() const
Returns a map with key-to-tag-mapping for osm-tags.
Definition: geo_object.cpp:30
Definition: color.hpp:40
bool Conv(const string &str, bool *out, bool tryToCache)
Use some extra function for bool conversion.
FunctionEnum
Enum with all functions.
Represents a string which could be cached into an internal cache.
FunctionOperationNode(const node_ptr &enclosed)
Creates a new Function operation which simply does nothing.
std::vector< node_ptr > enclosed
The subexpressions enclosed by this operation.
This file is part of alaCarte.
float a
Definition: color.hpp:135
op::FunctionEnum operation
The operation type.
T rand()
Definition: random.hpp:43
std::size_t hash_value(const TileIdentifier &ti)
Returns a hash for the TileIdentifier.
Specifies the color.
Definition: mapcss_def.hpp:108
virtual string eval(GeoObject *obj) const
Evaluates a subtree under this node.
string ToString(const bool &v)