All files / lib/api shapes-helper.service.ts

99.16% Statements 119/120
95.65% Branches 22/23
100% Functions 12/12
99.14% Lines 116/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282          31x 31x 31x 240x 240x 240x         240x         240x         240x 240x   31x     31x       8x     8x 7x 7x 7x 7x 7x 7x 7x 7x   1x 1x 1x 1x 1x 1x 1x 1x     8x 8x 8x 8x 8x   8x 8x 8x 8x   8x 8x 8x 8x   8x       11x 11x   11x 60x 60x 60x     11x     6x                   4x 2x 2x   4x 2x 2x     4x                     5x 2x 2x   5x 2x 2x     5x                       22x 22x 22x     22x                             22x           10x 10x 10x     10x                             10x         9x 9x 9x     9x                     9x         10x 10x     10x                     10x           51x 51x   51x   5x 5x 5x   2x 2x 2x   1x 1x 1x   2x 2x 2x   32x 32x 32x   2x 2x 2x   2x 2x 2x   1x 1x 1x   4x 4x 4x     536x          
import { Base } from "./inputs/inputs";
 
export class ShapesHelperService {
 
    starLines(innerRadius: number, outerRadius: number, numRays: number, half: boolean, offsetOuterEdges: number):Base.Line3[] {
        let lines: Base.Line3[] = [];
        const angle_step = (2 * Math.PI) / numRays;
        for (let i = 0; i < numRays; i++) {
            const angle_i = i * angle_step;
            const offset = offsetOuterEdges ? offsetOuterEdges : 0;
            const outer_point: Base.Point3 = [
                outerRadius * Math.cos(angle_i),
                offset,
                outerRadius * Math.sin(angle_i)
            ];
            const inner_point: Base.Point3 = [
                innerRadius * Math.cos(angle_i + angle_step / 2),
                0,
                innerRadius * Math.sin(angle_i + angle_step / 2)
            ];
            const next_outer_point: Base.Point3 = [
                outerRadius * Math.cos(angle_i + angle_step),
                offset,
                outerRadius * Math.sin(angle_i + angle_step)
            ];
            lines.push({ start: outer_point, end: inner_point });
            lines.push({ start: inner_point, end: next_outer_point });
        }
        Iif (half) {
            lines = lines.slice(0, lines.length / 2);
        }
        return lines;
    }
 
    parallelogram(width: number, height: number, angle: number, center: boolean): Base.Line3[] {
        const radians = (angle * Math.PI) / 180;
 
        let x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number;
        if (center) {
            x1 = -width / 2;
            y1 = -height / 2;
            x2 = width / 2;
            y2 = -height / 2;
            x3 = width / 2;
            y3 = height / 2;
            x4 = -width / 2;
            y4 = height / 2;
        } else {
            x1 = 0;
            y1 = 0;
            x2 = width;
            y2 = 0;
            x3 = width;
            y3 = height;
            x4 = 0;
            y4 = height;
        }
 
        const shift = height * Math.tan(radians) / 2;
        x1 += shift;
        x2 += shift;
        x3 -= shift;
        x4 -= shift;
 
        const pt1: Base.Point3 = [x1, 0, y1];
        const pt2: Base.Point3 = [x2, 0, y2];
        const pt3: Base.Point3 = [x3, 0, y3];
        const pt4: Base.Point3 = [x4, 0, y4];
 
        const line1: Base.Line3 = { start: pt1, end: pt2 };
        const line2: Base.Line3 = { start: pt2, end: pt3 };
        const line3: Base.Line3 = { start: pt3, end: pt4 };
        const line4: Base.Line3 = { start: pt4, end: pt1 };
 
        return [line1, line2, line3, line4];
    }
 
