/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */ /** */
import { MeshBuilder, RayHelper, Vector3, VertexBuffer } from '../loader/babylonjs-import';
const v1 = Vector3.Zero();
const v2 = Vector3.Zero();
const v3 = Vector3.Zero();
export class GeometryTools {
    _scene;
    static _tmp = {
        twoPointLineOptions: {
            points: [Vector3.Zero(), Vector3.Zero()],
            updatable: true,
            useVertexAlpha: false,
            //colors: [Color4.FromInts(0, 0, 0, 0), Color4.FromInts(0, 0, 0, 0)],
            instance: undefined
        }
    };
    constructor(_scene) {
        this._scene = _scene;
        return this;
    }
    createSphere(id, origin, size, segments = 4) {
        const sphere = MeshBuilder.CreateSphere(id, { diameter: size, segments: segments }, this._scene);
        sphere.position = origin;
        return sphere;
    }
    createStartDisc(id, origin, normal, size, segments = 6) {
        const cylinder = MeshBuilder.CreateCylinder(id, { height: 0.001, tessellation: segments, diameter: size }, this._scene);
        cylinder.position = origin;
        cylinder.setDirection(normal, 0, 1.57079633);
        return cylinder;
    }
    createLine(id, origin, target, color) {
        const o = GeometryTools._tmp.twoPointLineOptions;
        o.points[0].copyFrom(origin);
        o.points[1].copyFrom(target);
        // o.colors[0].copyFrom(color);
        // o.colors[1] = o.colors[0];
        o.instance = undefined;
        const mesh = MeshBuilder.CreateLines(id, o, this._scene);
        mesh.color.copyFromFloats(color.r, color.g, color.b);
        return mesh;
    }
    updateLine(instance, origin, target, color) {
        const positions = instance.getVerticesData(VertexBuffer.PositionKind);
        if (!positions) {
            return instance;
        }
        positions[0] = origin.x;
        positions[1] = origin.y;
        positions[2] = origin.z;
        positions[3] = target.x;
        positions[4] = target.y;
        positions[5] = target.z;
        instance.updateVerticesData(VertexBuffer.PositionKind, positions, true, false);
        return instance;
    }
    createRay(id, ray, color) {
        return RayHelper.CreateAndShow(ray, this._scene, color);
    }
    createMeshWireframe(mesh, inWorldSpace = true, color) {
        const vd = mesh.vertexData;
        const iLen = vd.indices.length;
        const localToWorldMatrix = mesh.transform;
        const lines = [];
        for (let i = 0; i < iLen; i += 3) {
            const i1 = vd.indices[i];
            const i2 = vd.indices[i + 1];
            const i3 = vd.indices[i + 2];
            v1.set(vd.positions[i1 * 3], vd.positions[i1 * 3 + 1], vd.positions[i1 * 3 + 2]);
            v2.set(vd.positions[i2 * 3], vd.positions[i2 * 3 + 1], vd.positions[i2 * 3 + 2]);
            v3.set(vd.positions[i3 * 3], vd.positions[i3 * 3 + 1], vd.positions[i3 * 3 + 2]);
            if (inWorldSpace) {
                Vector3.TransformCoordinatesToRef(v1, localToWorldMatrix, v1);
                Vector3.TransformCoordinatesToRef(v2, localToWorldMatrix, v2);
                Vector3.TransformCoordinatesToRef(v3, localToWorldMatrix, v3);
            }
            else {
                v1.scaleInPlace(0.001);
                v2.scaleInPlace(0.001);
                v3.scaleInPlace(0.001);
            }
            lines.push(this.createLine('st', v1, v2, color));
            lines.push(this.createLine('st', v1, v3, color));
            lines.push(this.createLine('st', v2, v3, color));
        }
        return lines;
    }
    viewObjectWireFrame(object, color) {
        const productLineMeshes = [];
        for (const m of object.productMeshes) {
            productLineMeshes.push(this.createMeshWireframe(object.mesh(m), true, color));
        }
        return productLineMeshes;
    }
}
//# sourceMappingURL=create-geometry.js.map