-
Notifications
You must be signed in to change notification settings - Fork 2
/
polyOffset.vfl
395 lines (346 loc) · 13.9 KB
/
polyOffset.vfl
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
float clampAngle(float inputAngle){
float x = inputAngle/3.14159265;
x=x%2;
if(x>1){
x=-1+(x%1);
}
if(x<-1){
x=1+(x%1);
}
x = x*3.14159265;
return x;
}
void createStar(vector p;int points[]){ //order points clockwise.?
printf("todo");
}
void mergeArray(int array1[];int array2[]){
foreach(int a2; array2){
if(find(array1,a2)<0){
push(array1, a2);
}
}
}
float angleBetweenVectors2(vector a; vector b){
a = normalize(a*{1,0,1});
b = normalize(b*{1,0,1});
vector c = normalize(a+b);
float result = atan2(c[2],c[0]);
return(result);
}
float sidecheckXZ(vector s; vector e; vector p){ //+=left
float result = (e[0]-s[0])*(p[2]-s[2]) - (e[2]-s[2])*(p[0]-s[0]);
return result;
}
float diameter = 0.14;
float radius = diameter/2;
float pi = 3.14159264;
float tau = 3.14159264*2;
int numprim = @numprim;//contains starting curves
//need to remove points that are too close to intersection, and keep track of new connect point.
//int intersections[];
string intersectionData[];
int pointleafleaf[]; // datastructure: p,left,right,p,left,right;
vector V_pll[]; //store positions of pointleafleaf, because point query is failing.
vector V_interP[]; //store locations of intersections.
int donotsweep[];
int allneigh[];
for(int p=0;p<npoints(0);p++){ //gather intersections
if(neighbourcount(0,p)>2){
vector loc = point(0,"P",p);
vector intersectP = point(0,"P", p);
int neighs[] = neighbours(0,p);
mergeArray(allneigh,neighs);
push(donotsweep,p);
//removepoint(0,p,1);
push(V_interP,intersectP);
string S_output = itoa(p)+",";
for(int n=0;n<len(neighs);n++){
if(neighs[n]>=0){
S_output+=itoa(neighs[n]);
}
if(n!=len(neighs)-1){S_output+=",";}
}
push(intersectionData,S_output);
//push(intersectionData,S_output);
//intersectionData+=S_output;
}
}
printf("intersectionData: %f\\n",intersectionData);
//printf("intersectionData[0]: %f\\n",intersectionData[0]);
//printf("intersectionData[1]: %f\\n",intersectionData[1]);
//printf("allneigh: %f\\n",allneigh);
//CREATE SWEEPS
int count = 0;
for(int i_prim=0;i_prim<numprim;i_prim++){
//for(int i_prim=0;i_prim<1;i_prim++){
count++;
//printf("count:%f",count);
int I_points[] = primpoints(0,i_prim);
int i_firstVert = I_points[0];
int i_lastVert = I_points[len(I_points)-1];
//printf("fv%f,lv%f\\n",i_firstVert,i_lastVert);
//printf("firstvert: %f, lastvert: %f\\n",i_firstVert,i_lastVert);
int zig = addprim(0,"polyline");
int zigPoints[];
for(int i_p=0;i_p<len(I_points)-1;i_p++){//expand line
//1. get angle to next point
if(find(donotsweep,I_points[i_p])<-0){ //greenlight sweep for point. if it isn't in the donotsweep[]!
vector thisposition = point(0,"P",I_points[i_p]);
vector nextposition = point(0,"P",I_points[i_p+1]);
float dx= nextposition[0] - thisposition[0];
float dy= nextposition[2] - thisposition[2];
float thisangle = atan2(dy,dx);
float angleLeft;
float angleRight;
int leftflag;
if(i_p%2==0){
angleLeft = clampAngle(thisangle+pi/2);//rotate counterclockwise 90°
angleRight = clampAngle(thisangle-pi/2);//rotate clockwise 90°
leftflag = 0;
}
else{
angleLeft = clampAngle(thisangle-pi/2);//rotate counterclockwise 90°
angleRight = clampAngle(thisangle+pi/2);//rotate clockwise 90°
leftflag = 1;
}
vector left = set(thisposition[0]+radius*cos(angleLeft),thisposition[1],thisposition[2]+radius*sin(angleLeft));
vector right = set(thisposition[0]+radius*cos(angleRight),thisposition[1],thisposition[2]+radius*sin(angleRight));
//colorPass = setpointattrib(0, "uv", I_points[i_p], set(i_p,5,0), "set");
int pointleft = addpoint(0,left);
int pointright= addpoint(0,right);
vector colorOne = {0,1,0.2};
if(leftflag==1){
int swap = pointleft;
pointleft = pointright;
pointright = swap;
}
int colorPass;
colorPass = setpointattrib(0, "uv", I_points[i_p], set(i_p,0.5,0), "set");
//colorPass = setpointattrib(0, "Cd", pointleft, set(0.5,1.5,0), "set");
colorPass = setpointattrib(0, "uv", pointleft, set(i_p,1,0), "set");
//colorPass = setpointattrib(0, "Cd", pointright, set(1,0,2), "set");
colorPass = setpointattrib(0, "uv", pointright, set(i_p,0,0), "set");
if(leftflag==1){
int swap = pointleft;
pointleft = pointright;
pointright = swap;
}
if(find(allneigh, I_points[i_p])>=0){ //POINT LEAF LEAF
push(pointleafleaf,I_points[i_p]);push(pointleafleaf,pointleft);push(pointleafleaf,pointright);//push the points to the pointleafleaf[];
push(V_pll,thisposition);push(V_pll,left);push(V_pll,right);//push the points to an equivilent vector storage var V_pll[];
}
int v0 = addvertex(0,zig,pointleft);
int v1 = addvertex(0,zig,I_points[i_p]);
int v2 = addvertex(0,zig,pointright);
//because primpoints was returning empty {}
push(zigPoints,pointleft);
push(zigPoints,I_points[i_p]);
push(zigPoints,pointright);
}
}
for(int g=1;g<len(zigPoints);g=g+3){
//algorithm is simple: strip with one division pattern is direction 1(open, uturn) direction2(open , uturn)
if(g+4>len(zigPoints)){break;}
int face1 = addprim(0,"poly");
int face2 = addprim(0,"poly");
if(g%2==0){
int v0,v1,v2,v3,v4,v5,v6,v7;
v0 = addvertex(0,face1,zigPoints[g+4]);
v1 = addvertex(0,face1,zigPoints[g+3]);
v2 = addvertex(0,face1,zigPoints[g ]);
v3 = addvertex(0,face1,zigPoints[g-1]);
v4 = addvertex(0,face2,zigPoints[g+3]);
v5 = addvertex(0,face2,zigPoints[g+2]);
v6 = addvertex(0,face2,zigPoints[g+1]);
v7 = addvertex(0,face2,zigPoints[g ]);
int pass;
pass = setvertexattrib(0, "Cd", face1, v0, {1,0,1}, "set");
pass = setvertexattrib(0, "uv", face2, v4, {0,50,0}, "set");
}
if(g%2==1){
int v0,v1,v2,v3,v4,v5,v6,v7;
v0 = addvertex(0,face1,zigPoints[g+2]);
v1 = addvertex(0,face1,zigPoints[g+3]);
v2 = addvertex(0,face1,zigPoints[g ]);
v3 = addvertex(0,face1,zigPoints[g+1]);
v4 = addvertex(0,face2,zigPoints[g+3]);
v5 = addvertex(0,face2,zigPoints[g+4]);
v6 = addvertex(0,face2,zigPoints[g-1]);
v7 = addvertex(0,face2,zigPoints[g ]);
int pass;
pass = setvertexattrib(0, "Cd", face1, v0, {0,0,0}, "set");
pass = setvertexattrib(0, "uv", face1, v4, {0,50,0}, "set");
}
}
removeprim(0,zig,0);
removeprim(0,i_prim,0);
}
//printf("pointleafleaf: %f\\n",pointleafleaf);
//CREATE STARS
for(int star=0;star<len(intersectionData);star++){
//for(int star=0;star<1;star++){
//sort angles.
//1. create array of angles.
string s_this_star[] = split(intersectionData[star],",");
int this_origin = atoi(s_this_star[0]);
vector this_origin_P = point(0,"P",atoi(s_this_star[0]));
int I_medians[];
for(int m = 1;m<len(s_this_star);m++){
//int var = atoi(s_this_star[m]);
push(I_medians,atoi(s_this_star[m]));
}
float F_medians[];
float angles[];
//now to make angles
for(int ma = 0; ma<len(I_medians);ma++){
vector mapos = point(0,"P",I_medians[ma]);
float dx = mapos[0]-this_origin_P[0];
float dy = mapos[2]-this_origin_P[2];
float angle = atan2(dy,dx);
push(angles,angle);
}
//2. argsort angles.
int argOrder[] = argsort(angles);
//3. reorder points, and angles
I_medians = reorder(I_medians,argOrder);
angles= reorder(angles,argOrder);
//4. find angles between angles, and create points.
float subAngles[];
//subAngles. Attempt 2
for(int a=0;a<len(I_medians);a++){
int a2 = (a+1)%len(I_medians);
vector medialone = point(0,"P",I_medians[a])-this_origin_P;
vector medialtwo = point(0,"P",I_medians[a2])-this_origin_P;
float angleBisect = angleBetweenVectors2(medialone,medialtwo);
push(subAngles,angleBisect);
}
//printf("\\n subAngles: %f\\n",subAngles);
//5. add ring points.
//int subPoints = addprim(0,"polyline");
//int intersectionRing[]; //pattern = regular, bisector, regular... etc.
//int subIntersectionRing[];
vector interRing_P[];
vector subinterRing_P[];
for(int i =0; i<len(subAngles);i++){
float oinA = angles[i];
float inA = subAngles[i];
vector mainposition = set(this_origin_P[0]+ radius*1.414*cos(oinA),this_origin_P[1],this_origin_P[2]+radius*1.414*sin(oinA));
vector subposition = set(this_origin_P[0]+ radius*1.414*cos(inA),this_origin_P[1],this_origin_P[2]+radius*1.414*sin(inA));
//int p = addpoint(0,mainposition);
//push(intersectionRing,p);
//p = addpoint(0,subposition);
//push(subIntersectionRing,p);
push(interRing_P,mainposition);
push(subinterRing_P,subposition);
}
//6. add all the polygons
for(int branch = 0; branch<len(I_medians);branch++){
//for(int branch = 0; branch<1;branch++){
int needle = I_medians[branch];
//printf("needle:%f\\n",needle);
int foundIndex = find(pointleafleaf,needle);
//if(foundIndex<0){printf("\\n %f not found\\n",I_medians[branch]);}
if(foundIndex>=0){
int middle = I_medians[branch];
vector middle_position = point(0,"P",middle);
int index1 =foundIndex+1;
int index2 =foundIndex+2;
int leafA = pointleafleaf[index1];
int leafB = pointleafleaf[index2];
vector pa = V_pll[index1];
vector pb = V_pll[index2];
int reversed = -1;
if(sidecheckXZ(middle_position,this_origin_P,pa)<0){ //if left is right,swap
int swap = leafA;
leafA = leafB;
leafB = swap;
reversed = 1;
}
//lets add polygons!
//quad 1:
//find original median ptnum....
int t0,t1,t2,t3;
int jl;
if(branch==0){jl = len(subinterRing_P)-1;}else{jl=branch-1;}
vector jl_uv =set(1*reversed,0,0);
vector jr_uv =set(1*reversed,1,0);
vector mid_uv =set(1*reversed,0.5,0);
vector borgin_uv =set(2*reversed,0.5,0);
if(reversed==1){
vector vswap=jl_uv;
jl_uv=jr_uv;
jr_uv=vswap;
}
//vector printuv;
//printuv = point(0,"P",73);
//printf("73uv:%f",printuv);
printf("middle:%f",middle);
printf("jl_uv:%fjr_uv:%fmid_uv:%forigin_uv:%f\\n",jl_uv,jr_uv,mid_uv,borgin_uv);
int uvadd;
int junctionLeft =addpoint(0,subinterRing_P[jl]); uvadd = setpointattrib(0,"uv", junctionLeft, jl_uv,"set");
int mid =addpoint(0,interRing_P[branch]); uvadd = setpointattrib(0,"uv", mid, mid_uv,"set");
int junctionRight =addpoint(0,subinterRing_P[branch]); uvadd = setpointattrib(0,"uv", junctionRight,jr_uv,"set");
int borgin =addpoint(0,this_origin_P); uvadd = setpointattrib(0,"uv", junctionRight,borgin_uv,"set");
int quadLeft = addprim(0,"poly");
int quadRight = addprim(0,"poly");
int triLeft = addprim(0,"poly");
int triRight = addprim(0,"poly");
//int jpass;
t0 = addvertex(0,quadLeft,middle);
t1 = addvertex(0,quadLeft,mid);
t2 = addvertex(0,quadLeft,junctionRight);
t3 = addvertex(0,quadLeft,leafB);
//jpass = setvertexattrib(0, "uv", quadLeft, t1, mid_uv, "set");
//jpass = setvertexattrib(0, "uv", quadLeft, t2, jr_uv, "set");
t0 = addvertex(0,quadRight,junctionLeft);
t1 = addvertex(0,quadRight,mid);
t2 = addvertex(0,quadRight,middle);
t3 = addvertex(0,quadRight,leafA);
//jpass = setvertexattrib(0, "uv", quadLeft, t0, jl_uv, "set");
//jpass = setvertexattrib(0, "uv", quadLeft, t1, mid_uv, "set");
//jpass = setvertexattrib(0, "uv", quadLeft, t3, jl_uv, "set");
t0 = addvertex(0,triLeft,junctionLeft);
t1 = addvertex(0,triLeft,borgin);
t2 = addvertex(0,triLeft,mid);
t0 = addvertex(0,triRight,junctionRight);
t1 = addvertex(0,triRight,mid);
t2 = addvertex(0,triRight,borgin);
//int starUV;
//starUV = setpointattrib(0, "uv", intersectionRing[branch] ,set(middle+(2*reversed),0,0), "set");
//starUV = setpointattrib(0, "uv", subIntersectionRing[branch-1] ,set(middle+(2*reversed),1,0), "set");
//starUV = setpointattrib(0, "uv", subIntersectionRing[branch] ,set(middle+(2*reversed),0,0), "set");
//starUV = setpointattrib(0, "uv", subIntersectionRing[branch] ,set(middle+(2*reversed),0,0), "set");
//starUV = setpointattrib(0, "uv", this_origin ,set(middle+(3*reversed),0.5,0), "set");
//int previousSubAngle = subIntersectionRing[]
//if(branch>-1){
// int setUV;
// int newpoly = addprim(0,"poly");
// int t0 = addvertex(0,newpoly,middle);
// int t1 = addvertex(0,newpoly,intersectionRing[branch]);
// int t2 = addvertex(0,newpoly,subIntersectionRing[branch]);
// int t3 = addvertex(0,newpoly,leafB);
// setUV = setvertexattrib(0, "uv", newpoly, t1, set(middle+(2*reversed*-1),0.5,0), "set");
// setUV = setvertexattrib(0, "uv", newpoly, t2, set(middle+(2*reversed*-1),0,0), "set");
// newpoly = addprim(0,"poly");//centertriangle
// t0 = addvertex(0,newpoly,subIntersectionRing[branch]);
// t1 = addvertex(0,newpoly,intersectionRing[branch]);
// t2 = addvertex(0,newpoly,this_origin);
// newpoly = addprim(0,"poly");
// t0 = addvertex(0,newpoly,middle);
// t3 = addvertex(0,newpoly,leafA);
// t2 = addvertex(0,newpoly,subIntersectionRing[branch-1]);
// t1 = addvertex(0,newpoly,intersectionRing[branch]);
// setUV = setvertexattrib(0, "uv", newpoly, t2, set(middle+(2*reversed*-1),1,0), "set");
// setUV = setvertexattrib(0, "uv", newpoly, t1, set(middle+(2*reversed*-1),0.5,0), "set");
// newpoly = addprim(0,"poly");//centertriangle
// t0 = addvertex(0,newpoly,subIntersectionRing[branch-1]);
// t1 = addvertex(0,newpoly,this_origin);
// t2 = addvertex(0,newpoly,intersectionRing[branch]);
// setUV = setvertexattrib(0, "uv", newpoly, t0, set(middle+(2*reversed*-1),10,0), "set");
// setUV = setvertexattrib(0, "uv", newpoly, t2, set(middle+(2*reversed*-1),10,0), "set");
// setUV = setvertexattrib(0, "uv", newpoly, t1, set(middle+(3*reversed*-1),10,0), "set");
// printf("%f",setUV);
//}
}
}
}