/*[bakje famili]*/

CHAMFER = 5.0; // mm
UNIT = 50.0; // mm
HIEGHT = 1.0; // units

/*[andere dingen]*/

INNER_TOLLERANCE = 0.0; // [0.0:0.05:0.5]
MATT_THIKNESS = 10.0; // [2.0:0.1:15.0]

WIDTH = 7; // [1:1:10] 
DEPTH = 5; // [1:1:10]

DIMENTION = "2d"; // [2d: 2d export view, 3d: 3d render]
POSITION = "inplace"; // [inplace: in place, origin: at the origin]
//RENDER = "all"; // [bot: bottom, lid: lid, front: front, back: back, left: left, right: right, all: all]
RENDER = "all";

LAYOUT_SEPERATION = 15; // [1:1:50]


/*[lid]*/

LID_ANGLE = 45; // [0:170]
/*
%square([600, 300]);
%square([700, 400]);
%square([400, 700]);
*/
function finger(flip=[1,0], dir="y") = [
	[
		0, 0
	],[
		(dir=="y") ? 0 : flip[0]*0.25*UNIT,
		(dir=="x") ? 0 : flip[1]*0.25*UNIT,
	],[
		(dir=="y") ? flip[0]*MATT_THIKNESS : flip[0]*0.25*UNIT,
		(dir=="x") ? flip[1]*MATT_THIKNESS : flip[1]*0.25*UNIT,
	],[
		(dir=="y") ? flip[0]*MATT_THIKNESS : flip[0]*0.75*UNIT,
		(dir=="x") ? flip[1]*MATT_THIKNESS : flip[1]*0.75*UNIT,
	],[
		(dir=="y") ? 0 : flip[0]*0.75*UNIT,
		(dir=="x") ? 0 : flip[1]*0.75*UNIT,
	],[
		(dir=="y") ? 0 : flip[0]*UNIT,
		(dir=="x") ? 0 : flip[1]*UNIT,
	],
];

module fingers(flip=[1,1], dir="y", number=2, nub=true){
	flipG = [
		(nub || dir == "x") ? flip[0] : -flip[0],
		(nub || dir == "y") ? flip[1] : -flip[1]
	];
	for (f = [0:1:number-1])
	translate([
		(dir=="x") ? f*UNIT : ((nub) ? 0 : flip[0]*MATT_THIKNESS),
		(dir=="y") ? f*UNIT : ((nub) ? 0 : flip[1]*MATT_THIKNESS)
	])
		polygon(
			points = concat(
				finger(flip=flipG, dir=dir),
				[
					[
						(dir=="y") ? flip[0]*((nub) ? -0.01 : -MATT_THIKNESS - 0.01) : flip[0]*UNIT,
						(dir=="x") ? flip[1]*((nub) ? -0.01 : -MATT_THIKNESS - 0.01) : flip[1]*UNIT
					],[
						(dir=="y") ? flip[0]*((nub) ? -0.01 : -MATT_THIKNESS - 0.01) : 0,
						(dir=="x") ? flip[1]*((nub) ? -0.01 : -MATT_THIKNESS - 0.01) : 0
					]
				]
			)
		);
}

module renderPart(3dPos, 3dRotation, 2dPos){
	if (DIMENTION == "2d"){
		if (POSITION == "inplace")
			translate(2dPos)
				children();
		if (POSITION == "origin")
			children();
	}
	if (DIMENTION == "3d"){
		if (POSITION == "inplace")
			translate(3dPos)
			rotate(3dRotation)
			linear_extrude(MATT_THIKNESS)
				children();
		if (POSITION == "origin")
			linear_extrude(MATT_THIKNESS)
				children();
	}
}

function split(str, sep=" ", i=0, word="", v=[]) =
	(i == len(str)) ? concat(v, word) :
		(str[i] == sep) ? split(str, sep, i+1, "", concat(v, word)) :
			split(str, sep, i+1, str(word, str[i]), v);

module render(name, 3dPos, 3dRotation, 2dPos){
	if (RENDER == "all")
		renderPart(3dPos, 3dRotation, 2dPos) children();
	else {
		for (i = split(RENDER, ",")){
			if (i == name){
				renderPart(3dPos, 3dRotation, 2dPos) children();
			}
		}
	}
}

render(
	name = "bot",
	2dPos = [0, 0],
	3dPos = [0, 0, 0],
	3dRotation = [0, 0, 0]
)
	bottom();

/*
render(
	name = "front",
	2dPos = [-(WIDTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION), 0],
	3dPos = [0, MATT_THIKNESS, 0],
	3dRotation = [90, 0, 0]
)
	front();*/
render(
	name = "front",
	2dPos = [
		0,
		2*DEPTH*UNIT + 4*MATT_THIKNESS + 2*LAYOUT_SEPERATION
	],
	3dPos = [0, MATT_THIKNESS, 0],
	3dRotation = [90, 0, 0]
)
	front();

render(
	name = "back",
	2dPos = [
		-(WIDTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION),
		HIEGHT*UNIT + MATT_THIKNESS + LAYOUT_SEPERATION
	],
	3dPos = [0, DEPTH*UNIT + 2*MATT_THIKNESS, 0],
	3dRotation = [90, 0, 0]
)
	back();

