Skip to content

rtree indexing with React Hooks API #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"@babel/preset-react": "^7.0.0",
"@babel/runtime": "^7.1.2",
"@storybook/addon-centered": "^3.4.11",
"@storybook/addon-knobs": "^4.0.0-rc.6",
"@storybook/addon-info": "^4.0.0-rc.6",
"@storybook/addon-knobs": "^4.0.0-rc.6",
"@storybook/addon-notes": "^4.0.0-rc.6",
"@storybook/addon-options": "^4.0.0-rc.6",
"@storybook/addon-storysource": "^4.0.0-rc.6",
Expand Down Expand Up @@ -61,7 +61,7 @@
},
"dependencies": {
"classnames": "^2.2.5",
"memoize-one": "^4.0.2"
"flatbush": "^3.1.0"
},
"files": [
"lib/**/*"
Expand Down
122 changes: 122 additions & 0 deletions src/IndexedEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import React, { useMutationEffect, useState } from "react";
import PropTypes from "prop-types";
import Flatbush from "flatbush";
import Hex from "./models/Hex";
import HexEngine from "./HexEngine";
import HexUtils from "./HexUtils";
import Orientation from "./models/Orientation";
import Point from "./models/Point";

const IndexedEngine = ({
children,
flat,
map,
origin,
size,
spacing,
viewBox,
...props
}) => {
const orientation = flat ? Orientation.Flat : Orientation.Pointy;
const layout = { orientation, origin, size, spacing };
const [visible, updateVisible] = useState([]);
const [index, setIndex] = useState(() => new Flatbush(map.length));

useMutationEffect(
() => {
console.log("new index effect");
setIndex(() => new Flatbush(map.length));
},
[map.length, orientation, origin, spacing, size]
);
useMutationEffect(
() => {
console.log("populate index effect");
map.forEach(hex => {
const point = HexUtils.hexToPixel(hex, layout);
index.add(
point.x - layout.size.x / 2,
point.y - layout.size.y / 2 - 1,
point.x + layout.size.x / 2 + 1,
point.y + layout.size.y / 2 + 1
);
});
index.finish();
},
[index]
);
useMutationEffect(
() => {
console.log("update visible effect");
updateVisible(() =>
index
.search(
viewBox.x,
viewBox.y,
viewBox.x + viewBox.width,
viewBox.y + viewBox.height
)
.map(i => map[i])
);
console.log(visible.length);
},
[viewBox, index]
);

return (
<HexEngine {...layout} {...props}>
{children(visible)}
</HexEngine>
);
};

IndexedEngine.propTypes = {
children: PropTypes.func.isRequired,
/** Determines if the hexagons are oriented with a point or an edge facing up */
flat: PropTypes.bool,
map: PropTypes.arrayOf(PropTypes.instanceOf(Hex)).isRequired,
/** Origin of the grid */
origin: PropTypes.oneOfType([
PropTypes.instanceOf(Point),
PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired
})
]),
/** Size of the hexagons in each dimension */
size: PropTypes.oneOfType([
PropTypes.instanceOf(Point),
PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired
})
]),
/** Space between hexagons */
spacing: PropTypes.number,
/** The viewBox {x,y,width,height} of the svg view area */
viewBox: PropTypes.shape({
height: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired
})
};

IndexedEngine.defaultProps = {
classes: { grid: "", layout: "" },
flat: true,
height: 480,
origin: new Point(0, 0),
size: new Point(10, 10),
spacing: 1.0,
width: 640,
viewBox: {
height: 100,
width: 100,
x: -50,
y: -50
}
};

export const IndexedEngine_ = IndexedEngine;
export default IndexedEngine_;
42 changes: 41 additions & 1 deletion stories/HexEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import GridGenerator from "../src/GridGenerator";
import Hexagon, { Hexagon_ } from "../src/Hexagon";
import HexEngine from "../src/HexEngine";
import Point from "../src/models/Point";
import IndexedEngine from "../src/IndexedEngine";
import "./Hexagon.css";

Hexagon.displayName = "Hexagon";
Hexagon_.displayName = "Hexagon";
Expand Down Expand Up @@ -129,4 +131,42 @@ stories
<Hexagon q={0} r={0} s={0} />
<Hexagon q={1} r={0} s={-1} />
</HexEngine>
));
))
.add("indexed", () => {
const viewBox = object(
"viewBox",
{ x: -50, y: -50, width: 100, height: 100 },
"HexEngine"
);
const map = GridGenerator.hexagon(50);
const classes = {
hexagon: "showCoordinates text",
highlighted: "highlighted",
hovered: "hovered",
selected: "selected",
q: "axis",
r: "axis",
s: "axis",
text: "text"
};
return (
<IndexedEngine
height={400}
map={map}
viewBox={viewBox}
width={400}
spacing={1.05}
>
{hexes =>
hexes.map(hex => (
<Hexagon
{...hex}
classes={classes}
showCoordinates
selected={Math.random() > 0.5}
/>
))
}
</IndexedEngine>
);
});
27 changes: 27 additions & 0 deletions test/src/HexEngine.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from "react";
import renderer from "react-test-renderer";
import Hex from "../../src//models/Hex";
import Hexagon from "../../src/Hexagon";
import HexEngine from "../../src/HexEngine";
import IndexedEngine from "../../src/IndexedEngine";

describe("HexEngine", () => {
it("HexEngine should render correctly with default props", () => {
Expand All @@ -26,3 +28,28 @@ describe("HexEngine", () => {
expect(tree).toMatchSnapshot();
});
});

describe("IndexedEngine", () => {
it("IndexedEngine should render correctly with default props", () => {
const map = [
new Hex(-4, 0, 0),
new Hex(-3, 0, 0),
new Hex(-2, 0, 0),
new Hex(-1, 0, 0),
new Hex(0, 0, 0),
new Hex(1, 0, 0),
new Hex(2, 0, 0),
new Hex(3, 0, 0),
new Hex(4, 0, 0)
];

const tree = renderer
.create(
<IndexedEngine map={map} classes={{ layout: "test1" }}>
{hexes => hexes.map(hex => <Hexagon {...hex} />)}
</IndexedEngine>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
142 changes: 142 additions & 0 deletions test/src/__snapshots__/HexEngine.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,145 @@ exports[`HexEngine HexEngine should render correctly with pointy orientation 1`]
</g>
</svg>
`;

exports[`IndexedEngine IndexedEngine should render correctly with default props 1`] = `
<svg
className="grid"
height={480}
version="1.1"
viewBox="-50 -50 100 100"
width={640}
xmlns="http://www.w3.org/2000/svg"
>
<g
className="test1"
>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(-45, -25.98076211353316)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(-30, -17.32050807568877)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(-15, -8.660254037844386)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(0, 0)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(15, 8.660254037844386)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(30, 17.32050807568877)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
<g
className="hexagon-group"
draggable="true"
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseOver={[Function]}
transform="translate(45, 25.98076211353316)"
>
<g
className="hexagon"
>
<polygon
className=""
points="10,0 5.000000000000001,8.660254037844386 -4.999999999999998,8.660254037844387 -10,1.2246467991473533e-15 -5.000000000000004,-8.660254037844386 5.000000000000001,-8.660254037844386"
/>
</g>
</g>
</g>
</svg>
`;
Loading