diff --git a/3d-generator.js b/3d-generator.js index f540381..280bc6d 100644 --- a/3d-generator.js +++ b/3d-generator.js @@ -17,90 +17,148 @@ export { async function generate(settings, mappings, wall_generator) { let wall = wall_generator.getWall(settings) - - const eye_offset = settings.three_dee.eye_offset; + const mirror_board = { + widthVector: vector(settings.three_dee.mirror_board_diameter, 0, 0), + heightVector: vector(0, settings.three_dee.mirror_board_diameter, 0), + center: vector(0,0,0), + } + const eye = { + pos: vector(0,0,0).add(settings.three_dee.eye_offset), + size: 0.1 + } - const mirror_diameter = settings.three_dee.mirror_diameter; - const mirror_padding = settings.three_dee.mirror_padding; - const mirror_thickness = settings.three_dee.mirror_thickness; - const mirror_board_diameter = settings.three_dee.mirror_board_diameter; + //const world_objects = getWorld3DObjects(settings, wall_generator); - const mirror = { - widthVector: vector(mirror_diameter, 0, 0), - heightVector: vector(0, mirror_diameter, 0), - thicknessVector: vector(0, 0, -mirror_thickness) - } + await createSection(settings, wall, mirror_board, eye, mappings.mapping); +} - const mirror_board = { - widthVector: vector(mirror_board_diameter, 0, 0), - heightVector: vector(0, mirror_board_diameter, 0), - center: vector(0,0,0), - } +async function createSection(settings, wall, mirror_board, eye, mappings) { + + const reflections = createReflections(settings, wall, mappings); + + //const rapidString = rapid.generate(reflections, wall, eye); + //await saveFile(path.join(settings.output.path, `output.mod`), rapidString); + + const gcodeString = gcode.generate(reflections, wall, eye); + await saveFile(path.join(settings.output.path, `output.cnc`), gcodeString); - const eye_position = vector(0,0,0).add(eye_offset); + const objString = objcode.generate(reflections, wall, eye, settings.three_dee.wall_face_divisions); + await saveFile(path.join(settings.output.path, `output.obj`), objString); + + await reflection_visualizer.visualize(settings, reflections, wall); + + await reflection_visualizer.visualizeArrangement(settings, reflections, mirror_board); + + await reflection_visualizer.visualizeMirrorColorGroups(settings, reflections, mirror_board); + + await reflection_visualizer.visualizeMirrorAngleDeviations(settings, reflections, mirror_board); + + await reflection_visualizer.visualizeMirrorColorGroupsCenterAndOptimal(settings, reflections, mirror_board); + + + + printSize(reflections) - const eye = {pos: eye_position, size: 0.1}; - await createSection(settings, wall, mirror, mirror_board, eye, mappings.mapping); } -async function createSection(settings, wall, mirror, mirror_board, eye, mappings) { +function createReflections(settings, wall, mappings) { - // Note that all positions are sclars between -0.5 to 0.5 + const reflections = mappings.map((mapping,id) => { + return createReflectionSetupForMirror(settings, id, mapping.mirror, mapping.palette, mapping.palette.colors, mapping.string, wall); + }) + + return reflections; + +} - let mirrors = []; - let reflections = []; - let id = 0; +function createReflectionSetupForMirror(settings, id, mirror_pixel_pos, target_palette_pos, colors, color_keys, wall) { - for (let mapping of mappings) { - /*const target = wall.center.add(wall.widthVector.scale(mapping.palette.x)) - .add(wall.heightVector.scale(mapping.palette.y * -1));*/ + const three_dee = convertTo3DWorldCoordinates(settings, mirror_pixel_pos, target_palette_pos, colors, color_keys, wall); + const mirror = createMirrorLookingAt(id++, + three_dee.mirror.pos, + three_dee.eye, + three_dee.target.pos, + three_dee.mirror.width.mag(), + three_dee.mirror.thickness.mag()); - const target_pos = wall.worldPosAtTextureCoord(mapping.palette.x, mapping.palette.y * -1) - const target_normal = wall.worldNormalAtTextureCoord(mapping.palette.x, mapping.palette.y * -1); + const ellipse_points = createReflectanceEllipsePoints(mirror, three_dee.eye.pos, three_dee.target, three_dee.wall); - const target = {pos: target_pos, normal: target_normal} + const reflection = { + mirror: mirror, + target: three_dee.target.pos, + target_normal: three_dee.target.normal, + eye: three_dee.eye, + ellipse_points: ellipse_points, + colors: three_dee.mirror.colors.colors, + color_keys: three_dee.mirror.color_keys + }; - const mirrorPos = vector(0,0,0).add(mirror_board.widthVector.scale(mapping.mirror.x)) - .add(mirror_board.heightVector.scale(mapping.mirror.y * -1)) - .add(mirror.thicknessVector) + return reflection; - const mirrorObj = createMirrorLookingAt(id++, mirrorPos, eye, target.pos, mirror.widthVector.mag(), mirror.thicknessVector.mag()); - mirrors.push(mirrorObj); +} - // TODO: This should be changed - const ellipse_points = createReflectanceEllipsePoints(mirrorObj, eye.pos, target, wall); +function getWorld3DObjects(settings, wall_generator) { - const colors = mapping.palette.colors || [{r:255, g:255, b: 255, a: 255}]; + let wall = wall_generator.getWall(settings) - reflections.push({mirror: mirrorObj, target: target.pos, target_normal, eye, ellipse_points, colors, color_keys: mapping.string}); + const mirror_board = { + widthVector: vector(settings.three_dee.mirror_board_diameter, 0, 0), + heightVector: vector(0, settings.three_dee.mirror_board_diameter, 0), + center: vector(0,0,0), } - const rapidString = rapid.generate(mirrors, reflections, wall, eye); - await saveFile(path.join(settings.output.path, `output.mod`), rapidString); + const eye = { + pos: vector(0,0,0).add(settings.three_dee.eye_offset), + size: 0.1 + } - const gcodeString = gcode.generate(mirrors, reflections, wall, eye); - await saveFile(path.join(settings.output.path, `output.cnc`), gcodeString); + return { + eye, + mirror_board, + wall + } - const objString = objcode.generate(mirrors, reflections, wall, eye, settings.three_dee.wall_face_divisions); - await saveFile(path.join(settings.output.path, `output.obj`), objString); +} - await reflection_visualizer.visualize(settings, reflections, wall); +function convertTo3DWorldCoordinates(settings, mirror_pixel_pos, target_palette_pos, colors, color_keys, wall) { - await reflection_visualizer.visualizeArrangement(settings, reflections, mirror_board); - - await reflection_visualizer.visualizeMirrorColorGroups(settings, reflections, mirror_board); + const mirror_board = { + widthVector: vector(settings.three_dee.mirror_diameter, 0, 0), + heightVector: vector(0, settings.three_dee.mirror_diameter, 0), + center: vector(0,0,0), + } - await reflection_visualizer.visualizeMirrorAngleDeviations(settings, reflections, mirror_board); + const eye = { + pos: vector(0,0,0).add(settings.three_dee.eye_offset), + size: 0.1 + }; - await reflection_visualizer.visualizeMirrorColorGroupsCenterAndOptimal(settings, reflections, mirror_board); + const mirror_pos = vector(0,0,0).add(mirror_board.widthVector.scale(mirror_pixel_pos.x)) + .add(mirror_board.heightVector.scale(mirror_pixel_pos.y * -1)) + .add(vector(0, 0, -settings.three_dee.mirror_thickness)) - + const mirror = { + width: vector(settings.three_dee.mirror_diameter, 0, 0), + height: vector(0, settings.three_dee.mirror_diameter, 0), + thickness: vector(0, 0, -settings.three_dee.mirror_thickness), + pos: mirror_pos, + colors: {key: color_keys, colors}, + } - printSize(reflections) + const target = { + pos: wall.worldPosAtTextureCoord(target_palette_pos.x, target_palette_pos.y * -1), + normal: wall.worldNormalAtTextureCoord(target_palette_pos.x, target_palette_pos.y * -1), + } + return { + mirror, + target, + eye, + wall, + } } diff --git a/gcode-generator.js b/gcode-generator.js index 798b829..1c99b32 100644 --- a/gcode-generator.js +++ b/gcode-generator.js @@ -3,13 +3,13 @@ import colors from 'colors'; import vector from './vector.js'; -function generate(mirrors, reflections, photowall, eye) { - const program = Array.from(generateModule(mirrors)).join('\n'); +function generate(reflections, photowall, eye) { + const program = Array.from(generateModule(reflections)).join('\n'); console.log(colors.green(`created gcode (.nc) file`)); return program; } -function* generateModule(mirrors) { +function* generateModule(reflections) { const tool = 20; const tool_diameter = 6.0; @@ -56,7 +56,7 @@ function* generateModule(mirrors) { yield `G00 X0. Y0. Z300. B0. C0.;`; - yield *indent('', generatePositions(mirrors, tool, tool_diameter)); + yield *indent('', generatePositions(reflections, tool, tool_diameter)); yield `(GO BACK TO STARTING POSITION)`; yield `G00 X0. Y0. Z300. B0. C0.;`; @@ -88,14 +88,15 @@ function* generateModule(mirrors) { } -function* generatePositions(mirrors, tool, tool_diameter) { +function* generatePositions(reflections, tool, tool_diameter) { const clearence_height = 30.0 // beware since this scale is depending on the scale that the 3d generator has // that come from the settings const scale = 1000.0; - const adjusted_mirrors = mirrors + const adjusted_mirrors = reflections + .map(reflection => reflection.mirror) .sort( (a,b) => { if (a.pos.x != b.pos.y) { return a.pos.x - b.pos.x; diff --git a/obj-generator.js b/obj-generator.js index 40ca33a..580e6c3 100644 --- a/obj-generator.js +++ b/obj-generator.js @@ -7,14 +7,14 @@ export { generate, } -function generate(mirrors, reflections, wall, eye, wall_face_divisions) { - const program = Array.from(createObjFile(mirrors, reflections, wall, eye, wall_face_divisions)).join('\n'); +function generate(reflections, wall, eye, wall_face_divisions) { + const program = Array.from(createObjFile(reflections, wall, eye, wall_face_divisions)).join('\n'); return program; } -function * createObjFile(mirrors, reflections, wall, eye, wall_face_divisions) { +function * createObjFile(reflections, wall, eye, wall_face_divisions) { - console.log('mirrors', mirrors.length); + console.log('mirrors', reflections.length); var vertex = {current: 1}; @@ -26,7 +26,7 @@ function * createObjFile(mirrors, reflections, wall, eye, wall_face_divisions) { yield * convertTexturesToObj(textures); - yield * convertMirrorsToObj(mirrors, vertex); + yield * convertMirrorsToObj(reflections, vertex); yield * convertWallToObj(wall, vertex, textures.wall_texture_ids); @@ -36,7 +36,7 @@ function * createObjFile(mirrors, reflections, wall, eye, wall_face_divisions) { yield * convertReflectionEllipsesToObj(reflections, vertex); - yield * convertMirrorNormalsToObj(mirrors, vertex); + yield * convertMirrorNormalsToObj(reflections, vertex); console.log(colors.green(`created mesh (.obj) file`)); } @@ -78,10 +78,10 @@ function getWallTextureMap(wall_face_divisions) { return output; } -function * convertMirrorsToObj(mirrors, vertex) { +function * convertMirrorsToObj(reflections, vertex) { //#yield `usemtl mirror_face`; - const mirrorFaces = mirrors.map( mirror => getMirrorPolygons(mirror, vertex)); + const mirrorFaces = reflections.map(r => r.mirror).map( mirror => getMirrorPolygons(mirror, vertex)); // create vertices for the mirror for (const face of mirrorFaces) { @@ -173,9 +173,10 @@ function vertice(vector) { } -function * convertMirrorNormalsToObj(mirrors, vertex) { +function * convertMirrorNormalsToObj(reflections, vertex) { yield `g debug_mirror_normals`; + const mirrors = reflections.map(r => r.mirror); for (let mirror of mirrors) { yield * convertMirrorNormalToObj(mirror, vertex); } diff --git a/rapid-generator.js b/rapid-generator.js index c80d453..cc7e5c6 100644 --- a/rapid-generator.js +++ b/rapid-generator.js @@ -25,13 +25,13 @@ const fromBottom = new Quaternion.fromBetweenVectors(referenceVector, [0, 0, 1]) const middle = [1000, 1000, 1000]; -function generate(mirrors, reflections, photowall, eye) { - const program = Array.from(generateModule(mirrors)).join('\n'); +function generate(reflections, photowall, eye) { + const program = Array.from(generateModule(reflections)).join('\n'); console.log(colors.green(`created rapid (.mod) file`)); return program; } -function* generateModule(mirrors) { +function* generateModule(reflections) { yield `MODULE MainModule`; // heldObject, fixed user coord used, name?, user coords, user rot, object coord, object rot yield ` PERS wobjdata platform := [ FALSE, TRUE, "", [ [800, 0, 600], [1, 0, 0 ,0] ], [ [0, 0, 0], [1, 0, 0 ,0] ] ];` @@ -41,12 +41,12 @@ function* generateModule(mirrors) { yield ` ConfL\\Off;`; yield ` ConfJ\\Off;`; yield ` SingArea\\Wrist;`; - yield *indent(' ', generatePositions(mirrors)); + yield *indent(' ', generatePositions(reflections)); yield ` ENDPROC`; yield `ENDMODULE`; } -function* generatePositions(mirrors) { +function* generatePositions(reflections) { // robtarget // position, quaternion, the quadrant to start (-4...4), some conf (9E9 == null) // [x,y,z], [q1, q2, q3, q4], [n,n,n,n] @@ -61,7 +61,7 @@ function* generatePositions(mirrors) { y: Number.MIN_VALUE, } }; - const min_max = mirrors.map( a => a.pos) + const min_max = reflections.map(r => r.mirror).map( a => a.pos) .reduce( (m, a) => { m.min.x = Math.min(m.min.x, a.x); m.min.y = Math.min(m.min.y, a.y); @@ -78,7 +78,7 @@ function* generatePositions(mirrors) { // offset everything for the robot to have easier to reach const offset = vector(0, -size.y / 2, 0); - const adjusted_mirrors = mirrors.map(mirror => { + const adjusted_mirrors = reflections.map(r => r.mirror).map(mirror => { //offset (in cm) ands cale from cm to to mm const adjusted_pos = mirror.pos.add(offset).scale(10); return {