    ngon(n: number, radius: number, center: Base.Point2): Base.Line3[] {
        const angle = (2 * Math.PI) / n;
        const edges: Base.Line3[] = [];
 
        for (let i = 0; i < n; i++) {
            const start = [center[0] + radius * Math.cos(i * angle), 0, center[1] + radius * Math.sin(i * angle)] as Base.Point3;
            const end = [center[0] + radius * Math.cos((i + 1) * angle), 0, center[1] + radius * Math.sin((i + 1) * angle)] as Base.Point3;
            edges.push({ start, end });
        }
 
        return edges;
    }
    polygonL(widthFirst: number, lengthFirst: number, widthSecond: number, lengthSecond: number): Base.Point3[] {
        return [
            [0, 0, 0],
            [lengthFirst, 0, 0],
            [lengthFirst, 0, -widthFirst],
            [-widthSecond, 0, -widthFirst],
            [-widthSecond, 0, lengthSecond],
            [0, 0, lengthSecond],
        ];
    }
    polygonLInverted(widthFirst: number, lengthFirst: number, widthSecond: number, lengthSecond: number): Base.Point3[] {
        if (widthFirst >= lengthSecond) {
            widthFirst = lengthSecond - 1e-6;
            console.warn("width first is bigger than length second, to make it work, width second is set to length first - 1e-6");
        }
        if (widthSecond >= lengthFirst) {
            widthSecond = lengthFirst - 1e-6;
            console.warn("width second is bigger than length first, to make it work, width second is set to length first - 1e-6");
        }
 
        return [
            [0, 0, 0],
            [lengthFirst, 0, 0],
            [lengthFirst, 0, widthFirst],
            [widthSecond, 0, widthFirst],
            [widthSecond, 0, lengthSecond],
            [0, 0, lengthSecond],
        ];
    }
 
    polygonLMiddle(widthFirst: number, lengthFirst: number, widthSecond: number, lengthSecond: number): Base.Point3[] {
        if (widthFirst >= lengthSecond) {
            widthFirst = lengthSecond - 1e-6;
            console.warn("width first is bigger than length second, to make it work, width second is set to length first - 1e-6");
        }
        if (widthSecond >= lengthFirst) {
            widthSecond = lengthFirst - 1e-6;
            console.warn("width second is bigger than length first, to make it work, width second is set to length first - 1e-6");
        }
 
        return [
            [widthSecond / 2, 0, widthFirst / 2],
            [widthSecond / 2, 0, lengthSecond ],
            [-widthSecond / 2, 0, lengthSecond],
            [-widthSecond / 2, 0, -widthFirst / 2],
            [lengthFirst, 0, -widthFirst / 2],
            [lengthFirst, 0, widthFirst / 2],
        ];
    }
 
    beamIProfile(width: number, height: number, webThickness: number, flangeThickness: number, alignment: Base.basicAlignmentEnum): Base.Point3[] {
        // Create I-beam profile centered at origin
        const halfWidth = width / 2;
        const halfHeight = height / 2;
        const halfWeb = webThickness / 2;
 
        // Points for I-beam (clockwise from top-left)
        const points: Base.Point3[] = [
            [-halfWidth, 0, halfHeight],
            [halfWidth, 0, halfHeight],
            [halfWidth, 0, halfHeight - flangeThickness],
            [halfWeb, 0, halfHeight - flangeThickness],
            [halfWeb, 0, -halfHeight + flangeThickness],
            [halfWidth, 0, -halfHeight + flangeThickness],
            [halfWidth, 0, -halfHeight],
            [-halfWidth, 0, -halfHeight],
            [-halfWidth, 0, -halfHeight + flangeThickness],
            [-halfWeb, 0, -halfHeight + flangeThickness],
            [-halfWeb, 0, halfHeight - flangeThickness],
            [-halfWidth, 0, halfHeight - flangeThickness],
        ];
 
        return this.applyBeamAlignment(points, width, height, alignment);
    }
 
    beamHProfile(width: number, height: number, webThickness: number, flangeThickness: number, alignment: Base.basicAlignmentEnum): Base.Point3[] {
        // H-beam is I-beam rotated 90 degrees (width and height swapped)
        // Create H-beam profile centered at origin (rotated I-beam)
        const halfWidth = width / 2;
        const halfHeight = height / 2;
        const halfWeb = webThickness / 2;
 
        // Points for H-beam (I-beam rotated 90 degrees, clockwise from top-left)
        const points: Base.Point3[] = [
            [-halfWidth, 0, halfHeight],
            [-halfWidth + flangeThickness, 0, halfHeight],
            [-halfWidth + flangeThickness, 0, halfWeb],
            [halfWidth - flangeThickness, 0, halfWeb],
            [halfWidth - flangeThickness, 0, halfHeight],
            [halfWidth, 0, halfHeight],
            [halfWidth, 0, -halfHeight],
            [halfWidth - flangeThickness, 0, -halfHeight],
            [halfWidth - flangeThickness, 0, -halfWeb],
            [-halfWidth + flangeThickness, 0, -halfWeb],
            [-halfWidth + flangeThickness, 0, -halfHeight],
            [-halfWidth, 0, -halfHeight],
        ];
 
        return this.applyBeamAlignment(points, width, height, alignment);
    }
 
