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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* As a special exception, you have permission to link this program
* with the CGAL library and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of the
* software in the executable aside from CGAL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "value.h"
#include "printutils.h"
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <sstream>
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/format.hpp>
#include "boost-utils.h"
#include "boosty.h"
/*Unicode support for string lengths and array accesses*/
#include <glib.h>
#include <boost/math/special_functions/fpclassify.hpp>
using boost::math::isnan;
using boost::math::isinf;
std::ostream &operator<<(std::ostream &stream, const Filename &filename)
{
fs::path fnpath = fs::path( (std::string)filename );
fs::path fpath = boostfs_uncomplete(fnpath, fs::current_path());
stream << QuotedString(boosty::stringy( fpath ));
return stream;
}
// FIXME: This could probably be done more elegantly using boost::regex
std::ostream &operator<<(std::ostream &stream, const QuotedString &s)
{
stream << '"';
BOOST_FOREACH(char c, s) {
switch (c) {
case '\t':
stream << "\\t";
break;
case '\n':
stream << "\\n";
break;
case '"':
case '\\':
stream << '\\';
stream << c;
break;
default:
stream << c;
}
}
stream << '"';
return stream;
}
Value Value::undefined;
Value::Value() : value(boost::blank())
{
// std::cout << "creating undef\n";
}
Value::Value(bool v) : value(v)
{
// std::cout << "creating bool\n";
}
Value::Value(int v) : value(double(v))
{
// std::cout << "creating int\n";
}
Value::Value(double v) : value(v)
{
// std::cout << "creating double " << v << "\n";
}
Value::Value(const std::string &v) : value(v)
{
// std::cout << "creating string\n";
}
Value::Value(const char *v) : value(std::string(v))
{
// std::cout << "creating string from char *\n";
}
Value::Value(char v) : value(std::string(1, v))
{
// std::cout << "creating string from char\n";
}
Value::Value(const VectorType &v) : value(v)
{
// std::cout << "creating vector\n";
}
Value::Value(const RangeType &v) : value(v)
{
// std::cout << "creating range\n";
}
Value::ValueType Value::type() const
{
return static_cast<ValueType>(this->value.which());
}
bool Value::isUndefined() const
{
return this->type() == UNDEFINED;
}
bool Value::toBool() const
{
switch (this->type()) {
case BOOL:
return boost::get<bool>(this->value);
break;
case NUMBER:
return boost::get<double>(this->value)!= 0;
break;
case STRING:
return boost::get<std::string>(this->value).size() > 0;
break;
case VECTOR:
return boost::get<VectorType >(this->value).size() > 0;
break;
case RANGE:
return true;
break;
default:
return false;
break;
}
}
double Value::toDouble() const
{
double d = 0;
getDouble(d);
return d;
}
bool Value::getDouble(double &v) const
{
const double *d = boost::get<double>(&this->value);
if (d) {
v = *d;
return true;
}
return false;
}
class tostring_visitor : public boost::static_visitor<std::string>
{
public:
template <typename T> std::string operator()(const T &op1) const {
// std::cout << "[generic tostring_visitor]\n";
return boost::lexical_cast<std::string>(op1);
}
std::string operator()(const double &op1) const {
if (op1 != op1) { // Fix for avoiding nan vs. -nan across platforms
return "nan";
}
if (op1 == 0) {
return "0"; // Don't return -0 (exactly -0 and 0 equal 0)
}
#ifdef OPENSCAD_TESTING
// Quick and dirty hack to work around floating point rounding differences
// across platforms for testing purposes.
std::stringstream tmp;
tmp.precision(12);
tmp.setf(std::ios_base::fixed);
tmp << op1;
std::string tmpstr = tmp.str();
size_t endpos = tmpstr.find_last_not_of('0');
if (endpos >= 0 && tmpstr[endpos] == '.') endpos--;
tmpstr = tmpstr.substr(0, endpos+1);
size_t dotpos = tmpstr.find('.');
if (dotpos != std::string::npos) {
if (tmpstr.size() - dotpos > 12) tmpstr.erase(dotpos + 12);
while (tmpstr[tmpstr.size()-1] == '0') tmpstr.erase(tmpstr.size()-1);
}
if ( tmpstr.compare("-0") == 0 ) tmpstr = "0";
tmpstr = two_digit_exp_format( tmpstr );
return tmpstr;
#else
// attempt to emulate Qt's QString.sprintf("%g"); from old OpenSCAD.
// see https://github.com/openscad/openscad/issues/158
std::stringstream tmp;
tmp.unsetf(std::ios::floatfield);
tmp << op1;
return tmp.str();
#endif
}
std::string operator()(const boost::blank &) const {
return "undef";
}
std::string operator()(const bool &v) const {
return v ? "true" : "false";
}
std::string operator()(const Value::VectorType &v) const {
std::stringstream stream;
stream << '[';
for (size_t i = 0; i < v.size(); i++) {
if (i > 0) stream << ", ";
stream << v[i];
}
stream << ']';
return stream.str();
}
std::string operator()(const Value::RangeType &v) const {
return (boost::format("[%1% : %2% : %3%]") % v.begin_val % v.step_val % v.end_val).str();
}
};
std::string Value::toString() const
{
return boost::apply_visitor(tostring_visitor(), this->value);
}
const Value::VectorType &Value::toVector() const
{
static VectorType empty;
const VectorType *v = boost::get<VectorType>(&this->value);
if (v) return *v;
else return empty;
}
bool Value::getVec2(double &x, double &y) const
{
if (this->type() != VECTOR) return false;
const VectorType &v = toVector();
if (v.size() != 2) return false;
return (v[0].getDouble(x) && v[1].getDouble(y));
}
bool Value::getVec3(double &x, double &y, double &z, double defaultval) const
{
if (this->type() != VECTOR) return false;
const VectorType &v = toVector();
if (v.size() == 2) {
getVec2(x, y);
z = defaultval;
return true;
}
else {
if (v.size() != 3) return false;
}
return (v[0].getDouble(x) && v[1].getDouble(y) && v[2].getDouble(z));
}
Value::RangeType Value::toRange() const
{
const RangeType *val = boost::get<RangeType>(&this->value);
if (val) {
return *val;
}
else return RangeType(0,0,0);
}
Value &Value::operator=(const Value &v)
{
if (this != &v) {
this->value = v.value;
}
return *this;
}
class equals_visitor : public boost::static_visitor<bool>
{
public:
template <typename T, typename U> bool operator()(const T &, const U &) const {
return false;
}
template <typename T> bool operator()(const T &op1, const T &op2) const {
return op1 == op2;
}
};
bool Value::operator==(const Value &v) const
{
return boost::apply_visitor(equals_visitor(), this->value, v.value);
}
bool Value::operator!=(const Value &v) const
{
return !(*this == v);
}
class less_visitor : public boost::static_visitor<bool>
{
public:
template <typename T, typename U> bool operator()(const T &, const U &) const {
return false;
}
bool operator()(const double &op1, const double &op2) const {
return op1 < op2;
}
bool operator()(const std::string &op1, const std::string &op2) const {
return op1 < op2;
}
};
class greater_visitor : public boost::static_visitor<bool>
{
public:
template <typename T, typename U> bool operator()(const T &, const U &) const {
return false;
}
bool operator()(const double &op1, const double &op2) const {
return op1 > op2;
}
bool operator()(const std::string &op1, const std::string &op2) const {
return op1 > op2;
}
};
bool Value::operator<(const Value &v) const
{
return boost::apply_visitor(less_visitor(), this->value, v.value);
}
bool Value::operator>=(const Value &v) const
{
return !(*this < v);
}
bool Value::operator>(const Value &v) const
{
return boost::apply_visitor(greater_visitor(), this->value, v.value);
}
bool Value::operator<=(const Value &v) const
{
return !(*this > v);
}
class plus_visitor : public boost::static_visitor<Value>
{
public:
template <typename T, typename U> Value operator()(const T &, const U &) const {
return Value::undefined;
}
Value operator()(const double &op1, const double &op2) const {
return Value(op1 + op2);
}
Value operator()(const Value::VectorType &op1, const Value::VectorType &op2) const {
Value::VectorType sum;
for (size_t i = 0; i < op1.size() && i < op2.size(); i++) {
sum.push_back(op1[i] + op2[i]);
}
return Value(sum);
}
};
Value Value::operator+(const Value &v) const
{
return boost::apply_visitor(plus_visitor(), this->value, v.value);
}
class minus_visitor : public boost::static_visitor<Value>
{
public:
template <typename T, typename U> Value operator()(const T &, const U &) const {
return Value::undefined;
}
Value operator()(const double &op1, const double &op2) const {
return Value(op1 - op2);
}
Value operator()(const Value::VectorType &op1, const Value::VectorType &op2) const {
Value::VectorType sum;
for (size_t i = 0; i < op1.size() && i < op2.size(); i++) {
sum.push_back(op1[i] - op2[i]);
}
return Value(sum);
}
};
Value Value::operator-(const Value &v) const
{
return boost::apply_visitor(minus_visitor(), this->value, v.value);
}
Value Value::multvecnum(const Value &vecval, const Value &numval)
{
// Vector * Number
VectorType dstv;
BOOST_FOREACH(const Value &val, vecval.toVector()) {
dstv.push_back(val * numval);
}
return Value(dstv);
}
Value Value::multmatvec(const Value &matrixval, const Value &vectorval)
{
const VectorType &matrixvec = matrixval.toVector();
const VectorType &vectorvec = vectorval.toVector();
// Matrix * Vector
VectorType dstv;
for (size_t i=0;i<matrixvec.size();i++) {
if (matrixvec[i].type() != VECTOR ||
matrixvec[i].toVector().size() != vectorvec.size()) {
return Value();
}
double r_e = 0.0;
for (size_t j=0;j<matrixvec[i].toVector().size();j++) {
if (matrixvec[i].toVector()[j].type() != NUMBER || vectorvec[j].type() != NUMBER) {
return Value();
}
r_e += matrixvec[i].toVector()[j].toDouble() * vectorvec[j].toDouble();
}
dstv.push_back(Value(r_e));
}
return Value(dstv);
}
Value Value::multvecmat(const Value &vectorval, const Value &matrixval)
{
const VectorType &vectorvec = vectorval.toVector();
const VectorType &matrixvec = matrixval.toVector();
assert(vectorvec.size() == matrixvec.size());
// Vector * Matrix
VectorType dstv;
for (size_t i=0;i<matrixvec[0].toVector().size();i++) {
double r_e = 0.0;
for (size_t j=0;j<vectorvec.size();j++) {
if (matrixvec[j].type() != VECTOR ||
matrixvec[j].toVector()[i].type() != NUMBER ||
vectorvec[j].type() != NUMBER) {
return Value::undefined;
}
r_e += vectorvec[j].toDouble() * matrixvec[j].toVector()[i].toDouble();
}
dstv.push_back(Value(r_e));
}
return Value(dstv);
}
Value Value::operator*(const Value &v) const
{
if (this->type() == NUMBER && v.type() == NUMBER) {
return Value(this->toDouble() * v.toDouble());
}
else if (this->type() == VECTOR && v.type() == NUMBER) {
return multvecnum(*this, v);
}
else if (this->type() == NUMBER && v.type() == VECTOR) {
return multvecnum(v, *this);
}
else if (this->type() == VECTOR && v.type() == VECTOR) {
const VectorType &vec1 = this->toVector();
const VectorType &vec2 = v.toVector();
if (vec1[0].type() == NUMBER && vec2[0].type() == NUMBER &&
vec1.size() == vec2.size()) {
// Vector dot product.
double r = 0.0;
for (size_t i=0;i<vec1.size();i++) {
if (vec1[i].type() != NUMBER || vec2[i].type() != NUMBER) {
return Value::undefined;
}
r += (vec1[i].toDouble() * vec2[i].toDouble());
}
return Value(r);
} else if (vec1[0].type() == VECTOR && vec2[0].type() == NUMBER &&
vec1[0].toVector().size() == vec2.size()) {
return multmatvec(vec1, vec2);
} else if (vec1[0].type() == NUMBER && vec2[0].type() == VECTOR &&
vec1.size() == vec2.size()) {
return multvecmat(vec1, vec2);
} else if (vec1[0].type() == VECTOR && vec2[0].type() == VECTOR &&
vec1[0].toVector().size() == vec2.size()) {
// Matrix * Matrix
VectorType dstv;
BOOST_FOREACH(const Value &srcrow, vec1) {
dstv.push_back(multvecmat(srcrow, vec2));
}
return Value(dstv);
}
}
return Value::undefined;
}
Value Value::operator/(const Value &v) const
{
if (this->type() == NUMBER && v.type() == NUMBER) {
return Value(this->toDouble() / v.toDouble());
}
else if (this->type() == VECTOR && v.type() == NUMBER) {
const VectorType &vec = this->toVector();
VectorType dstv;
BOOST_FOREACH(const Value &vecval, vec) {
dstv.push_back(vecval / v);
}
return Value(dstv);
}
else if (this->type() == NUMBER && v.type() == VECTOR) {
const VectorType &vec = v.toVector();
VectorType dstv;
BOOST_FOREACH(const Value &vecval, vec) {
dstv.push_back(*this / vecval);
}
return Value(dstv);
}
return Value::undefined;
}
Value Value::operator%(const Value &v) const
{
if (this->type() == NUMBER && v.type() == NUMBER) {
return Value(fmod(boost::get<double>(this->value), boost::get<double>(v.value)));
}
return Value::undefined;
}
Value Value::operator-() const
{
if (this->type() == NUMBER) {
return Value(-this->toDouble());
}
else if (this->type() == VECTOR) {
const VectorType &vec = this->toVector();
VectorType dstv;
BOOST_FOREACH(const Value &vecval, vec) {
dstv.push_back(-vecval);
}
return Value(dstv);
}
return Value::undefined;
}
/*!
Append a value to this vector.
This must be of valtype VECTOR.
*/
/*
void Value::append(Value *val)
{
assert(this->type() == VECTOR);
this->vec.push_back(val);
}
*/
/*
* bracket operation [] detecting multi-byte unicode.
* If the string is multi-byte unicode then the index will offset to the character (2 or 4 byte) and not to the byte.
* A 'normal' string with byte chars are a subset of unicode and still work.
*/
class bracket_visitor : public boost::static_visitor<Value>
{
public:
Value operator()(const std::string &str, const double &idx) const {
int i = int(idx);
Value v;
//Check that the index is positive and less than the size in bytes
if ((i >= 0) && (i < (int)str.size())) {
//Ensure character (not byte) index is inside the character/glyph array
if( (unsigned) i < g_utf8_strlen( str.c_str(), str.size() ) ) {
gchar utf8_of_cp[6] = ""; //A buffer for a single unicode character to be copied into
gchar* ptr = g_utf8_offset_to_pointer(str.c_str(), i);
if(ptr) {
g_utf8_strncpy(utf8_of_cp, ptr, 1);
}
v = std::string(utf8_of_cp);
}
// std::cout << "bracket_visitor: " << v << "\n";
}
return v;
}
Value operator()(const Value::VectorType &vec, const double &idx) const {
int i = int(idx);
if ((i >= 0) && (i < (int)vec.size())) return vec[int(idx)];
return Value::undefined;
}
Value operator()(const Value::RangeType &range, const double &idx) const {
switch(int(idx)) {
case 0: return Value(range.begin_val);
case 1: return Value(range.step_val);
case 2: return Value(range.end_val);
}
return Value::undefined;
}
template <typename T, typename U> Value operator()(const T &, const U &) const {
// std::cout << "generic bracket_visitor\n";
return Value::undefined;
}
};
Value Value::operator[](const Value &v)
{
return boost::apply_visitor(bracket_visitor(), this->value, v.value);
}
void Value::RangeType::normalize() {
if ((step_val>0) && (end_val < begin_val)) {
std::swap(begin_val,end_val);
PRINT("DEPRECATED: Using ranges of the form [begin:end] with begin value greater than the end value is deprecated.");
}
}
uint32_t Value::RangeType::nbsteps() const {
if (isnan(step_val) || isinf(begin_val) || (isinf(end_val))) {
return std::numeric_limits<uint32_t>::max();
}
if ((begin_val == end_val) || isinf(step_val)) {
return 0;
}
if (step_val == 0) {
return std::numeric_limits<uint32_t>::max();
}
double steps;
if (step_val < 0) {
if (begin_val < end_val) {
return 0;
}
steps = (begin_val - end_val) / (-step_val);
} else {
if (begin_val > end_val) {
return 0;
}
steps = (end_val - begin_val) / step_val;
}
return steps;
}
Value::RangeType::iterator::iterator(Value::RangeType &range, type_t type) : range(range), val(range.begin_val)
{
this->type = type;
update_type();
}
void Value::RangeType::iterator::update_type()
{
if (range.step_val == 0) {
type = RANGE_TYPE_END;
} else if (range.step_val < 0) {
if (val < range.end_val) {
type = RANGE_TYPE_END;
}
} else {
if (val > range.end_val) {
type = RANGE_TYPE_END;
}
}
}
Value::RangeType::iterator::reference Value::RangeType::iterator::operator*()
{
return val;
}
Value::RangeType::iterator::pointer Value::RangeType::iterator::operator->()
{
return &(operator*());
}
Value::RangeType::iterator::self_type Value::RangeType::iterator::operator++()
{
if (type < 0) {
type = RANGE_TYPE_RUNNING;
}
val += range.step_val;
update_type();
return *this;
}
Value::RangeType::iterator::self_type Value::RangeType::iterator::operator++(int)
{
self_type tmp(*this);
operator++();
return tmp;
}
bool Value::RangeType::iterator::operator==(const self_type &other) const
{
if (type == RANGE_TYPE_RUNNING) {
return (type == other.type) && (val == other.val) && (range == other.range);
} else {
return (type == other.type) && (range == other.range);
}
}
bool Value::RangeType::iterator::operator!=(const self_type &other) const
{
return !(*this == other);
}
|