All files / lib occ-service.ts

97.45% Statements 115/118
68.75% Branches 11/16
100% Functions 8/8
100% Lines 108/108

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                                                          1x 1x 1x 1x 1x 1x 1x   1x       2x         4x 4x 4x   4x 4x         4x   4x 3x 3x     3x 3x 3x       4x 4x     4x 4x 4x   9x 9x 9x   9x                   9x 9x     9x 9x 9x 15342x 15342x 15342x 15342x 15342x 15342x 15342x 15342x       9x 9x 9x 9x 15342x 15342x 15342x 15342x       9x 9x 9x 9x 9x 30336x 30336x 30336x 30336x 30336x 20222x 20222x 20222x   30336x 30336x 30336x 30336x   9x 9x   9x   9x 9x 9x 9x           4x 9x 9x       4x 4x 21x         21x 21x 21x     21x 21x 21x 21x 984x 984x         984x   21x   21x 984x 21x 21x 21x 21x     4x 4x 4x 72x 72x 72x       4x 4x   4x 4x          
import { OpenCascadeInstance, Handle_Poly_Triangulation, TopoDS_Shape } from "../bitbybit-dev-occt/bitbybit-dev-occt";
import * as Inputs from "./api/inputs/inputs";
import { OCCTBooleans } from "./services/booleans";
import { OCCTGeom } from "./services/geom/geom";
import { OCCTIO } from "./services/io";
import { OCCTOperations } from "./services/operations";
import { OCCTShapes } from "./services/shapes/shapes";
import { OCCTTransforms } from "./services/transforms";
import { OCCTFillets } from "./services/fillets";
// import { OCCTAssembly } from "./services/assembly";
import { OccHelper } from "./occ-helper";
import { OCCTShapeFix } from "./services/shape-fix";
 
export class OCCTService {
    public readonly shapes: OCCTShapes;
    public readonly geom: OCCTGeom;
    public readonly transforms: OCCTTransforms;
    public readonly operations: OCCTOperations;
    public readonly booleans: OCCTBooleans;
    public readonly fillets: OCCTFillets;
    // public readonly assembly: OCCTAssembly;
    public readonly shapeFix: OCCTShapeFix;
    public readonly io: OCCTIO;
    public plugins?;
 
    constructor(
        private readonly occ: OpenCascadeInstance,
        private readonly och: OccHelper
    ) {
        this.shapes = new OCCTShapes(occ, och);
        this.geom = new OCCTGeom(occ, och);
        this.transforms = new OCCTTransforms(occ, och);
        this.operations = new OCCTOperations(occ, och);
        this.booleans = new OCCTBooleans(occ, och);
        this.fillets = new OCCTFillets(occ, och);
        this.shapeFix = new OCCTShapeFix(occ, och);
        // this.assembly = new OCCTAssembly(occ, och);
        this.io = new OCCTIO(occ, och);
    }
 
    shapesToMeshes(shapes, maxDeviation, adjustYtoZ): Inputs.OCCT.DecomposedMeshDto[] {
        return shapes.map(shape => this.shapeToMesh(shape, maxDeviation, adjustYtoZ));
    }
 