    beamTProfile(width: number, height: number, webThickness: number, flangeThickness: number, alignment: Base.basicAlignmentEnum): Base.Point3[] {
        // Create T-beam profile centered at origin
        const halfWidth = width / 2;
        const halfHeight = height / 2;
        const halfWeb = webThickness / 2;
 
        // Points for T-beam (clockwise from top-left)
        const points: Base.Point3[] = [
            [-halfWidth, 0, halfHeight],
            [halfWidth, 0, halfHeight],
            [halfWidth, 0, halfHeight - flangeThickness],
            [halfWeb, 0, halfHeight - flangeThickness],
            [halfWeb, 0, -halfHeight],
            [-halfWeb, 0, -halfHeight],
            [-halfWeb, 0, halfHeight - flangeThickness],
            [-halfWidth, 0, halfHeight - flangeThickness],
        ];
 
        return this.applyBeamAlignment(points, width, height, alignment);
    }
 
    beamUProfile(width: number, height: number, webThickness: number, flangeThickness: number, flangeWidth: number, alignment: Base.basicAlignmentEnum): Base.Point3[] {
        // Create U-beam profile centered at origin (opening upward - rotated 180 degrees from previous)
        const halfWidth = width / 2;
        const halfHeight = height / 2;
 
        // Points for U-beam (clockwise from top-left outside, opening upward)
        const points: Base.Point3[] = [
            [-halfWidth, 0, halfHeight],
            [-halfWidth + flangeThickness, 0, halfHeight],
            [-halfWidth + flangeThickness, 0, -halfHeight + flangeWidth],
            [halfWidth - webThickness, 0, -halfHeight + flangeWidth],
            [halfWidth - webThickness, 0, halfHeight],
            [halfWidth, 0, halfHeight],
            [halfWidth, 0, -halfHeight],
            [-halfWidth, 0, -halfHeight],
        ];
 
        return this.applyBeamAlignment(points, width, height, alignment);
    }
 
    private applyBeamAlignment(points: Base.Point3[], width: number, height: number, alignment: Base.basicAlignmentEnum): Base.Point3[] {
        // Alignment logic: the specified corner/edge of the shape is placed at the origin
        // For example, topLeft means the top-left corner of the shape is at (0,0)
        let offsetX = 0;
        let offsetZ = 0;
        
        switch (alignment) {
        case Base.basicAlignmentEnum.topLeft:
            offsetX = width / 2;
            offsetZ = -height / 2;
            break;
        case Base.basicAlignmentEnum.topMid:
            offsetX = 0;
            offsetZ = -height / 2;
            break;
        case Base.basicAlignmentEnum.topRight:
            offsetX = -width / 2;
            offsetZ = -height / 2;
            break;
        case Base.basicAlignmentEnum.midLeft:
            offsetX = width / 2;
            offsetZ = 0;
            break;
        case Base.basicAlignmentEnum.midMid:
            offsetX = 0;
            offsetZ = 0;
            break;
        case Base.basicAlignmentEnum.midRight:
            offsetX = -width / 2;
            offsetZ = 0;
            break;
        case Base.basicAlignmentEnum.bottomLeft:
            offsetX = width / 2;
            offsetZ = height / 2;
            break;
        case Base.basicAlignmentEnum.bottomMid:
            offsetX = 0;
            offsetZ = height / 2;
            break;
        case Base.basicAlignmentEnum.bottomRight:
            offsetX = -width / 2;
            offsetZ = height / 2;
            break;
        }
        
        return points.map(pt => [pt[0] + offsetX, pt[1], pt[2] + offsetZ] as Base.Point3);
    }
}