private static void TestGridPlacement(ref MyGridPlacementSettings settings, ref MatrixD worldMatrix, ref MyCubeGrid touchingGrid, float gridSize, bool isStatic, ref BoundingBoxD localAABB, MyCubeBlockDefinition blockDefinition,
           MyBlockOrientation? blockOrientation, ref bool entityOverlap, ref bool touchingStaticGrid, MyCubeGrid grid)
        {
            var worldAabb = localAABB.Transform(ref worldMatrix);
            var invWorldMatrix = grid.PositionComp.WorldMatrixNormalizedInv;
            var otherLocalAabb = worldAabb.Transform(ref invWorldMatrix);

            Vector3D minToWorld = Vector3D.Transform(localAABB.Min, worldMatrix);
            Vector3D maxToWorld = Vector3D.Transform(localAABB.Max, worldMatrix);
            Vector3D tempMinLocal = Vector3D.Transform(minToWorld, invWorldMatrix);
            Vector3D tempMaxLocal = Vector3D.Transform(maxToWorld, invWorldMatrix);

            Vector3D otherMinLocal = Vector3D.Min(tempMinLocal, tempMaxLocal);
            Vector3D otherMaxLocal = Vector3D.Max(tempMinLocal, tempMaxLocal);

            var scaledMin = (otherMinLocal + gridSize / 2) / grid.GridSize;
            var scaledMax = (otherMaxLocal - gridSize / 2) / grid.GridSize;
            var tempMin = Vector3I.Round(scaledMin);
            var tempMax = Vector3I.Round(scaledMax);
            var min = Vector3I.Min(tempMin, tempMax);
            var max = Vector3I.Max(tempMin, tempMax);

            MyBlockOrientation? gridBlockOrientation = null;
            if (MyFakes.ENABLE_COMPOUND_BLOCKS && isStatic && grid.IsStatic && blockOrientation != null)
            {
                Matrix blockRotation;
                blockOrientation.Value.GetMatrix(out blockRotation);
                Matrix rotationInGrid = blockRotation * worldMatrix;
                rotationInGrid = rotationInGrid * invWorldMatrix;
                rotationInGrid.Translation = Vector3.Zero;

                Base6Directions.Direction forwardDir = Base6Directions.GetForward(ref rotationInGrid);
                Base6Directions.Direction upDir = Base6Directions.GetUp(ref rotationInGrid);
                if (Base6Directions.IsValidBlockOrientation(forwardDir, upDir))
                    gridBlockOrientation = new MyBlockOrientation(forwardDir, upDir);
            }

            if (!grid.CanAddCubes(min, max, gridBlockOrientation, blockDefinition))
            {
                entityOverlap = true;
                return;
            }

            if (settings.CanAnchorToStaticGrid && grid.IsTouchingAnyNeighbor(min, max))
            {
                touchingStaticGrid = true;
                if (touchingGrid == null)
                    touchingGrid = grid;
            }
        }