-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.cpp
305 lines (280 loc) · 13 KB
/
base.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
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
#include <iostream>
#include <cmath>//for abs()
#include <string>//for to_string()
#include <fstream>//for ppm and
#include <thread>
#include <mutex>
#include <sstream>
#include <chrono>
#include "complex.h"
#define width 200//aka. real axis, x
#define height 120//aka imaginary axis, y
#define thickness 60//must be at least 2, z the axis where c in //f()=z^2+c comes from
//#define theforth 100
//#include "mathPrinter.h"
#define highTHRESHOLD 100//how outwards can a pixel can go without being out of bounds
//#define lowTHRESHOLD -100
#define PIXEL_WEIGHT 10 //how long a pixel has to be tested to concur it stays in bounds //also number of shades of color
using namespace std;
mutex coutMutex;
int mandel(complex number, complex z)//f()=z^2+c
{
int counter=PIXEL_WEIGHT;
//while(((z.geti()+z.getr())/2)>highTHRESHOLD||(z.geti()+z.getr())<lowTHRESHOLD)
for (int i = 0; i < PIXEL_WEIGHT && !(((abs(z.geti())+abs(z.getr()))/2)>highTHRESHOLD) ; i++)
{
//number.seti(number.getr());//erase this later, this is me trying to get the julia set, Its fucks up spectacularly
z=z*z+number;
//number=z;//this should not be here for the standard but it was interesting
counter--;
//cout << z.getr() << " + " <<z.geti() <<endl; //for debugging calculation
}
//if(((abs(z.geti())+abs(z.getr()))/2)>highTHRESHOLD)
// return false;//it got out of bounds
//return true;//it was in bounds
if(counter==0)//
return true;//
else//
return false;// this is when you want monochrome pictures
//return counter;//this is when you want grayscale pictures
}
//void picCreate(double z)
//{
// complex compz;
// ofstream pic;
//
// string s=to_string((int)z) + "th_layer.ppm";
// pic.open (s);
// //use the next line and delete the one after if you want to make grayscale images
// //pic << "P2" << endl << height << " " << width << endl << PIXEL_WEIGHT << endl;//first you have enter the color mode width height and number of colors that will be used on that color mode to make the file
// pic << "P1" << endl << height << " " << width << endl << endl;
// compz.seti(z/(thickness/2));
// coutMutex.lock();
// cout <<"layer "<< z << " has been initiated" << endl;
// coutMutex.unlock();
//
// for(int x = -(width*1.25)/2; x< (width*0.75)/2;x++)
// {
// for(int y = -(height)/2; y<(height)/2; y++)
// {
// complex n;
// n.seti(y/(height/4.0));
// n.setr(x/(width/4.0));
// //table.input(x,y,mandel(n));
// pic << mandel(n,compz) << " ";
// }
// pic << endl;
// }
// pic.close();
// coutMutex.lock();
// cout <<"layer "<< z << " has been completed" << endl;
// coutMutex.unlock();
//}
void BetterPicCreate(double z)
{
complex compz;
ofstream pic;
string s=to_string((int)z) + "th_layer.ppm";
pic.open (s);
//use the next line and delete the one after if you want to make grayscale images
//pic << "P2" << endl << height << " " << width << endl << PIXEL_WEIGHT << endl;//first you have enter the color mode width height and number of colors that will be used on that color mode to make the file
pic << "P1" << endl << height << " " << width << endl << endl;
compz.seti(z/(thickness/2.0));//changed the the divider to 2.o because it was compressing the z too much
//compz.setr(z/(thickness/4.0));//delete later!!!!!!!
coutMutex.lock();
cout <<"layer "<< z << " has been initiated" << endl;
coutMutex.unlock();
for(int x = -(width*0.55); x< (width*0.45);x++)//o.55 and o.45 is for pushing the view a bit downwards
{
for(int y = -height/2; y<(height/2); y++)//same as x just pushing the view down
{
complex n;
n.seti(y/(height/4.0));
n.setr(x/(width/4.0));
//table.input(x,y,mandel(n));
pic << mandel(n,compz) << " ";
}
pic << endl;
}
pic.close();
coutMutex.lock();
cout <<"layer "<< z << " has been completed" << endl;
coutMutex.unlock();
}
//struct voxel
//{
// ostream x = "facet normal 1.000000 0.000000 0.000000"<< endl <<"outer loop"<< endl <<"vertex 1.0 1.0 0.0 "<< endl <<"vertex 1.0 1.0 1.0 "<< endl <<"vertex 1.0 0.0 0.0"<< endl <<"endloop"<< endl <<" endfacet"<< endl <<"facet normal 1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 1.0 0.0 0.0"<< endl <<"vertex 1.0 1.0 1.0"<< endl <<"vertex 1.0 0.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<""
// ostream negx = "facet normal -1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 0.0 1.0 1.0"<< endl <<"vertex 0.0 1.0 0.0"<< endl <<"vertex 0.0 0.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal -1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 0.0 0.0 1.0"<< endl <<"vertex 0.0 1.0 0.0"<< endl <<" vertex 0.0 0.0 0.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<""
// ostream y = "facet normal 0.0 1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 1.0 1.0 1.0"<< endl <<"vertex 1.0 1.0 0.0"<< endl <<" 0.0 1.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 1.0 0.0"<< endl <<" loop"<< endl <<"vertex 0.0 1.0 1.0"<< endl <<"vertex 1.0 1.0 0.0"<< endl <<"vertex 0.0 1.0 0.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<""
// ostream negy = "facet normal 0.0 -1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 1.0 0.0 0.0"<< endl <<"vertex 1.0 0.0 1.0"<< endl <<"vertex 0.0 0.0 0.0"<< endl <<"endloop"<< endl <<" endfacet"<< endl <<"facet normal 0.0 -1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex 0.0 0.0 0.0"<< endl <<"vertex 1.0 0.0 1.0"<< endl <<"vertex 0.0 0.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<""
// ostream z = "facet normal 0.0 0.0 1.0"<< endl <<"outer loop"<< endl <<"vertex 1.0 1.0 1.0"<< endl <<"vertex 0.0 1.0 1.0"<< endl <<"vertex 1.0 0.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 0.0 1.0"<< endl <<"outer loop"<< endl <<"vertex 1.0 0.0 1.0"<< endl <<"vertex 0.0 1.0 1.0"<< endl <<"vertex 0.0 0.0 1.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<""
// ostream negz = "facet normal 0.0 0.0 -1.0"<< endl <<" loop"<< endl <<"vertex 0.0 1.0 0.0"<< endl <<"vertex 1.0 1.0 0.0"<< endl <<"vertex 0.0 0.0 0.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 0.0 -1.0"<< endl <<"outer loop"<< endl <<"vertex 0.0 0.0 0.0"<< endl <<"vertex 1.0 1.0 0.0"<< endl <<"vertex 1.0 0.0 0.0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"
//};
void print(bool nowmat [width][height])
{
for(int y = 0; y<height; y++)
{
for(int x = 0; x<width ;x++)
cout << nowmat[x][y];
cout << endl;
}
}
int main()
{
int num_of_threads=2;//thickness is the outer dimension that I use for creating the layers, hence there is a ppm file for each layer
thread * threads = new thread [num_of_threads];//creates a thread for each file
for(int i=-thickness/2; i<(thickness/2)/num_of_threads;i++)
{
for(int a=0; a<num_of_threads;a++)
threads[a]=thread(BetterPicCreate,i*num_of_threads+a);//each thread also need to know which layer they are and this is why picCreate function has a double z input
for(int b = 0; b<num_of_threads;b++)
threads[b].join();
}
/*for(int i=0; i<num_of_threads;i++)
BetterPicCreate(i);*/
bool nowmat [width][height];
bool beforemat [width][height];
bool aftermat [width][height];
ofstream stl;
stl.open("file.stl");
stl << "solid" << endl;
//for(int z= 0 ; z<thickness ; z++)//only positive values
for(int z=-thickness/2;z<thickness/2;z++)//both negative and positive values (make sure you gave enough "thickness" leeway
{
if(z==0)//if(z==-thickness/2)
{
ifstream now;
string s=to_string((int)z) + "th_layer.ppm";
now.open (s);
getline(now,s);
getline(now,s);
getline(now,s);
for(int x = 0; x<width ;x++)
{
for(int y = 0; y<height; y++)
{
bool b;
now >> b;
nowmat[x][y] = b;
}
}
now.close();
cout << z << "th_layer.ppm read as a now file" << endl;
}
else//nowmat=aftermat;
{
for(int x = 0; x<width ;x++)
{
for(int y = 0; y<height; y++)
{
bool b;
b=aftermat[x][y];
nowmat[x][y] = b;
}
}
}
if(z==0)//if(z==-thickness/2)
{
for(int x = 0; x<width ;x++)
for(int y = 0; y<height; y++)
beforemat[x][y] = false;
cout << "empty before matrix created"<< endl;
}
else
{
/*ifstream before;
string s=to_string((int)(z-1)) + "th_layer.ppm";
before.open (s);
getline(before,s);
getline(before,s);
getline(before,s);*/
for(int x = 0; x<width ;x++)
{
for(int y = 0; y<height; y++)
{
bool b;
b = nowmat[x][y];
beforemat[x][y] = b;
}
}
cout << z-1 << "th_layer.ppm read as a before file" << endl;
/*before.close();*/
}
//if(z==thickness/2-1)
if(z==thickness-1)
{
for(int x = 0; x<width ;x++)
for(int y = 0; y<height; y++)
aftermat[x][y] = false;
cout << "empty after matrix created"<< endl;
}
else
{
ifstream after;
string s=to_string((int)(z+1)) + "th_layer.ppm";
after.open (s);
getline(after,s);
getline(after,s);
getline(after,s);
for(int x = 0; x<width ;x++)
{
for(int y = 0; y<height; y++)
{
bool b;
after >> b;
aftermat[x][y] = b;
}
}
after.close();
cout << z+1 << "th_layer.ppm read as a after file" << endl;
}
/*cout << "printing now matrix" << endl;
print(nowmat);
cout << "printing before matrix" << endl;
print (beforemat);
cout << "printing after matrix" << endl;
print(aftermat);
*/
for(int x = 0; x<width ;x++)
{
for(int y = 0; y<height; y++)
{
if(nowmat[x][y]==1)
{
if(x+1!=width && nowmat[x+1][y]==0)
stl << "facet normal 1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z<<".0 "<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z+1<<".0 "<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<" endfacet"<< endl <<"facet normal 1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
if(x-1!=-1 && nowmat[x-1][y]==0) //negx
stl << "facet normal -1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal -1.0 0.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
if(y+1!= height && nowmat[x][y+1]==0) //y
stl << "facet normal 0.0 1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
if(y-1==-1 && nowmat[x][y]==1 || nowmat[x][y-1]==0) //negy
stl << "facet normal 0.0 -1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 -1.0 0.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
if(aftermat[x][y]==0) //z
stl << "facet normal 0.0 0.0 1.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 0.0 1.0"<< endl <<"outer loop"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z+1<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z+1<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
if(beforemat[x][y]==1) //negz
stl << "facet normal 0.0 0.0 -1.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl <<"facet normal 0.0 0.0 -1.0"<< endl <<"outer loop"<< endl <<"vertex "<<x<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y+1<<".0 "<<z<<".0"<< endl <<"vertex "<<x+1<<".0 "<<y<<".0 "<<z<<".0"<< endl <<"endloop"<< endl <<"endfacet"<< endl;
}
}
}
}
stl << "endsolid";
stl.close();
//for(int x = -width/2; x< width;x++)
//{
// for(int y = -height/2; y<height; y++)
// cout << table.reader(x,y);
// cout << endl;
//}
/*for(int x = -width/2; x< width/2;x++)
{
for(int y = -height/2; y<height/2; y++)
pic << table.reader(x,y) << " ";
pic << endl;
}*/
return 0;
}
//for checking if mandel and complex.h is working
/*complex z;
z.seti(-0.5);
z.setr(-0.75);
cout << mandel(z)<< endl ;*/