Skip to content

Instantly share code, notes, and snippets.

@RH2
Last active July 31, 2023 07:18
Show Gist options
  • Save RH2/0437491b4c8c64a05321062e59334333 to your computer and use it in GitHub Desktop.
Save RH2/0437491b4c8c64a05321062e59334333 to your computer and use it in GitHub Desktop.
//logic
//three components:
//front-door
float fd_width = chf("Front_Door_Width");
//wide-garage
float wg_width = chf("Wide_Garage_Width");
//narrow-garage
float ng_width = chf("Narrow_Garage_Width");
//spacing
float margin = chf("Spacing_Between_Features");
//spice
float seed = chf("Seed");
//precompute requirements...
float fullpackage = fd_width + margin + wg_width + margin + ng_width;
float midpackage = fd_width + margin + wg_width;
float smallpackage = fd_width + margin + ng_width;
float tinypackage = fd_width;
int totalprims = nprimitives(0);
float area = prim(1,"area",0); // get area of house;
float perimeter = 0; //total length
float lengths[] = {};
int fcapable[] = {};
int mcapable[] = {};
int scapable[] = {};
int tcapable[] = {};
for(int i=0; i < totalprims; i++){
int pts[] = primpoints(0,i);
vector a = point(0,"P",pts[0]);
vector b = point(0,"P",pts[1]);
float d = distance(a,b);
perimeter += d;
append(lengths, d);
}
for(int i=0;i<len(lengths);i++){
if(lengths[i]>fullpackage){
append(fcapable,1);
}else{append(fcapable,0);}
if(lengths[i]>midpackage){
append(mcapable,1);
}else{append(mcapable,0);}
if(lengths[i]>smallpackage){
append(scapable,1);
}else{append(scapable,0);}
if(lengths[i]>tinypackage){
append(tcapable,1);
}else{append(tcapable,0);}
}
//keep track of objects placed
int fd_placed = 0;
int wg_placed = 0;
int ng_placed = 0;
//attempt to pack
int haystack[] = find(fcapable, 1);
if(len(haystack)!=0){
//choose a random index from set of capable edges.
//oops actually, the index IS the whereever the 1 is found. because it is not a reference.
float fchoice = fit( rand(seed) , 0.0,1.0,0.0,float(len(haystack)));
int choice = haystack[int(fchoice)];
int pts[] = primpoints(0,choice);
vector a = point(0,"P",pts[0]);
vector b = point(0,"P",pts[1]);
vector dir = normalize(b-a);
vector vd0 = a;
vector vd1 = a+dir*(fd_width);
vector vg0 = a+dir*(fd_width+margin);
vector vg1 = a+dir*(fd_width+margin+wg_width);
vector vg3 = a+dir*(fd_width+margin+wg_width+margin);
vector vg4 = a+dir*(fd_width+margin+wg_width+margin+ng_width);
int d0 = addpoint(0,vd0);
int d1 = addpoint(0,vd1);
int wg0 = addpoint(0,vg0);
int wg1 = addpoint(0,vg1);
int ng0 = addpoint(0,vg3);
int ng1 = addpoint(0,vg4);
int frontdoor = addprim(0,"polyline",d0,d1);
int widegarage = addprim(0,"polyline",wg0,wg1);
int narrowgarage = addprim(0,"polyline",ng0,ng1);
setprimgroup(0,"frontdoor",frontdoor,1,"set");
setprimgroup(0,"garage_wide",widegarage,1,"set");
setprimgroup(0,"garage_narrow",narrowgarage,1,"set");
}
else{
haystack = find(mcapable, 1);
if(len(haystack)!=0){
//choose a random index from set of capable edges.
float fchoice = fit( rand(seed) , 0.0,1.0,0.0,float(len(haystack)));
int choice = haystack[int(fchoice)];
int pts[] = primpoints(0,choice);
vector a = point(0,"P",pts[0]);
vector b = point(0,"P",pts[1]);
vector dir = normalize(b-a);
vector vd0 = a;
vector vd1 = a+dir*(fd_width);
vector vg0 = a+dir*(fd_width+margin);
vector vg1 = a+dir*(fd_width+margin+wg_width);
int d0 = addpoint(0,vd0);
int d1 = addpoint(0,vd1);
int wg0 = addpoint(0,vg0);
int wg1 = addpoint(0,vg1);
int frontdoor = addprim(0,"polyline",d0,d1);
int widegarage = addprim(0,"polyline",wg0,wg1);
setprimgroup(0,"frontdoor",frontdoor,1,"set");
setprimgroup(0,"garage_wide",widegarage,1,"set");
}
else{
haystack = find(scapable, 1);
if(len(haystack)!=0){
//choose a random index from set of capable edges.
float fchoice = fit( rand(seed) , 0.0,1.0,0.0,float(len(haystack)));
int choice = haystack[int(fchoice)];
int pts[] = primpoints(0,choice);
vector a = point(0,"P",pts[0]);
vector b = point(0,"P",pts[1]);
vector dir = normalize(b-a);
vector vd0 = a;
vector vd1 = a+dir*(fd_width);
vector vg0 = a+dir*(fd_width+margin);
vector vg1 = a+dir*(fd_width+margin+ng_width);
int d0 = addpoint(0,vd0);
int d1 = addpoint(0,vd1);
int wg0 = addpoint(0,vg0);
int wg1 = addpoint(0,vg1);
int frontdoor = addprim(0,"polyline",d0,d1);
int narrowgarage = addprim(0,"polyline",wg0,wg1);
setprimgroup(0,"frontdoor",frontdoor,1,"set");
setprimgroup(0,"garage_narrow",narrowgarage,1,"set");
}
else{
haystack = find(tcapable, 1);
if(len(haystack)!=0){
//choose a random index from set of capable edges.
float fchoice = fit( rand(seed) , 0.0,1.0,0.0,float(len(haystack)));
int choice = haystack[int(fchoice)];
int pts[] = primpoints(0,choice);
vector a = point(0,"P",pts[0]);
vector b = point(0,"P",pts[1]);
vector dir = normalize(b-a);
vector vd0 = a;
vector vd1 = a+dir*(fd_width);
int d0 = addpoint(0,vd0);
int d1 = addpoint(0,vd1);
int frontdoor = addprim(0,"polyline",d0,d1);
setprimgroup(0,"frontdoor",frontdoor,1,"set");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment