Skip to content

Commit

Permalink
Factor segment processing, output raw segs JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimmons committed Jan 10, 2019
1 parent 52abd14 commit 02a9a56
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 43 deletions.
99 changes: 58 additions & 41 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,7 @@ function* generateEdgesVertical(triGrid) {
}
}

function combineSegments(segments) {
const result = [];

// Segment we are building but have not yet "emittted"
let openSeg;

function emitAnySeg() {
if (openSeg) {
result.push(openSeg);
openSeg = undefined;
}
}

for (const seg of segments) {
if (!openSeg || (seg[0].x !== openSeg[1].x) || (seg[0].y !== openSeg[1].y)) {
emitAnySeg();
openSeg = [{x: seg[0].x, y: seg[0].y}, {x: seg[1].x, y: seg[1].y}]; // clone seg
} else {
openSeg[1].x = seg[1].x;
openSeg[1].y = seg[1].y;
}
}
emitAnySeg();

return result;
}

function triGridToDumbSegments(triGrid) {
function triGridToUncombinedIntegerSegmentGroups(triGrid) {
const segmentsNESW = [];
for (const {tAbove, tBelow, leftV, rightV} of generateEdgesNESW(triGrid)) {
let draw = false;
Expand Down Expand Up @@ -268,18 +241,54 @@ function triGridToDumbSegments(triGrid) {
}
});

return [].concat(combineSegments(segmentsNESW), combineSegments(segmentsNWSE), combineSegments(segmentsVertical));
return {
nesw: segmentsNESW,
nwse: segmentsNWSE,
vertical: segmentsVertical,
};
}

function combineSegments(segments) {
const result = [];

// Segment we are building but have not yet "emittted"
let openSeg;

function emitAnySeg() {
if (openSeg) {
result.push(openSeg);
openSeg = undefined;
}
}

for (const seg of segments) {
if (!openSeg || (seg[0].x !== openSeg[1].x) || (seg[0].y !== openSeg[1].y)) {
emitAnySeg();
openSeg = [{x: seg[0].x, y: seg[0].y}, {x: seg[1].x, y: seg[1].y}]; // clone seg
} else {
openSeg[1].x = seg[1].x;
openSeg[1].y = seg[1].y;
}
}
emitAnySeg();

return result;
}

// Makes all triangles equilateral with side length 1
const VERT_COORD_SCALE_X = 0.5*Math.sqrt(3);
const VERT_COORD_SCALE_Y = 0.5;

function scaleIntegerSegments(segments) {
return segments.map(([a, b]) => ([{x: VERT_COORD_SCALE_X*a.x, y: VERT_COORD_SCALE_Y*a.y}, {x: VERT_COORD_SCALE_X*b.x, y: VERT_COORD_SCALE_Y*b.y}]));
}

function segmentsToSVG(triGrid, segments, svgOpts) {
function segmentGroupsToSVG(triGrid, segmentGroups, svgOpts) {
const padFrac = (svgOpts && svgOpts.padFrac !== undefined) ? svgOpts.padFrac : 0;
const splitPaths = (svgOpts && svgOpts.splitPaths !== undefined) ? svgOpts.splitPaths : false;

// Makes all triangles equilateral with side length 1
const scaleX = 0.5*Math.sqrt(3);
const scaleY = 0.5;

const scaledSegments = segments.map(([a, b]) => ([{x: scaleX*a.x, y: scaleY*a.y}, {x: scaleX*b.x, y: scaleY*b.y}]));
const combinedSegments = [].concat(combineSegments(segmentGroups.nesw), combineSegments(segmentGroups.nwse), combineSegments(segmentGroups.vertical));
const scaledSegments = scaleIntegerSegments(combinedSegments);

const pathPieces = [];
if (splitPaths) {
Expand All @@ -295,8 +304,8 @@ function segmentsToSVG(triGrid, segments, svgOpts) {
}
const pathStr = pathPieces.join('');

const unframedWidth = scaleX*triGrid.size.x;
const unframedHeight = scaleY*triGrid.size.y;
const unframedWidth = VERT_COORD_SCALE_X*triGrid.size.x;
const unframedHeight = VERT_COORD_SCALE_Y*triGrid.size.y;
const frameDim = padFrac*Math.max(unframedWidth, unframedHeight);
const viewBoxOrigX = -frameDim;
const viewBoxOrigY = -frameDim;
Expand All @@ -306,13 +315,21 @@ function segmentsToSVG(triGrid, segments, svgOpts) {
return `<svg viewBox="${viewBoxOrigX} ${viewBoxOrigY} ${viewBoxWidth} ${viewBoxHeight}" xmlns="http://www.w3.org/2000/svg"><g stroke="black" stroke-width="0.4" stroke-linecap="round">${pathStr}</g></svg>`;
}

function renderSceneToSVG(scene, svgOpts) {
function segmentGroupsToFlatUncombinedScaled(segmentGroups) {
const flatSegments = [].concat(segmentGroups.nesw, segmentGroups.nwse, segmentGroups.vertical);
return scaleIntegerSegments(flatSegments);
}

function renderScene(scene, svgOpts) {
const {triGrid, offset} = createTriGridForScene(scene);
renderSceneToTriGrid(scene, triGrid, offset);
const segments = triGridToDumbSegments(triGrid);
return segmentsToSVG(triGrid, segments, svgOpts);
const segmentGroups = triGridToUncombinedIntegerSegmentGroups(triGrid);
return {
svg: segmentGroupsToSVG(triGrid, segmentGroups, svgOpts),
uncombinedSegments: segmentGroupsToFlatUncombinedScaled(segmentGroups),
};
}

module.exports = {
renderSceneToSVG,
renderScene,
};
5 changes: 3 additions & 2 deletions sketches/util.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const fs = require('fs');

const {createScene, ops} = require('../lib/scene');
const {renderSceneToSVG} = require('../lib/render');
const {renderScene} = require('../lib/render');

function makeSketch(size, svgOpts, genFunc) {
const scene = createScene(size);

genFunc(scene, ops);

const svg = renderSceneToSVG(scene, svgOpts);
const {svg, uncombinedSegments} = renderScene(scene, svgOpts);
fs.writeFileSync('out.svg', svg);
fs.writeFileSync('out_uncombined_segments.json', JSON.stringify(uncombinedSegments));
}

module.exports = {
Expand Down

0 comments on commit 02a9a56

Please sign in to comment.