int ite = detail(2, "iteration");
vector norm = v@N;
///// split control
int split = -1;
float rd = rand(@primnum + ite + chf("seed"));
int stopcondition = chi("stopIterConditon");
float splitthreshold = chf("splitthreshold");
if(ite>stopcondition){
if (rd<splitthreshold ){
split = 0;
} else {
split = 1;
}
} else {
split = 1;
}
////Split direction contorl
int npt = nearpoint(1, @P);
vector tangentu = point(1, "tangentu", npt);
vector dir1;
if (ite%2==1){
dir1 = tangentu;
} else {
dir1 = cross(tangentu, {0,1,0});
}
vector center = @P;
float minfloat, maxfloat;
minfloat = 999;
maxfloat = -999;
float dotlist[] = array();
int ppts[] = primpoints(0, @primnum);
foreach(int pt; ppts)
{
vector pos = point(0, "P", pt);
vector dir = normalize(@P - pos);
float dot = dot(dir,dir1);
append(dotlist, dot);
setpointattrib(0, "dot", pt, dot, "set");
}
int index[] = argsort(dotlist);
ppts = reorder(ppts, index);
int startpt = ppts[0];
int foundpt = ppts[-1];
vector startpos = point(0, "P", startpt);
vector foundpos = point(0, "P", foundpt);
int hedge1 = pointhedge(0, startpt);
int hedge2 = pointhedge(0, foundpt);
int hedge1_orig = hedge1;
int hedge2_orig = hedge2;
int prim1 = addprim(0, "poly");
int prim2 = addprim(0, "poly");
vector color1 = set(1,1,0);
vector color2 = set(0,1,1);
/////First Split
int count1 = 0;
while(hedge2!=hedge1)
{
if (count1 == 0){
int pt = addpoint(0, foundpos);
addvertex(0, prim1, pt);
setpointattrib(0, "Cd", pt, color1, "set");
} else if (count1 == 1){
int pt = addpoint(0, startpos);
addvertex(0, prim1, pt);
setpointattrib(0, "Cd", pt, color1, "set");
} else if (count1 > 1){
int dstpt = hedge_dstpoint(0, hedge1);
vector dstpos = point(0, "P", dstpt);
int pt = addpoint(0, dstpos);
hedge1 = hedge_next(0,hedge1);
addvertex(0, prim1, pt);
setpointattrib(0, "Cd", pt, color1, "set");
}
count1++;
if (hedge2==hedge1+1)
{
break;
}
}
///Second Split
hedge1 = hedge1_orig;
hedge2 = hedge2_orig;
int count2 = 0;
while(hedge1!=hedge2)
{
if (count2==0)
{
int pt = addpoint(0, startpos);
addvertex(0, prim2, pt);
setpointattrib(0,"Cd",pt,color2);
} else if (count2==1){
int pt = addpoint(0, foundpos);
addvertex(0, prim2, pt);
setpointattrib(0,"Cd",pt,color2);
}
else if (count2>1){
int dstpt = hedge_dstpoint(0, hedge2);
vector dstpos = point(0, "P", dstpt);
int pt = addpoint(0, dstpos);
hedge2 = hedge_next(0, hedge2);
addvertex(0, prim2, pt);
setpointattrib(0,"Cd",pt,color2);
}
count2++;
if (hedge1 == hedge2+1)
{
break;
}
}
append(i[]@iteration, int(ite));
setprimgroup(0, "split", prim1, split, "set");
setprimgroup(0, "split", prim2, split, "set");
setprimattrib(0, "N", prim1, norm, "set");
setprimattrib(0, "N", prim2, norm, "set");
///
removeprim(0, @primnum, 1);