render(
	name = "left",
	2dPos = [
		-(DEPTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION),
		2*HIEGHT*UNIT + 2*MATT_THIKNESS + 2*LAYOUT_SEPERATION
	],
	3dPos = [0, 0, 0],
	3dRotation = [90, 0, 90]
)
	side();

render(
	name = "right",
	2dPos = [
		-(DEPTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION),
		3*HIEGHT*UNIT + 4*MATT_THIKNESS + 3*LAYOUT_SEPERATION
	],
	3dPos = [WIDTH*UNIT + MATT_THIKNESS, 0, 0],
	3dRotation = [90, 0, 90]
)
	side();

totalDepth = DEPTH*UNIT + MATT_THIKNESS*2;

/*if (RENDER == "lid" || RENDER == "all")
render(
	2dPos = [
		WIDTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION,
		0
	],
	3dPos = [
		MATT_THIKNESS,
		totalDepth - totalDepth*cos(LID_ANGLE),
		HIEGHT*UNIT + MATT_THIKNESS + totalDepth*sin(LID_ANGLE)
	],
	3dRotation = [-LID_ANGLE, 0, 0]
)
	lid();*/
render(
	name = "lid",
	2dPos = [
		0,
		DEPTH*UNIT + 2*MATT_THIKNESS + LAYOUT_SEPERATION
	],
	3dPos = [
		MATT_THIKNESS,
		totalDepth - totalDepth*cos(LID_ANGLE),
		HIEGHT*UNIT + MATT_THIKNESS + totalDepth*sin(LID_ANGLE)
	],
	3dRotation = [-LID_ANGLE, 0, 0]
)
	lid();

module bottom(){
	translate([MATT_THIKNESS, MATT_THIKNESS])
		union(){
			translate([0.005, 0.005])
				square(size=[WIDTH*UNIT-0.01, DEPTH*UNIT-0.01]);
			fingers(flip=[-1,1], dir="y", number=DEPTH, nub=true);
			translate([WIDTH*UNIT, 0])
				fingers(flip=[1,1], dir="y", number=DEPTH, nub=true);
			fingers(flip=[1,-1], dir="x", number=WIDTH, nub=true);
			translate([0, DEPTH*UNIT])
				fingers(flip=[1,1], dir="x", number=WIDTH, nub=true);
		}
}

module front(){
	translate([MATT_THIKNESS, MATT_THIKNESS])
	difference(){
		union(){
			translate([0.005, 0.005])
				square(size=[WIDTH*UNIT-0.01, HIEGHT*UNIT-0.005]);
			fingers(flip=[-1,1], dir="y", number=HIEGHT, nub=true);
			translate([WIDTH*UNIT, 0])
				fingers(flip=[1,1], dir="y", number=HIEGHT, nub=true);
			fingers(flip=[1,-1], dir="x", number=WIDTH, nub=false);
		}
		translate([52, HIEGHT*UNIT-10])
			circle(d=3);
		translate([77, HIEGHT*UNIT-10])
			circle(d=3);
		translate([WIDTH*UNIT-52, HIEGHT*UNIT-10])
			circle(d=3);
		translate([WIDTH*UNIT-77, HIEGHT*UNIT-10])
			circle(d=3);
	}
}

module back(){
	translate([MATT_THIKNESS, MATT_THIKNESS])
	union(){
		translate([0.005, 0.005])
			square(size=[WIDTH*UNIT-0.01, HIEGHT*UNIT-0.005]);
		fingers(flip=[-1,1], dir="y", number=HIEGHT, nub=true);
		translate([WIDTH*UNIT, 0])
			fingers(flip=[1,1], dir="y", number=HIEGHT, nub=true);
		fingers(flip=[1,-1], dir="x", number=WIDTH, nub=false);
	}
}

module side(){
	translate([MATT_THIKNESS, MATT_THIKNESS])
	union(){
		translate([0.005, 0.005])
			square(size=[DEPTH*UNIT-0.01, HIEGHT*UNIT-0.005 + MATT_THIKNESS]);

		translate([-MATT_THIKNESS, -MATT_THIKNESS])
			square(size=[MATT_THIKNESS+0.01, MATT_THIKNESS+0.01]);
		translate([DEPTH*UNIT-0.01, -MATT_THIKNESS])
			square(size=[MATT_THIKNESS+0.01, MATT_THIKNESS+0.01]);
		translate([-MATT_THIKNESS, HIEGHT*UNIT-0.01])
			square(size=[MATT_THIKNESS+0.01, MATT_THIKNESS+0.01]);
		translate([DEPTH*UNIT-0.01, HIEGHT*UNIT-0.01])
			square(size=[MATT_THIKNESS+0.01, MATT_THIKNESS+0.01]);

		fingers(flip=[-1,1], dir="y", number=HIEGHT, nub=false);
		translate([DEPTH*UNIT, 0])
			fingers(flip=[1,1], dir="y", number=HIEGHT, nub=false);
		fingers(flip=[1,-1], dir="x", number=DEPTH, nub=false);
	}
}

module lid(){
	square([WIDTH*UNIT, DEPTH*UNIT + 2*MATT_THIKNESS]);
}
