/*
 * Open SMA design for cylindrical microphone capsules
 * Copyright 2023 Pierre Lecomte - sekisushai@gmail.com
 *
 * The open SMA configuration is realized by means of microphone holding parts (in yellow) connected to each other with thin metal rods (in blue).
 * The SMA is fixed to a mic stand by the mean of a threaded part, attached to neighbouring microphones (in red)
 *
 */
 
// To visualize the whole SMA
open_sma();

// To render only one attach or the support, ready to print
//ready_to_print(5);

// To echo le metal rod length for i-th microphone:
//studs(0);

// EDIT BELOW
//$fn=128; //for final render

/*[ Array parameters ]*/
// Last parameters [r, theta, phi] are for the threaded mic stand support.

// sphere radius (mm):
r = [100, 100, 100, 100, 34, 150];

// microphones zenith coordinates (°):
theta = [0, 109.471, 109.471, 109.471, 70.5284, 180];

// microphone azimuth coordinates (°):
phi = [0, 240, 120, 0, 180, 0];

/* Connection matrix: For the i-th microphone, the neighbouring microphones number are listed in the i-th row (i starts at 0).
*/
// connection matrix
neighbors = 
[
[1, 2, 3, 4], //0
[2, 3, 0, 4, 5], //1
[3, 0, 1, 4, 5], //2
[0, 1, 2, 5], //3
[0, 1, 2], //4
[1, 2, 3], // support
];

// DO NOT EDIT BELOW HERE
include <threads.scad>;

module attach(id_mic, rmic=3.82, rstud=1.75, color="yellow"){
    rtp = [r[id_mic], theta[id_mic], phi[id_mic]];
    center = sph2cart(rtp);

    difference(){
    union(){
        for (i=[0:len(neighbors[id_mic])-1]){
           neighbor = 
            sph2cart(
            [
            r[neighbors[id_mic][i]], 
            theta[neighbors[id_mic][i]], 
            phi[neighbors[id_mic][i]]
            ]
            );
           rtp2=cart2sph(neighbor-center);
           translate(center)
           rotate([0, rtp2[1], rtp2[2]]){
           difference(){
               cylinder(r=rstud+2,h=22);
               translate([0,0,16])
               cylinder(r=rstud,h=25);
           }
       }
    }
    translate(center)
    rotate([0, rtp[1], rtp[2]]){
    cylinder(r=rmic+2,h=20,center=true);
        }
    }
    translate(center)
    {
    rotate([0, rtp[1], rtp[2]]){
    cylinder(r=rmic,h=40, center=true); 
    translate([rmic,0,5])
    rotate([90,0,90])
    linear_extrude(3)
    text(str(id_mic+1), size=4, valign="center", halign="center");}}
}
}

// treaded attach
module support(stud=1.75){
        id_mic = len(r)-1;
        rtp = [r[id_mic], theta[id_mic], phi[id_mic]];
        center = sph2cart(rtp);
        difference(){
        color("red")
        hull(){
        for(i=[0:len(neighbors[id_mic])-1]){
        neighbor = 
            sph2cart(
            [
            r[neighbors[id_mic][i]], 
            theta[neighbors[id_mic][i]], 
            phi[neighbors[id_mic][i]]
            ]
            );
        rtp2=cart2sph(neighbor-center); 
        translate(center)
        rotate([0, rtp2[1], rtp2[2]]){
        cylinder(r=stud+2,h=22);
        }
    }
    translate(center)
    rotate([0, rtp[1], rtp[2]])
    translate([0, 0, -10])
    cylinder(r=6,h=20, center=false);

}
 for(i=[0:len(neighbors[id_mic])-1]){
        neighbor = 
            sph2cart(
            [
            r[neighbors[id_mic][i]], 
            theta[neighbors[id_mic][i]], 
            phi[neighbors[id_mic][i]]
            ]
            );
        rtp2=cart2sph(neighbor-center);    
        translate(center)
        rotate([0, rtp2[1], rtp2[2]]){
        translate([0,0,16])
        cylinder(r=stud,h=25);
        };
    };
translate(center)
rotate([0, rtp[1], rtp[2]])
translate([0,0,-30])
render()
    /* 
    Change here the type of threaded hole for mounting on a mic stand. See threads.scad file for help
    */
english_thread(3/8,16,2,internal=true);
}
}

module studs(id_mic, rstud=1.75){
    rtp = [r[id_mic], theta[id_mic], phi[id_mic]];
    center = sph2cart(rtp);
    for (i=[0:len(neighbors[id_mic])-1]){
        neighbor = sph2cart([r[neighbors[id_mic][i]], theta[neighbors[id_mic][i]], phi[neighbors[id_mic][i]]]);
           rtp2=cart2sph(neighbor-center);
           h = norm(neighbor-center)-2*16;
           echo("Metal rod length:", h);
           translate(center)
           rotate([0, rtp2[1], rtp2[2]])
           color("blue")
           translate([0,0,16])
           cylinder(r=rstud,h=h);
           }
}

function cart2sph(vec)
    = [norm(vec), acos(vec[2]/norm(vec)), atan2(vec[1],vec[0])];

function sph2cart(vec)
    = [vec[0]*sin(vec[1])*cos(vec[2]), vec[0]*sin(vec[1])*sin(vec[2]), vec[0]*cos(vec[1])];

// draw the whole array with attach and studs and support
module open_sma(){
for (i = [0:len(theta)-2]){
    attach(i);
    studs(i);
};
support();
};

module ready_to_print(i){
    mirror([0, 0, -1])
    translate([0, 0, -r[i]])
    rotate([0, -theta[i], 0])
    rotate([0, 0, -phi[i]])
    if (i != len(r)-1)
    attach(i);
    else
    support();
}