-
Notifications
You must be signed in to change notification settings - Fork 12
/
openscad-triangle-writer.cpp
130 lines (110 loc) · 3.64 KB
/
openscad-triangle-writer.cpp
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
/*
* Copyright 2019 Assaf Gordon <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*/
#include <ostream>
#include <iostream>
#include <vector>
#include <gp_Pnt.hxx>
#include "triangle.h"
using namespace std;
/* Write the faces/triangles as an ASCII stl file
(with invalud 'normals' value - but these are ignored anyhow in OpenSCAD */
void write_triangles_ascii_stl(const Face_vector& faces)
{
cout << "solid" << endl;
for (auto &f : faces)
f.write_ascii_stl(cout);
cout << "endsolid" << endl;
}
/* Write the faces/triangles as two vectors (one "POINTS", one "FACES")
that will be used with a single call to "polyhedron"). */
void write_triangle_scad(const Face_vector& faces)
{
Face all;
// Merge all faces (i.e. triangles from all faces)
// into one "face" (just a container, no special meaning of a "face").
for (auto &f : faces)
all.add_face(f);
// Write vector of points and faces
cout << "points = " ;
all.write_points_vector(cout);
cout << "faces = ";
all.write_face_vector(cout);
// Call Polyhedron
cout << "module solid_object() {" << endl;
cout << " polyhedron (points,faces);"<< endl;
cout << "}" << endl;
cout << endl;
cout << "solid_object();" << endl;
}
#define NUM_COLORS 12
const char* colors[NUM_COLORS] = {
"black",
"Violet",
"red",
"blue",
"LawnGreen",
"Orange",
"DeepPink",
"Gold",
"Cyan",
"Olive",
"Gray",
"SpringGreen"
};
/* Write every faces (i.e. all trianges of each face) into a separate points/faces
vector pairs.
Include $preview code to show each face in a different color
(sadly, resulting in an invalid 3D object which can't be exported to STL,
because each face is a 2D object with depth of ZERO).
In non-preview mode,
Include code to merge all the vectors and make a single "Polyhedron" call. */
void write_faces_scad (const Face_vector& faces)
{
int i = 1;
for (auto &f : faces) {
cout << "face_" << i << "_points = " ;
f.write_points_vector(cout);
cout << "face_" << i << "_faces = " ;
f.write_face_vector(cout);
cout << endl ;
++i;
}
/* crazy colors version, draw each face by itself */
cout << "module crazy_colors() {" << endl;
for (i=1;i<=faces.size();++i) {
const char* color = colors[i%NUM_COLORS] ;
cout << "color(\"" << color << "\")" << endl;
cout << "polyhedron(face_" << i <<"_points, face_" << i << "_faces);" << endl ;
}
cout << "}" << endl;
cout << "function add_offset(vec,ofs) = [for (x=vec) x + [ofs,ofs,ofs]];" << endl;
cout << "module solid_object() {" << endl;
cout << " tmp1_points = face_1_points;" << endl;
cout << " tmp1_faces = face_1_faces;" << endl;
cout << endl;
for (i=2;i<=faces.size();++i) {
cout << " tmp"<<i<<"_points = concat(tmp"<<(i-1)<<"_points, face_"<<i<<"_points);" << endl;
cout << " tmp"<<i<<"_faces = concat(tmp"<<(i-1)<<"_faces,add_offset(face_"<<i<<"_faces,len(tmp"<<(i-1)<<"_points)));" << endl;
cout << endl;
}
cout << " polyhedron (tmp"<<(faces.size())<<"_points, tmp"<<(faces.size())<<"_faces);"<< endl;
cout << "}" << endl;
cout << endl;
cout << endl;
cout << "if ($preview) {;" << endl;
cout << " crazy_colors();" << endl;
cout << "} else {" << endl;
cout << " solid_object();" << endl;
cout << "}" << endl;
}