Skip to content

Commit

Permalink
chore: refactor code so that non mapped code can access 3d-coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
potmo committed Feb 24, 2022
1 parent ab813bc commit 799dac0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 75 deletions.
164 changes: 111 additions & 53 deletions 3d-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

}

Expand Down
13 changes: 7 additions & 6 deletions gcode-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.;`;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions obj-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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);

Expand All @@ -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`));
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions rapid-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] ] ];`
Expand All @@ -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]
Expand All @@ -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);
Expand All @@ -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 {
Expand Down

0 comments on commit 799dac0

Please sign in to comment.