    shapeToMesh(shape, maxDeviation, adjustYtoZ): Inputs.OCCT.DecomposedMeshDto {
 
        const faceList: Inputs.OCCT.DecomposedFaceDto[] = [];
        const edgeList: Inputs.OCCT.DecomposedEdgeDto[] = [];
        const pointsList: Inputs.Base.Point3[] = [];
 
        let shapeToUse = shape as TopoDS_Shape;
        Iif (shapeToUse.IsNull()) return { faceList, edgeList, pointsList: [] };
 
        // This could be made optional...
        // Clean cached triangulation data for the shape.
        // This allows to get lower res models out of higher res that was once computed and cached.
        this.occ.BRepTools.Clean(shapeToUse, true);
 
        if (adjustYtoZ) {
            const shapeToUseRotated = this.och.transformsService.rotate({ shape, axis: [1, 0, 0], angle: -90 });
            const shapeMirrored = this.och.transformsService.mirrorAlongNormal(
                { shape: shapeToUseRotated, origin: [0, 0, 0], normal: [0, 0, 1] }
            );
            shapeToUseRotated.delete();
            shapeToUse.delete();
            shapeToUse = shapeMirrored;
        }
 
        // Iterate through the faces and triangulate each one
        const triangulations: Handle_Poly_Triangulation[] = [];
        const faces = this.och.shapeGettersService.getFaces({ shape: shapeToUse });
 
        let incrementalMeshBuilder;
        Eif (faces && faces.length) {
            incrementalMeshBuilder = new this.occ.BRepMesh_IncrementalMesh_2(shapeToUse, maxDeviation, false, 0.5, false);
            faces.forEach((myFace, faceIndex) => {
 
                const aLocation = new this.occ.TopLoc_Location_1();
                const myT = this.occ.BRep_Tool.Triangulation(myFace, aLocation, 0);
                Iif (myT.IsNull()) { console.error("Encountered Null Face!"); return; }
 
                const thisFace: Inputs.OCCT.DecomposedFaceDto = {
                    vertex_coord: [],
                    normal_coord: [],
                    uvs: [],
                    tri_indexes: [],
                    vertex_coord_vec: [],
                    number_of_triangles: 0,
                    face_index: faceIndex
                };
 
                const pc = new this.occ.Poly_Connect_2(myT);
                const triangulation = myT.get();
 
                // write vertex buffer
                thisFace.vertex_coord = new Array(triangulation.NbNodes() * 3);
                thisFace.vertex_coord_vec = [];
                for (let i = 0; i < triangulation.NbNodes(); i++) {
                    const p = triangulation.Node(i + 1).Transformed(aLocation.Transformation());
                    const uv = triangulation.UVNode(i + 1);
                    thisFace.uvs[(i * 2) + 0] = uv.X();
                    thisFace.uvs[(i * 2) + 1] = uv.Y();
                    thisFace.vertex_coord[(i * 3) + 0] = p.X();
                    thisFace.vertex_coord[(i * 3) + 1] = p.Y();
                    thisFace.vertex_coord[(i * 3) + 2] = p.Z();
                    thisFace.vertex_coord_vec.push([p.X(), p.Y(), p.Z()]);
                }
 
                // write normal buffer
                const myNormal = new this.occ.TColgp_Array1OfDir_2(1, triangulation.NbNodes());
                this.occ.StdPrs_ToolTriangulatedShape.Normal(myFace, pc, myNormal);
                thisFace.normal_coord = new Array(myNormal.Length() * 3);
                for (let i = 0; i < myNormal.Length(); i++) {
                    const d = myNormal.Value(i + 1);
                    thisFace.normal_coord[(i * 3) + 0] = d.X();
                    thisFace.normal_coord[(i * 3) + 1] = d.Y();
                    thisFace.normal_coord[(i * 3) + 2] = d.Z();
                }
 
                // write triangle buffer
                const orient = myFace.Orientation_1();
                const triangles = myT.get().Triangles();
                thisFace.tri_indexes = new Array(triangles.Length() * 3);
                let validFaceTriCount = 0;
                for (let nt = 1; nt <= myT.get().NbTriangles(); nt++) {
                    const t = triangles.Value(nt);
                    let n1 = t.Value(1);
                    let n2 = t.Value(2);
                    const n3 = t.Value(3);
                    if (orient !== this.occ.TopAbs_Orientation.TopAbs_FORWARD) {
                        const tmp = n1;
                        n1 = n2;
                        n2 = tmp;
                    }
                    thisFace.tri_indexes[(validFaceTriCount * 3) + 0] = n1 - 1;
                    thisFace.tri_indexes[(validFaceTriCount * 3) + 1] = n2 - 1;
                    thisFace.tri_indexes[(validFaceTriCount * 3) + 2] = n3 - 1;
                    validFaceTriCount++;
                }
                thisFace.number_of_triangles = validFaceTriCount;
                faceList.push(thisFace);
 
                triangulations.push(myT);
 
                aLocation.delete();
                myNormal.delete();
                triangles.delete();
                pc.delete();
 
            });
        }
 
        // Nullify Triangulations between runs so they're not stored in the cache
        for (let i = 0; i < triangulations.length; i++) {
            triangulations[i].Nullify();
            triangulations[i].delete();
        }
 
        // Get the free edges that aren't on any triangulated face/surface
        const edges = this.och.shapeGettersService.getEdges({ shape: shapeToUse });
        edges.forEach((myEdge, index) => {
            const thisEdge: Inputs.OCCT.DecomposedEdgeDto = {
                vertex_coord: [],
                edge_index: -1
            };
 
            const aLocation = new this.occ.TopLoc_Location_1();
            const adaptorCurve = new this.occ.BRepAdaptor_Curve_2(myEdge);
            const tangDef = new this.occ.GCPnts_TangentialDeflection_2(adaptorCurve, maxDeviation, 0.1, 2, 1.0e-9, 1.0e-7);
 
            // write vertex buffer
            thisEdge.vertex_coord = [];
            const nrPoints = tangDef.NbPoints();
            const tangDefValues = [];
            for (let j = 0; j < nrPoints; j++) {
                const tangDefVal = tangDef.Value(j + 1);
                thisEdge.vertex_coord.push([
                    tangDefVal.X(),
                    tangDefVal.Y(),
                    tangDefVal.Z()
                ]);
                tangDefValues.push(tangDefVal);
            }
            thisEdge.edge_index = index;
 
            edgeList.push(thisEdge);
            tangDefValues.forEach(v => v.delete());
            aLocation.delete();
            adaptorCurve.delete();
            tangDef.delete();
            this.occ.BRepTools.Clean(myEdge, true);
        });
 
        const vertices = this.och.shapeGettersService.getVertices({ shape: shapeToUse });
        Eif (vertices.length > 0) {
            vertices.forEach(v => {
                const pt = this.occ.BRep_Tool.Pnt(v);
                pointsList.push([pt.X(), pt.Y(), pt.Z()]);
                pt.delete();
            });
        }
 
        Eif (incrementalMeshBuilder) {
            incrementalMeshBuilder.Delete();
        }
        this.occ.BRepTools.Clean(shapeToUse, true);
        return { faceList, edgeList, pointsList };
    }
 
 
}