Polyhedron now constructed using indices.
[rapcad:rapcad.git] / src / module / polyhedronmodule.cpp
1 /*
2  *   RapCAD - Rapid prototyping CAD IDE (www.rapcad.org)
3  *   Copyright (C) 2010-2014 Giles Bathgate
4  *
5  *   This program is free software: you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, either version 3 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "polyhedronmodule.h"
20 #include "context.h"
21 #include "vectorvalue.h"
22 #include "numbervalue.h"
23 #include "node/primitivenode.h"
24
25 PolyhedronModule::PolyhedronModule() : PrimitiveModule("polyhedron")
26 {
27         addParameter("points");
28         addParameter("surfaces");
29 }
30
31 Node* PolyhedronModule::evaluate(Context* ctx)
32 {
33         VectorValue* points=dynamic_cast<VectorValue*>(getParameterArgument(ctx,0));
34         VectorValue* surfaces=dynamic_cast<VectorValue*>(ctx->getArgumentDeprecated(1,"surfaces","triangles"));
35
36         PrimitiveNode* p=new PrimitiveNode();
37         if(!points||!surfaces)
38                 return p;
39
40         QList<Value*> children = points->getChildren();
41         foreach(Value* child,children) {
42                 VectorValue* point=dynamic_cast<VectorValue*>(child);
43                 if(point) {
44                         Point pt = point->getPoint();
45                         p->createVertex(pt);
46                 }
47         }
48         foreach(Value* s,surfaces->getChildren()) {
49                 Polygon* pg=p->createPolygon();
50                 VectorValue* surface=dynamic_cast<VectorValue*>(s);
51                 foreach(Value* indexVal,surface->getChildren()) {
52                         NumberValue* indexNum=dynamic_cast<NumberValue*>(indexVal);
53                         if(indexNum) {
54                                 int index = indexNum->getNumber();
55                                 if(index>=0&&index<children.count()) {
56                                         pg->append(index);
57                                 }
58                         }
59                 }
60
61         }
62
63         return p;
64
65 }