onMouseOverCell(event, x, y, z)}
+ on:mousedown={(event) => onMouseDownCell(event, x, y, z)}
+ on:mouseup={(event) => onMouseUpCell(event, x, y, z)}
+ />
+ {/each}
+
+ {/each}
+
+ {/each}
+
+
+
\ No newline at end of file
diff --git a/src/OrbitControls.js b/src/OrbitControls.js
new file mode 100644
index 0000000..5c4f668
--- /dev/null
+++ b/src/OrbitControls.js
@@ -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 };
\ No newline at end of file
diff --git a/src/Polycube.js b/src/Polycube.js
deleted file mode 100644
index ac41718..0000000
--- a/src/Polycube.js
+++ /dev/null
@@ -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;
diff --git a/src/Polycube.ts b/src/Polycube.ts
deleted file mode 100644
index df81443..0000000
--- a/src/Polycube.ts
+++ /dev/null
@@ -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));
- }
-}
\ No newline at end of file
diff --git a/src/Polycube3D.svelte b/src/Polycube3D.svelte
new file mode 100644
index 0000000..2eda31a
--- /dev/null
+++ b/src/Polycube3D.svelte
@@ -0,0 +1,22 @@
+
+
+
\ No newline at end of file
diff --git a/src/Sidebar.svelte b/src/Sidebar.svelte
new file mode 100644
index 0000000..3d64697
--- /dev/null
+++ b/src/Sidebar.svelte
@@ -0,0 +1,50 @@
+
+
+
+
Somaesque
+
Settings
+
+
+
Cube Dimension: {$somaDimension}
+
+
+
+
+
+
Cubes: {numCubes}
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/SolutionInteractor.svelte b/src/SolutionInteractor.svelte
new file mode 100644
index 0000000..10ddbff
--- /dev/null
+++ b/src/SolutionInteractor.svelte
@@ -0,0 +1,27 @@
+
+
+
+ {#each {length: numCubes} as _, cubeNo}
+
+
+
+ {/each}
+
+
+
+
\ No newline at end of file
diff --git a/src/SomaSolver.js b/src/SomaSolver.js
deleted file mode 100644
index fda6de3..0000000
--- a/src/SomaSolver.js
+++ /dev/null
@@ -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;
diff --git a/src/SomaSolver.ts b/src/SomaSolver.ts
deleted file mode 100644
index 1a24af7..0000000
--- a/src/SomaSolver.ts
+++ /dev/null
@@ -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);
- }
- }
- }
- }
- }
- }
- }
-}
\ No newline at end of file
diff --git a/src/VoxelSpace.js b/src/VoxelSpace.js
deleted file mode 100644
index 023a4b4..0000000
--- a/src/VoxelSpace.js
+++ /dev/null
@@ -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;
diff --git a/src/main.js b/src/main.js
deleted file mode 100644
index 5291352..0000000
--- a/src/main.js
+++ /dev/null
@@ -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]);
diff --git a/src/main.ts b/src/main.ts
index aa2c480..d6cacbb 100644
--- a/src/main.ts
+++ b/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;
\ No newline at end of file
diff --git a/src/solver/SomaSolution.ts b/src/solver/SomaSolution.ts
new file mode 100644
index 0000000..f53edac
--- /dev/null
+++ b/src/solver/SomaSolution.ts
@@ -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;
+ }
+}
\ No newline at end of file
diff --git a/src/solver/SomaSolver.ts b/src/solver/SomaSolver.ts
new file mode 100644
index 0000000..953926d
--- /dev/null
+++ b/src/solver/SomaSolver.ts
@@ -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);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/VoxelSpace.ts b/src/solver/VoxelSpace.ts
similarity index 52%
rename from src/VoxelSpace.ts
rename to src/solver/VoxelSpace.ts
index 1f0a573..5bf241b 100644
--- a/src/VoxelSpace.ts
+++ b/src/solver/VoxelSpace.ts
@@ -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(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;
}
}
\ No newline at end of file
diff --git a/src/solver/main.js b/src/solver/main.js
new file mode 100644
index 0000000..d6cacbb
--- /dev/null
+++ b/src/solver/main.js
@@ -0,0 +1,10 @@
+import App from './App.svelte';
+
+const app = new App({
+ target: document.body,
+ props: {
+ name: 'world'
+ }
+});
+
+export default app;
\ No newline at end of file
diff --git a/src/solver/main.ts b/src/solver/main.ts
new file mode 100644
index 0000000..7fbe282
--- /dev/null
+++ b/src/solver/main.ts
@@ -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]);
diff --git a/src/store.ts b/src/store.ts
new file mode 100644
index 0000000..ed7255f
--- /dev/null
+++ b/src/store.ts
@@ -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([{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}%)`;
+}
\ No newline at end of file
diff --git a/src/test.html b/src/test.html
deleted file mode 100644
index f1d4c75..0000000
--- a/src/test.html
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
- Title
-
-
-
-
-Start
-
-
-
\ No newline at end of file
diff --git a/src/test.js b/src/test.js
deleted file mode 100644
index 47e8bd4..0000000
--- a/src/test.js
+++ /dev/null
@@ -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]);
-}
diff --git a/src/test.ts b/src/test.ts
deleted file mode 100644
index 305a0ba..0000000
--- a/src/test.ts
+++ /dev/null
@@ -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(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]);
-}
diff --git a/src/threeTest.ts b/src/threeTest.ts
new file mode 100644
index 0000000..0383207
--- /dev/null
+++ b/src/threeTest.ts
@@ -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;
+ }
+}
diff --git a/tsconfig.json b/tsconfig.json
index 9545063..29f6d75 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,66 +1,9 @@
{
+ "extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
- /* Basic Options */
- // "incremental": true, /* Enable incremental compilation */
- "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
- "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
- "lib": ["dom", "ES6"], /* Specify library files to be included in the compilation. */
- // "allowJs": true, /* Allow javascript files to be compiled. */
- // "checkJs": true, /* Report errors in .js files. */
- // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
- // "declaration": true, /* Generates corresponding '.d.ts' file. */
- // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
- // "sourceMap": true, /* Generates corresponding '.map' file. */
- // "outFile": "./", /* Concatenate and emit output to single file. */
- // "outDir": "./", /* Redirect output structure to the directory. */
- // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
- // "composite": true, /* Enable project compilation */
- // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
- // "removeComments": true, /* Do not emit comments to output. */
- // "noEmit": true, /* Do not emit outputs. */
- // "importHelpers": true, /* Import emit helpers from 'tslib'. */
- // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
- // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
-
- /* Strict Type-Checking Options */
- "strict": true, /* Enable all strict type-checking options. */
- // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
- // "strictNullChecks": true, /* Enable strict null checks. */
- // "strictFunctionTypes": true, /* Enable strict checking of function types. */
- // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
- // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
- // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
- // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
-
- /* Additional Checks */
- // "noUnusedLocals": true, /* Report errors on unused locals. */
- // "noUnusedParameters": true, /* Report errors on unused parameters. */
- // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
- // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
-
- /* Module Resolution Options */
- // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
- // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
- // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
- // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
- // "typeRoots": [], /* List of folders to include type definitions from. */
- // "types": [], /* Type declaration files to be included in compilation. */
- // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
- "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
- // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
-
- /* Source Map Options */
- // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
- // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
- // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
-
- /* Experimental Options */
- // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
- // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
-
- /* Advanced Options */
- "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
- }
-}
+ "lib": ["es2020", "dom"],
+ "target": "ES2020"
+ },
+ "include": ["src/**/*"],
+ "exclude": ["node_modules/*", "__sapper__/*", "public/*"]
+}
\ No newline at end of file