include <BOSL2/std.scad>;

/*
section=20;
height=800;
depth=560;
width=563;


side_length = 20; // Length of each side of the square
thickness = 3;   // Thickness of the material
*/
$fn=32;

module miter_profile(length=200,height=30,depth=20) {
    delta=1;
    difference() {
        cuboid([length,depth,height],anchor=CENTER,rounding=1);
        miter();            // Right Miter    
        xflip() miter();    // Left Miter                                
    }
    module miter() {
        translate([length/2, 0, -height/2])                
            yrot(-45)
                color([0.5,0.5,0,0.5]) 
                    cube([height, depth+2*delta, height*sqrt(2)+2],anchor=CENTER+LEFT+BOT);
    }
}



/**
 * Miter Tubes Module
 *
 * This module generates a sequence of mitered tubes based on the provided section lengths,
 * with options for customizing their dimensions and unfolding their arrangement.
 *
 * Parameters:
 * - `sections` (array): An array of section lengths, each specifying the length of an individual tube.
 *   Example: `[10, 20, 30]` will create three tubes of lengths 10, 20, and 30 units respectively.
 *
 * - `height` (number): The height of each tube. This value defines the dimension perpendicular
 *   to the tube's length and depth.
 *
 * - `depth` (number): The depth of each tube. This value defines the dimension perpendicular
 *   to the tube's length and height.
 *
 * - `unfold` (boolean, optional): Determines whether the sequence of tubes should be unfolded
 *   for easier visualization or fabrication. When `true`, the tubes are laid out in a flat arrangement.
 *   Defaults to `false`.
 *
 * Internal Variables and Process:
 * - `moves` (array): Computed positions and rotations for each tube, determined by the `rotSlide`
 *   function applied to the `sections` and `unfold` parameters.
 *
 * - Loop: Iterates through the `sections` array to position and render each tube with the following steps:
 *   1. Extracts the length of the current tube.
 *   2. Retrieves the computed translation and rotation values for the tube from the `moves` array.
 *   3. Logs debugging information about the current tube's dimensions and transformations.
 *   4. Translates and rotates the coordinate system to align with the tube's placement.
 *   5. Calls the `miter_tube` submodule to render the individual tube with specified dimensions.
 *
 * Submodules:
 * - `miter_tube`: A separate submodule (not defined here) that handles the actual geometry creation
 *   for each tube, based on its length, height, and depth.
 *
 * Usage:
 * ```
 * miter_tubes([10, 20, 30], height=5, depth=3, unfold=true);
 * ```
 */
module miter_tubes( sections, height, depth, start=true,end=true,unfold=false){
    for (index = [0:len(sections)-1]) {
        length = sections[index];
        m = computeSectionPositions( sections, unfold )[index];
        translate([m[0],m[1], m[2]])
            yrot(-m[3])    
                miter_tube( length, height, depth, start = index==0 ? start : true,end = index==len(sections)-1 ? end : true    );
    }
}

/**
 * Creates a mitered tube with specified dimensions, wall thickness, and rounding.
 * 
 * Parameters:
 * - `length` (number): The length of the tube along its main axis (default: 200).
 * - `height` (number): The height of the rectangular cross-section (default: 30).
 * - `depth` (number): The depth of the rectangular cross-section (default: 20).
 * - `rounding` (number): The rounding radius of the tube's edges (default: 2).
 * - `wall` (number): The thickness of the tube walls (default: 1.5).
 * - `start` (boolean): TRue if a miter cut should be done at the start position
 * - `end` (boolean): TRue if a miter cut should be done at the end position
 */
module miter_tube(length=200,height=30,depth=20,rounding=2,wall=1.5,start=true,end=true) {
    delta=1;
    translate([length,0,0])
    difference() {
        left(length/2)  
            yrot(90) 
                rect_tube(size=[height,depth], wall=wall, h=length,rounding=rounding, anchor=[1,0,0]);
        if ( end ) miter(-45); // Right Miter    
        if ( start )  translate([-length,0,0]) zrot(180) miter(-45);    // Left Miter         
    }
    module miter(rot) {
        translate([0,0,0])
            yrot(rot)
                color([0.5,0.5,0,0.5]) 
                    cube([height, depth+2*delta, height*sqrt(2)+2],anchor=CENTER+LEFT+BOT);
    }
    
}



/**
 * Computes the positions and rotations for sections of a tube structure.
 *
 * Parameters:
 * - `v` (array): An array of section lengths. Each element represents the length of a tube segment.
 * - `unfold` (boolean, optional): If true, the structure is laid flat, skipping rotation increments.
 *   Defaults to `false`.
 *
 * Returns:
 * - An array of 4-element arrays, each representing the position and rotation of a section in the form [x, y, z, r]:
 *   - `x`, `y`, `z`: The computed coordinates for the section.
 *   - `r`: The rotation angle for the section.
 *
 * Example:
 * computeSectionPositions([10, 20, 30], unfold=false);
 * // Returns computed positions and rotations for each section.
 */
 
function computeSectionPositions(sections, unfold=false) =
    sections == [] ? [] :
    assert(is_consistent(sections), "Input array is not consistent.") [
        for (
            x = 0, y = 0, z = 0, r = 0, idx = 0;
            idx < len(sections);
            xFactor = r == 0 ? 1 : (r == 180 ? -1 : 0),
            zFactor = r == 90 ? 1 : (r == 270 ? -1 : 0),
            x = x + sections[idx] * xFactor,
            z = z + sections[idx] * zFactor,
            r = r + (unfold ? 0 : 90),
            idx = idx + 1
        )
        [x, y, z, r]
    ];
   

            
//miter_tubes([400,500,600,700,800],30,20);
//echo ("rotSlide",rotSlide([400,500,600,500]));