All files / lib/services/base vertices.service.ts

87.69% Statements 57/65
78.57% Branches 11/14
88% Functions 22/25
86.66% Lines 52/60

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                                                  8x       6x       1x 1x       1x       1x 1x       14x 82x 82x 82x 82x                           2007x       12x 12x       6x 6x 12x         12x 12x     12x 32x 12x 2x 10x 2x 8x 2x 6x 6x               24x 6x       4x 4x 4x 12x 12x 4x 4x     4x       4x 4x 4x 12x 12x 12x 12x     4x       24x 24x 24x 24x        
import { OpenCascadeInstance, TopoDS_Compound, TopoDS_Shape, TopoDS_Vertex, TopoDS_Wire } from "../../../bitbybit-dev-occt/bitbybit-dev-occt";
import * as Inputs from "../../api/inputs/inputs";
import { BooleansService } from "./booleans.service";
import { ConverterService } from "./converter.service";
import { EntitiesService } from "./entities.service";
import { ShapeGettersService } from "./shape-getters";
import { WiresService } from "./wires.service";
 
export class VerticesService {
 
    constructor(
        private readonly occ: OpenCascadeInstance,
        private readonly entitiesService: EntitiesService,
        private readonly converterService: ConverterService,
        private readonly shapeGettersService: ShapeGettersService,
        public wiresService: WiresService,
        public booleansService: BooleansService,
    ) { }
 
 
    vertexFromXYZ(inputs: Inputs.OCCT.XYZDto): TopoDS_Vertex {
        return this.entitiesService.makeVertex([inputs.x, inputs.y, inputs.z]);
    }
 
    vertexFromPoint(inputs: Inputs.OCCT.PointDto): TopoDS_Vertex {
        return this.entitiesService.makeVertex(inputs.point);
    }
 
    verticesFromPoints(inputs: Inputs.OCCT.PointsDto): TopoDS_Vertex[] {
        return inputs.points.map(p => this.vertexFromPoint({ point: p }));
    }
 
    verticesCompoundFromPoints(inputs: Inputs.OCCT.PointsDto): TopoDS_Compound {
        const vertexes = this.verticesFromPoints(inputs);
        return this.converterService.makeCompound({ shapes: vertexes });
    }
 
    getVertices(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): TopoDS_Vertex[] {
        return this.shapeGettersService.getVertices(inputs);
    }
 
    getVerticesAsPoints(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): Inputs.Base.Point3[] {
        const vertices = this.shapeGettersService.getVertices(inputs);
        return this.verticesToPoints({ shapes: vertices });
    }
 
    verticesToPoints(inputs: Inputs.OCCT.ShapesDto<TopoDS_Vertex>): Inputs.Base.Point3[] {
        return inputs.shapes.map(v => {
            const pt = this.occ.BRep_Tool.Pnt(v);
            const res = [pt.X(), pt.Y(), pt.Z()] as Inputs.Base.Point3;
            pt.delete();
            return res;
        });
    }
 
    pointsToVertices(inputs: Inputs.OCCT.ShapesDto<TopoDS_Vertex>): Inputs.Base.Point3[] {
        return inputs.shapes.map(v => {
            const pt = this.occ.BRep_Tool.Pnt(v);
            const res = [pt.X(), pt.Y(), pt.Z()] as Inputs.Base.Point3;
            pt.delete();
            return res;
        });
    }
 
    vertexToPoint(inputs: Inputs.OCCT.ShapeDto<TopoDS_Vertex>): Inputs.Base.Point3 {
        return this.converterService.vertexToPoint(inputs);
    }
 
    projectPoints(inputs: Inputs.OCCT.ProjectPointsOnShapeDto<TopoDS_Shape>): Inputs.Base.Point3[] {
        const pointsAlongDir = inputs.points.map(p => [p[0] + inputs.direction[0], p[1] + inputs.direction[1], p[2] + inputs.direction[2]] as Inputs.Base.Point3);
        const lines = pointsAlongDir.map((p, i) => ({
            start: inputs.points[i],
            end: p
        }));
        const wiresFromPoints = this.wiresService.createLines({ lines, returnCompound: false }) as TopoDS_Wire[];
        const allPoints = wiresFromPoints.map((wire, index) => {
            const x = this.booleansService.intersection({
                shapes: [wire, inputs.shape],
                keepEdges: false
            });
 
            const res = x.map(s => {
                return this.shapeGettersService.getVertices({ shape: s });
            });
            
            if (res) {
                const pts = this.verticesToPoints({ shapes: res.flat().filter(s => s !== undefined) });
                if (inputs.projectionType === Inputs.OCCT.pointProjectionTypeEnum.closest) {
                    return [this.getClosestPointFromPoints(pts, inputs.points[index])];
                } else if (inputs.projectionType === Inputs.OCCT.pointProjectionTypeEnum.furthest) {
                    return [this.getFurthestPointFromPoints(pts, inputs.points[index])];
                } else if (inputs.projectionType === Inputs.OCCT.pointProjectionTypeEnum.closestAndFurthest) {
                    return [this.getClosestPointFromPoints(pts, inputs.points[index]), this.getFurthestPointFromPoints(pts, inputs.points[index])];
                } else if (inputs.projectionType === Inputs.OCCT.pointProjectionTypeEnum.all) {
                    return pts;
                } else E{
                    return undefined;
                }
            } else E{
                return undefined;
            }
        });
        const points = allPoints.flat().filter(s => s !== undefined);
        return points;
    }
 
    private getClosestPointFromPoints(points: Inputs.Base.Point3[], point: Inputs.Base.Point3): Inputs.Base.Point3 {
        let minDist = Number.MAX_VALUE;
        let closestPoint: Inputs.Base.Point3 = [0, 0, 0];
        points.forEach(p => {
            const dist = this.distanceBetweenTwoPoints(point, p);
            if (dist < minDist) {
                minDist = dist;
                closestPoint = p;
            }
        });
        return closestPoint;
    }
 
    private getFurthestPointFromPoints(points: Inputs.Base.Point3[], point: Inputs.Base.Point3): Inputs.Base.Point3 {
        let maxDist = 0;
        let furthestPoint: Inputs.Base.Point3 = [0, 0, 0];
        points.forEach(p => {
            const dist = this.distanceBetweenTwoPoints(point, p);
            Eif (dist > maxDist) {
                maxDist = dist;
                furthestPoint = p;
            }
        });
        return furthestPoint;
    }
 
    private distanceBetweenTwoPoints(p1: Inputs.Base.Point3, p2: Inputs.Base.Point3): number {
        const x = p2[0] - p1[0];
        const y = p2[1] - p1[1];
        const z = p2[2] - p1[2];
        return Math.sqrt(x * x + y * y + z * z);
    }
 
}