-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBREP_Booleans.html
More file actions
117 lines (100 loc) · 3.93 KB
/
BREP_Booleans.html
File metadata and controls
117 lines (100 loc) · 3.93 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>BREP API Example: Booleans</title>
<link rel="stylesheet" href="./example.css" />
</head>
<body>
<main>
<header class="example-header">
<div>
<h1>BREP Booleans</h1>
<p class="lead">Runs <code>union</code>, <code>subtract</code>, and <code>intersect</code> using two primitives.</p>
</div>
<nav class="example-nav">
<a href="./index.html">All Examples</a>
<a href="../index.html">BREP CAD App</a>
<a href="https://github.com/mmiscool/BREP/blob/master/apiExamples/BREP_Booleans.html" target="_blank" rel="noopener noreferrer">Source on GitHub</a>
</nav>
</header>
<div class="row">
<button id="btn-run">Run Example</button>
<span id="status" class="status pending">Ready</span>
</div>
<pre id="log">(Click "Run Example")</pre>
</main>
<script type="module">
import { BREP } from '../dist-kernel/brep-kernel.js';
const statusEl = document.getElementById('status');
const logEl = document.getElementById('log');
const btnRun = document.getElementById('btn-run');
const setStatus = (text, cls) => {
statusEl.textContent = text;
statusEl.className = `status ${cls}`;
};
const fmt = (num) => Number(num).toFixed(6);
const write = (...parts) => {
const line = parts.map((part) => (typeof part === 'string' ? part : JSON.stringify(part))).join(' ');
logEl.textContent += `${line}\n`;
console.log(...parts);
};
const summarize = (solid) => ({
volume: solid.volume(),
surfaceArea: solid.surfaceArea(),
triangles: solid.getTriangleCount(),
});
const runExample = () => {
logEl.textContent = '';
setStatus('Running...', 'pending');
const cube = new BREP.Cube({ x: 2.2, y: 2.2, z: 2.2, name: 'Cube' });
const sphere = new BREP.Sphere({ r: 1.35, resolution: 40, name: 'Sphere' });
const union = cube.union(sphere);
const subtract = cube.subtract(sphere);
const intersect = cube.intersect(sphere);
const report = {
cube: summarize(cube),
sphere: summarize(sphere),
union: summarize(union),
subtract: summarize(subtract),
intersect: summarize(intersect),
};
write('--- Boolean Results ---');
for (const [name, stats] of Object.entries(report)) {
write(
`${name.padEnd(10)} volume=${fmt(stats.volume)} area=${fmt(stats.surfaceArea)} triangles=${stats.triangles}`,
);
}
const epsilon = 1e-6;
if (report.union.volume + epsilon < Math.max(report.cube.volume, report.sphere.volume)) {
throw new Error('Union volume is unexpectedly smaller than both source solids.');
}
if (report.intersect.volume - epsilon > Math.min(report.cube.volume, report.sphere.volume)) {
throw new Error('Intersection volume is unexpectedly larger than a source solid.');
}
if (report.subtract.volume - epsilon > report.cube.volume) {
throw new Error('Subtract volume is unexpectedly larger than the original cube.');
}
setStatus('Completed', 'ok');
};
btnRun.addEventListener('click', () => {
try {
runExample();
} catch (error) {
console.error(error);
write('ERROR:', error?.stack || error?.message || String(error));
setStatus('Failed', 'err');
}
});
try {
runExample();
} catch (error) {
console.error(error);
write('ERROR:', error?.stack || error?.message || String(error));
setStatus('Failed', 'err');
}
</script>
</body>
</html>