circuit-json-to-gltf 0.0.149 → 0.0.150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +88 -38
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -14532,6 +14532,52 @@ var boundsOfPositions = (positions) => {
14532
14532
  return bounds.result();
14533
14533
  };
14534
14534
 
14535
+ // lib/utils/mesh-orientation.ts
14536
+ function getLinearTransformDeterminant(m) {
14537
+ return m[0] * (m[4] * m[8] - m[5] * m[7]) - m[1] * (m[3] * m[8] - m[5] * m[6]) + m[2] * (m[3] * m[7] - m[4] * m[6]);
14538
+ }
14539
+ function assertInvertibleLinearTransform(m) {
14540
+ const determinant = getLinearTransformDeterminant(m);
14541
+ if (!Number.isFinite(determinant) || determinant === 0) {
14542
+ throw new RangeError(
14543
+ "Mesh transform must have a finite, nonzero determinant"
14544
+ );
14545
+ }
14546
+ }
14547
+ function applyLinearTransform(p, m) {
14548
+ return {
14549
+ x: m[0] * p.x + m[1] * p.y + m[2] * p.z,
14550
+ y: m[3] * p.x + m[4] * p.y + m[5] * p.z,
14551
+ z: m[6] * p.x + m[7] * p.y + m[8] * p.z
14552
+ };
14553
+ }
14554
+ function transformNormal(n, m) {
14555
+ assertInvertibleLinearTransform(m);
14556
+ const sign = Math.sign(getLinearTransformDeterminant(m));
14557
+ const x = sign * ((m[4] * m[8] - m[5] * m[7]) * n.x + (m[5] * m[6] - m[3] * m[8]) * n.y + (m[3] * m[7] - m[4] * m[6]) * n.z);
14558
+ const y = sign * ((m[2] * m[7] - m[1] * m[8]) * n.x + (m[0] * m[8] - m[2] * m[6]) * n.y + (m[1] * m[6] - m[0] * m[7]) * n.z);
14559
+ const z = sign * ((m[1] * m[5] - m[2] * m[4]) * n.x + (m[2] * m[3] - m[0] * m[5]) * n.y + (m[0] * m[4] - m[1] * m[3]) * n.z);
14560
+ const length = Math.hypot(x, y, z);
14561
+ return length === 0 ? { x: 0, y: 0, z: 0 } : {
14562
+ x: x / length,
14563
+ y: y / length,
14564
+ z: z / length
14565
+ };
14566
+ }
14567
+ function transformTriangle(t, m) {
14568
+ assertInvertibleLinearTransform(m);
14569
+ const reflected = getLinearTransformDeterminant(m) < 0;
14570
+ const order = reflected ? [0, 2, 1] : [0, 1, 2];
14571
+ return {
14572
+ ...t,
14573
+ vertices: order.map(
14574
+ (i) => applyLinearTransform(t.vertices[i], m)
14575
+ ),
14576
+ normal: transformNormal(t.normal, m),
14577
+ ...t.uvs ? { uvs: order.map((i) => t.uvs[i]) } : {}
14578
+ };
14579
+ }
14580
+
14535
14581
  // lib/gltf/geometry.ts
