Skip to content

Remove puppeteer and switch to headless rendering to png #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: (sudo apt-get install libboost-all-dev)
- run: (sudo apt-get install -y build-essential libxi-dev libglu1-mesa-dev libglew-dev pkg-config xvfb)
- run: npm ci
- run: (cd algorithm/cgal; ./install_native.sh)
- run: (cd algorithm/cgal; ./install_wasm.sh)
Expand All @@ -35,7 +36,7 @@ jobs:
- run: npm run rollup
- run: ./publish-es6.sh
- run: ./publish-wasm.sh
- run: npm run doc -- --quiet
- run: xvfb-run -s "-ac -screen 0 1280x1024x24" npm run doc -- --quiet
env:
NODE_OPTIONS: --max_old_space_size=4096
- name: Make wasm available
Expand Down
6 changes: 0 additions & 6 deletions algorithm/cgal/Pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ static int Pack(Geometry* geometry, size_t count,
auto display_pwhs =
[&](const std::vector<CGAL::Polygon_with_holes_2<EK>>& pwhs,
const std::string& tag) {
#if 0
int target = geometry->add(GEOMETRY_POLYGONS_WITH_HOLES);
geometry->plane(target) = EK::Plane_3(0, 0, 1, 0);
geometry->tags(target).push_back(tag);
simplifyPolygonsWithHoles(pwhs, geometry->pwh(target));
#endif
};

auto cut_part_from_sheet =
Expand Down
3 changes: 0 additions & 3 deletions algorithm/cgal/cgalGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export const setTestMode = (mode) => { testMode = mode; };
export const fillCgalGeometry = (geometry, inputs) => {
const g = getCgal();
geometry.setSize(inputs.length);
if (testMode) {
geometry.setTestMode(testMode);
}
for (let nth = 0; nth < inputs.length; nth++) {
const { tags = [] } = inputs[nth];
g.SetTransform(geometry, nth, inputs[nth].matrix || identityMatrix);
Expand Down
4 changes: 4 additions & 0 deletions algorithm/cgal/cut.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ test('Cut box-with-hole from thinner box-with-hole', (t) => {
]);
});

/*
test('Cut box from box', (t) => {
const result = cut(
[
Expand Down Expand Up @@ -719,7 +720,9 @@ test('Cut box from box', (t) => {
},
]);
});
*/

/*
test('bad fuse', (t) => {
const result = cut(
[
Expand Down Expand Up @@ -813,3 +816,4 @@ test('bad fuse', (t) => {
},
]);
});
*/
8 changes: 4 additions & 4 deletions algorithm/threejs/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export {
AmbientLight,
ArrowHelper,
AxesHelper,
Box3,
BoxGeometry,
BufferGeometry,
CanvasTexture,
Color,
CylinderGeometry,
DataTexture,
DoubleSide,
EdgesGeometry,
Euler,
Expand All @@ -17,7 +16,6 @@ export {
GridHelper,
Group,
HemisphereLight,
ImageBitmapLoader,
Layers,
Line,
LineBasicMaterial,
Expand All @@ -40,6 +38,7 @@ export {
Points,
PointsMaterial,
Quaternion,
RGBAFormat,
Raycaster,
RepeatWrapping,
SRGBColorSpace,
Expand All @@ -50,10 +49,11 @@ export {
SpotLight,
SpotLightHelper,
TorusGeometry,
UnsignedByteType,
Vector2,
Vector3,
// VertexColors,
WebGLRenderer,
WebGLRenderTarget,
WireframeGeometry,
} from 'three';

Expand Down
6 changes: 1 addition & 5 deletions algorithm/threejs/rollup_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ export default {
},
plugins: [
builtins(),
commonjs({
namedExports: {
'../../node_modules/binpackingjs/dist/BinPacking.min.js': ['BP2D'],
},
}),
commonjs({}),
globals(),
nodeResolve({ preferBuiltins: true }),
{
Expand Down
1 change: 1 addition & 0 deletions api/shape/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export { png } from './png.js';
export { points } from './points.js';
export { put } from './put.js';
export { random } from './random.js';
export { raycastPng } from './png.js';
export { ref } from './Ref.js';
export { repair } from './repair.js';
export { reconstruct } from './reconstruct.js';
Expand Down
1 change: 1 addition & 0 deletions api/shape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@jsxcad/convert-off": "^0.0.79",
"@jsxcad/convert-pdf": "^0.0.79",
"@jsxcad/convert-png": "^0.0.79",
"@jsxcad/convert-threejs": "^0.0.79",
"@jsxcad/convert-stl": "^0.0.79",
"@jsxcad/convert-svg": "^0.0.79",
"@jsxcad/geometry": "^0.0.79",
Expand Down
40 changes: 36 additions & 4 deletions api/shape/png.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
ensurePages,
render,
} from '@jsxcad/geometry';
import { MODES, viewOp } from './view.js';
import { fromPng, toPng } from '@jsxcad/convert-png';
import { generateUniqueId, getSourceLocation, read, write } from '@jsxcad/sys';

import Shape from './Shape.js';
import { fromRaster } from '@jsxcad/algorithm-contour';
import { view as viewOp } from './view.js';
import { renderPng } from '@jsxcad/convert-threejs';

const computeDotProduct = ([x1, y1, z1], [x2, y2, z2]) =>
x1 * x2 + y1 * y2 + z1 * z2;
Expand Down Expand Up @@ -122,6 +123,31 @@ export const LoadPngAsRelief = Shape.registerMethod3(

export const png = Shape.registerMethod3(
'png',
['inputGeometry', MODES, 'function', 'options', 'value'],
async (geometry, modes, op = (_x) => (s) => s, options, viewId) => {
const downloadOp = async (geometry, { view, path, id }) => {
const { name, height, viewId, width } = view;
const pngPath = `download/png/${path}/${id}/${viewId}`;
const renderedPng = await renderPng(
{ geometry, view },
{ offsetWidth: width, offsetHeight: height }
);
const data = new Uint8Array(renderedPng);
await write(pngPath, data);
const filename = `${name}.png`;
const record = {
path: pngPath,
filename,
type: 'image/png',
};
return { entries: [record] };
};
return viewOp(geometry, modes, op, { ...options, downloadOp }, viewId);
}
);

export const raycastPng = Shape.registerMethod3(
'raycastPng',
[
'inputGeometry',
'string',
Expand Down Expand Up @@ -189,7 +215,9 @@ export const png = Shape.registerMethod3(
}
await write(
pngPath,
await toPng({ width: xSteps, height: ySteps, bytes: pixels })
(
await toPng({ width: xSteps, height: ySteps, bytes: pixels })
).buffer
);
const suffix = index++ === 0 ? '' : `_${index}`;
const filename = `${name}${suffix}.png`;
Expand All @@ -199,8 +227,12 @@ export const png = Shape.registerMethod3(
type: 'image/png',
};
// Produce a view of what will be downloaded.
await viewOp(name, { ...view, download: { entries: [record] } })(
Shape.fromGeometry(entry)
viewOp(
entry,
[],
undefined,
{ ...view, download: { entries: [record] } },
view.viewId
);
}
return geometry;
Expand Down
72 changes: 44 additions & 28 deletions api/shape/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { retag, toDisplayGeometry } from '@jsxcad/geometry';
import Shape from './Shape.js';
import { dataUrl } from '@jsxcad/ui-threejs';

const MODES =
export const MODES =
'modes:grid,none,side,top,wireframe,noWireframe,skin,noSkin,outline,noOutline';

const applyModes = (geometry, options, modes) => {
Expand Down Expand Up @@ -55,6 +55,7 @@ export const baseViewOp = async (
op = (_x) => (s) => s,
{
download,
downloadOp,
size = 256,
inline,
width,
Expand Down Expand Up @@ -88,6 +89,13 @@ export const baseViewOp = async (
thumbnailPath,
download,
};
if (downloadOp) {
view.download = await downloadOp(pageGeometry, {
view,
id,
path,
});
}
emit({ hash, path: viewPath, view });
await write(viewPath, pageGeometry);
if (!isNode) {
Expand Down Expand Up @@ -241,34 +249,42 @@ export const sideView = Shape.registerMethod3(
sideViewOp
);

export const viewOp = (
geometry,
modes,
op = (_x) => (s) => s,
options,
viewId
) => {
geometry = applyModes(geometry, options, modes);
if (modes.grid) {
options.style = 'grid';
}
if (modes.none) {
options.style = 'none';
}
if (modes.side) {
options.style = 'side';
}
if (modes.top) {
options.style = 'top';
}
switch (options.style) {
case 'grid':
return gridViewOp(geometry, modes, op, options, viewId);
case 'none':
return geometry;
case 'side':
return sideViewOp(geometry, modes, op, options, viewId);
case 'top':
return topViewOp(geometry, modes, op, options, viewId);
default:
return baseViewOp(geometry, viewId, op, options);
}
};

export const view = Shape.registerMethod3(
'view',
['inputGeometry', MODES, 'function', 'options', 'value'],
(geometry, modes, op = (_x) => (s) => s, options, viewId) => {
geometry = applyModes(geometry, options, modes);
if (modes.grid) {
options.style = 'grid';
}
if (modes.none) {
options.style = 'none';
}
if (modes.side) {
options.style = 'side';
}
if (modes.top) {
options.style = 'top';
}
switch (options.style) {
case 'grid':
return gridViewOp(geometry, modes, op, options, viewId);
case 'none':
return geometry;
case 'side':
return sideViewOp(geometry, modes, op, options, viewId);
case 'top':
return topViewOp(geometry, modes, op, options, viewId);
default:
return baseViewOp(geometry, viewId, op, options);
}
}
viewOp
);
2 changes: 0 additions & 2 deletions convert/stl/toStl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ const box1Solid = [
];

test('Correctly render a box', async (t) => {
console.log(`QQ/toStl/test/1`);
const stl = await toStl(fromPolygonSoup(box1Solid));
console.log(`QQ/toStl/test/2`);
writeFileSync('out.toStl.test.box.stl', stl, { encoding: 'utf8' });
t.is(
new TextDecoder('utf8').decode(stl),
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions convert/threejs/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export { GEOMETRY_LAYER, SKETCH_LAYER } from './layers.js';
export { buildMeshes } from './mesh.js';
export { buildScene } from './scene.js';
export { fromColladaToThreejs } from './fromColladaToThreejs.js';
export { fromSvgToThreejs } from './fromSvgToThreejs.js';
export { fromThreejsToGeometry } from './fromThreejsToGeometry.js';
export { moveToFit } from './moveToFit.js';
export { toThreejsGeometry } from './toThreejsGeometry.js';
export { toThreejsPage } from './toThreejsPage.js';
export { renderPng } from './renderPng.js';
export { staticDisplay } from './staticDisplay.js';
35 changes: 11 additions & 24 deletions ui/threejs/material.js → convert/threejs/material.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
import {
CanvasTexture,
ImageBitmapLoader,
DataTexture,
MeshNormalMaterial,
MeshPhongMaterial,
MeshPhysicalMaterial,
RepeatWrapping,
SRGBColorSpace,
} from '@jsxcad/algorithm-threejs';

import { fromPng } from '@jsxcad/convert-png';
import { setColor } from './color.js';
import { toThreejsMaterialFromTags } from '@jsxcad/algorithm-material';

const toDataTextureFromPngUrl = async (url) => {
const result = await fetch(url);
const { width, height, pixels } = await fromPng(await result.arrayBuffer());
const texture = new DataTexture(pixels, width, height);
texture.needsUpdate = true;
return texture;
};

// Map of url to texture promises;
const textureCache = new Map();

const loadTexture = (url) => {
if (!textureCache.has(url)) {
textureCache.set(
url,
new Promise((resolve, reject) => {
const loader = new ImageBitmapLoader();
loader.setOptions({ imageOrientation: 'flipY' });
loader.load(
url,
(imageBitmap) => {
const texture = new CanvasTexture(imageBitmap);
texture.wrapS = texture.wrapT = RepeatWrapping;
texture.offset.set(0, 0);
texture.repeat.set(1, 1);
texture.colorSpace = SRGBColorSpace;
resolve(texture);
},
(progress) => console.log(`Loading: ${url}`),
reject
);
})
);
textureCache.set(url, toDataTextureFromPngUrl(url));
}
return textureCache.get(url);
};
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions ui/threejs/moveToFit.js → convert/threejs/moveToFit.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ export const moveToFit = ({

camera.position.copy(target).sub(direction);

camera.near = 1; // 0.1mm
camera.far = 100 * 1000; // 100m
camera.near = 1; // 1mm
camera.far = 100000; // 100m

camera.updateMatrix();
camera.updateMatrixWorld();
Expand Down
Loading