Merge branch 'children' of git://github.com/vicnet/openscad into vicnet-children

Conflicts:
	tests/CMakeLists.txt
brodykenrick-master
Marius Kintel 2013-10-13 13:19:34 -04:00
commit e77615be30
9 changed files with 286 additions and 57 deletions

View File

@ -74,7 +74,7 @@
:group 'scad-font-lock)
(defcustom scad-modules
'("child" "echo" "assign" "for" "intersection_for" "if" "else" ;;control.cc
'("child" "children" "echo" "assign" "for" "intersection_for" "if" "else" ;;control.cc
"cube" "sphere" "cylinder" "polyhedron" "square" "circle" "polygon" ;;primitives.cc
"scale" "rotate" "translate" "mirror" "multmatrix" ;;transform.cc
"union" "difference" "intersection" ;;csgops.cc

View File

@ -24,6 +24,7 @@
*
*/
#include <boost/foreach.hpp>
#include "module.h"
#include "node.h"
#include "evalcontext.h"
@ -33,24 +34,42 @@
#include <sstream>
#include "mathc99.h"
enum control_type_e {
CHILD,
ECHO,
ASSIGN,
FOR,
INT_FOR,
IF
};
#define foreach BOOST_FOREACH
class ControlModule : public AbstractModule
{
public:
control_type_e type;
ControlModule(control_type_e type) : type(type) { }
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const;
};
public: // types
enum Type {
CHILD,
CHILDREN,
ECHO,
ASSIGN,
FOR,
INT_FOR,
IF
};
public: // methods
ControlModule(Type type)
: type(type)
{ }
void for_eval(AbstractNode &node, const ModuleInstantiation &inst, size_t l,
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const;
static void for_eval(AbstractNode &node, const ModuleInstantiation &inst, size_t l,
const Context *ctx, const EvalContext *evalctx);
static const EvalContext* getLastModuleCtx(const EvalContext *evalctx);
static AbstractNode* getChild(const Value& value, const EvalContext* modulectx);
private: // data
Type type;
}; // class ControlModule
void ControlModule::for_eval(AbstractNode &node, const ModuleInstantiation &inst, size_t l,
const Context *ctx, const EvalContext *evalctx)
{
if (evalctx->numArgs() > l) {
@ -59,12 +78,8 @@ void for_eval(AbstractNode &node, const ModuleInstantiation &inst, size_t l,
Context c(ctx);
if (it_values.type() == Value::RANGE) {
Value::RangeType range = it_values.toRange();
if (range.end < range.begin) {
double t = range.begin;
range.begin = range.end;
range.end = t;
}
if (range.step > 0 && (range.begin-range.end)/range.step < 10000) {
range.normalize();
if (range.nbsteps()<10000) {
for (double i = range.begin; i <= range.end; i += range.step) {
c.set_variable(it_name, Value(i));
for_eval(node, inst, l+1, &c, evalctx);
@ -87,7 +102,58 @@ void for_eval(AbstractNode &node, const ModuleInstantiation &inst, size_t l,
}
}
AbstractNode *ControlModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
const EvalContext* ControlModule::getLastModuleCtx(const EvalContext *evalctx)
{
// Find the last custom module invocation, which will contain
// an eval context with the children of the module invokation
const Context *tmpc = evalctx;
while (tmpc->parent) {
const ModuleContext *modulectx = dynamic_cast<const ModuleContext*>(tmpc->parent);
if (modulectx) {
// This will trigger if trying to invoke child from the root of any file
// assert(filectx->evalctx);
if (modulectx->evalctx) {
return modulectx->evalctx;
}
return NULL;
}
tmpc = tmpc->parent;
}
return NULL;
}
// static
AbstractNode* ControlModule::getChild(const Value& value, const EvalContext* modulectx)
{
if (value.type()!=Value::NUMBER) {
// Invalid parameter
// (e.g. first child of difference is invalid)
PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number, vector, range.", value.toString());
return NULL;
}
double v;
if (!value.getDouble(v)) {
PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number, vector, range.", value.toString());
return NULL;
}
int n = trunc(v);
if (n < 0) {
PRINTB("WARNING: Negative children index (%d) not allowed", n);
return NULL; // Disallow negative child indices
}
if (n>=(int)modulectx->numChildren()) {
// How to deal with negative objects in this case?
// (e.g. first child of difference is invalid)
PRINTB("WARNING: Children index (%d) out of bounds (%d children)"
, n % modulectx->numChildren());
return NULL;
}
// OK
return modulectx->getChild(n)->evaluate(modulectx);
}
AbstractNode *ControlModule::instantiate(const Context* /*ctx*/, const ModuleInstantiation *inst, const EvalContext *evalctx) const
{
AbstractNode *node = NULL;
@ -107,29 +173,80 @@ AbstractNode *ControlModule::instantiate(const Context *ctx, const ModuleInstant
// Find the last custom module invocation, which will contain
// an eval context with the children of the module invokation
const Context *tmpc = evalctx;
while (tmpc->parent) {
const ModuleContext *filectx = dynamic_cast<const ModuleContext*>(tmpc->parent);
if (filectx) {
// This will trigger if trying to invoke child from the root of any file
// assert(filectx->evalctx);
if (filectx->evalctx) {
if (n < (int)filectx->evalctx->numChildren()) {
node = filectx->evalctx->getChild(n)->evaluate(filectx->evalctx);
}
else {
// How to deal with negative objects in this case?
const EvalContext *modulectx = getLastModuleCtx(evalctx);
if (modulectx==NULL) {
return NULL;
}
// This will trigger if trying to invoke child from the root of any file
if (n < (int)modulectx->numChildren()) {
node = modulectx->getChild(n)->evaluate(modulectx);
}
else {
// How to deal with negative objects in this case?
// (e.g. first child of difference is invalid)
PRINTB("WARNING: Child index (%d) out of bounds (%d children)",
n % filectx->evalctx->numChildren());
}
PRINTB("WARNING: Child index (%d) out of bounds (%d children)",
n % modulectx->numChildren());
}
return node;
}
if (type == CHILDREN)
{
const EvalContext *modulectx = getLastModuleCtx(evalctx);
if (modulectx==NULL) {
return NULL;
}
// This will trigger if trying to invoke child from the root of any file
// assert(filectx->evalctx);
if (evalctx->numArgs()<=0) {
// no parameters => all children
AbstractNode* node = new AbstractNode(inst);
for (int n = 0; n < (int)modulectx->numChildren(); ++n) {
AbstractNode* childnode = modulectx->getChild(n)->evaluate(modulectx);
if (childnode==NULL) continue; // error
node->children.push_back(childnode);
}
return node;
}
else if (evalctx->numArgs()>0) {
// one (or more ignored) parameter
const Value& value = evalctx->getArgValue(0);
if (value.type() == Value::NUMBER) {
return getChild(value,modulectx);
}
else if (value.type() == Value::VECTOR) {
AbstractNode* node = new AbstractNode(inst);
const Value::VectorType& vect = value.toVector();
foreach (const Value::VectorType::value_type& vectvalue, vect) {
AbstractNode* childnode = getChild(vectvalue,modulectx);
if (childnode==NULL) continue; // error
node->children.push_back(childnode);
}
return node;
}
tmpc = tmpc->parent;
else if (value.type() == Value::RANGE) {
AbstractNode* node = new AbstractNode(inst);
Value::RangeType range = value.toRange();
range.normalize();
if (range.nbsteps()>=10000) {
PRINTB("WARNING: Bad range parameter for children: too many elements (%d).", (int)((range.begin-range.end)/range.step));
return NULL;
}
for (double i = range.begin; i <= range.end; i += range.step) {
AbstractNode* childnode = getChild(Value(i),modulectx); // with error cases
if (childnode==NULL) continue; // error
node->children.push_back(childnode);
}
return node;
}
else {
// Invalid parameter
// (e.g. first child of difference is invalid)
PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number, vector, range.", value.toString());
return NULL;
}
}
return node;
return NULL;
}
if (type == INT_FOR)
@ -183,10 +300,11 @@ AbstractNode *ControlModule::instantiate(const Context *ctx, const ModuleInstant
void register_builtin_control()
{
Builtins::init("child", new ControlModule(CHILD));
Builtins::init("echo", new ControlModule(ECHO));
Builtins::init("assign", new ControlModule(ASSIGN));
Builtins::init("for", new ControlModule(FOR));
Builtins::init("intersection_for", new ControlModule(INT_FOR));
Builtins::init("if", new ControlModule(IF));
Builtins::init("child", new ControlModule(ControlModule::CHILD));
Builtins::init("children", new ControlModule(ControlModule::CHILDREN));
Builtins::init("echo", new ControlModule(ControlModule::ECHO));
Builtins::init("assign", new ControlModule(ControlModule::ASSIGN));
Builtins::init("for", new ControlModule(ControlModule::FOR));
Builtins::init("intersection_for", new ControlModule(ControlModule::INT_FOR));
Builtins::init("if", new ControlModule(ControlModule::IF));
}

View File

@ -119,7 +119,8 @@ Value Expression::evaluate(const Context *context) const
Value v2 = this->children[1]->evaluate(context);
Value v3 = this->children[2]->evaluate(context);
if (v1.type() == Value::NUMBER && v2.type() == Value::NUMBER && v3.type() == Value::NUMBER) {
return Value(v1.toDouble(), v2.toDouble(), v3.toDouble());
Value::RangeType range(v1.toDouble(), v2.toDouble(), v3.toDouble());
return Value(range);
}
return Value();
}

View File

@ -154,7 +154,7 @@ Highlighter::Highlighter(QTextDocument *parent)
tokentypes["import"] << "include" << "use" << "import_stl" << "import" << "import_dxf" << "dxf_dim" << "dxf_cross" << "surface";
typeformats["import"].setForeground(Qt::darkYellow);
tokentypes["special"] << "$children" << "child" << "$fn" << "$fa" << "$fs" << "$t" << "$vpt" << "$vpr";
tokentypes["special"] << "$children" << "child" << "children" << "$fn" << "$fa" << "$fs" << "$t" << "$vpt" << "$vpr";
typeformats["special"].setForeground(Qt::darkGreen);
tokentypes["extrude"] << "linear_extrude" << "rotate_extrude";

View File

@ -117,11 +117,6 @@ Value::Value(const RangeType &v) : value(v)
// std::cout << "creating range\n";
}
Value::Value(double begin, double step, double end) : value(RangeType(begin, step, end))
{
// std::cout << "creating range from numbers\n";
}
Value::ValueType Value::type() const
{
return static_cast<ValueType>(this->value.which());
@ -590,7 +585,7 @@ public:
Value operator()(const std::string &str, const double &idx) const {
int i = int(idx);
Value v;
if (i >= 0 && i < str.size()) {
if ((i >= 0) && (i < (int)str.size())) {
v = Value(str[int(idx)]);
// std::cout << "bracket_visitor: " << v << "\n";
}
@ -599,7 +594,7 @@ public:
Value operator()(const Value::VectorType &vec, const double &idx) const {
int i = int(idx);
if (i >= 0 && i < vec.size()) return vec[int(idx)];
if ((i >= 0) && (i < (int)vec.size())) return vec[int(idx)];
return Value::undefined;
}

View File

@ -3,6 +3,8 @@
#include <vector>
#include <string>
#include <algorithm>
#include <limits>
// Workaround for https://bugreports.qt-project.org/browse/QTBUG-22829
#ifndef Q_MOC_RUN
@ -39,6 +41,20 @@ public:
this->end == other.end;
}
/// inverse begin/end if begin is upper than end
void normalize() {
if ((step>0) && (end < begin)) {
std::swap(begin,end);
}
}
/// return number of steps, max int value if step is null
int nbsteps() const {
if (step<=0) {
return std::numeric_limits<int>::max();
}
return (int)((begin-end)/step);
}
double begin;
double step;
double end;
@ -65,7 +81,6 @@ public:
Value(const char v);
Value(const VectorType &v);
Value(const RangeType &v);
Value(double begin, double step, double end);
~Value() {}
ValueType type() const;

64
testdata/scad/misc/children-tests.scad vendored Normal file
View File

@ -0,0 +1,64 @@
module child1() {
echo("child1");
}
module child2() {
echo("child2");
}
module child3() {
echo("child3");
}
module child4() {
echo("child4");
}
module child5() {
echo("child5");
}
module test_children_empty() {
echo("Children empty: begin");
children();
echo("Children empty: end");
}
test_children_empty() {
child1();child2();child3();child4();child5();
}
module test_children_scalar() {
echo("Children scalar: begin");
children(0); // child1
children(4); // child5
children(2); // child3
children(5); // out
children(-1); // out
echo("Children scalar: end");
}
test_children_scalar() {
child1();child2();child3();child4();child5();
}
module test_children_vector() {
echo("Children vector: begin");
children([4]); // child5 last
children([0,3,1]); // child1, child4, child2
children([5,-1]); // out, out
echo("Children vector: end");
}
test_children_vector() {
child1();child2();child3();child4();child5();
}
module test_children_range() {
echo("Children range: begin");
children([0:4]); // all
children([1:2]); // child2, child3
children([0:2:4]); // child1, child3, child5
children([4:-1:0]); // out, out
echo("Children range: end");
}
test_children_range() {
child1();child2();child3();child4();child5();
}
// to avoid no object error
cube(1.0);

View File

@ -776,7 +776,8 @@ list(APPEND ECHO_FILES ${FUNCTION_FILES}
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/variable-scope-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/lookup-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/expression-shortcircuit-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/parent_module-tests.scad)
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/parent_module-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/children-tests.scad)
list(APPEND DUMPTEST_FILES ${FEATURES_FILES} ${EXAMPLE_FILES})
list(APPEND DUMPTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/escape-test.scad

View File

@ -0,0 +1,35 @@
ECHO: "Children empty: begin"
ECHO: "child1"
ECHO: "child2"
ECHO: "child3"
ECHO: "child4"
ECHO: "child5"
ECHO: "Children empty: end"
ECHO: "Children scalar: begin"
ECHO: "child1"
ECHO: "child5"
ECHO: "child3"
WARNING: Children index (5) out of bounds (5 children)
WARNING: Negative children index (-1) not allowed
ECHO: "Children scalar: end"
ECHO: "Children vector: begin"
ECHO: "child5"
ECHO: "child1"
ECHO: "child4"
ECHO: "child2"
WARNING: Children index (5) out of bounds (5 children)
WARNING: Negative children index (-1) not allowed
ECHO: "Children vector: end"
ECHO: "Children range: begin"
ECHO: "child1"
ECHO: "child2"
ECHO: "child3"
ECHO: "child4"
ECHO: "child5"
ECHO: "child2"
ECHO: "child3"
ECHO: "child1"
ECHO: "child3"
ECHO: "child5"
WARNING: Bad range parameter for children: too many elements (-4).
ECHO: "Children range: end"