Spaces:
Running
Running
Upload 8 files
Browse files- app.js +22 -129
- index.html +3 -3
- system_prompt.txt +1 -1
app.js
CHANGED
|
@@ -1,133 +1,8 @@
|
|
| 1 |
-
const
|
| 2 |
-
harmonic: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*((1-cos(pi*t/D))/2)" },
|
| 3 |
-
cycloid: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*((t/D)-(sin(2*pi*t/D)/(2*pi)))" },
|
| 4 |
-
poly345: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*(10*(t/D)^3-15*(t/D)^4+6*(t/D)^5)" },
|
| 5 |
-
poly4567: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*(35*(t/D)^4-84*(t/D)^5+70*(t/D)^6-20*(t/D)^7)" },
|
| 6 |
-
};
|
| 7 |
-
|
| 8 |
-
function escapeRegExp(value) {
|
| 9 |
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
function splitArguments(source) {
|
| 13 |
-
const values = [];
|
| 14 |
-
let start = 0;
|
| 15 |
-
let depth = 0;
|
| 16 |
-
for (let index = 0; index < source.length; index += 1) {
|
| 17 |
-
if (source[index] === "(") depth += 1;
|
| 18 |
-
if (source[index] === ")") depth -= 1;
|
| 19 |
-
if (source[index] === "," && depth === 0) {
|
| 20 |
-
values.push(source.slice(start, index).trim());
|
| 21 |
-
start = index + 1;
|
| 22 |
-
}
|
| 23 |
-
}
|
| 24 |
-
values.push(source.slice(start).trim());
|
| 25 |
-
return values;
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
function replaceTokens(source, replacements) {
|
| 29 |
-
return Object.entries(replacements)
|
| 30 |
-
.sort(([left], [right]) => right.length - left.length)
|
| 31 |
-
.reduce(
|
| 32 |
-
(text, [name, value]) => text.replace(new RegExp(`\\b${escapeRegExp(name)}\\b`, "g"), value),
|
| 33 |
-
source,
|
| 34 |
-
);
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
function selectConditionalBranches(source) {
|
| 38 |
-
const conditional = /@if\s+([^\r\n]+)\r?\n([\s\S]*?)(?:@else\r?\n([\s\S]*?))?@endif/g;
|
| 39 |
-
let result = source;
|
| 40 |
-
let previous;
|
| 41 |
-
do {
|
| 42 |
-
previous = result;
|
| 43 |
-
result = result.replace(conditional, (_match, condition, yes, no = "") => {
|
| 44 |
-
const [left, right] = condition.split("==").map((value) => value.trim());
|
| 45 |
-
return left === right ? yes : no;
|
| 46 |
-
});
|
| 47 |
-
} while (result !== previous);
|
| 48 |
-
return result;
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
function expandFollower(macroSource, callLine) {
|
| 52 |
-
const declaration = macroSource.match(/^\s*@macro\s+([A-Za-z_]\w*)\(([^)]*)\)\s*\{/);
|
| 53 |
-
const call = callLine.match(/^\s*@([A-Za-z_]\w*)\((.*)\)\s*$/);
|
| 54 |
-
if (!declaration || !call || declaration[1] !== call[1]) {
|
| 55 |
-
throw new Error("The generated follower call does not match its macro file.");
|
| 56 |
-
}
|
| 57 |
-
const parameters = splitArguments(declaration[2]).map((parameter) => {
|
| 58 |
-
const equals = parameter.indexOf("=");
|
| 59 |
-
return equals < 0
|
| 60 |
-
? { name: parameter.trim(), fallback: undefined }
|
| 61 |
-
: { name: parameter.slice(0, equals).trim(), fallback: parameter.slice(equals + 1).trim() };
|
| 62 |
-
});
|
| 63 |
-
const argumentsList = splitArguments(call[2]);
|
| 64 |
-
const replacements = {};
|
| 65 |
-
parameters.forEach((parameter, index) => {
|
| 66 |
-
const value = argumentsList[index] || parameter.fallback;
|
| 67 |
-
if (!value) throw new Error(`The ${declaration[1]} call is missing ${parameter.name}.`);
|
| 68 |
-
replacements[parameter.name] = value;
|
| 69 |
-
});
|
| 70 |
-
const bodyStart = declaration.index + declaration[0].length;
|
| 71 |
-
const bodyEnd = macroSource.lastIndexOf("}");
|
| 72 |
-
if (bodyEnd < bodyStart) throw new Error("The follower macro file is incomplete.");
|
| 73 |
-
const substituted = replaceTokens(macroSource.slice(bodyStart, bodyEnd), replacements);
|
| 74 |
-
return selectConditionalBranches(substituted)
|
| 75 |
-
.split(/\r?\n/)
|
| 76 |
-
.map((line) => line.trim())
|
| 77 |
-
.filter((line) => line && !line.startsWith("@"))
|
| 78 |
-
.join("\n");
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
function findClosingParenthesis(source, openingIndex) {
|
| 82 |
-
let depth = 0;
|
| 83 |
-
for (let index = openingIndex; index < source.length; index += 1) {
|
| 84 |
-
if (source[index] === "(") depth += 1;
|
| 85 |
-
if (source[index] === ")") {
|
| 86 |
-
depth -= 1;
|
| 87 |
-
if (depth === 0) return index;
|
| 88 |
-
}
|
| 89 |
-
}
|
| 90 |
-
return -1;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
function expandMotionLaw(source, name, law) {
|
| 94 |
-
let result = source;
|
| 95 |
-
while (result.includes(`${name}(`)) {
|
| 96 |
-
const start = result.lastIndexOf(`${name}(`);
|
| 97 |
-
const opening = start + name.length;
|
| 98 |
-
const closing = findClosingParenthesis(result, opening);
|
| 99 |
-
if (closing < 0) throw new Error(`The ${name} motion expression is incomplete.`);
|
| 100 |
-
const argumentsList = splitArguments(result.slice(opening + 1, closing));
|
| 101 |
-
if (argumentsList.length !== law.parameters.length) {
|
| 102 |
-
throw new Error(`The ${name} motion expression has the wrong number of arguments.`);
|
| 103 |
-
}
|
| 104 |
-
const replacements = Object.fromEntries(
|
| 105 |
-
law.parameters.map((parameter, index) => [parameter, `(${argumentsList[index]})`]),
|
| 106 |
-
);
|
| 107 |
-
const expanded = `(${replaceTokens(law.expression, replacements)})`;
|
| 108 |
-
result = `${result.slice(0, start)}${expanded}${result.slice(closing + 1)}`;
|
| 109 |
-
}
|
| 110 |
-
return result;
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
function expandMotionLaws(source) {
|
| 114 |
-
return Object.entries(MOTION_LAWS).reduce(
|
| 115 |
-
(text, [name, law]) => expandMotionLaw(text, name, law),
|
| 116 |
-
source,
|
| 117 |
-
);
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
function compileProgram(macroSource, generatedBody) {
|
| 121 |
-
const lines = generatedBody.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
| 122 |
-
if (lines.length !== 3) throw new Error("The model must return exactly three CoreGX lines.");
|
| 123 |
-
const geometry = expandFollower(macroSource, lines[0]);
|
| 124 |
-
const motion = expandMotionLaws(lines[1]);
|
| 125 |
-
return `${geometry}\n${motion}\n${lines[2]}\n`;
|
| 126 |
-
}
|
| 127 |
|
| 128 |
const LOCAL_MODE = window.CAM_STUDIO_LOCAL === true;
|
| 129 |
const COREGX_PROXY_URL = String(window.CAM_STUDIO_CONFIG?.coregxProxyUrl || "").trim();
|
| 130 |
-
const BUILD_ID = "20260921-
|
| 131 |
|
| 132 |
const TEMPLATES = {
|
| 133 |
recip_flat: {
|
|
@@ -148,6 +23,13 @@ const TEMPLATES = {
|
|
| 148 |
},
|
| 149 |
};
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
const EXAMPLES = {
|
| 152 |
"recip-roller": "Reciprocating roller follower, base radius 20, roller radius 3, offset 2. Cycloidal rise from 0 to 5 over 20 degrees, dwell to 60 degrees, 3-4-5 polynomial rise from 5 to 12 over 30 degrees, dwell to 120 degrees, then 4-5-6-7 polynomial return from 12 to 0 over 40 degrees. Clockwise.",
|
| 153 |
"recip-flat": "Reciprocating flat follower with a 40 mm base radius and 9 mm offset. Cycloidal 6 mm rise over 60 degrees followed immediately by cycloidal return over 60 degrees. Clockwise.",
|
|
@@ -208,8 +90,19 @@ async function textFile(path) {
|
|
| 208 |
}
|
| 209 |
|
| 210 |
async function assembleProgram(name, body) {
|
| 211 |
-
const followerMacro = await
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
| 214 |
|
| 215 |
function proxyEndpoint(path) {
|
|
|
|
| 1 |
+
const MOTION_LAW_NAMES = ["harmonic", "cycloid", "poly345", "poly4567"];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
const LOCAL_MODE = window.CAM_STUDIO_LOCAL === true;
|
| 4 |
const COREGX_PROXY_URL = String(window.CAM_STUDIO_CONFIG?.coregxProxyUrl || "").trim();
|
| 5 |
+
const BUILD_ID = "20260921-5";
|
| 6 |
|
| 7 |
const TEMPLATES = {
|
| 8 |
recip_flat: {
|
|
|
|
| 23 |
},
|
| 24 |
};
|
| 25 |
|
| 26 |
+
const EXAMPLE_MOTION_LAWS = {
|
| 27 |
+
recip_flat: ["cycloid"],
|
| 28 |
+
recip_roller: ["harmonic", "cycloid", "poly345", "poly4567"],
|
| 29 |
+
osc_flat: ["cycloid"],
|
| 30 |
+
osc_roller: ["harmonic", "cycloid"],
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
const EXAMPLES = {
|
| 34 |
"recip-roller": "Reciprocating roller follower, base radius 20, roller radius 3, offset 2. Cycloidal rise from 0 to 5 over 20 degrees, dwell to 60 degrees, 3-4-5 polynomial rise from 5 to 12 over 30 degrees, dwell to 120 degrees, then 4-5-6-7 polynomial return from 12 to 0 over 40 degrees. Clockwise.",
|
| 35 |
"recip-flat": "Reciprocating flat follower with a 40 mm base radius and 9 mm offset. Cycloidal 6 mm rise over 60 degrees followed immediately by cycloidal return over 60 degrees. Clockwise.",
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
async function assembleProgram(name, body) {
|
| 93 |
+
const [followerMacro, motionLawSource] = await Promise.all([
|
| 94 |
+
textFile(`macros/${TEMPLATES[name].file}`),
|
| 95 |
+
textFile("macros/motion_laws.coregx"),
|
| 96 |
+
]);
|
| 97 |
+
const requiredNames = new Set(EXAMPLE_MOTION_LAWS[name]);
|
| 98 |
+
for (const law of MOTION_LAW_NAMES) {
|
| 99 |
+
if (body.includes(`${law}(`)) requiredNames.add(law);
|
| 100 |
+
}
|
| 101 |
+
const selectedMotionLaws = motionLawSource
|
| 102 |
+
.split(/\r?\n/)
|
| 103 |
+
.filter((line) => [...requiredNames].some((law) => line.startsWith(`@macro ${law}(`)))
|
| 104 |
+
.join("\n");
|
| 105 |
+
return `${followerMacro}\n\n${selectedMotionLaws}\n\n${body}\n`;
|
| 106 |
}
|
| 107 |
|
| 108 |
function proxyEndpoint(path) {
|
index.html
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
<meta name="description" content="Generate animated CoreGX cam mechanisms in your browser.">
|
| 7 |
<title>CoreGX Cam Studio</title>
|
| 8 |
-
<link rel="stylesheet" href="styles.css?v=20260921-
|
| 9 |
</head>
|
| 10 |
<body>
|
| 11 |
<main class="page">
|
|
@@ -47,7 +47,7 @@
|
|
| 47 |
</details>
|
| 48 |
</main>
|
| 49 |
|
| 50 |
-
<script src="config.js?v=20260921-
|
| 51 |
-
<script type="module" src="app.js?v=20260921-
|
| 52 |
</body>
|
| 53 |
</html>
|
|
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
<meta name="description" content="Generate animated CoreGX cam mechanisms in your browser.">
|
| 7 |
<title>CoreGX Cam Studio</title>
|
| 8 |
+
<link rel="stylesheet" href="styles.css?v=20260921-5">
|
| 9 |
</head>
|
| 10 |
<body>
|
| 11 |
<main class="page">
|
|
|
|
| 47 |
</details>
|
| 48 |
</main>
|
| 49 |
|
| 50 |
+
<script src="config.js?v=20260921-5"></script>
|
| 51 |
+
<script type="module" src="app.js?v=20260921-5"></script>
|
| 52 |
</body>
|
| 53 |
</html>
|
system_prompt.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
You generate the three-line
|
| 2 |
|
| 3 |
FOLLOW THE WORKING MACRO EXAMPLES
|
| 4 |
Keep the geometry call, motion expression, phase convention, and argument order isomorphic to the examples below. Do not invent geometry or rename points.
|
|
|
|
| 1 |
+
You generate the three-line body for an animated cam mechanism. The application prepends the exact working follower macro and the required motion-law macros from the separate macro files. Return exactly: one follower macro call, one motion constraint, and one animate line. Do not output macro definitions, Markdown, comments, prose, or extra lines.
|
| 2 |
|
| 3 |
FOLLOW THE WORKING MACRO EXAMPLES
|
| 4 |
Keep the geometry call, motion expression, phase convention, and argument order isomorphic to the examples below. Do not invent geometry or rename points.
|