14536
14582
  function createBoxMesh(size) {
14537
14583
  const hw = size.x / 2;
@@ -14845,6 +14891,42 @@ function transformMesh(mesh, translation, rotation, scale) {
14845
14891
  if (mesh.colors) {
14846
14892
  result.colors = [...mesh.colors];
14847
14893
  }
14894
+ if (scale) {
14895
+ const matrix = [
14896
+ scale.x,
14897
+ 0,
14898
+ 0,
14899
+ 0,
14900
+ scale.y,
14901
+ 0,
14902
+ 0,
14903
+ 0,
14904
+ scale.z
14905
+ ];
14906
+ assertInvertibleLinearTransform(matrix);
14907
+ for (let i = 0; i < result.normals.length; i += 3) {
14908
+ const normal = transformNormal(
14909
+ {
14910
+ x: result.normals[i],
14911
+ y: result.normals[i + 1],
14912
+ z: result.normals[i + 2]
14913
+ },
14914
+ matrix
14915
+ );
14916
+ result.normals[i] = normal.x;
14917
+ result.normals[i + 1] = normal.y;
14918
+ result.normals[i + 2] = normal.z;
14919
+ }
14920
+ if (getLinearTransformDeterminant(matrix) < 0) {
14921
+ for (let i = 0; i < result.indices.length; i += 3) {
14922
+ ;
14923
+ [result.indices[i + 1], result.indices[i + 2]] = [
14924
+ result.indices[i + 2],
14925
+ result.indices[i + 1]
14926
+ ];
14927
+ }
14928
+ }
14929
+ }
14848
14930
  for (let i = 0; i < result.positions.length; i += 3) {
14849
14931
  let x = result.positions[i];
14850
14932
  let y = result.positions[i + 1];
@@ -15952,20 +16034,6 @@ import { createCableMeshes } from "jscad-electronics/cables";
15952
16034
  import { vec3 } from "gl-matrix";
15953
16035
 
15954
16036
  // lib/utils/mesh-scale.ts
15955
- function scalePoint(point, scale) {
15956
- return {
15957
- x: point.x * scale,
15958
- y: point.y * scale,
15959
- z: point.z * scale
15960
- };
15961
- }
15962
- function scalePointByAxis(point, scale) {
15963
- return {
15964
- x: point.x * scale.x,
15965
- y: point.y * scale.y,
15966
- z: point.z * scale.z
15967
- };
15968
- }
15969
16037
  function rotatePoint(point, rotationDeg) {
15970
16038
  let { x, y, z } = point;
15971
16039
  if (rotationDeg.x !== 0) {
@@ -15997,39 +16065,21 @@ function rotatePoint(point, rotationDeg) {
15997
16065
  }
15998
16066
  return { x, y, z };
15999
16067
  }
16000
- function scaleTriangle(triangle, scale) {
16001
- return {
16002
- ...triangle,
16003
- vertices: triangle.vertices.map((vertex) => scalePoint(vertex, scale))
16004
- };
16005
- }
16006
16068
  function scaleMesh(mesh, scale) {
16007
16069
  if (!Number.isFinite(scale) || scale === 1) {
16008
16070
  return mesh;
16009
16071
  }
16010
- const scaledTriangles = mesh.triangles.map(
16011
- (triangle) => scaleTriangle(triangle, scale)
16012
- );
16013
- const scaledBoundingBox = {
16014
- min: scalePoint(mesh.boundingBox.min, scale),
16015
- max: scalePoint(mesh.boundingBox.max, scale)
16016
- };
16017
- return {
16018
- ...mesh,
16019
- triangles: scaledTriangles,
16020
- boundingBox: scaledBoundingBox
16021
- };
16072
+ return scaleMeshByAxis(mesh, { x: scale, y: scale, z: scale });
16022
16073
  }
16023
16074
  function scaleMeshByAxis(mesh, scale) {
16024
16075
  if (!Number.isFinite(scale.x) || !Number.isFinite(scale.y) || !Number.isFinite(scale.z) || scale.x === 1 && scale.y === 1 && scale.z === 1) {
16025
16076
  return mesh;
16026
16077
  }
16027
- const scaledTriangles = mesh.triangles.map((triangle) => ({
16028
- ...triangle,
16029
- vertices: triangle.vertices.map(
16030
- (vertex) => scalePointByAxis(vertex, scale)
16031
- )
16032
- }));
16078
+ const matrix = [scale.x, 0, 0, 0, scale.y, 0, 0, 0, scale.z];
16079
+ assertInvertibleLinearTransform(matrix);
16080
+ const scaledTriangles = mesh.triangles.map(
16081
+ (triangle) => transformTriangle(triangle, matrix)
16082
+ );
16033
16083
  return {
16034
16084
  ...mesh,
16035
16085
  triangles: scaledTriangles,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "circuit-json-to-gltf",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.149",
5
+ "version": "0.0.150",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/tscircuit/circuit-json-to-gltf.git"