cool
This commit is contained in:
57
src/App.svelte
Normal file
57
src/App.svelte
Normal file
@@ -0,0 +1,57 @@
|
||||
<script lang="ts">
|
||||
import Sidebar from "./Sidebar.svelte";
|
||||
import SolutionInteractor from "./SolutionInteractor.svelte";
|
||||
</script>
|
||||
|
||||
<style>
|
||||
main {
|
||||
display: flex;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
}
|
||||
.sidebarContainer {
|
||||
background-color: black;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 20%;
|
||||
height: 100%;
|
||||
display: inline-block;
|
||||
}
|
||||
.solutionBodyContainer {
|
||||
background-color: grey;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 80%;
|
||||
height: 100%;
|
||||
display: inline-block;
|
||||
}
|
||||
:global(body) {
|
||||
color: white;
|
||||
background: #333333;
|
||||
}
|
||||
@media(max-width: 1600px) {
|
||||
.solutionBodyContainer {
|
||||
width: 100%;
|
||||
}
|
||||
.sidebarContainer {
|
||||
width: 20%;
|
||||
}
|
||||
}
|
||||
@media(max-width: 1200px) {
|
||||
.solutionBodyContainer {
|
||||
width: 100%;
|
||||
}
|
||||
.sidebarContainer {
|
||||
width: 15em;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<main>
|
||||
<div class="sidebarContainer">
|
||||
<Sidebar />
|
||||
</div>
|
||||
<div class="solutionBodyContainer">
|
||||
<SolutionInteractor />
|
||||
</div>
|
||||
</main>
|
||||
121
src/CubeInput.svelte
Normal file
121
src/CubeInput.svelte
Normal file
@@ -0,0 +1,121 @@
|
||||
<script lang="ts">
|
||||
import {somaDimension, polycubes, selectedCube} from "./store";
|
||||
export let cubeNo: number;
|
||||
|
||||
$: dimension = $somaDimension;
|
||||
$: cube = $polycubes[cubeNo];
|
||||
$: cubeColor = cube.color;
|
||||
$: currentlyVisualised = $selectedCube === cubeNo;
|
||||
let cellStartDragInitialVal: boolean = false;
|
||||
let cellStartDrag: number = 0;
|
||||
let cellDragStartPos: {x: number, y: number} = {x: 0, y: 0};
|
||||
let cellEndDrag: number = 0;
|
||||
let cellDragEndPos: {x: number, y: number} = {x: 0, y: 0};
|
||||
|
||||
function cellNo(x: number, y: number, z: number) {
|
||||
return dimension ** 2 * x + dimension * y + z;
|
||||
}
|
||||
|
||||
function at(rep: bigint, x: number, y: number, z: number) {
|
||||
const mask = BigInt(1) << BigInt(cellNo(x, y, z));
|
||||
return (rep & mask) !== BigInt(0);
|
||||
}
|
||||
|
||||
function onMouseOverCell(event: MouseEvent, x: number, y: number, z: number) {
|
||||
if (event.buttons !== 0) {
|
||||
polycubes.set(cubeNo, event.buttons === 1, x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
function onMouseDownCell(event: MouseEvent, x: number, y: number, z: number) {
|
||||
cellStartDrag = cellNo(x, y, z);
|
||||
cellStartDragInitialVal = at(cube.rep, x, y, z);
|
||||
cellDragStartPos.x = event.clientX;
|
||||
cellDragStartPos.y = event.clientY;
|
||||
}
|
||||
|
||||
function onMouseUpCell(event: MouseEvent, x: number, y: number, z: number) {
|
||||
cellEndDrag = cellNo(x, y, z);
|
||||
cellDragEndPos.x = event.clientX;
|
||||
cellDragEndPos.y = event.clientY;
|
||||
if (cellStartDrag === cellEndDrag && dragDist() < 30) {
|
||||
let val;
|
||||
if (event.button === 0) {
|
||||
val = !cellStartDragInitialVal;
|
||||
} else if (event.button === 2) {
|
||||
val = false;
|
||||
}
|
||||
polycubes.set(cubeNo, val, x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
function dragDist() {
|
||||
return Math.sqrt((cellDragStartPos.x - cellDragEndPos.x) ** 2 + (cellDragStartPos.y - cellDragEndPos.y) ** 2);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="cube"
|
||||
class:active={currentlyVisualised}
|
||||
style="--color: {cubeColor}; --dimension: {dimension};"
|
||||
on:contextmenu|preventDefault
|
||||
on:mousedown={() => selectedCube.set(cubeNo)}
|
||||
>
|
||||
<h1>Cube: {cubeNo + 1}</h1>
|
||||
{#each {length: dimension} as _, x}
|
||||
<div class="layer">
|
||||
{#each {length: dimension} as _, y}
|
||||
<div class="row">
|
||||
{#each {length: dimension} as _, z}
|
||||
<div
|
||||
class="cell"
|
||||
class:filled={at(cube.rep, x, y, z)}
|
||||
on:mousemove={(event) => onMouseOverCell(event, x, y, z)}
|
||||
on:mousedown={(event) => onMouseDownCell(event, x, y, z)}
|
||||
on:mouseup={(event) => onMouseUpCell(event, x, y, z)}
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
* {
|
||||
--cell-size: 30px;
|
||||
}
|
||||
.active {
|
||||
border: 1px solid red;
|
||||
}
|
||||
h1 {
|
||||
font-size: 1em;
|
||||
text-align: center;
|
||||
}
|
||||
.cube {
|
||||
padding: 1em;
|
||||
user-select: none;
|
||||
}
|
||||
.cell {
|
||||
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5);
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
display: inline-block;
|
||||
border: 1px var(--color) solid;
|
||||
border-radius: 4px;
|
||||
height: var(--cell-size);
|
||||
width: var(--cell-size);
|
||||
background-color: #aaaaaa;
|
||||
margin: 1px;
|
||||
}
|
||||
.row {
|
||||
display: flex;
|
||||
margin: 0;
|
||||
}
|
||||
.layer {
|
||||
margin-top: 10px;
|
||||
}
|
||||
.filled {
|
||||
background: var(--color);
|
||||
}
|
||||
</style>
|
||||
795
src/OrbitControls.js
Normal file
795
src/OrbitControls.js
Normal file
@@ -0,0 +1,795 @@
|
||||
import {
|
||||
EventDispatcher,
|
||||
MOUSE,
|
||||
Quaternion,
|
||||
Spherical,
|
||||
TOUCH,
|
||||
Vector2,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
// This set of controls performs orbiting, dollying (zooming), and panning.
|
||||
// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
|
||||
//
|
||||
// Orbit - left mouse / touch: one-finger move
|
||||
// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
|
||||
// Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
|
||||
|
||||
const _changeEvent = { type: 'change' };
|
||||
const _startEvent = { type: 'start' };
|
||||
const _endEvent = { type: 'end' };
|
||||
|
||||
class OrbitControls extends EventDispatcher {
|
||||
constructor( object, domElement ) {
|
||||
super();
|
||||
if ( domElement === undefined ) console.warn( 'THREE.OrbitControls: The second parameter "domElement" is now mandatory.' );
|
||||
if ( domElement === document ) console.error( 'THREE.OrbitControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.' );
|
||||
this.object = object;
|
||||
this.domElement = domElement;
|
||||
|
||||
// Set to false to disable this control
|
||||
this.enabled = true;
|
||||
|
||||
// "target" sets the location of focus, where the object orbits around
|
||||
this.target = new Vector3();
|
||||
|
||||
// How far you can dolly in and out ( PerspectiveCamera only )
|
||||
this.minDistance = 0;
|
||||
this.maxDistance = Infinity;
|
||||
|
||||
// How far you can zoom in and out ( OrthographicCamera only )
|
||||
this.minZoom = 0;
|
||||
this.maxZoom = Infinity;
|
||||
|
||||
// How far you can orbit vertically, upper and lower limits.
|
||||
// Range is 0 to Math.PI radians.
|
||||
this.minPolarAngle = 0; // radians
|
||||
this.maxPolarAngle = Math.PI; // radians
|
||||
|
||||
// How far you can orbit horizontally, upper and lower limits.
|
||||
// If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
|
||||
this.minAzimuthAngle = - Infinity; // radians
|
||||
this.maxAzimuthAngle = Infinity; // radians
|
||||
|
||||
// Set to true to enable damping (inertia)
|
||||
// If damping is enabled, you must call controls.update() in your animation loop
|
||||
this.enableDamping = false;
|
||||
this.dampingFactor = 0.05;
|
||||
|
||||
// This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
|
||||
// Set to false to disable zooming
|
||||
this.enableZoom = true;
|
||||
this.zoomSpeed = 1.0;
|
||||
|
||||
// Set to false to disable rotating
|
||||
this.enableRotate = true;
|
||||
this.rotateSpeed = 1.0;
|
||||
|
||||
// Set to false to disable panning
|
||||
this.enablePan = true;
|
||||
this.panSpeed = 1.0;
|
||||
this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
|
||||
this.keyPanSpeed = 7.0; // pixels moved per arrow key push
|
||||
|
||||
// Set to true to automatically rotate around the target
|
||||
// If auto-rotate is enabled, you must call controls.update() in your animation loop
|
||||
this.autoRotate = false;
|
||||
this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
|
||||
|
||||
// The four arrow keys
|
||||
this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
|
||||
|
||||
// Mouse buttons
|
||||
this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
|
||||
|
||||
// Touch fingers
|
||||
this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
|
||||
|
||||
// for reset
|
||||
this.target0 = this.target.clone();
|
||||
this.position0 = this.object.position.clone();
|
||||
this.zoom0 = this.object.zoom;
|
||||
|
||||
// the target DOM element for key events
|
||||
this._domElementKeyEvents = null;
|
||||
|
||||
//
|
||||
// public methods
|
||||
//
|
||||
|
||||
this.getPolarAngle = function () {
|
||||
return spherical.phi;
|
||||
};
|
||||
|
||||
this.getAzimuthalAngle = function () {
|
||||
return spherical.theta;
|
||||
};
|
||||
|
||||
this.listenToKeyEvents = function ( domElement ) {
|
||||
domElement.addEventListener( 'keydown', onKeyDown );
|
||||
this._domElementKeyEvents = domElement;
|
||||
};
|
||||
|
||||
this.saveState = function () {
|
||||
scope.target0.copy( scope.target );
|
||||
scope.position0.copy( scope.object.position );
|
||||
scope.zoom0 = scope.object.zoom;
|
||||
};
|
||||
|
||||
this.reset = function () {
|
||||
scope.target.copy( scope.target0 );
|
||||
scope.object.position.copy( scope.position0 );
|
||||
scope.object.zoom = scope.zoom0;
|
||||
scope.object.updateProjectionMatrix();
|
||||
scope.dispatchEvent( _changeEvent );
|
||||
scope.update();
|
||||
state = STATE.NONE;
|
||||
};
|
||||
|
||||
// this method is exposed, but perhaps it would be better if we can make it private...
|
||||
this.update = function () {
|
||||
const offset = new Vector3();
|
||||
// so camera.up is the orbit axis
|
||||
const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
|
||||
const quatInverse = quat.clone().invert();
|
||||
const lastPosition = new Vector3();
|
||||
const lastQuaternion = new Quaternion();
|
||||
const twoPI = 2 * Math.PI;
|
||||
return function update() {
|
||||
const position = scope.object.position;
|
||||
offset.copy( position ).sub( scope.target );
|
||||
// rotate offset to "y-axis-is-up" space
|
||||
offset.applyQuaternion( quat );
|
||||
// angle from z-axis around y-axis
|
||||
spherical.setFromVector3( offset );
|
||||
if ( scope.autoRotate && state === STATE.NONE ) {
|
||||
rotateLeft( getAutoRotationAngle() );
|
||||
}
|
||||
if ( scope.enableDamping ) {
|
||||
spherical.theta += sphericalDelta.theta * scope.dampingFactor;
|
||||
spherical.phi += sphericalDelta.phi * scope.dampingFactor;
|
||||
} else {
|
||||
spherical.theta += sphericalDelta.theta;
|
||||
spherical.phi += sphericalDelta.phi;
|
||||
}
|
||||
|
||||
// restrict theta to be between desired limits
|
||||
let min = scope.minAzimuthAngle;
|
||||
let max = scope.maxAzimuthAngle;
|
||||
if ( isFinite( min ) && isFinite( max ) ) {
|
||||
if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
|
||||
if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
|
||||
if ( min <= max ) {
|
||||
spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
|
||||
} else {
|
||||
spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
|
||||
Math.max( min, spherical.theta ) :
|
||||
Math.min( max, spherical.theta );
|
||||
}
|
||||
}
|
||||
|
||||
// restrict phi to be between desired limits
|
||||
spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
|
||||
spherical.makeSafe();
|
||||
spherical.radius *= scale;
|
||||
|
||||
// restrict radius to be between desired limits
|
||||
spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
|
||||
|
||||
// move target to panned location
|
||||
if ( scope.enableDamping === true ) {
|
||||
scope.target.addScaledVector( panOffset, scope.dampingFactor );
|
||||
} else {
|
||||
scope.target.add( panOffset );
|
||||
}
|
||||
offset.setFromSpherical( spherical );
|
||||
// rotate offset back to "camera-up-vector-is-up" space
|
||||
offset.applyQuaternion( quatInverse );
|
||||
position.copy( scope.target ).add( offset );
|
||||
scope.object.lookAt( scope.target );
|
||||
if ( scope.enableDamping === true ) {
|
||||
sphericalDelta.theta *= ( 1 - scope.dampingFactor );
|
||||
sphericalDelta.phi *= ( 1 - scope.dampingFactor );
|
||||
panOffset.multiplyScalar( 1 - scope.dampingFactor );
|
||||
} else {
|
||||
sphericalDelta.set( 0, 0, 0 );
|
||||
panOffset.set( 0, 0, 0 );
|
||||
}
|
||||
scale = 1;
|
||||
|
||||
// update condition is:
|
||||
// min(camera displacement, camera rotation in radians)^2 > EPS
|
||||
// using small-angle approximation cos(x/2) = 1 - x^2 / 8
|
||||
|
||||
if ( zoomChanged ||
|
||||
lastPosition.distanceToSquared( scope.object.position ) > EPS ||
|
||||
8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
|
||||
scope.dispatchEvent( _changeEvent );
|
||||
lastPosition.copy( scope.object.position );
|
||||
lastQuaternion.copy( scope.object.quaternion );
|
||||
zoomChanged = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}();
|
||||
|
||||
this.dispose = function () {
|
||||
scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
|
||||
scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
|
||||
scope.domElement.removeEventListener( 'wheel', onMouseWheel );
|
||||
scope.domElement.removeEventListener( 'touchstart', onTouchStart );
|
||||
scope.domElement.removeEventListener( 'touchend', onTouchEnd );
|
||||
scope.domElement.removeEventListener( 'touchmove', onTouchMove );
|
||||
scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
|
||||
scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
|
||||
if ( scope._domElementKeyEvents !== null ) {
|
||||
scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
|
||||
}
|
||||
//scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
|
||||
};
|
||||
|
||||
//
|
||||
// internals
|
||||
//
|
||||
|
||||
const scope = this;
|
||||
|
||||
const STATE = {
|
||||
NONE: - 1,
|
||||
ROTATE: 0,
|
||||
DOLLY: 1,
|
||||
PAN: 2,
|
||||
TOUCH_ROTATE: 3,
|
||||
TOUCH_PAN: 4,
|
||||
TOUCH_DOLLY_PAN: 5,
|
||||
TOUCH_DOLLY_ROTATE: 6
|
||||
};
|
||||
|
||||
let state = STATE.NONE;
|
||||
|
||||
const EPS = 0.000001;
|
||||
|
||||
// current position in spherical coordinates
|
||||
const spherical = new Spherical();
|
||||
const sphericalDelta = new Spherical();
|
||||
|
||||
let scale = 1;
|
||||
const panOffset = new Vector3();
|
||||
let zoomChanged = false;
|
||||
|
||||
const rotateStart = new Vector2();
|
||||
const rotateEnd = new Vector2();
|
||||
const rotateDelta = new Vector2();
|
||||
|
||||
const panStart = new Vector2();
|
||||
const panEnd = new Vector2();
|
||||
const panDelta = new Vector2();
|
||||
|
||||
const dollyStart = new Vector2();
|
||||
const dollyEnd = new Vector2();
|
||||
const dollyDelta = new Vector2();
|
||||
|
||||
function getAutoRotationAngle() {
|
||||
return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
|
||||
}
|
||||
|
||||
function getZoomScale() {
|
||||
return Math.pow( 0.95, scope.zoomSpeed );
|
||||
}
|
||||
|
||||
function rotateLeft( angle ) {
|
||||
sphericalDelta.theta -= angle;
|
||||
}
|
||||
|
||||
function rotateUp( angle ) {
|
||||
sphericalDelta.phi -= angle;
|
||||
}
|
||||
|
||||
const panLeft = function () {
|
||||
const v = new Vector3();
|
||||
return function panLeft( distance, objectMatrix ) {
|
||||
v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
|
||||
v.multiplyScalar( - distance );
|
||||
panOffset.add( v );
|
||||
};
|
||||
}();
|
||||
|
||||
const panUp = function () {
|
||||
const v = new Vector3();
|
||||
return function panUp( distance, objectMatrix ) {
|
||||
if ( scope.screenSpacePanning === true ) {
|
||||
v.setFromMatrixColumn( objectMatrix, 1 );
|
||||
} else {
|
||||
v.setFromMatrixColumn( objectMatrix, 0 );
|
||||
v.crossVectors( scope.object.up, v );
|
||||
}
|
||||
v.multiplyScalar( distance );
|
||||
panOffset.add( v );
|
||||
};
|
||||
}();
|
||||
|
||||
// deltaX and deltaY are in pixels; right and down are positive
|
||||
const pan = function () {
|
||||
const offset = new Vector3();
|
||||
return function pan( deltaX, deltaY ) {
|
||||
const element = scope.domElement;
|
||||
if ( scope.object.isPerspectiveCamera ) {
|
||||
// perspective
|
||||
const position = scope.object.position;
|
||||
offset.copy( position ).sub( scope.target );
|
||||
let targetDistance = offset.length();
|
||||
// half of the fov is center to top of screen
|
||||
targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
|
||||
// we use only clientHeight here so aspect ratio does not distort speed
|
||||
panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
|
||||
panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
|
||||
} else if ( scope.object.isOrthographicCamera ) {
|
||||
// orthographic
|
||||
panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
|
||||
panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
|
||||
} else {
|
||||
// camera neither orthographic nor perspective
|
||||
console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
|
||||
scope.enablePan = false;
|
||||
}
|
||||
};
|
||||
}();
|
||||
|
||||
function dollyOut( dollyScale ) {
|
||||
if ( scope.object.isPerspectiveCamera ) {
|
||||
scale /= dollyScale;
|
||||
} else if ( scope.object.isOrthographicCamera ) {
|
||||
scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
|
||||
scope.object.updateProjectionMatrix();
|
||||
zoomChanged = true;
|
||||
} else {
|
||||
console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
||||
scope.enableZoom = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function dollyIn( dollyScale ) {
|
||||
if ( scope.object.isPerspectiveCamera ) {
|
||||
scale *= dollyScale;
|
||||
} else if ( scope.object.isOrthographicCamera ) {
|
||||
scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
|
||||
scope.object.updateProjectionMatrix();
|
||||
zoomChanged = true;
|
||||
} else {
|
||||
console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
||||
scope.enableZoom = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// event callbacks - update the object state
|
||||
//
|
||||
function handleMouseDownRotate( event ) {
|
||||
rotateStart.set( event.clientX, event.clientY );
|
||||
}
|
||||
|
||||
function handleMouseDownDolly( event ) {
|
||||
dollyStart.set( event.clientX, event.clientY );
|
||||
}
|
||||
|
||||
function handleMouseDownPan( event ) {
|
||||
panStart.set( event.clientX, event.clientY );
|
||||
}
|
||||
|
||||
function handleMouseMoveRotate( event ) {
|
||||
rotateEnd.set( event.clientX, event.clientY );
|
||||
rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
|
||||
const element = scope.domElement;
|
||||
rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
|
||||
rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
|
||||
rotateStart.copy( rotateEnd );
|
||||
scope.update();
|
||||
}
|
||||
|
||||
function handleMouseMoveDolly( event ) {
|
||||
dollyEnd.set( event.clientX, event.clientY );
|
||||
dollyDelta.subVectors( dollyEnd, dollyStart );
|
||||
if ( dollyDelta.y > 0 ) {
|
||||
dollyOut( getZoomScale() );
|
||||
} else if ( dollyDelta.y < 0 ) {
|
||||
dollyIn( getZoomScale() );
|
||||
}
|
||||
dollyStart.copy( dollyEnd );
|
||||
scope.update();
|
||||
}
|
||||
|
||||
function handleMouseMovePan( event ) {
|
||||
panEnd.set( event.clientX, event.clientY );
|
||||
panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
|
||||
pan( panDelta.x, panDelta.y );
|
||||
panStart.copy( panEnd );
|
||||
scope.update();
|
||||
}
|
||||
|
||||
function handleMouseUp( /*event*/ ) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
function handleMouseWheel( event ) {
|
||||
if ( event.deltaY < 0 ) {
|
||||
dollyIn( getZoomScale() );
|
||||
} else if ( event.deltaY > 0 ) {
|
||||
dollyOut( getZoomScale() );
|
||||
}
|
||||
scope.update();
|
||||
}
|
||||
|
||||
function handleKeyDown( event ) {
|
||||
let needsUpdate = false;
|
||||
switch ( event.code ) {
|
||||
case scope.keys.UP:
|
||||
pan( 0, scope.keyPanSpeed );
|
||||
needsUpdate = true;
|
||||
break;
|
||||
case scope.keys.BOTTOM:
|
||||
pan( 0, - scope.keyPanSpeed );
|
||||
needsUpdate = true;
|
||||
break;
|
||||
case scope.keys.LEFT:
|
||||
pan( scope.keyPanSpeed, 0 );
|
||||
needsUpdate = true;
|
||||
break;
|
||||
case scope.keys.RIGHT:
|
||||
pan( - scope.keyPanSpeed, 0 );
|
||||
needsUpdate = true;
|
||||
break;
|
||||
}
|
||||
if ( needsUpdate ) {
|
||||
// prevent the browser from scrolling on cursor keys
|
||||
event.preventDefault();
|
||||
scope.update();
|
||||
}
|
||||
}
|
||||
|
||||
function handleTouchStartRotate( event ) {
|
||||
if ( event.touches.length == 1 ) {
|
||||
rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
|
||||
} else {
|
||||
const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
|
||||
const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
|
||||
rotateStart.set( x, y );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function handleTouchStartPan( event ) {
|
||||
if ( event.touches.length == 1 ) {
|
||||
panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
|
||||
} else {
|
||||
const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
|
||||
const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
|
||||
panStart.set( x, y );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function handleTouchStartDolly( event ) {
|
||||
const dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
|
||||
const dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
|
||||
const distance = Math.sqrt( dx * dx + dy * dy );
|
||||
dollyStart.set( 0, distance );
|
||||
}
|
||||
|
||||
function handleTouchStartDollyPan( event ) {
|
||||
if ( scope.enableZoom ) handleTouchStartDolly( event );
|
||||
if ( scope.enablePan ) handleTouchStartPan( event );
|
||||
}
|
||||
|
||||
function handleTouchStartDollyRotate( event ) {
|
||||
if ( scope.enableZoom ) handleTouchStartDolly( event );
|
||||
if ( scope.enableRotate ) handleTouchStartRotate( event );
|
||||
}
|
||||
|
||||
function handleTouchMoveRotate( event ) {
|
||||
if ( event.touches.length == 1 ) {
|
||||
rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
|
||||
} else {
|
||||
const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
|
||||
const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
|
||||
rotateEnd.set( x, y );
|
||||
}
|
||||
rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
|
||||
const element = scope.domElement;
|
||||
rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
|
||||
rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
|
||||
rotateStart.copy( rotateEnd );
|
||||
}
|
||||
|
||||
function handleTouchMovePan( event ) {
|
||||
if ( event.touches.length == 1 ) {
|
||||
panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
|
||||
} else {
|
||||
const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
|
||||
const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
|
||||
panEnd.set( x, y );
|
||||
}
|
||||
panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
|
||||
pan( panDelta.x, panDelta.y );
|
||||
panStart.copy( panEnd );
|
||||
}
|
||||
|
||||
function handleTouchMoveDolly( event ) {
|
||||
const dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
|
||||
const dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
|
||||
const distance = Math.sqrt( dx * dx + dy * dy );
|
||||
dollyEnd.set( 0, distance );
|
||||
dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
|
||||
dollyOut( dollyDelta.y );
|
||||
dollyStart.copy( dollyEnd );
|
||||
}
|
||||
|
||||
function handleTouchMoveDollyPan( event ) {
|
||||
if ( scope.enableZoom ) handleTouchMoveDolly( event );
|
||||
if ( scope.enablePan ) handleTouchMovePan( event );
|
||||
}
|
||||
|
||||
function handleTouchMoveDollyRotate( event ) {
|
||||
if ( scope.enableZoom ) handleTouchMoveDolly( event );
|
||||
if ( scope.enableRotate ) handleTouchMoveRotate( event );
|
||||
}
|
||||
|
||||
function handleTouchEnd( /*event*/ ) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
//
|
||||
// event handlers - FSM: listen for events and reset state
|
||||
//
|
||||
|
||||
function onPointerDown( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
switch ( event.pointerType ) {
|
||||
case 'mouse':
|
||||
case 'pen':
|
||||
onMouseDown( event );
|
||||
break;
|
||||
// TODO touch
|
||||
}
|
||||
}
|
||||
|
||||
function onPointerMove( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
switch ( event.pointerType ) {
|
||||
case 'mouse':
|
||||
case 'pen':
|
||||
onMouseMove( event );
|
||||
break;
|
||||
// TODO touch
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function onPointerUp( event ) {
|
||||
switch ( event.pointerType ) {
|
||||
case 'mouse':
|
||||
case 'pen':
|
||||
onMouseUp( event );
|
||||
break;
|
||||
// TODO touch
|
||||
}
|
||||
}
|
||||
|
||||
function onMouseDown( event ) {
|
||||
// Prevent the browser from scrolling.
|
||||
event.preventDefault();
|
||||
// Manually set the focus since calling preventDefault above
|
||||
// prevents the browser from setting it automatically.
|
||||
scope.domElement.focus ? scope.domElement.focus() : window.focus();
|
||||
let mouseAction;
|
||||
switch ( event.button ) {
|
||||
case 0:
|
||||
mouseAction = scope.mouseButtons.LEFT;
|
||||
break;
|
||||
case 1:
|
||||
mouseAction = scope.mouseButtons.MIDDLE;
|
||||
break;
|
||||
case 2:
|
||||
mouseAction = scope.mouseButtons.RIGHT;
|
||||
break;
|
||||
default:
|
||||
mouseAction = - 1;
|
||||
}
|
||||
|
||||
switch ( mouseAction ) {
|
||||
case MOUSE.DOLLY:
|
||||
if ( scope.enableZoom === false ) return;
|
||||
handleMouseDownDolly( event );
|
||||
state = STATE.DOLLY;
|
||||
break;
|
||||
case MOUSE.ROTATE:
|
||||
if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
||||
if ( scope.enablePan === false ) return;
|
||||
handleMouseDownPan( event );
|
||||
state = STATE.PAN;
|
||||
} else {
|
||||
if ( scope.enableRotate === false ) return;
|
||||
handleMouseDownRotate( event );
|
||||
state = STATE.ROTATE;
|
||||
}
|
||||
break;
|
||||
case MOUSE.PAN:
|
||||
if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
||||
if ( scope.enableRotate === false ) return;
|
||||
handleMouseDownRotate( event );
|
||||
state = STATE.ROTATE;
|
||||
} else {
|
||||
if ( scope.enablePan === false ) return;
|
||||
handleMouseDownPan( event );
|
||||
state = STATE.PAN;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
state = STATE.NONE;
|
||||
}
|
||||
|
||||
if ( state !== STATE.NONE ) {
|
||||
scope.domElement.ownerDocument.addEventListener( 'pointermove', onPointerMove );
|
||||
scope.domElement.ownerDocument.addEventListener( 'pointerup', onPointerUp );
|
||||
scope.dispatchEvent( _startEvent );
|
||||
}
|
||||
}
|
||||
|
||||
function onMouseMove( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
event.preventDefault();
|
||||
switch ( state ) {
|
||||
case STATE.ROTATE:
|
||||
if ( scope.enableRotate === false ) return;
|
||||
handleMouseMoveRotate( event );
|
||||
break;
|
||||
case STATE.DOLLY:
|
||||
if ( scope.enableZoom === false ) return;
|
||||
handleMouseMoveDolly( event );
|
||||
break;
|
||||
case STATE.PAN:
|
||||
if ( scope.enablePan === false ) return;
|
||||
handleMouseMovePan( event );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function onMouseUp( event ) {
|
||||
scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
|
||||
scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
|
||||
if ( scope.enabled === false ) return;
|
||||
handleMouseUp( event );
|
||||
scope.dispatchEvent( _endEvent );
|
||||
state = STATE.NONE;
|
||||
}
|
||||
|
||||
function onMouseWheel( event ) {
|
||||
if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
|
||||
event.preventDefault();
|
||||
scope.dispatchEvent( _startEvent );
|
||||
handleMouseWheel( event );
|
||||
scope.dispatchEvent( _endEvent );
|
||||
}
|
||||
|
||||
function onKeyDown( event ) {
|
||||
if ( scope.enabled === false || scope.enablePan === false ) return;
|
||||
handleKeyDown( event );
|
||||
}
|
||||
|
||||
function onTouchStart( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
event.preventDefault(); // prevent scrolling
|
||||
switch ( event.touches.length ) {
|
||||
case 1:
|
||||
switch ( scope.touches.ONE ) {
|
||||
case TOUCH.ROTATE:
|
||||
if ( scope.enableRotate === false ) return;
|
||||
handleTouchStartRotate( event );
|
||||
state = STATE.TOUCH_ROTATE;
|
||||
break;
|
||||
case TOUCH.PAN:
|
||||
if ( scope.enablePan === false ) return;
|
||||
handleTouchStartPan( event );
|
||||
state = STATE.TOUCH_PAN;
|
||||
break;
|
||||
default:
|
||||
state = STATE.NONE;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
switch ( scope.touches.TWO ) {
|
||||
case TOUCH.DOLLY_PAN:
|
||||
if ( scope.enableZoom === false && scope.enablePan === false ) return;
|
||||
handleTouchStartDollyPan( event );
|
||||
state = STATE.TOUCH_DOLLY_PAN;
|
||||
break;
|
||||
case TOUCH.DOLLY_ROTATE:
|
||||
if ( scope.enableZoom === false && scope.enableRotate === false ) return;
|
||||
handleTouchStartDollyRotate( event );
|
||||
state = STATE.TOUCH_DOLLY_ROTATE;
|
||||
break;
|
||||
default:
|
||||
state = STATE.NONE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
state = STATE.NONE;
|
||||
}
|
||||
if ( state !== STATE.NONE ) {
|
||||
scope.dispatchEvent( _startEvent );
|
||||
}
|
||||
}
|
||||
|
||||
function onTouchMove( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
event.preventDefault(); // prevent scrolling
|
||||
switch ( state ) {
|
||||
case STATE.TOUCH_ROTATE:
|
||||
if ( scope.enableRotate === false ) return;
|
||||
handleTouchMoveRotate( event );
|
||||
scope.update();
|
||||
break;
|
||||
case STATE.TOUCH_PAN:
|
||||
if ( scope.enablePan === false ) return;
|
||||
handleTouchMovePan( event );
|
||||
scope.update();
|
||||
break;
|
||||
case STATE.TOUCH_DOLLY_PAN:
|
||||
if ( scope.enableZoom === false && scope.enablePan === false ) return;
|
||||
handleTouchMoveDollyPan( event );
|
||||
scope.update();
|
||||
break;
|
||||
case STATE.TOUCH_DOLLY_ROTATE:
|
||||
if ( scope.enableZoom === false && scope.enableRotate === false ) return;
|
||||
handleTouchMoveDollyRotate( event );
|
||||
scope.update();
|
||||
break;
|
||||
default:
|
||||
state = STATE.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
function onTouchEnd( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
handleTouchEnd( event );
|
||||
scope.dispatchEvent( _endEvent );
|
||||
state = STATE.NONE;
|
||||
}
|
||||
|
||||
function onContextMenu( event ) {
|
||||
if ( scope.enabled === false ) return;
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
scope.domElement.addEventListener( 'contextmenu', onContextMenu );
|
||||
scope.domElement.addEventListener( 'pointerdown', onPointerDown );
|
||||
scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
|
||||
scope.domElement.addEventListener( 'touchstart', onTouchStart, { passive: false } );
|
||||
scope.domElement.addEventListener( 'touchend', onTouchEnd );
|
||||
scope.domElement.addEventListener( 'touchmove', onTouchMove, { passive: false } );
|
||||
// force an update at start
|
||||
this.update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// This set of controls performs orbiting, dollying (zooming), and panning.
|
||||
// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
|
||||
// This is very similar to OrbitControls, another set of touch behavior
|
||||
//
|
||||
// Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
|
||||
// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
|
||||
// Pan - left mouse, or arrow keys / touch: one-finger move
|
||||
|
||||
class MapControls extends OrbitControls {
|
||||
constructor( object, domElement ) {
|
||||
super( object, domElement );
|
||||
this.mouseButtons.LEFT = MOUSE.ROTATE;
|
||||
this.mouseButtons.RIGHT = null;
|
||||
this.mouseButtons.MIDDLE = null;
|
||||
}
|
||||
}
|
||||
|
||||
export { OrbitControls, MapControls };
|
||||
@@ -1,84 +0,0 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var VoxelSpace_1 = __importDefault(require("./VoxelSpace"));
|
||||
var Polycube = /** @class */ (function (_super) {
|
||||
__extends(Polycube, _super);
|
||||
function Polycube(dims, vals, id) {
|
||||
var _this = _super.call(this, dims, vals.map(function (val) { return val ? id : 0; }), true) || this;
|
||||
_this.id = id;
|
||||
return _this;
|
||||
}
|
||||
Polycube.prototype.getId = function () {
|
||||
return this.id;
|
||||
};
|
||||
Polycube.prototype.print = function () {
|
||||
var accum = "";
|
||||
console.log("---");
|
||||
for (var i = 0; i < this.dims[0]; i++) {
|
||||
for (var j = 0; j < this.dims[1]; j++) {
|
||||
for (var k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k) === 0 ? "O" : "#";
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
};
|
||||
Polycube.prototype.matches = function (cube) {
|
||||
var otherDims = cube.getDims();
|
||||
for (var i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var otherVals = cube.getVals();
|
||||
for (var i = 0; i < this.vals.length; i++) {
|
||||
if (Number(this.vals[i]) !== Number(otherVals[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Polycube.prototype.size = function () {
|
||||
var size = 0;
|
||||
this.forEachCell(function (val) {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
};
|
||||
Polycube.prototype.rotated90 = function (dim) {
|
||||
var rotated = _super.prototype.rotated90.call(this, dim);
|
||||
return new Polycube(rotated.getDims(), rotated.getVals(), this.id);
|
||||
};
|
||||
Polycube.prototype.clone = function () {
|
||||
return new Polycube(this.getDims(), this.getVals(), this.id);
|
||||
};
|
||||
Polycube.prototype.getUniqueRotations = function () {
|
||||
var _this = this;
|
||||
return _super.prototype.getUniqueRotations.call(this).map(function (rot) { return new Polycube(rot.getDims(), rot.getVals(), _this.id); });
|
||||
};
|
||||
return Polycube;
|
||||
}(VoxelSpace_1.default));
|
||||
exports.default = Polycube;
|
||||
@@ -1,70 +0,0 @@
|
||||
import VoxelSpace, {DimensionDef} from "./VoxelSpace";
|
||||
|
||||
export default class Polycube extends VoxelSpace {
|
||||
private id: number;
|
||||
constructor(dims: DimensionDef, vals: boolean[], id: number) {
|
||||
super(dims, vals.map(val => val ? id : 0), true);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let i = 0; i < this.dims[0]; i++) {
|
||||
for (let j = 0; j < this.dims[1]; j++) {
|
||||
for (let k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k) === 0 ? "O" : "#";
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
matches(cube: VoxelSpace) {
|
||||
const otherDims = cube.getDims();
|
||||
for (let i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const otherVals = cube.getVals();
|
||||
for (let i = 0; i < this.vals.length; i++) {
|
||||
if (Number(this.vals[i]) !== Number(otherVals[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size() {
|
||||
let size = 0;
|
||||
this.forEachCell((val) => {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
}
|
||||
|
||||
rotated90(dim: "x" | "y" | "z"): Polycube {
|
||||
const rotated = super.rotated90(dim);
|
||||
return new Polycube(rotated.getDims(), rotated.getVals() as unknown as boolean[], this.id);
|
||||
}
|
||||
|
||||
clone(): Polycube {
|
||||
return new Polycube(this.getDims(), this.getVals() as unknown as boolean[], this.id);
|
||||
}
|
||||
|
||||
getUniqueRotations(): Polycube[] {
|
||||
return super.getUniqueRotations().map(rot => new Polycube(rot.getDims(), rot.getVals() as unknown as boolean[], this.id));
|
||||
}
|
||||
}
|
||||
22
src/Polycube3D.svelte
Normal file
22
src/Polycube3D.svelte
Normal file
@@ -0,0 +1,22 @@
|
||||
<script lang="ts">
|
||||
import PolycubeScene from "./threeTest.ts";
|
||||
import {onMount} from "svelte";
|
||||
import {polycubes, somaDimension, selectedCube} from "./store";
|
||||
import VoxelSpace from "./solver/VoxelSpace.ts";
|
||||
|
||||
$: cube = $polycubes[$selectedCube];
|
||||
let el: HTMLCanvasElement;
|
||||
let threeTest: TestScene;
|
||||
|
||||
onMount(() => {
|
||||
threeTest = new PolycubeScene(el);
|
||||
});
|
||||
|
||||
$: threeTest?.setPolycube(cube.rep, $somaDimension, cube.color);
|
||||
</script>
|
||||
|
||||
<canvas
|
||||
bind:this={el}
|
||||
width="640"
|
||||
height="480"
|
||||
></canvas>
|
||||
50
src/Sidebar.svelte
Normal file
50
src/Sidebar.svelte
Normal file
@@ -0,0 +1,50 @@
|
||||
<script lang="ts">
|
||||
import {isMaxDimension, isMinDimension, isMaxPolycubes, isMinPolycubes, somaDimension, polycubes} from "./store";
|
||||
|
||||
$: numCubes = $polycubes.length;
|
||||
|
||||
function solve() {
|
||||
console.log("SOLVING!");
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
<h1>Somaesque</h1>
|
||||
<h3>Settings</h3>
|
||||
|
||||
<div class="option">
|
||||
<p>Cube Dimension: {$somaDimension}</p>
|
||||
<button on:click={somaDimension.dec} disabled={$isMinDimension}>-</button>
|
||||
<button on:click={somaDimension.inc} disabled={$isMaxDimension}>+</button>
|
||||
</div>
|
||||
|
||||
<div class="option">
|
||||
<p>Cubes: {numCubes}</p>
|
||||
<button on:click={polycubes.removeCube} disabled={$isMinPolycubes}>-</button>
|
||||
<button on:click={polycubes.addCube} disabled={$isMaxPolycubes}>+</button>
|
||||
</div>
|
||||
|
||||
<div class="option">
|
||||
<button on:click={solve}>Solve</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
p {
|
||||
display: inline-block;
|
||||
}
|
||||
button {
|
||||
display: inline-block;
|
||||
}
|
||||
.container {
|
||||
height: 100%;
|
||||
background-color: #333333;
|
||||
padding: 1em;
|
||||
}
|
||||
h1 {
|
||||
margin: 0;
|
||||
color: #ff3e00;
|
||||
font-size: 3em;
|
||||
font-weight: 100;
|
||||
}
|
||||
</style>
|
||||
27
src/SolutionInteractor.svelte
Normal file
27
src/SolutionInteractor.svelte
Normal file
@@ -0,0 +1,27 @@
|
||||
<script lang="ts">
|
||||
import {polycubes} from "./store";
|
||||
import CubeInput from "./CubeInput.svelte";
|
||||
import Polycube3D from "./Polycube3D.svelte";
|
||||
$: numCubes = $polycubes.length;
|
||||
</script>
|
||||
|
||||
<div class="input-container">
|
||||
{#each {length: numCubes} as _, cubeNo}
|
||||
<div class="cube-input">
|
||||
<CubeInput
|
||||
cubeNo={cubeNo}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
<Polycube3D/>
|
||||
|
||||
<style>
|
||||
.cube-input {
|
||||
}
|
||||
.input-container {
|
||||
display: flex;
|
||||
justify-content: space-around;
|
||||
flex-flow: row wrap;
|
||||
}
|
||||
</style>
|
||||
@@ -1,58 +0,0 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var VoxelSpace_1 = __importDefault(require("./VoxelSpace"));
|
||||
var SomaSolver = /** @class */ (function () {
|
||||
function SomaSolver(dimension) {
|
||||
this.solutions = [];
|
||||
this.iterations = 0;
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace_1.default([dimension, dimension, dimension], Array(Math.pow(dimension, 3)).fill(0));
|
||||
}
|
||||
SomaSolver.prototype.solve = function (polycubes) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
var cumulativeSize = polycubes.reduce(function (prev, curr) { return prev + curr.size(); }, 0);
|
||||
if (cumulativeSize !== Math.pow(this.dim, 3)) {
|
||||
throw new Error("The polycubes passed do not add up to exactly enough units to form a cube of dimension " + this.dim + "! Got: " + cumulativeSize + ", need: " + Math.pow(this.dim, 3));
|
||||
}
|
||||
this.iterations = 0;
|
||||
this.backtrackSolve(this.solutionCube, polycubes);
|
||||
this.solutions = VoxelSpace_1.default.filterUnique(this.solutions);
|
||||
this.solutions.forEach(function (sol) { return sol.print(); });
|
||||
console.log(this.solutions.length);
|
||||
};
|
||||
SomaSolver.prototype.backtrackSolve = function (workingSolution, polycubes, depth) {
|
||||
if (depth === void 0) { depth = 0; }
|
||||
var nextCube = polycubes[0];
|
||||
var rots = depth === 0 ? [nextCube] : nextCube.getUniqueRotations();
|
||||
for (var i = 0; i < rots.length; i++) {
|
||||
var polyCubeDims = rots[i].getDims();
|
||||
for (var x = 0; x < this.dim - polyCubeDims[0] + 1; x++) {
|
||||
for (var y = 0; y < this.dim - polyCubeDims[1] + 1; y++) {
|
||||
for (var z = 0; z < this.dim - polyCubeDims[2] + 1; z++) {
|
||||
var successfulFusion = workingSolution.plus(rots[i], x, y, z);
|
||||
if (successfulFusion) {
|
||||
if (polycubes.length === 1) {
|
||||
console.log("soln", this.iterations++);
|
||||
this.solutions.push(successfulFusion);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
this.backtrackSolve(successfulFusion, polycubes.slice(1), depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return SomaSolver;
|
||||
}());
|
||||
exports.default = SomaSolver;
|
||||
@@ -1,55 +0,0 @@
|
||||
import Polycube from "./Polycube";
|
||||
import VoxelSpace from "./VoxelSpace";
|
||||
|
||||
export default class SomaSolver {
|
||||
private solutionCube: VoxelSpace;
|
||||
private dim: number;
|
||||
private solutions: VoxelSpace[] = [];
|
||||
private iterations: number = 0;
|
||||
constructor(dimension: number) {
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace([dimension, dimension, dimension], Array(dimension**3).fill(0));
|
||||
}
|
||||
|
||||
solve(polycubes: Polycube[]) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
let cumulativeSize = polycubes.reduce((prev, curr) => prev + curr.size(), 0);
|
||||
if (cumulativeSize !== this.dim**3) {
|
||||
throw new Error(`The polycubes passed do not add up to exactly enough units to form a cube of dimension ${this.dim}! Got: ${cumulativeSize}, need: ${this.dim**3}`);
|
||||
}
|
||||
this.iterations = 0;
|
||||
this.backtrackSolve(this.solutionCube, polycubes);
|
||||
this.solutions = VoxelSpace.filterUnique(this.solutions);
|
||||
this.solutions.forEach(sol => sol.print());
|
||||
console.log(this.solutions.length);
|
||||
}
|
||||
|
||||
private backtrackSolve(workingSolution: VoxelSpace, polycubes: Polycube[], depth = 0) {
|
||||
const nextCube = polycubes[0];
|
||||
const rots = depth === 0 ? [nextCube] : nextCube.getUniqueRotations();
|
||||
for (let i = 0; i < rots.length; i++) {
|
||||
const polyCubeDims = rots[i].getDims();
|
||||
for (let x = 0; x < this.dim - polyCubeDims[0] + 1; x++) {
|
||||
for (let y = 0; y < this.dim - polyCubeDims[1] + 1; y++) {
|
||||
for (let z = 0; z < this.dim - polyCubeDims[2] + 1; z++) {
|
||||
const successfulFusion = workingSolution.plus(rots[i], x, y, z);
|
||||
if (successfulFusion) {
|
||||
if (polycubes.length === 1) {
|
||||
console.log("soln", this.iterations++);
|
||||
this.solutions.push(successfulFusion);
|
||||
return;
|
||||
} else {
|
||||
this.backtrackSolve(successfulFusion, polycubes.slice(1), depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
"use strict";
|
||||
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
||||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
||||
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
||||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
||||
r[k] = a[j];
|
||||
return r;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var VoxelSpace = /** @class */ (function () {
|
||||
function VoxelSpace(dims, vals, cullEmpty) {
|
||||
if (vals.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
}
|
||||
this.dims = dims;
|
||||
this.vals = vals;
|
||||
if (cullEmpty) {
|
||||
this.cullEmptySpace();
|
||||
}
|
||||
}
|
||||
VoxelSpace.prototype.cullEmptySpace = function () {
|
||||
var extrema = {
|
||||
xMax: -Infinity,
|
||||
xMin: Infinity,
|
||||
yMax: -Infinity,
|
||||
yMin: Infinity,
|
||||
zMax: -Infinity,
|
||||
zMin: Infinity,
|
||||
};
|
||||
var newVals = [];
|
||||
this.forEachCell(function (val, i, j, k) {
|
||||
if (val !== 0) {
|
||||
extrema.xMax = Math.max(extrema.xMax, i);
|
||||
extrema.xMin = Math.min(extrema.xMin, i);
|
||||
extrema.yMax = Math.max(extrema.yMax, j);
|
||||
extrema.yMin = Math.min(extrema.yMin, j);
|
||||
extrema.zMax = Math.max(extrema.zMax, k);
|
||||
extrema.zMin = Math.min(extrema.zMin, k);
|
||||
}
|
||||
});
|
||||
for (var i = extrema.xMin; i <= extrema.xMax; i++) {
|
||||
for (var j = extrema.yMin; j <= extrema.yMax; j++) {
|
||||
for (var k = extrema.zMin; k <= extrema.zMax; k++) {
|
||||
newVals.push(this.at(i, j, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dims[0] = extrema.xMax - extrema.xMin + 1;
|
||||
this.dims[1] = extrema.yMax - extrema.yMin + 1;
|
||||
this.dims[2] = extrema.zMax - extrema.zMin + 1;
|
||||
this.vals = newVals;
|
||||
};
|
||||
VoxelSpace.prototype.forEachCell = function (cb) {
|
||||
loopStart: for (var x = 0; x < this.dims[0]; x++) {
|
||||
for (var y = 0; y < this.dims[1]; y++) {
|
||||
for (var z = 0; z < this.dims[2]; z++) {
|
||||
if (cb(this.at(x, y, z), x, y, z) === 0) {
|
||||
break loopStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
VoxelSpace.prototype.print = function () {
|
||||
var accum = "";
|
||||
console.log("---");
|
||||
for (var i = 0; i < this.dims[0]; i++) {
|
||||
for (var j = 0; j < this.dims[1]; j++) {
|
||||
for (var k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k);
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
};
|
||||
VoxelSpace.prototype.getUniqueRotations = function () {
|
||||
var rotations = [];
|
||||
var refSpace = this.clone();
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
};
|
||||
VoxelSpace.filterUnique = function (spaces) {
|
||||
if (spaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
var uniqueSpaces = [spaces[0]];
|
||||
for (var _i = 0, spaces_1 = spaces; _i < spaces_1.length; _i++) {
|
||||
var space = spaces_1[_i];
|
||||
var foundMatch = false;
|
||||
for (var _a = 0, _b = space.getUniqueRotations(); _a < _b.length; _a++) {
|
||||
var rotation = _b[_a];
|
||||
var end = uniqueSpaces.length;
|
||||
for (var i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSpaces[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSpaces.push(space);
|
||||
}
|
||||
}
|
||||
return uniqueSpaces;
|
||||
};
|
||||
VoxelSpace.pushNewUniqueSpaces = function (existingSpaces, newSpaces) {
|
||||
for (var _i = 0, newSpaces_1 = newSpaces; _i < newSpaces_1.length; _i++) {
|
||||
var newSpace = newSpaces_1[_i];
|
||||
var matchFound = false;
|
||||
for (var _a = 0, existingSpaces_1 = existingSpaces; _a < existingSpaces_1.length; _a++) {
|
||||
var existingSpace = existingSpaces_1[_a];
|
||||
if (newSpace.matches(existingSpace)) {
|
||||
matchFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matchFound) {
|
||||
existingSpaces.push(newSpace);
|
||||
}
|
||||
}
|
||||
};
|
||||
VoxelSpace.prototype.matches = function (space) {
|
||||
var otherDims = space.getDims();
|
||||
for (var i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var otherVals = space.getVals();
|
||||
for (var i = 0; i < this.vals.length; i++) {
|
||||
if (this.vals[i] !== otherVals[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
VoxelSpace.prototype.clone = function () {
|
||||
return new VoxelSpace(this.getDims(), this.getVals());
|
||||
};
|
||||
VoxelSpace.prototype.getAxisSpins = function (axis) {
|
||||
var rotations = [this.clone()];
|
||||
for (var i = 0; i < 3; i++) {
|
||||
rotations.push(rotations[i].rotated90(axis));
|
||||
}
|
||||
return rotations;
|
||||
};
|
||||
VoxelSpace.prototype.getDims = function () {
|
||||
return this.dims.slice();
|
||||
};
|
||||
VoxelSpace.prototype.getVals = function () {
|
||||
return this.vals.slice();
|
||||
};
|
||||
// [1, 0, 0] [x] [ x]
|
||||
// [0, 0, -1] * [y] = [-z]
|
||||
// [0, 1, 0] [z] [ y]
|
||||
VoxelSpace.prototype.newIndexRotX = function (x, y, z) {
|
||||
return this.dims[2] * this.dims[1] * x + this.dims[1] * (this.dims[2] - 1 - z) + y;
|
||||
};
|
||||
// [ 0, 0, 1] [x] [ z]
|
||||
// [ 0, 1, 0] * [y] = [ y]
|
||||
// [-1, 0, 0] [z] [-x]
|
||||
VoxelSpace.prototype.newIndexRotY = function (x, y, z) {
|
||||
return this.dims[1] * this.dims[0] * z + this.dims[0] * y + (this.dims[0] - 1 - x);
|
||||
};
|
||||
// [0, -1, 0] [x] [-y]
|
||||
// [1, 0, 0] * [y] = [ x]
|
||||
// [0, 0, 1] [z] [ z]
|
||||
VoxelSpace.prototype.newIndexRotZ = function (x, y, z) {
|
||||
return this.dims[0] * this.dims[2] * (this.dims[1] - 1 - y) + this.dims[2] * x + z;
|
||||
};
|
||||
VoxelSpace.prototype.at = function (x, y, z) {
|
||||
return this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z];
|
||||
};
|
||||
VoxelSpace.prototype.set = function (x, y, z, val) {
|
||||
this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z] = val;
|
||||
};
|
||||
VoxelSpace.prototype.rotated90 = function (dim) {
|
||||
var newVals = __spreadArrays(this.vals);
|
||||
var newDims;
|
||||
var rotIndex;
|
||||
if (dim === 'x') {
|
||||
newDims = [this.dims[0], this.dims[2], this.dims[1]];
|
||||
rotIndex = this.newIndexRotX.bind(this);
|
||||
}
|
||||
else if (dim === 'y') {
|
||||
newDims = [this.dims[2], this.dims[1], this.dims[0]];
|
||||
rotIndex = this.newIndexRotY.bind(this);
|
||||
}
|
||||
else {
|
||||
newDims = [this.dims[1], this.dims[0], this.dims[2]];
|
||||
rotIndex = this.newIndexRotZ.bind(this);
|
||||
}
|
||||
this.forEachCell(function (val, i, j, k) {
|
||||
newVals[rotIndex(i, j, k)] = val;
|
||||
});
|
||||
return new VoxelSpace(newDims, newVals);
|
||||
};
|
||||
VoxelSpace.prototype.rot90 = function (dim) {
|
||||
var rot = this.rotated90(dim);
|
||||
this.vals = rot.getVals();
|
||||
this.dims = rot.getDims();
|
||||
};
|
||||
VoxelSpace.prototype.plus = function (space, startX, startY, startZ) {
|
||||
var result = this.clone();
|
||||
var spaceDims = space.getDims();
|
||||
for (var i = 0; i < spaceDims[0]; i++) {
|
||||
for (var j = 0; j < spaceDims[1]; j++) {
|
||||
for (var k = 0; k < spaceDims[2]; k++) {
|
||||
var sourceVal = space.at(i, j, k);
|
||||
var targetEmpty = result.at(startX + i, startY + j, startZ + k) === 0;
|
||||
if (sourceVal !== 0 && targetEmpty) {
|
||||
result.set(startX + i, startY + j, startZ + k, sourceVal);
|
||||
}
|
||||
else if (sourceVal !== 0 && !targetEmpty) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return VoxelSpace;
|
||||
}());
|
||||
exports.default = VoxelSpace;
|
||||
139
src/main.js
139
src/main.js
@@ -1,139 +0,0 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Polycube_1 = __importDefault(require("./Polycube"));
|
||||
var SomaSolver_1 = __importDefault(require("./SomaSolver"));
|
||||
// const testCube = new Cube([4, 2, 5], [
|
||||
// "000", "001", "002", "003", "004",
|
||||
// "010", "011", "012", "013", "014",
|
||||
// "100", "101", "102", "103", "104",
|
||||
// "110", "111", "112", "113", "114",
|
||||
// "200", "201", "202", "203", "204",
|
||||
// "210", "211", "212", "213", "214",
|
||||
// "300", "301", "302", "303", "304",
|
||||
// "310", "311", "312", "313", "314",
|
||||
// ]);
|
||||
// const somaCube = new Polycube([3, 3, 3], [
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
//
|
||||
// true, true, true,
|
||||
// true, true, true,
|
||||
// false, false, false,
|
||||
//
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// ], 1);
|
||||
var unitCube1 = new Polycube_1.default([1, 1, 1], [true], 1);
|
||||
var unitCube2 = new Polycube_1.default([1, 1, 1], [true], 2);
|
||||
var unitCube3 = new Polycube_1.default([1, 1, 1], [true], 3);
|
||||
var unitCube4 = new Polycube_1.default([1, 1, 1], [true], 4);
|
||||
var unitCube5 = new Polycube_1.default([1, 1, 1], [true], 5);
|
||||
var unitCube6 = new Polycube_1.default([1, 1, 1], [true], 6);
|
||||
var unitCube7 = new Polycube_1.default([1, 1, 1], [true], 7);
|
||||
var unitCube8 = new Polycube_1.default([1, 1, 1], [true], 8);
|
||||
var unitCube9 = new Polycube_1.default([1, 1, 1], [true], 9);
|
||||
var unitCube10 = new Polycube_1.default([1, 1, 1], [true], 10);
|
||||
var unitCube11 = new Polycube_1.default([1, 1, 1], [true], 11);
|
||||
var unitCube12 = new Polycube_1.default([1, 1, 1], [true], 12);
|
||||
var unitCube13 = new Polycube_1.default([1, 1, 1], [true], 13);
|
||||
var unitCube14 = new Polycube_1.default([1, 1, 1], [true], 14);
|
||||
var unitCube15 = new Polycube_1.default([1, 1, 1], [true], 15);
|
||||
var unitCube16 = new Polycube_1.default([1, 1, 1], [true], 16);
|
||||
var unitCube17 = new Polycube_1.default([1, 1, 1], [true], 17);
|
||||
var unitCube18 = new Polycube_1.default([1, 1, 1], [true], 18);
|
||||
var unitCube19 = new Polycube_1.default([1, 1, 1], [true], 19);
|
||||
var unitCube20 = new Polycube_1.default([1, 1, 1], [true], 20);
|
||||
var unitCube21 = new Polycube_1.default([1, 1, 1], [true], 21);
|
||||
var unitCube22 = new Polycube_1.default([1, 1, 1], [true], 22);
|
||||
var unitCube23 = new Polycube_1.default([1, 1, 1], [true], 23);
|
||||
var unitCube24 = new Polycube_1.default([1, 1, 1], [true], 24);
|
||||
var unitCube25 = new Polycube_1.default([1, 1, 1], [true], 25);
|
||||
var unitCube26 = new Polycube_1.default([1, 1, 1], [true], 26);
|
||||
var unitCube27 = new Polycube_1.default([1, 1, 1], [true], 27);
|
||||
var tetromino1 = new Polycube_1.default([3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 1);
|
||||
var tetromino2 = new Polycube_1.default([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 2);
|
||||
var tetromino3 = new Polycube_1.default([3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 3);
|
||||
var tetromino4 = new Polycube_1.default([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 4);
|
||||
var tetromino5 = new Polycube_1.default([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 5);
|
||||
var tetromino6 = new Polycube_1.default([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 6);
|
||||
var triomino1 = new Polycube_1.default([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 7);
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
var solver = new SomaSolver_1.default(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
164
src/main.ts
164
src/main.ts
@@ -1,158 +1,10 @@
|
||||
import Polycube from "./Polycube";
|
||||
import SomaSolver from "./SomaSolver";
|
||||
import App from './App.svelte';
|
||||
|
||||
// const testCube = new Cube([4, 2, 5], [
|
||||
// "000", "001", "002", "003", "004",
|
||||
// "010", "011", "012", "013", "014",
|
||||
// "100", "101", "102", "103", "104",
|
||||
// "110", "111", "112", "113", "114",
|
||||
// "200", "201", "202", "203", "204",
|
||||
// "210", "211", "212", "213", "214",
|
||||
// "300", "301", "302", "303", "304",
|
||||
// "310", "311", "312", "313", "314",
|
||||
// ]);
|
||||
// const somaCube = new Polycube([3, 3, 3], [
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
//
|
||||
// true, true, true,
|
||||
// true, true, true,
|
||||
// false, false, false,
|
||||
//
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// ], 1);
|
||||
const unitCube1 = new Polycube([1, 1, 1], [true], 1);
|
||||
const unitCube2 = new Polycube([1, 1, 1], [true], 2);
|
||||
const unitCube3 = new Polycube([1, 1, 1], [true], 3);
|
||||
const unitCube4 = new Polycube([1, 1, 1], [true], 4);
|
||||
const unitCube5 = new Polycube([1, 1, 1], [true], 5);
|
||||
const unitCube6 = new Polycube([1, 1, 1], [true], 6);
|
||||
const unitCube7 = new Polycube([1, 1, 1], [true], 7);
|
||||
const unitCube8 = new Polycube([1, 1, 1], [true], 8);
|
||||
const unitCube9 = new Polycube([1, 1, 1], [true], 9);
|
||||
const unitCube10 = new Polycube([1, 1, 1], [true], 10);
|
||||
const unitCube11 = new Polycube([1, 1, 1], [true], 11);
|
||||
const unitCube12 = new Polycube([1, 1, 1], [true], 12);
|
||||
const unitCube13 = new Polycube([1, 1, 1], [true], 13);
|
||||
const unitCube14 = new Polycube([1, 1, 1], [true], 14);
|
||||
const unitCube15 = new Polycube([1, 1, 1], [true], 15);
|
||||
const unitCube16 = new Polycube([1, 1, 1], [true], 16);
|
||||
const unitCube17 = new Polycube([1, 1, 1], [true], 17);
|
||||
const unitCube18 = new Polycube([1, 1, 1], [true], 18);
|
||||
const unitCube19 = new Polycube([1, 1, 1], [true], 19);
|
||||
const unitCube20 = new Polycube([1, 1, 1], [true], 20);
|
||||
const unitCube21 = new Polycube([1, 1, 1], [true], 21);
|
||||
const unitCube22 = new Polycube([1, 1, 1], [true], 22);
|
||||
const unitCube23 = new Polycube([1, 1, 1], [true], 23);
|
||||
const unitCube24 = new Polycube([1, 1, 1], [true], 24);
|
||||
const unitCube25 = new Polycube([1, 1, 1], [true], 25);
|
||||
const unitCube26 = new Polycube([1, 1, 1], [true], 26);
|
||||
const unitCube27 = new Polycube([1, 1, 1], [true], 27);
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
props: {
|
||||
name: 'world'
|
||||
}
|
||||
});
|
||||
|
||||
const tetromino1 = new Polycube([3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 1);
|
||||
|
||||
const tetromino2 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 2);
|
||||
|
||||
const tetromino3 = new Polycube([3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 3);
|
||||
|
||||
const tetromino4 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 4);
|
||||
|
||||
const tetromino5 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 5);
|
||||
|
||||
const tetromino6 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 6);
|
||||
|
||||
const triomino1 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 7);
|
||||
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
|
||||
const solver = new SomaSolver(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
export default app;
|
||||
99
src/solver/SomaSolution.ts
Normal file
99
src/solver/SomaSolution.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type VoxelSpace from "./VoxelSpace";
|
||||
|
||||
export default class SomaSolution {
|
||||
private solutionSpaces: VoxelSpace[];
|
||||
private dim: number;
|
||||
constructor(dim: number) {
|
||||
if (dim < 0 || dim % 1 !== 0) {
|
||||
throw new Error("Dimension must be a whole positive integer!");
|
||||
}
|
||||
this.dim = dim;
|
||||
this.solutionSpaces = [];
|
||||
}
|
||||
|
||||
static filterUnique(solutions: SomaSolution[]): SomaSolution[] {
|
||||
if (solutions.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const uniqueSolns = [solutions[0]];
|
||||
for (const solution of solutions) {
|
||||
let foundMatch = false;
|
||||
for (const rotation of solution.getUniqueRotations()) {
|
||||
let end = uniqueSolns.length;
|
||||
for (let i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSolns[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSolns.push(solution);
|
||||
}
|
||||
}
|
||||
return uniqueSolns;
|
||||
}
|
||||
|
||||
getUniqueRotations(): SomaSolution[] {
|
||||
if (this.solutionSpaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const result: SomaSolution[] = [];
|
||||
const allRots = this.solutionSpaces.map(space => space.getAllRotations());
|
||||
for (let i = 0; i < allRots[0].length; i++) {
|
||||
const solnRot = new SomaSolution(this.dim);
|
||||
allRots.forEach(rotGroup => solnRot.addSpace(rotGroup[i]));
|
||||
result.push(solnRot);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
matches(solution: SomaSolution) {
|
||||
for (let i = 0; i < this.solutionSpaces.length; i++) {
|
||||
if (!this.solutionSpaces[i].matches(solution.solutionSpaces[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
addSpace(space: VoxelSpace) {
|
||||
this.solutionSpaces.push(space);
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let x = 0; x < this.dim; x++) {
|
||||
for (let y = 0; y < this.dim; y++) {
|
||||
for (let z = 0; z < this.dim; z++) {
|
||||
for (const space of this.solutionSpaces) {
|
||||
if (space.at(x, y, z)) {
|
||||
accum += space.getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (x !== this.dim - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
at(x: number, y: number, z: number) {
|
||||
for (const space of this.solutionSpaces) {
|
||||
if (space.at(x, y, z)) {
|
||||
return space.getId();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
clone() {
|
||||
const clone = new SomaSolution(this.dim);
|
||||
clone.solutionSpaces = this.solutionSpaces.slice();
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
49
src/solver/SomaSolver.ts
Normal file
49
src/solver/SomaSolver.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import VoxelSpace from "./VoxelSpace";
|
||||
import SomaSolution from "./SomaSolution";
|
||||
|
||||
export default class SomaSolver {
|
||||
private solutionCube: VoxelSpace;
|
||||
private dim: number;
|
||||
private solutions: SomaSolution[] = [];
|
||||
private iterations: number = 0;
|
||||
constructor(dimension: number) {
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace(0, [dimension, dimension, dimension], Array(dimension**3).fill(0));
|
||||
}
|
||||
|
||||
solve(polycubes: VoxelSpace[]) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
let cumulativeSize = polycubes.reduce((prev, curr) => prev + curr.size(), 0);
|
||||
if (cumulativeSize !== this.dim**3) {
|
||||
throw new Error(`The polycubes passed do not add up to exactly enough units to form a cube of dimension ${this.dim}! Got: ${cumulativeSize}, need: ${this.dim**3}`);
|
||||
}
|
||||
const combosWithRots = polycubes.slice(1).map(polycube => polycube.getUniqueRotations().map(rot => rot.getAllPositionsInCube(this.dim)).flat());
|
||||
const combos = [polycubes[0].getAllPositionsInCube(this.dim), ...combosWithRots];
|
||||
this.backtrackSolve(this.solutionCube, combos, new SomaSolution(this.dim));
|
||||
this.solutions = SomaSolution.filterUnique(this.solutions);
|
||||
this.solutions.forEach(sol => sol.print());
|
||||
}
|
||||
|
||||
private backtrackSolve(workingSolution: VoxelSpace, polycubes: VoxelSpace[][], currentSoln: SomaSolution, depth = 0) {
|
||||
const nextCubeGroup = polycubes[0];
|
||||
for (let i = 0; i < nextCubeGroup.length; i++) {
|
||||
const fusionAttempt = workingSolution.plus(nextCubeGroup[i]);
|
||||
if (fusionAttempt) {
|
||||
const nextSoln = currentSoln.clone();
|
||||
nextSoln.addSpace(nextCubeGroup[i]);
|
||||
if (polycubes.length === 1) {
|
||||
this.solutions.push(nextSoln);
|
||||
currentSoln = new SomaSolution(this.dim);
|
||||
return;
|
||||
} else {
|
||||
this.backtrackSolve(fusionAttempt, polycubes.slice(1), nextSoln, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,42 @@
|
||||
import Polycube from "./Polycube";
|
||||
|
||||
export type DimensionDef = [number, number, number];
|
||||
|
||||
export default class VoxelSpace {
|
||||
protected vals: number[];
|
||||
protected dims: DimensionDef;
|
||||
constructor(dims: DimensionDef, vals: number[], cullEmpty?: boolean) {
|
||||
if (vals.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
private dims: DimensionDef;
|
||||
private length: number;
|
||||
private space: bigint;
|
||||
private id: number;
|
||||
constructor(id: number, dims: DimensionDef, space?: boolean[] | bigint, cullEmpty?: boolean) {
|
||||
if (!space) {
|
||||
space = 0n;
|
||||
} else if (Array.isArray(space)) {
|
||||
if (space.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
}
|
||||
space = VoxelSpace.boolArrayToBigInt(space)
|
||||
}
|
||||
this.id = id;
|
||||
this.length = dims[0] * dims[1] * dims[2];
|
||||
this.dims = dims;
|
||||
this.vals = vals;
|
||||
this.space = space;
|
||||
if (cullEmpty) {
|
||||
this.cullEmptySpace();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolArrayToBigInt(boolArray: boolean[]): bigint {
|
||||
let result = 0n;
|
||||
for (let i = 0; i < boolArray.length; i++) {
|
||||
if (boolArray[i]) {
|
||||
result |= BigInt(1 << i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
binaryRep() {
|
||||
return this.space.toString(2);
|
||||
}
|
||||
|
||||
private cullEmptySpace() {
|
||||
const extrema = {
|
||||
xMax: -Infinity,
|
||||
@@ -25,31 +46,35 @@ export default class VoxelSpace {
|
||||
zMax: -Infinity,
|
||||
zMin: Infinity,
|
||||
};
|
||||
const newVals: number[] = [];
|
||||
this.forEachCell((val, i, j, k) => {
|
||||
if (val !== 0) {
|
||||
extrema.xMax = Math.max(extrema.xMax, i);
|
||||
extrema.xMin = Math.min(extrema.xMin, i);
|
||||
extrema.yMax = Math.max(extrema.yMax, j);
|
||||
extrema.yMin = Math.min(extrema.yMin, j);
|
||||
extrema.zMax = Math.max(extrema.zMax, k);
|
||||
extrema.zMin = Math.min(extrema.zMin, k);
|
||||
let newSpace = 0n;
|
||||
this.forEachCell((val, x, y, z) => {
|
||||
if (val) {
|
||||
extrema.xMax = Math.max(extrema.xMax, x);
|
||||
extrema.xMin = Math.min(extrema.xMin, x);
|
||||
extrema.yMax = Math.max(extrema.yMax, y);
|
||||
extrema.yMin = Math.min(extrema.yMin, y);
|
||||
extrema.zMax = Math.max(extrema.zMax, z);
|
||||
extrema.zMin = Math.min(extrema.zMin, z);
|
||||
}
|
||||
});
|
||||
for (let i = extrema.xMin; i <= extrema.xMax; i++) {
|
||||
for (let j = extrema.yMin; j <= extrema.yMax; j++) {
|
||||
for (let k = extrema.zMin; k <= extrema.zMax; k++) {
|
||||
newVals.push(this.at(i, j, k));
|
||||
let index = 0n;
|
||||
for (let x = extrema.xMin; x <= extrema.xMax; x++) {
|
||||
for (let y = extrema.yMin; y <= extrema.yMax; y++) {
|
||||
for (let z = extrema.zMin; z <= extrema.zMax; z++) {
|
||||
if (this.at(x, y, z)) {
|
||||
newSpace |= 1n << index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dims[0] = extrema.xMax - extrema.xMin + 1;
|
||||
this.dims[1] = extrema.yMax - extrema.yMin + 1;
|
||||
this.dims[2] = extrema.zMax - extrema.zMin + 1;
|
||||
this.vals = newVals;
|
||||
this.space = newSpace;
|
||||
}
|
||||
|
||||
forEachCell(cb: (val: number, x: number, y: number, z: number) => any) {
|
||||
forEachCell(cb: (val: boolean, x: number, y: number, z: number) => any) {
|
||||
loopStart: for (let x = 0; x < this.dims[0]; x++) {
|
||||
for (let y = 0; y < this.dims[1]; y++) {
|
||||
for (let z = 0; z < this.dims[2]; z++) {
|
||||
@@ -61,13 +86,17 @@ export default class VoxelSpace {
|
||||
}
|
||||
}
|
||||
|
||||
getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let i = 0; i < this.dims[0]; i++) {
|
||||
for (let j = 0; j < this.dims[1]; j++) {
|
||||
for (let k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k);
|
||||
accum += this.at(i, j, k) ? '#' : 'O';
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
@@ -97,26 +126,22 @@ export default class VoxelSpace {
|
||||
return rotations;
|
||||
}
|
||||
|
||||
static filterUnique<T extends Polycube | VoxelSpace>(spaces: T[]): T[] {
|
||||
if (spaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const uniqueSpaces = [spaces[0]];
|
||||
for (const space of spaces) {
|
||||
let foundMatch = false;
|
||||
for (const rotation of space.getUniqueRotations()) {
|
||||
let end = uniqueSpaces.length;
|
||||
for (let i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSpaces[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSpaces.push(space);
|
||||
}
|
||||
}
|
||||
return uniqueSpaces;
|
||||
getAllRotations() {
|
||||
const rotations: VoxelSpace[] = [];
|
||||
const refSpace = this.clone();
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
}
|
||||
|
||||
protected static pushNewUniqueSpaces(existingSpaces: VoxelSpace[], newSpaces: VoxelSpace[]) {
|
||||
@@ -134,6 +159,26 @@ export default class VoxelSpace {
|
||||
}
|
||||
}
|
||||
|
||||
getAllPositionsInCube(cubeDim: number): VoxelSpace[] {
|
||||
if ((cubeDim > 0) && (cubeDim % 1 === 0)) {
|
||||
const cubePositions: VoxelSpace[] = [];
|
||||
for (let x = 0; x < cubeDim - this.dims[0] + 1; x++) {
|
||||
for (let y = 0; y < cubeDim - this.dims[1] + 1; y++) {
|
||||
for (let z = 0; z < cubeDim - this.dims[2] + 1; z++) {
|
||||
const cubePos = new VoxelSpace(this.id, [cubeDim, cubeDim, cubeDim]);
|
||||
this.forEachCell((val, rotX, rotY, rotZ) => {
|
||||
cubePos.set(x + rotX, y + rotY, z + rotZ, val);
|
||||
});
|
||||
cubePositions.push(cubePos);
|
||||
}
|
||||
}
|
||||
}
|
||||
return cubePositions;
|
||||
} else {
|
||||
throw new Error("cubeDim must be a positive integer.");
|
||||
}
|
||||
}
|
||||
|
||||
matches(space: VoxelSpace) {
|
||||
const otherDims = space.getDims();
|
||||
for (let i = 0; i < this.dims.length; i++) {
|
||||
@@ -141,17 +186,11 @@ export default class VoxelSpace {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const otherVals = space.getVals();
|
||||
for (let i = 0; i < this.vals.length; i++) {
|
||||
if (this.vals[i] !== otherVals[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return this.space === space.getRaw();
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new VoxelSpace(this.getDims(), this.getVals());
|
||||
return new VoxelSpace(this.id, this.getDims(), this.getRaw());
|
||||
}
|
||||
|
||||
private getAxisSpins(axis: 'x' | 'y' | 'z'): VoxelSpace[] {
|
||||
@@ -166,8 +205,8 @@ export default class VoxelSpace {
|
||||
return this.dims.slice() as DimensionDef;
|
||||
}
|
||||
|
||||
getVals() {
|
||||
return this.vals.slice();
|
||||
getRaw() {
|
||||
return this.space;
|
||||
}
|
||||
|
||||
// [1, 0, 0] [x] [ x]
|
||||
@@ -192,15 +231,26 @@ export default class VoxelSpace {
|
||||
}
|
||||
|
||||
at(x: number, y: number, z: number) {
|
||||
return this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z];
|
||||
const mask = 1n << BigInt(this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
return (this.space & mask) !== 0n;
|
||||
}
|
||||
|
||||
set(x: number, y: number, z: number, val: number) {
|
||||
this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z] = val;
|
||||
toggle(x: number, y: number, z: number) {
|
||||
const mask = BigInt(1 << this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
this.space ^= mask;
|
||||
}
|
||||
|
||||
set(x: number, y: number, z: number, val: boolean) {
|
||||
const mask = BigInt(1 << this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
if (val) {
|
||||
this.space |= mask;
|
||||
} else {
|
||||
this.space &= ~mask;
|
||||
}
|
||||
}
|
||||
|
||||
rotated90(dim: 'x' | 'y' | 'z') {
|
||||
const newVals = [...this.vals];
|
||||
let newSpace = 0n;
|
||||
let newDims: DimensionDef;
|
||||
let rotIndex: (i: number, j: number, k: number) => number;
|
||||
if (dim === 'x') {
|
||||
@@ -214,33 +264,34 @@ export default class VoxelSpace {
|
||||
rotIndex = this.newIndexRotZ.bind(this);
|
||||
}
|
||||
this.forEachCell((val, i, j, k) => {
|
||||
newVals[rotIndex(i, j, k)] = val;
|
||||
if (val) {
|
||||
newSpace |= BigInt(1 << rotIndex(i, j, k));
|
||||
}
|
||||
})
|
||||
return new VoxelSpace(newDims, newVals);
|
||||
return new VoxelSpace(this.id, newDims, newSpace);
|
||||
}
|
||||
|
||||
rot90(dim: 'x' | 'y' | 'z') {
|
||||
const rot = this.rotated90(dim);
|
||||
this.vals = rot.getVals();
|
||||
this.space = rot.getRaw();
|
||||
this.dims = rot.getDims();
|
||||
}
|
||||
|
||||
plus(space: VoxelSpace, startX: number, startY: number, startZ: number): VoxelSpace | null {
|
||||
let result: VoxelSpace = this.clone();
|
||||
const spaceDims = space.getDims();
|
||||
for (let i = 0; i < spaceDims[0]; i++) {
|
||||
for (let j = 0; j < spaceDims[1]; j++) {
|
||||
for (let k = 0; k < spaceDims[2]; k++) {
|
||||
const sourceVal = space.at(i, j, k);
|
||||
const targetEmpty = result.at(startX + i, startY + j, startZ + k) === 0;
|
||||
if (sourceVal !== 0 && targetEmpty) {
|
||||
result.set(startX + i, startY + j, startZ + k, sourceVal);
|
||||
} else if (sourceVal !== 0 && !targetEmpty) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
plus(space: VoxelSpace): VoxelSpace | null {
|
||||
const otherSpace = space.getRaw();
|
||||
if ((this.space | otherSpace) === (this.space ^ otherSpace)) {
|
||||
return new VoxelSpace(this.id, this.dims, otherSpace | this.space);
|
||||
}
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
size() {
|
||||
let size = 0;
|
||||
this.forEachCell((val) => {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
}
|
||||
}
|
||||
10
src/solver/main.js
Normal file
10
src/solver/main.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import App from './App.svelte';
|
||||
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
props: {
|
||||
name: 'world'
|
||||
}
|
||||
});
|
||||
|
||||
export default app;
|
||||
109
src/solver/main.ts
Normal file
109
src/solver/main.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import SomaSolver from "./SomaSolver";
|
||||
import VoxelSpace from "./VoxelSpace";
|
||||
|
||||
const tetromino1 = new VoxelSpace(1, [3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino2 = new VoxelSpace(2, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino3 = new VoxelSpace(3, [3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino4 = new VoxelSpace(4, [3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino5 = new VoxelSpace(5, [3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino6 = new VoxelSpace(6, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const triomino1 = new VoxelSpace(7, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
|
||||
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
|
||||
const solver = new SomaSolver(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
101
src/store.ts
Normal file
101
src/store.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { derived, writable } from 'svelte/store';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
type PolycubeInput = {
|
||||
color: string,
|
||||
rep: bigint,
|
||||
}
|
||||
|
||||
const MAX_DIMS = 5;
|
||||
const MIN_DIMS = 2;
|
||||
|
||||
const store = {
|
||||
polycubes: writable<PolycubeInput[]>([{rep: BigInt(0), color: colorFromIndex(0)}]),
|
||||
somaDimension: writable(3),
|
||||
};
|
||||
|
||||
export const selectedCube = writable(0);
|
||||
export const isMaxDimension = derived(store.somaDimension, $somaDimension => $somaDimension >= MAX_DIMS);
|
||||
export const isMinDimension = derived(store.somaDimension, $somaDimension => $somaDimension <= MIN_DIMS);
|
||||
export const isMaxPolycubes = derived([store.polycubes, store.somaDimension], ([$polycubes, $somaDimension]) => $polycubes.length >= $somaDimension ** 3);
|
||||
export const isMinPolycubes = derived(store.polycubes, ($polycubes) => $polycubes.length <= 1);
|
||||
|
||||
export const somaDimension = {
|
||||
subscribe: store.somaDimension.subscribe,
|
||||
inc() {
|
||||
if (!get(isMaxDimension)) {
|
||||
store.somaDimension.update(dims => {
|
||||
polycubes.reset(dims + 1);
|
||||
return dims + 1;
|
||||
});
|
||||
}
|
||||
},
|
||||
dec() {
|
||||
if (!get(isMinDimension)) {
|
||||
store.somaDimension.update(dims => {
|
||||
polycubes.reset(dims - 1);
|
||||
return dims - 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const polycubes = {
|
||||
subscribe: store.polycubes.subscribe,
|
||||
addCube() {
|
||||
const isMaxPolycubes = get(store.polycubes).length >= get(store.somaDimension) ** 3;
|
||||
if (!isMaxPolycubes) {
|
||||
store.polycubes.update(polycubes => polycubes.concat({
|
||||
rep: BigInt(0),
|
||||
color: colorFromIndex(polycubes.length),
|
||||
}));
|
||||
}
|
||||
},
|
||||
removeCube() {
|
||||
const isMinPolycubes = get(store.polycubes).length <= 1;
|
||||
if (!isMinPolycubes) {
|
||||
store.polycubes.update(polycubes => polycubes.splice(0, polycubes.length - 1));
|
||||
}
|
||||
},
|
||||
toggle(cubeIndex: number, x: number, y: number, z: number) {
|
||||
const dims = get(store.somaDimension);
|
||||
const mask = BigInt(1) << BigInt(dims ** 2 * x + dims * y + z);
|
||||
const cubes = get(store.polycubes);
|
||||
cubes[cubeIndex].rep ^= mask;
|
||||
store.polycubes.set(cubes);
|
||||
},
|
||||
set(cubeIndex: number, val: boolean, x: number, y: number, z: number) {
|
||||
const dims = get(store.somaDimension);
|
||||
const mask = BigInt(1) << BigInt(dims ** 2 * x + dims * y + z);
|
||||
const cubes = get(store.polycubes);
|
||||
if (val) {
|
||||
cubes[cubeIndex].rep |= mask
|
||||
} else {
|
||||
cubes[cubeIndex].rep &= ~mask
|
||||
}
|
||||
store.polycubes.set(cubes);
|
||||
},
|
||||
reset(dims: number) {
|
||||
store.polycubes.update(polycubes => {
|
||||
const result: PolycubeInput[] = [];
|
||||
for (let i = 0; i < Math.min(polycubes.length, dims**3); i++) {
|
||||
result.push({
|
||||
rep: BigInt(0),
|
||||
color: colorFromIndex(i),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function colorFromIndex(index: number) {
|
||||
const colorWheelCycle = Math.floor(index / 6);
|
||||
const darknessCycle = Math.floor(index / 12);
|
||||
const spacing = (360 / 6);
|
||||
const offset = colorWheelCycle === 0 ? 0 : spacing / (colorWheelCycle + 2);
|
||||
let hue = spacing * (index % 6) + offset;
|
||||
const saturation = 100;
|
||||
const lightness = 1 / (2 + darknessCycle) * 100;
|
||||
return `hsl(${hue},${saturation}%,${lightness}%)`;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="application/javascript" src="test.js"></script>
|
||||
<a onclick="start();">Start</a>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
499
src/test.js
499
src/test.js
@@ -1,499 +0,0 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
||||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
||||
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
||||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
||||
r[k] = a[j];
|
||||
return r;
|
||||
};
|
||||
var SomaSolver = /** @class */ (function () {
|
||||
function SomaSolver(dimension) {
|
||||
this.solutions = [];
|
||||
this.iterations = 0;
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace([dimension, dimension, dimension], Array(Math.pow(dimension, 3)).fill(0));
|
||||
}
|
||||
SomaSolver.prototype.solve = function (polycubes) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
var cumulativeSize = polycubes.reduce(function (prev, curr) { return prev + curr.size(); }, 0);
|
||||
if (cumulativeSize !== Math.pow(this.dim, 3)) {
|
||||
throw new Error("The polycubes passed do not add up to exactly enough units to form a cube of dimension " + this.dim + "! Got: " + cumulativeSize + ", need: " + Math.pow(this.dim, 3));
|
||||
}
|
||||
this.iterations = 0;
|
||||
this.backtrackSolve(this.solutionCube, polycubes);
|
||||
this.solutions = VoxelSpace.filterUnique(this.solutions);
|
||||
this.solutions.forEach(function (sol) { return sol.print(); });
|
||||
console.log(this.solutions.length);
|
||||
};
|
||||
SomaSolver.prototype.backtrackSolve = function (workingSolution, polycubes, depth) {
|
||||
if (depth === void 0) { depth = 0; }
|
||||
var nextCube = polycubes[0];
|
||||
var rots = depth === 0 ? [nextCube] : nextCube.getUniqueRotations();
|
||||
for (var i = 0; i < rots.length; i++) {
|
||||
var polyCubeDims = rots[i].getDims();
|
||||
for (var x = 0; x < this.dim - polyCubeDims[0] + 1; x++) {
|
||||
for (var y = 0; y < this.dim - polyCubeDims[1] + 1; y++) {
|
||||
for (var z = 0; z < this.dim - polyCubeDims[2] + 1; z++) {
|
||||
var successfulFusion = workingSolution.plus(rots[i], x, y, z);
|
||||
if (successfulFusion) {
|
||||
if (polycubes.length === 1) {
|
||||
console.log("soln", this.iterations++);
|
||||
this.solutions.push(successfulFusion);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
this.backtrackSolve(successfulFusion, polycubes.slice(1), depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return SomaSolver;
|
||||
}());
|
||||
var VoxelSpace = /** @class */ (function () {
|
||||
function VoxelSpace(dims, vals, cullEmpty) {
|
||||
if (vals.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
}
|
||||
this.dims = dims;
|
||||
this.vals = vals;
|
||||
if (cullEmpty) {
|
||||
this.cullEmptySpace();
|
||||
}
|
||||
}
|
||||
VoxelSpace.prototype.cullEmptySpace = function () {
|
||||
var extrema = {
|
||||
xMax: -Infinity,
|
||||
xMin: Infinity,
|
||||
yMax: -Infinity,
|
||||
yMin: Infinity,
|
||||
zMax: -Infinity,
|
||||
zMin: Infinity,
|
||||
};
|
||||
var newVals = [];
|
||||
this.forEachCell(function (val, i, j, k) {
|
||||
if (val !== 0) {
|
||||
extrema.xMax = Math.max(extrema.xMax, i);
|
||||
extrema.xMin = Math.min(extrema.xMin, i);
|
||||
extrema.yMax = Math.max(extrema.yMax, j);
|
||||
extrema.yMin = Math.min(extrema.yMin, j);
|
||||
extrema.zMax = Math.max(extrema.zMax, k);
|
||||
extrema.zMin = Math.min(extrema.zMin, k);
|
||||
}
|
||||
});
|
||||
for (var i = extrema.xMin; i <= extrema.xMax; i++) {
|
||||
for (var j = extrema.yMin; j <= extrema.yMax; j++) {
|
||||
for (var k = extrema.zMin; k <= extrema.zMax; k++) {
|
||||
newVals.push(this.at(i, j, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dims[0] = extrema.xMax - extrema.xMin + 1;
|
||||
this.dims[1] = extrema.yMax - extrema.yMin + 1;
|
||||
this.dims[2] = extrema.zMax - extrema.zMin + 1;
|
||||
this.vals = newVals;
|
||||
};
|
||||
VoxelSpace.prototype.forEachCell = function (cb) {
|
||||
loopStart: for (var x = 0; x < this.dims[0]; x++) {
|
||||
for (var y = 0; y < this.dims[1]; y++) {
|
||||
for (var z = 0; z < this.dims[2]; z++) {
|
||||
if (cb(this.at(x, y, z), x, y, z) === 0) {
|
||||
break loopStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
VoxelSpace.prototype.print = function () {
|
||||
var accum = "";
|
||||
console.log("---");
|
||||
for (var i = 0; i < this.dims[0]; i++) {
|
||||
for (var j = 0; j < this.dims[1]; j++) {
|
||||
for (var k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k);
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
};
|
||||
VoxelSpace.prototype.getUniqueRotations = function () {
|
||||
var rotations = [];
|
||||
var refSpace = this.clone();
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
};
|
||||
VoxelSpace.filterUnique = function (spaces) {
|
||||
if (spaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
var uniqueSpaces = [spaces[0]];
|
||||
for (var _i = 0, spaces_1 = spaces; _i < spaces_1.length; _i++) {
|
||||
var space = spaces_1[_i];
|
||||
var foundMatch = false;
|
||||
for (var _a = 0, _b = space.getUniqueRotations(); _a < _b.length; _a++) {
|
||||
var rotation = _b[_a];
|
||||
var end = uniqueSpaces.length;
|
||||
for (var i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSpaces[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSpaces.push(space);
|
||||
}
|
||||
}
|
||||
return uniqueSpaces;
|
||||
};
|
||||
VoxelSpace.pushNewUniqueSpaces = function (existingSpaces, newSpaces) {
|
||||
for (var _i = 0, newSpaces_1 = newSpaces; _i < newSpaces_1.length; _i++) {
|
||||
var newSpace = newSpaces_1[_i];
|
||||
var matchFound = false;
|
||||
for (var _a = 0, existingSpaces_1 = existingSpaces; _a < existingSpaces_1.length; _a++) {
|
||||
var existingSpace = existingSpaces_1[_a];
|
||||
if (newSpace.matches(existingSpace)) {
|
||||
matchFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matchFound) {
|
||||
existingSpaces.push(newSpace);
|
||||
}
|
||||
}
|
||||
};
|
||||
VoxelSpace.prototype.matches = function (space) {
|
||||
var otherDims = space.getDims();
|
||||
for (var i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var otherVals = space.getVals();
|
||||
for (var i = 0; i < this.vals.length; i++) {
|
||||
if (this.vals[i] !== otherVals[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
VoxelSpace.prototype.clone = function () {
|
||||
return new VoxelSpace(this.getDims(), this.getVals());
|
||||
};
|
||||
VoxelSpace.prototype.getAxisSpins = function (axis) {
|
||||
var rotations = [this.clone()];
|
||||
for (var i = 0; i < 3; i++) {
|
||||
rotations.push(rotations[i].rotated90(axis));
|
||||
}
|
||||
return rotations;
|
||||
};
|
||||
VoxelSpace.prototype.getDims = function () {
|
||||
return this.dims.slice();
|
||||
};
|
||||
VoxelSpace.prototype.getVals = function () {
|
||||
return this.vals.slice();
|
||||
};
|
||||
// [1, 0, 0] [x] [ x]
|
||||
// [0, 0, -1] * [y] = [-z]
|
||||
// [0, 1, 0] [z] [ y]
|
||||
VoxelSpace.prototype.newIndexRotX = function (x, y, z) {
|
||||
return this.dims[2] * this.dims[1] * x + this.dims[1] * (this.dims[2] - 1 - z) + y;
|
||||
};
|
||||
// [ 0, 0, 1] [x] [ z]
|
||||
// [ 0, 1, 0] * [y] = [ y]
|
||||
// [-1, 0, 0] [z] [-x]
|
||||
VoxelSpace.prototype.newIndexRotY = function (x, y, z) {
|
||||
return this.dims[1] * this.dims[0] * z + this.dims[0] * y + (this.dims[0] - 1 - x);
|
||||
};
|
||||
// [0, -1, 0] [x] [-y]
|
||||
// [1, 0, 0] * [y] = [ x]
|
||||
// [0, 0, 1] [z] [ z]
|
||||
VoxelSpace.prototype.newIndexRotZ = function (x, y, z) {
|
||||
return this.dims[0] * this.dims[2] * (this.dims[1] - 1 - y) + this.dims[2] * x + z;
|
||||
};
|
||||
VoxelSpace.prototype.at = function (x, y, z) {
|
||||
return this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z];
|
||||
};
|
||||
VoxelSpace.prototype.set = function (x, y, z, val) {
|
||||
this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z] = val;
|
||||
};
|
||||
VoxelSpace.prototype.rotated90 = function (dim) {
|
||||
var newVals = __spreadArrays(this.vals);
|
||||
var newDims;
|
||||
var rotIndex;
|
||||
if (dim === 'x') {
|
||||
newDims = [this.dims[0], this.dims[2], this.dims[1]];
|
||||
rotIndex = this.newIndexRotX.bind(this);
|
||||
}
|
||||
else if (dim === 'y') {
|
||||
newDims = [this.dims[2], this.dims[1], this.dims[0]];
|
||||
rotIndex = this.newIndexRotY.bind(this);
|
||||
}
|
||||
else {
|
||||
newDims = [this.dims[1], this.dims[0], this.dims[2]];
|
||||
rotIndex = this.newIndexRotZ.bind(this);
|
||||
}
|
||||
this.forEachCell(function (val, i, j, k) {
|
||||
newVals[rotIndex(i, j, k)] = val;
|
||||
});
|
||||
return new VoxelSpace(newDims, newVals);
|
||||
};
|
||||
VoxelSpace.prototype.rot90 = function (dim) {
|
||||
var rot = this.rotated90(dim);
|
||||
this.vals = rot.getVals();
|
||||
this.dims = rot.getDims();
|
||||
};
|
||||
VoxelSpace.prototype.plus = function (space, startX, startY, startZ) {
|
||||
var result = this.clone();
|
||||
var spaceDims = space.getDims();
|
||||
for (var i = 0; i < spaceDims[0]; i++) {
|
||||
for (var j = 0; j < spaceDims[1]; j++) {
|
||||
for (var k = 0; k < spaceDims[2]; k++) {
|
||||
var sourceVal = space.at(i, j, k);
|
||||
var targetEmpty = result.at(startX + i, startY + j, startZ + k) === 0;
|
||||
if (sourceVal !== 0 && targetEmpty) {
|
||||
result.set(startX + i, startY + j, startZ + k, sourceVal);
|
||||
}
|
||||
else if (sourceVal !== 0 && !targetEmpty) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return VoxelSpace;
|
||||
}());
|
||||
var Polycube = /** @class */ (function (_super) {
|
||||
__extends(Polycube, _super);
|
||||
function Polycube(dims, vals, id) {
|
||||
var _this = _super.call(this, dims, vals.map(function (val) { return val ? id : 0; }), true) || this;
|
||||
_this.id = id;
|
||||
return _this;
|
||||
}
|
||||
Polycube.prototype.getId = function () {
|
||||
return this.id;
|
||||
};
|
||||
Polycube.prototype.print = function () {
|
||||
var accum = "";
|
||||
console.log("---");
|
||||
for (var i = 0; i < this.dims[0]; i++) {
|
||||
for (var j = 0; j < this.dims[1]; j++) {
|
||||
for (var k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k) === 0 ? "O" : "#";
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
};
|
||||
Polycube.prototype.matches = function (cube) {
|
||||
var otherDims = cube.getDims();
|
||||
for (var i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var otherVals = cube.getVals();
|
||||
for (var i = 0; i < this.vals.length; i++) {
|
||||
if (Number(this.vals[i]) !== Number(otherVals[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Polycube.prototype.size = function () {
|
||||
var size = 0;
|
||||
this.forEachCell(function (val) {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
};
|
||||
Polycube.prototype.rotated90 = function (dim) {
|
||||
var rotated = _super.prototype.rotated90.call(this, dim);
|
||||
return new Polycube(rotated.getDims(), rotated.getVals(), this.id);
|
||||
};
|
||||
Polycube.prototype.clone = function () {
|
||||
return new Polycube(this.getDims(), this.getVals(), this.id);
|
||||
};
|
||||
Polycube.prototype.getUniqueRotations = function () {
|
||||
var _this = this;
|
||||
return _super.prototype.getUniqueRotations.call(this).map(function (rot) { return new Polycube(rot.getDims(), rot.getVals(), _this.id); });
|
||||
};
|
||||
return Polycube;
|
||||
}(VoxelSpace));
|
||||
// const testCube = new Cube([4, 2, 5], [
|
||||
// "000", "001", "002", "003", "004",
|
||||
// "010", "011", "012", "013", "014",
|
||||
// "100", "101", "102", "103", "104",
|
||||
// "110", "111", "112", "113", "114",
|
||||
// "200", "201", "202", "203", "204",
|
||||
// "210", "211", "212", "213", "214",
|
||||
// "300", "301", "302", "303", "304",
|
||||
// "310", "311", "312", "313", "314",
|
||||
// ]);
|
||||
// const somaCube = new Polycube([3, 3, 3], [
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
//
|
||||
// true, true, true,
|
||||
// true, true, true,
|
||||
// false, false, false,
|
||||
//
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// ], 1);
|
||||
var unitCube1 = new Polycube([1, 1, 1], [true], 1);
|
||||
var unitCube2 = new Polycube([1, 1, 1], [true], 2);
|
||||
var unitCube3 = new Polycube([1, 1, 1], [true], 3);
|
||||
var unitCube4 = new Polycube([1, 1, 1], [true], 4);
|
||||
var unitCube5 = new Polycube([1, 1, 1], [true], 5);
|
||||
var unitCube6 = new Polycube([1, 1, 1], [true], 6);
|
||||
var unitCube7 = new Polycube([1, 1, 1], [true], 7);
|
||||
var unitCube8 = new Polycube([1, 1, 1], [true], 8);
|
||||
var unitCube9 = new Polycube([1, 1, 1], [true], 9);
|
||||
var unitCube10 = new Polycube([1, 1, 1], [true], 10);
|
||||
var unitCube11 = new Polycube([1, 1, 1], [true], 11);
|
||||
var unitCube12 = new Polycube([1, 1, 1], [true], 12);
|
||||
var unitCube13 = new Polycube([1, 1, 1], [true], 13);
|
||||
var unitCube14 = new Polycube([1, 1, 1], [true], 14);
|
||||
var unitCube15 = new Polycube([1, 1, 1], [true], 15);
|
||||
var unitCube16 = new Polycube([1, 1, 1], [true], 16);
|
||||
var unitCube17 = new Polycube([1, 1, 1], [true], 17);
|
||||
var unitCube18 = new Polycube([1, 1, 1], [true], 18);
|
||||
var unitCube19 = new Polycube([1, 1, 1], [true], 19);
|
||||
var unitCube20 = new Polycube([1, 1, 1], [true], 20);
|
||||
var unitCube21 = new Polycube([1, 1, 1], [true], 21);
|
||||
var unitCube22 = new Polycube([1, 1, 1], [true], 22);
|
||||
var unitCube23 = new Polycube([1, 1, 1], [true], 23);
|
||||
var unitCube24 = new Polycube([1, 1, 1], [true], 24);
|
||||
var unitCube25 = new Polycube([1, 1, 1], [true], 25);
|
||||
var unitCube26 = new Polycube([1, 1, 1], [true], 26);
|
||||
var unitCube27 = new Polycube([1, 1, 1], [true], 27);
|
||||
var tetromino1 = new Polycube([3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 1);
|
||||
var tetromino2 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 2);
|
||||
var tetromino3 = new Polycube([3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 3);
|
||||
var tetromino4 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 4);
|
||||
var tetromino5 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 5);
|
||||
var tetromino6 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 6);
|
||||
var triomino1 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 7);
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
function start() {
|
||||
var solver = new SomaSolver(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
}
|
||||
524
src/test.ts
524
src/test.ts
@@ -1,524 +0,0 @@
|
||||
class SomaSolver {
|
||||
private solutionCube: VoxelSpace;
|
||||
private dim: number;
|
||||
private solutions: VoxelSpace[] = [];
|
||||
private iterations: number = 0;
|
||||
constructor(dimension: number) {
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace([dimension, dimension, dimension], Array(dimension**3).fill(0));
|
||||
}
|
||||
|
||||
solve(polycubes: Polycube[]) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
let cumulativeSize = polycubes.reduce((prev, curr) => prev + curr.size(), 0);
|
||||
if (cumulativeSize !== this.dim**3) {
|
||||
throw new Error(`The polycubes passed do not add up to exactly enough units to form a cube of dimension ${this.dim}! Got: ${cumulativeSize}, need: ${this.dim**3}`);
|
||||
}
|
||||
this.iterations = 0;
|
||||
this.backtrackSolve(this.solutionCube, polycubes);
|
||||
this.solutions = VoxelSpace.filterUnique(this.solutions);
|
||||
this.solutions.forEach(sol => sol.print());
|
||||
console.log(this.solutions.length);
|
||||
}
|
||||
|
||||
private backtrackSolve(workingSolution: VoxelSpace, polycubes: Polycube[], depth = 0) {
|
||||
const nextCube = polycubes[0];
|
||||
const rots = depth === 0 ? [nextCube] : nextCube.getUniqueRotations();
|
||||
for (let i = 0; i < rots.length; i++) {
|
||||
const polyCubeDims = rots[i].getDims();
|
||||
for (let x = 0; x < this.dim - polyCubeDims[0] + 1; x++) {
|
||||
for (let y = 0; y < this.dim - polyCubeDims[1] + 1; y++) {
|
||||
for (let z = 0; z < this.dim - polyCubeDims[2] + 1; z++) {
|
||||
const successfulFusion = workingSolution.plus(rots[i], x, y, z);
|
||||
if (successfulFusion) {
|
||||
if (polycubes.length === 1) {
|
||||
console.log("soln", this.iterations++);
|
||||
this.solutions.push(successfulFusion);
|
||||
return;
|
||||
} else {
|
||||
this.backtrackSolve(successfulFusion, polycubes.slice(1), depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VoxelSpace {
|
||||
protected vals: number[];
|
||||
protected dims: DimensionDef;
|
||||
constructor(dims: DimensionDef, vals: number[], cullEmpty?: boolean) {
|
||||
if (vals.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
}
|
||||
this.dims = dims;
|
||||
this.vals = vals;
|
||||
if (cullEmpty) {
|
||||
this.cullEmptySpace();
|
||||
}
|
||||
}
|
||||
|
||||
private cullEmptySpace() {
|
||||
const extrema = {
|
||||
xMax: -Infinity,
|
||||
xMin: Infinity,
|
||||
yMax: -Infinity,
|
||||
yMin: Infinity,
|
||||
zMax: -Infinity,
|
||||
zMin: Infinity,
|
||||
};
|
||||
const newVals: number[] = [];
|
||||
this.forEachCell((val, i, j, k) => {
|
||||
if (val !== 0) {
|
||||
extrema.xMax = Math.max(extrema.xMax, i);
|
||||
extrema.xMin = Math.min(extrema.xMin, i);
|
||||
extrema.yMax = Math.max(extrema.yMax, j);
|
||||
extrema.yMin = Math.min(extrema.yMin, j);
|
||||
extrema.zMax = Math.max(extrema.zMax, k);
|
||||
extrema.zMin = Math.min(extrema.zMin, k);
|
||||
}
|
||||
});
|
||||
for (let i = extrema.xMin; i <= extrema.xMax; i++) {
|
||||
for (let j = extrema.yMin; j <= extrema.yMax; j++) {
|
||||
for (let k = extrema.zMin; k <= extrema.zMax; k++) {
|
||||
newVals.push(this.at(i, j, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dims[0] = extrema.xMax - extrema.xMin + 1;
|
||||
this.dims[1] = extrema.yMax - extrema.yMin + 1;
|
||||
this.dims[2] = extrema.zMax - extrema.zMin + 1;
|
||||
this.vals = newVals;
|
||||
}
|
||||
|
||||
forEachCell(cb: (val: number, x: number, y: number, z: number) => any) {
|
||||
loopStart: for (let x = 0; x < this.dims[0]; x++) {
|
||||
for (let y = 0; y < this.dims[1]; y++) {
|
||||
for (let z = 0; z < this.dims[2]; z++) {
|
||||
if (cb(this.at(x, y, z), x, y, z) === 0) {
|
||||
break loopStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let i = 0; i < this.dims[0]; i++) {
|
||||
for (let j = 0; j < this.dims[1]; j++) {
|
||||
for (let k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k);
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
getUniqueRotations() {
|
||||
const rotations: VoxelSpace[] = [];
|
||||
const refSpace = this.clone();
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
}
|
||||
|
||||
static filterUnique<T extends Polycube | VoxelSpace>(spaces: T[]): T[] {
|
||||
if (spaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const uniqueSpaces = [spaces[0]];
|
||||
for (const space of spaces) {
|
||||
let foundMatch = false;
|
||||
for (const rotation of space.getUniqueRotations()) {
|
||||
let end = uniqueSpaces.length;
|
||||
for (let i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSpaces[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSpaces.push(space);
|
||||
}
|
||||
}
|
||||
return uniqueSpaces;
|
||||
}
|
||||
|
||||
protected static pushNewUniqueSpaces(existingSpaces: VoxelSpace[], newSpaces: VoxelSpace[]) {
|
||||
for (const newSpace of newSpaces) {
|
||||
let matchFound = false;
|
||||
for (const existingSpace of existingSpaces) {
|
||||
if (newSpace.matches(existingSpace)) {
|
||||
matchFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matchFound) {
|
||||
existingSpaces.push(newSpace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
matches(space: VoxelSpace) {
|
||||
const otherDims = space.getDims();
|
||||
for (let i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const otherVals = space.getVals();
|
||||
for (let i = 0; i < this.vals.length; i++) {
|
||||
if (this.vals[i] !== otherVals[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new VoxelSpace(this.getDims(), this.getVals());
|
||||
}
|
||||
|
||||
private getAxisSpins(axis: 'x' | 'y' | 'z'): VoxelSpace[] {
|
||||
const rotations = [this.clone()];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rotations.push(rotations[i].rotated90(axis));
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
|
||||
getDims(): DimensionDef {
|
||||
return this.dims.slice() as DimensionDef;
|
||||
}
|
||||
|
||||
getVals() {
|
||||
return this.vals.slice();
|
||||
}
|
||||
|
||||
// [1, 0, 0] [x] [ x]
|
||||
// [0, 0, -1] * [y] = [-z]
|
||||
// [0, 1, 0] [z] [ y]
|
||||
private newIndexRotX(x: number, y: number, z: number) {
|
||||
return this.dims[2] * this.dims[1] * x + this.dims[1] * (this.dims[2] - 1 - z) + y;
|
||||
}
|
||||
|
||||
// [ 0, 0, 1] [x] [ z]
|
||||
// [ 0, 1, 0] * [y] = [ y]
|
||||
// [-1, 0, 0] [z] [-x]
|
||||
private newIndexRotY(x: number, y: number, z: number) {
|
||||
return this.dims[1] * this.dims[0] * z + this.dims[0] * y + (this.dims[0] - 1 - x);
|
||||
}
|
||||
|
||||
// [0, -1, 0] [x] [-y]
|
||||
// [1, 0, 0] * [y] = [ x]
|
||||
// [0, 0, 1] [z] [ z]
|
||||
private newIndexRotZ(x: number, y: number, z: number) {
|
||||
return this.dims[0] * this.dims[2] * (this.dims[1] - 1 - y) + this.dims[2] * x + z;
|
||||
}
|
||||
|
||||
at(x: number, y: number, z: number) {
|
||||
return this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z];
|
||||
}
|
||||
|
||||
set(x: number, y: number, z: number, val: number) {
|
||||
this.vals[this.dims[1] * this.dims[2] * x + this.dims[2] * y + z] = val;
|
||||
}
|
||||
|
||||
rotated90(dim: 'x' | 'y' | 'z') {
|
||||
const newVals = [...this.vals];
|
||||
let newDims: DimensionDef;
|
||||
let rotIndex: (i: number, j: number, k: number) => number;
|
||||
if (dim === 'x') {
|
||||
newDims = [this.dims[0], this.dims[2], this.dims[1]];
|
||||
rotIndex = this.newIndexRotX.bind(this);
|
||||
} else if (dim === 'y') {
|
||||
newDims = [this.dims[2], this.dims[1], this.dims[0]];
|
||||
rotIndex = this.newIndexRotY.bind(this);
|
||||
} else {
|
||||
newDims = [this.dims[1], this.dims[0], this.dims[2]];
|
||||
rotIndex = this.newIndexRotZ.bind(this);
|
||||
}
|
||||
this.forEachCell((val, i, j, k) => {
|
||||
newVals[rotIndex(i, j, k)] = val;
|
||||
})
|
||||
return new VoxelSpace(newDims, newVals);
|
||||
}
|
||||
|
||||
rot90(dim: 'x' | 'y' | 'z') {
|
||||
const rot = this.rotated90(dim);
|
||||
this.vals = rot.getVals();
|
||||
this.dims = rot.getDims();
|
||||
}
|
||||
|
||||
plus(space: VoxelSpace, startX: number, startY: number, startZ: number): VoxelSpace | null {
|
||||
let result: VoxelSpace = this.clone();
|
||||
const spaceDims = space.getDims();
|
||||
for (let i = 0; i < spaceDims[0]; i++) {
|
||||
for (let j = 0; j < spaceDims[1]; j++) {
|
||||
for (let k = 0; k < spaceDims[2]; k++) {
|
||||
const sourceVal = space.at(i, j, k);
|
||||
const targetEmpty = result.at(startX + i, startY + j, startZ + k) === 0;
|
||||
if (sourceVal !== 0 && targetEmpty) {
|
||||
result.set(startX + i, startY + j, startZ + k, sourceVal);
|
||||
} else if (sourceVal !== 0 && !targetEmpty) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class Polycube extends VoxelSpace {
|
||||
private id: number;
|
||||
constructor(dims: DimensionDef, vals: boolean[], id: number) {
|
||||
super(dims, vals.map(val => val ? id : 0), true);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let i = 0; i < this.dims[0]; i++) {
|
||||
for (let j = 0; j < this.dims[1]; j++) {
|
||||
for (let k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k) === 0 ? "O" : "#";
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
matches(cube: VoxelSpace) {
|
||||
const otherDims = cube.getDims();
|
||||
for (let i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const otherVals = cube.getVals();
|
||||
for (let i = 0; i < this.vals.length; i++) {
|
||||
if (Number(this.vals[i]) !== Number(otherVals[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size() {
|
||||
let size = 0;
|
||||
this.forEachCell((val) => {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
}
|
||||
|
||||
rotated90(dim: "x" | "y" | "z"): Polycube {
|
||||
const rotated = super.rotated90(dim);
|
||||
return new Polycube(rotated.getDims(), rotated.getVals() as unknown as boolean[], this.id);
|
||||
}
|
||||
|
||||
clone(): Polycube {
|
||||
return new Polycube(this.getDims(), this.getVals() as unknown as boolean[], this.id);
|
||||
}
|
||||
|
||||
getUniqueRotations(): Polycube[] {
|
||||
return super.getUniqueRotations().map(rot => new Polycube(rot.getDims(), rot.getVals() as unknown as boolean[], this.id));
|
||||
}
|
||||
}
|
||||
|
||||
type DimensionDef = [number, number, number];
|
||||
|
||||
// const testCube = new Cube([4, 2, 5], [
|
||||
// "000", "001", "002", "003", "004",
|
||||
// "010", "011", "012", "013", "014",
|
||||
// "100", "101", "102", "103", "104",
|
||||
// "110", "111", "112", "113", "114",
|
||||
// "200", "201", "202", "203", "204",
|
||||
// "210", "211", "212", "213", "214",
|
||||
// "300", "301", "302", "303", "304",
|
||||
// "310", "311", "312", "313", "314",
|
||||
// ]);
|
||||
// const somaCube = new Polycube([3, 3, 3], [
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
//
|
||||
// true, true, true,
|
||||
// true, true, true,
|
||||
// false, false, false,
|
||||
//
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// false, false, false,
|
||||
// ], 1);
|
||||
const unitCube1 = new Polycube([1, 1, 1], [true], 1);
|
||||
const unitCube2 = new Polycube([1, 1, 1], [true], 2);
|
||||
const unitCube3 = new Polycube([1, 1, 1], [true], 3);
|
||||
const unitCube4 = new Polycube([1, 1, 1], [true], 4);
|
||||
const unitCube5 = new Polycube([1, 1, 1], [true], 5);
|
||||
const unitCube6 = new Polycube([1, 1, 1], [true], 6);
|
||||
const unitCube7 = new Polycube([1, 1, 1], [true], 7);
|
||||
const unitCube8 = new Polycube([1, 1, 1], [true], 8);
|
||||
const unitCube9 = new Polycube([1, 1, 1], [true], 9);
|
||||
const unitCube10 = new Polycube([1, 1, 1], [true], 10);
|
||||
const unitCube11 = new Polycube([1, 1, 1], [true], 11);
|
||||
const unitCube12 = new Polycube([1, 1, 1], [true], 12);
|
||||
const unitCube13 = new Polycube([1, 1, 1], [true], 13);
|
||||
const unitCube14 = new Polycube([1, 1, 1], [true], 14);
|
||||
const unitCube15 = new Polycube([1, 1, 1], [true], 15);
|
||||
const unitCube16 = new Polycube([1, 1, 1], [true], 16);
|
||||
const unitCube17 = new Polycube([1, 1, 1], [true], 17);
|
||||
const unitCube18 = new Polycube([1, 1, 1], [true], 18);
|
||||
const unitCube19 = new Polycube([1, 1, 1], [true], 19);
|
||||
const unitCube20 = new Polycube([1, 1, 1], [true], 20);
|
||||
const unitCube21 = new Polycube([1, 1, 1], [true], 21);
|
||||
const unitCube22 = new Polycube([1, 1, 1], [true], 22);
|
||||
const unitCube23 = new Polycube([1, 1, 1], [true], 23);
|
||||
const unitCube24 = new Polycube([1, 1, 1], [true], 24);
|
||||
const unitCube25 = new Polycube([1, 1, 1], [true], 25);
|
||||
const unitCube26 = new Polycube([1, 1, 1], [true], 26);
|
||||
const unitCube27 = new Polycube([1, 1, 1], [true], 27);
|
||||
|
||||
const tetromino1 = new Polycube([3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 1);
|
||||
|
||||
const tetromino2 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 2);
|
||||
|
||||
const tetromino3 = new Polycube([3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 3);
|
||||
|
||||
const tetromino4 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 4);
|
||||
|
||||
const tetromino5 = new Polycube([3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 5);
|
||||
|
||||
const tetromino6 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 6);
|
||||
|
||||
const triomino1 = new Polycube([3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], 7);
|
||||
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
|
||||
function start() {
|
||||
const solver = new SomaSolver(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
}
|
||||
134
src/threeTest.ts
Normal file
134
src/threeTest.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import * as THREE from 'three';
|
||||
import { MapControls } from './OrbitControls.js';
|
||||
import VoxelSpace from './solver/VoxelSpace.js';
|
||||
import {somaDimension, polycubes} from './store';
|
||||
import {get} from 'svelte/store';
|
||||
import type { MeshPhongMaterial } from 'three';
|
||||
|
||||
export default class PolycubeScene {
|
||||
private renderer: THREE.WebGLRenderer;
|
||||
private camera: THREE.Camera;
|
||||
private mainScene: THREE.Scene;
|
||||
private polycubeMeshes: THREE.Mesh[] = [];
|
||||
private controls: typeof MapControls;
|
||||
private light: THREE.Light;
|
||||
private cameraLightScene: THREE.Group;
|
||||
private lastDims: number = 0;
|
||||
private currentPolycubeId: number = 0;
|
||||
private lastColor: string = "#FF0000";
|
||||
private lastPolycube: bigint = 0n;
|
||||
|
||||
constructor(el: HTMLCanvasElement) {
|
||||
this.renderer = new THREE.WebGLRenderer({canvas: el});
|
||||
const fov = 75;
|
||||
const aspect = el.clientWidth / el.clientHeight;
|
||||
const near = 0.1;
|
||||
const far = 10;
|
||||
this.camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
|
||||
this.camera.position.z = 5;
|
||||
this.camera.lookAt(0, 0, 0);
|
||||
this.mainScene = new THREE.Scene();
|
||||
this.light = this.setupLight();
|
||||
this.mainScene.add(this.light);
|
||||
this.mainScene.rotateX(Math.PI/4);
|
||||
this.mainScene.rotateY(Math.PI/4);
|
||||
this.cameraLightScene = new THREE.Group();
|
||||
this.controls = new MapControls(this.camera, el);
|
||||
requestAnimationFrame((timestamp) => this.render(timestamp));
|
||||
}
|
||||
|
||||
private setPolycube(polycube: bigint, dims: number, color: string) {
|
||||
if (dims !== this.lastDims) {
|
||||
this.mainScene.remove(...this.polycubeMeshes);
|
||||
this.polycubeMeshes = [];
|
||||
this.polycubeMeshes = Array.from(Array(dims ** 3).keys()).map(() => {
|
||||
const cube = this.newRoundedCube(0.2, 3, color);
|
||||
cube.position.set(1000, 1000, 1000);
|
||||
this.mainScene.add(cube);
|
||||
return cube;
|
||||
});
|
||||
this.lastDims = dims;
|
||||
}
|
||||
|
||||
if (polycube !== this.lastPolycube) {
|
||||
let i = 0;
|
||||
const voxelSpace = new VoxelSpace(0, [dims, dims, dims], polycube);
|
||||
voxelSpace.forEachCell((val, x, y, z) => {
|
||||
if (val) {
|
||||
this.polycubeMeshes[i].position.set(
|
||||
-((dims - 1)/2) + z,
|
||||
((dims - 1)/2) - y,
|
||||
-((dims - 1)/2) + x,
|
||||
);
|
||||
} else {
|
||||
this.polycubeMeshes[i].position.set(1000, 1000, 1000);
|
||||
}
|
||||
i++;
|
||||
});
|
||||
this.lastPolycube = polycube;
|
||||
}
|
||||
|
||||
if (color !== this.lastColor) {
|
||||
this.polycubeMeshes.forEach(mesh => (mesh.material as MeshPhongMaterial).color.set(color));
|
||||
this.lastColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
private updateFromCurrentPolycube() {
|
||||
const {color: cubeColor, rep: cubeRep} = get(polycubes)[this.currentPolycubeId];
|
||||
const dims = get(somaDimension);
|
||||
const voxelSpace = new VoxelSpace(this.currentPolycubeId, [dims, dims, dims], cubeRep);
|
||||
this.mainScene.remove(...this.polycubeMeshes);
|
||||
voxelSpace.forEachCell((val, x, y, z) => {
|
||||
if (val) {
|
||||
const cube = this.newRoundedCube(0.2, 3, cubeColor);
|
||||
cube.position.set(
|
||||
-((dims - 1)/2) + z,
|
||||
((dims - 1)/2) - y,
|
||||
-((dims - 1)/2) + x,
|
||||
);
|
||||
this.mainScene.add(cube);
|
||||
this.polycubeMeshes.push(cube);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private setupLight() {
|
||||
const color = 0xFFFFFF;
|
||||
const intensity = 1;
|
||||
const light = new THREE.DirectionalLight(color, intensity);
|
||||
light.position.set(-1, 2, 4);
|
||||
return light;
|
||||
}
|
||||
|
||||
private render(time: number) {
|
||||
this.renderer.render(this.mainScene, this.camera);
|
||||
requestAnimationFrame((timestamp) => this.render(timestamp));
|
||||
}
|
||||
|
||||
private newRoundedCube(radius: number, smoothness: number, color: string) {
|
||||
const width = 1;
|
||||
const height = 1;
|
||||
const depth = 1;
|
||||
const shape = new THREE.Shape();
|
||||
const eps = 0.00001;
|
||||
const radius0 = radius - eps;
|
||||
shape.absarc(eps, eps, eps, -Math.PI / 2, -Math.PI, true);
|
||||
shape.absarc(eps, height - radius0 * 2, eps, Math.PI, Math.PI / 2, true);
|
||||
shape.absarc(width - radius0 * 2, height - radius0 * 2, eps, Math.PI / 2, 0, true);
|
||||
shape.absarc(width - radius0 * 2, eps, eps, 0, -Math.PI / 2, true );
|
||||
const geometry = new THREE.ExtrudeBufferGeometry(shape, {
|
||||
depth: depth - radius0 * 2,
|
||||
bevelEnabled: true,
|
||||
bevelSegments: smoothness * 2,
|
||||
steps: 1,
|
||||
bevelSize: radius0,
|
||||
bevelThickness: radius0,
|
||||
curveSegments: smoothness
|
||||
});
|
||||
geometry.center();
|
||||
const material = new THREE.MeshPhongMaterial({color});
|
||||
const cube = new THREE.Mesh(geometry, material);
|
||||
return cube;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user