Exemplo n.º 1
0
        public void IRenderableMaze(Network network, PictureBox renderBox)
        {
            if (bmp != null)
            {
                bmp.Dispose();
            }

            this.network  = network;
            colBackground = backgroundPanel.BackColor;
            colCorridor   = corridorPanel.BackColor;
            colWall       = wallPanel.BackColor;

            if (network is ShapeNetwork)
            {
                BoxF bb = ((ShapeNetwork)network).BoundingBox;

                bmp = new Bitmap(1 + (/*(int)corridorWidth.Value **/ (int)nodeSize.Value * ((int)(Math.Ceiling(bb.Width)))),
                                 1 + (/*(int)corridorWidth.Value **/ (int)nodeSize.Value * ((int)(Math.Ceiling(bb.Height)))));

                DrawMaze(Graphics.FromImage(bmp));

                renderBox.Image = bmp;
            }
            else
            {
                MessageBox.Show("CurveRenderer does not support \"" + network.GetType().ToString() + "\" network.");
            }
        }
Exemplo n.º 2
0
        public bool Intersects(ref BoxF box)
        {
            Matrix3F transposedAxis;

            box.Axis.GetTranspose(out transposedAxis);

            //Vec3 diff = origin - box.center;
            Vector3F diff;

            Vector3F.Subtract(ref Origin, ref box.Center, out diff);

            //Vec3 localSpherePosition = inverseBoxAxis * diff;
            Vector3F localSpherePosition;

            Matrix3F.Multiply(ref transposedAxis, ref diff, out localSpherePosition);

            float distanceSquared = 0;

            for (int n = 0; n < 3; n++)
            {
                if (localSpherePosition[n] > box.Extents[n])
                {
                    float z = localSpherePosition[n] - box.Extents[n];
                    distanceSquared += z * z;
                }
                else if (localSpherePosition[n] < -box.Extents[n])
                {
                    float z = -box.Extents[n] - localSpherePosition[n];
                    distanceSquared += z * z;
                }
            }

            return(distanceSquared <= Radius * Radius);
        }
Exemplo n.º 3
0
        public unsafe void IntersectsFuzz(int count)
        {
            var boxes = new BoxF[count];

            var rng = RandomNumberGenerator.Create();

            fixed(BoxF *pBoxes = &boxes[0])
            rng.GetBytes(new Span <byte>(pBoxes, count * sizeof(BoxF)));

            for (var i = 0; i < count; ++i)
            {
                boxes[i].Normalize();
            }

            for (var i = 0; i < count; ++i)
            {
                for (var j = 0; j < count; ++j)
                {
                    Assert.AreEqual(
                        BoxF.IsIntersectingNaive(boxes[i], boxes[j]),
                        BoxF.IsIntersectingSse(boxes[i], boxes[j]),
                        $"Mismatch {boxes[i]} vs. {boxes[j]}"
                        );
                }
            }
        }
Exemplo n.º 4
0
        protected void RenderShapeNetwork(Network network)
        {
            BoxF bb = ((ShapeNetwork)network).BoundingBox;

            bmp = new Bitmap(1 + ((int)scaleCtrl.Value * ((int)(Math.Ceiling(bb.Width)))),
                             1 + ((int)scaleCtrl.Value * ((int)(Math.Ceiling(bb.Height)))));

            DrawMaze(network, Graphics.FromImage(bmp));
        }
Exemplo n.º 5
0
        public void TestBoxFCenter()
        {
            // ARRANGE.
            var box    = new BoxF(new Vector3F(2, 3, 4), new Vector3F(6, 8, 10));
            var center = new Vector3F(5, 7, 9);

            // ASSERT.
            Assert.AreEqual(center, box.Center);
        }
Exemplo n.º 6
0
        public void TestBoxIntersectsBox()
        {
            // ARRANGE.
            var first = new BoxF(new Vector3F(0.0f, 0.0f, 0.0f), new Vector3F(2.0f, 2.0f, 2.0f));
            var second = new BoxF(new Vector3F(1.0f, 1.0f, 1.0f), new Vector3F(2.0f, 2.0f, 2.0f));

            // ASSERT.
            Assert.IsTrue(first.Intersects(second));
        }
Exemplo n.º 7
0
        public void TestBoxIntersectsBox()
        {
            // ARRANGE.
            var first  = new BoxF(new Vector3F(0.0f, 0.0f, 0.0f), new Vector3F(2.0f, 2.0f, 2.0f));
            var second = new BoxF(new Vector3F(1.0f, 1.0f, 1.0f), new Vector3F(2.0f, 2.0f, 2.0f));

            // ASSERT.
            Assert.IsTrue(first.Intersects(second));
        }
Exemplo n.º 8
0
        void RenderShapeNetwork(ShapeNetwork network, PictureBox renderBox)
        {
            BoxF bb = network.BoundingBox;

            bmp = new Bitmap(1 + ((int)corridorWidth.Value * ((int)(Math.Ceiling(bb.Width)))),
                             1 + ((int)corridorWidth.Value * ((int)(Math.Ceiling(bb.Height)))));

            DrawMaze(network, Graphics.FromImage(bmp));
            renderBox.Image = bmp;
        }
Exemplo n.º 9
0
        public static void FindFacesAndGroups(Map map, out List <LMFace> faces, out List <LightmapGroup> lmGroups)
        {
            faces    = new List <LMFace>();
            lmGroups = new List <LightmapGroup>();
            foreach (Solid solid in map.WorldSpawn.Find(x => x is Solid).OfType <Solid>())
            {
                foreach (Face tface in solid.Faces)
                {
                    tface.Vertices.ForEach(v => { v.LMU = -500.0f; v.LMV = -500.0f; });
                    tface.UpdateBoundingBox();
                    if (tface.Texture?.Texture == null)
                    {
                        continue;
                    }
                    if (tface.Texture.Name.ToLowerInvariant() == "tooltextures/invisible_collision")
                    {
                        continue;
                    }
                    if (tface.Texture.Name.ToLowerInvariant() == "tooltextures/remove_face")
                    {
                        continue;
                    }
                    if (tface.Texture.Name.ToLowerInvariant() == "tooltextures/block_light")
                    {
                        continue;
                    }
                    if (tface.Texture.Texture.HasTransparency())
                    {
                        continue;
                    }
                    LMFace        face    = new LMFace(tface, solid);
                    LightmapGroup group   = LightmapGroup.FindCoplanar(lmGroups, face);
                    BoxF          faceBox = new BoxF(face.BoundingBox.Start - new CoordinateF(3.0f, 3.0f, 3.0f), face.BoundingBox.End + new CoordinateF(3.0f, 3.0f, 3.0f));
                    if (group == null)
                    {
                        group             = new LightmapGroup();
                        group.BoundingBox = faceBox;
                        group.Faces       = new List <LMFace>();
                        group.Plane       = new PlaneF(face.Plane.Normal, face.Vertices[0].Location);
                        lmGroups.Add(group);
                    }
#if DEBUG
                    if (face.Texture.ToLowerInvariant() == "tooltextures/debug_breakpoint")
                    {
                        group.DebugBreakpoint = true;
                    }
#endif
                    group.Faces.Add(face);
                    group.Plane       = new PlaneF(group.Plane.Normal, (face.Vertices[0].Location + group.Plane.PointOnPlane) / 2);
                    group.BoundingBox = new BoxF(new BoxF[] { group.BoundingBox, faceBox });
                }
            }
        }
Exemplo n.º 10
0
 public void UnionedSanity()
 {
     Assert.AreEqual(Boxes1[1], (BoxF)BoxF.UnionedNaive(Boxes1[1], Boxes1[0]));
     Assert.AreEqual(Boxes2[0], (BoxF)BoxF.UnionedNaive(Boxes2[0], Boxes2[1]));
     Assert.AreEqual(Boxes1[1], (BoxF)BoxF.UnionedNaive(Boxes1[0], Boxes1[1]));
     Assert.AreEqual(Boxes2[0], (BoxF)BoxF.UnionedNaive(Boxes2[1], Boxes2[0]));
     for (var i = 0; i < 4; ++i)
     {
         Assert.AreEqual(Boxes2[0], (BoxF)BoxF.UnionedNaive(Boxes2[0], Boxes2[3 + i]));
         Assert.AreEqual(Boxes2[0], (BoxF)BoxF.UnionedNaive(Boxes2[3 + i], Boxes2[0]));
     }
 }
Exemplo n.º 11
0
 public bool Contains(ref BoxF box)
 {
     Vector3F[] points = null;
     box.ToPoints(ref points);
     foreach (Vector3F point in points)
     {
         if (!Contains(point))
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 12
0
        public void ContainsSanity()
        {
            Assert.True(BoxF.ContainsRectNaive(Boxes1[1], Boxes1[0]));
            Assert.True(BoxF.ContainsRectNaive(Boxes2[0], Boxes2[1]));
            Assert.False(BoxF.ContainsRectNaive(Boxes1[0], Boxes1[1]));
            Assert.False(BoxF.ContainsRectNaive(Boxes2[1], Boxes2[0]));
            for (var i = 0; i < 4; ++i)
            {
                Assert.True(BoxF.ContainsRectNaive(Boxes2[0], Boxes2[3 + i]));
                Assert.False(BoxF.ContainsRectNaive(Boxes2[3 + i], Boxes2[0]));
            }

            for (var i = 0; i < 4; ++i)
            {
                Assert.False(BoxF.ContainsRectNaive(Boxes2[1], Boxes2[3 + i]));
                Assert.False(BoxF.ContainsRectNaive(Boxes2[2], Boxes2[3 + i]));
            }
        }
Exemplo n.º 13
0
        public void IntersectsSanity()
        {
            Assert.True(BoxF.IsIntersectingNaive(Boxes1[1], Boxes1[0]));
            Assert.True(BoxF.IsIntersectingNaive(Boxes2[0], Boxes2[1]));
            Assert.True(BoxF.IsIntersectingNaive(Boxes1[0], Boxes1[1]));
            Assert.True(BoxF.IsIntersectingNaive(Boxes2[1], Boxes2[0]));
            for (var i = 0; i < 4; ++i)
            {
                Assert.True(BoxF.IsIntersectingNaive(Boxes2[0], Boxes2[3 + i]));
                Assert.True(BoxF.IsIntersectingNaive(Boxes2[3 + i], Boxes2[0]));
            }

            for (var i = 0; i < 4; ++i)
            {
                Assert.False(BoxF.IsIntersectingNaive(Boxes2[1], Boxes2[3 + i]));
                Assert.False(BoxF.IsIntersectingNaive(Boxes2[2], Boxes2[3 + i]));
            }
        }
Exemplo n.º 14
0
 public static LightmapGroup FindCoplanar(List <LightmapGroup> lmGroups, LMFace otherFace)
 {
     foreach (LightmapGroup group in lmGroups)
     {
         if ((group.Plane.Normal - otherFace.Plane.Normal).LengthSquared() < 0.01f)
         {
             PlaneF plane2 = new PlaneF(otherFace.Plane.Normal, otherFace.Vertices[0].Location);
             if (Math.Abs(plane2.EvalAtPoint((group.Plane.PointOnPlane))) > 4.0f)
             {
                 continue;
             }
             BoxF faceBox = new BoxF(otherFace.BoundingBox.Start - new CoordinateF(3.0f, 3.0f, 3.0f), otherFace.BoundingBox.End + new CoordinateF(3.0f, 3.0f, 3.0f));
             if (faceBox.IntersectsWith(group.BoundingBox))
             {
                 return(group);
             }
         }
     }
     return(null);
 }
Exemplo n.º 15
0
        public unsafe void UnionedFuzz(int count)
        {
            var boxes = new BoxF[count];

            var rng = RandomNumberGenerator.Create();

            fixed(BoxF *pBoxes = &boxes[0])
            rng.GetBytes(new Span <byte>(pBoxes, count * sizeof(BoxF)));

            for (var i = 0; i < count; ++i)
            {
                ref var box = ref boxes[i];
                box.Normalize();
                box = new BoxF(
                    float.IsNaN(box.X1) ? 0 : box.X1,
                    float.IsNaN(box.Y1) ? 0 : box.Y1,
                    float.IsNaN(box.X2) ? 0 : box.X2,
                    float.IsNaN(box.Y2) ? 0 : box.Y2
                    );
            }
Exemplo n.º 16
0
        protected static CoordinateF GetIntersectionPoint(LMFace face, LineF line, bool ignoreDirection = false)
        {
            var plane     = face.Plane;
            var intersect = plane.GetIntersectionPoint(line, ignoreDirection);
            List <CoordinateF> coordinates = face.Vertices.Select(x => x.Location).ToList();

            if (intersect == null)
            {
                return(null);
            }
            BoxF bbox = new BoxF(face.BoundingBox.Start - new CoordinateF(0.5f, 0.5f, 0.5f), face.BoundingBox.End + new CoordinateF(0.5f, 0.5f, 0.5f));

            if (!bbox.CoordinateIsInside(intersect))
            {
                return(null);
            }

            CoordinateF centerPoint = face.BoundingBox.Center;

            for (var i = 0; i < coordinates.Count; i++)
            {
                var i1 = i;
                var i2 = (i + 1) % coordinates.Count;

                var lineMiddle     = (coordinates[i1] + coordinates[i2]) * 0.5f;
                var middleToCenter = centerPoint - lineMiddle;
                var v          = coordinates[i1] - coordinates[i2];
                var lineNormal = face.Plane.Normal.Cross(v);

                if ((middleToCenter - lineNormal).LengthSquared() > (middleToCenter + lineNormal).LengthSquared())
                {
                    lineNormal = -lineNormal;
                }

                if (lineNormal.Dot(intersect - lineMiddle) < 0.0f)
                {
                    return(null);
                }
            }
            return(intersect);
        }
Exemplo n.º 17
0
 public static void FindFacesAndGroups(Map map, out List <LMFace> faces, out List <LightmapGroup> lmGroups)
 {
     faces    = new List <LMFace>();
     lmGroups = new List <LightmapGroup>();
     foreach (Solid solid in map.WorldSpawn.Find(x => x is Solid).OfType <Solid>())
     {
         foreach (Face tface in solid.Faces)
         {
             tface.Vertices.ForEach(v => { v.LMU = -500.0f; v.LMV = -500.0f; });
             tface.UpdateBoundingBox();
             if (tface.Texture?.Texture == null)
             {
                 continue;
             }
             if (tface.Texture.IsToolTexture)
             {
                 continue;
             }
             if (tface.Texture.Texture.HasTransparency())
             {
                 continue;
             }
             LMFace        face    = new LMFace(tface, solid);
             LightmapGroup group   = LightmapGroup.FindCoplanar(lmGroups, face);
             BoxF          faceBox = new BoxF(face.BoundingBox.Start - new CoordinateF(3.0f, 3.0f, 3.0f), face.BoundingBox.End + new CoordinateF(3.0f, 3.0f, 3.0f));
             if (group == null)
             {
                 group             = new LightmapGroup();
                 group.BoundingBox = faceBox;
                 group.Faces       = new List <LMFace>();
                 group.Plane       = new PlaneF(face.Plane.Normal, face.Vertices[0].Location);
                 lmGroups.Add(group);
             }
             group.Faces.Add(face);
             group.Plane       = new PlaneF(group.Plane.Normal, (face.Vertices[0].Location + group.Plane.PointOnPlane) / 2);
             group.BoundingBox = new BoxF(new BoxF[] { group.BoundingBox, faceBox });
         }
     }
 }
Exemplo n.º 18
0
        /// <summary>
        /// Test this face to see if the given bounding box intersects with it
        /// </summary>
        /// <param name="box">The box to test against</param>
        /// <returns>True if the box intersects</returns>
        public bool IntersectsWithBox(BoxF box)
        {
            var verts = Vertices.ToList();

            return(box.GetBoxLines().Any(x => GetIntersectionPoint(this, x, true) != null));
        }
Exemplo n.º 19
0
 /// <summary>
 /// Test all the edges of this face against a bounding box to see if they intersect.
 /// </summary>
 /// <param name="box">The box to intersect</param>
 /// <returns>True if one of the face's edges intersects with the box.</returns>
 public bool IntersectsWithLine(BoxF box)
 {
     // Shortcut through the bounding box to avoid the line computations if they aren't needed
     return(BoundingBox.IntersectsWith(box) && GetLines().Any(box.IntersectsWith));
 }
Exemplo n.º 20
0
 public virtual void UpdateBoundingBox()
 {
     BoundingBox = new BoxF(Vertices.Select(x => x.Location));
 }
Exemplo n.º 21
0
        private static void RenderLightOntoFace(Document doc, float[][] bitmaps, List <Light> lights, LightmapGroup group, LMFace targetFace, IEnumerable <LMFace> blockerFaces)
        {
            Random rand = new Random();

            int writeX = group.writeX;
            int writeY = group.writeY;

            int textureDims;

            lock (doc.TextureCollection.Lightmaps)
            {
                textureDims = doc.TextureCollection.Lightmaps[0].Width;
            }

            lights = lights.FindAll(x =>
            {
                float range   = x.Range;
                BoxF lightBox = new BoxF(x.Origin - new CoordinateF(range, range, range), x.Origin + new CoordinateF(range, range, range));
                return(lightBox.IntersectsWith(targetFace.BoundingBox));
            });

            float?minX = null; float?maxX = null;
            float?minY = null; float?maxY = null;

            foreach (CoordinateF coord in targetFace.Vertices.Select(x => x.Location))
            {
                float x = coord.Dot(group.uAxis);
                float y = coord.Dot(group.vAxis);

                if (minX == null || x < minX)
                {
                    minX = x;
                }
                if (minY == null || y < minY)
                {
                    minY = y;
                }
                if (maxX == null || x > maxX)
                {
                    maxX = x;
                }
                if (maxY == null || y > maxY)
                {
                    maxY = y;
                }
            }

            CoordinateF leewayPoint = group.Plane.PointOnPlane + (group.Plane.Normal * Math.Max(LightmapConfig.DownscaleFactor * 0.25f, 1.5f));

            minX -= LightmapConfig.DownscaleFactor; minY -= LightmapConfig.DownscaleFactor;
            maxX += LightmapConfig.DownscaleFactor; maxY += LightmapConfig.DownscaleFactor;

            minX /= LightmapConfig.DownscaleFactor; minX = (float)Math.Ceiling(minX.Value); minX *= LightmapConfig.DownscaleFactor;
            minY /= LightmapConfig.DownscaleFactor; minY = (float)Math.Ceiling(minY.Value); minY *= LightmapConfig.DownscaleFactor;
            maxX /= LightmapConfig.DownscaleFactor; maxX = (float)Math.Ceiling(maxX.Value); maxX *= LightmapConfig.DownscaleFactor;
            maxY /= LightmapConfig.DownscaleFactor; maxY = (float)Math.Ceiling(maxY.Value); maxY *= LightmapConfig.DownscaleFactor;

            foreach (LMFace.Vertex vert in targetFace.Vertices)
            {
                float x = vert.Location.Dot(group.uAxis);
                float y = vert.Location.Dot(group.vAxis);

                float u = (writeX + 0.5f + (x - group.minTotalX.Value) / LightmapConfig.DownscaleFactor);
                float v = (writeY + 0.5f + (y - group.minTotalY.Value) / LightmapConfig.DownscaleFactor);

                targetFace.LmIndex = (u >= LightmapConfig.TextureDims ? 1 : 0) + (v >= LightmapConfig.TextureDims ? 2 : 0);

                u /= (float)textureDims;
                v /= (float)textureDims;

                vert.LMU = u; vert.LMV = v;
                vert.OriginalVertex.LMU = u; vert.OriginalVertex.LMV = v;
            }

            float centerX           = (maxX.Value + minX.Value) / 2;
            float centerY           = (maxY.Value + minY.Value) / 2;

            int iterX = (int)Math.Ceiling((maxX.Value - minX.Value) / LightmapConfig.DownscaleFactor);
            int iterY = (int)Math.Ceiling((maxY.Value - minY.Value) / LightmapConfig.DownscaleFactor);

            float[][,] r = new float[4][, ];
            r[0]         = new float[iterX, iterY];
            r[1]         = new float[iterX, iterY];
            r[2]         = new float[iterX, iterY];
            r[3]         = new float[iterX, iterY];
            float[][,] g = new float[4][, ];
            g[0]         = new float[iterX, iterY];
            g[1]         = new float[iterX, iterY];
            g[2]         = new float[iterX, iterY];
            g[3]         = new float[iterX, iterY];
            float[][,] b = new float[4][, ];
            b[0]         = new float[iterX, iterY];
            b[1]         = new float[iterX, iterY];
            b[2]         = new float[iterX, iterY];
            b[3]         = new float[iterX, iterY];

            foreach (Light light in lights)
            {
                CoordinateF lightPos   = light.Origin;
                float       lightRange = light.Range;
                CoordinateF lightColor = light.Color * (1.0f / 255.0f) * light.Intensity;

                BoxF          lightBox = new BoxF(new BoxF[] { targetFace.BoundingBox, new BoxF(light.Origin - new CoordinateF(30.0f, 30.0f, 30.0f), light.Origin + new CoordinateF(30.0f, 30.0f, 30.0f)) });
                List <LMFace> applicableBlockerFaces = blockerFaces.Where(x =>
                {
                    if (x == targetFace)
                    {
                        return(false);
                    }
                    if (group.Faces.Contains(x))
                    {
                        return(false);
                    }
                    //return true;
                    if (lightBox.IntersectsWith(x.BoundingBox))
                    {
                        return(true);
                    }
                    return(false);
                }).ToList();

                bool[,] illuminated = new bool[iterX, iterY];

                for (int y = 0; y < iterY; y++)
                {
                    for (int x = 0; x < iterX; x++)
                    {
                        illuminated[x, y] = true;
                    }
                }

                for (int y = 0; y < iterY; y++)
                {
                    for (int x = 0; x < iterX; x++)
                    {
                        int tX = (int)(writeX + x + (int)(minX - group.minTotalX) / LightmapConfig.DownscaleFactor);
                        int tY = (int)(writeY + y + (int)(minY - group.minTotalY) / LightmapConfig.DownscaleFactor);

                        if (tX >= 0 && tY >= 0 && tX < textureDims && tY < textureDims)
                        {
                            int offset = (tX + tY * textureDims) * Bitmap.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8;
                            bitmaps[0][offset + 3] = 1.0f;
                            bitmaps[1][offset + 3] = 1.0f;
                            bitmaps[2][offset + 3] = 1.0f;
                            bitmaps[3][offset + 3] = 1.0f;
                        }
                    }
                }

                for (int y = 0; y < iterY; y++)
                {
                    for (int x = 0; x < iterX; x++)
                    {
                        float       ttX          = minX.Value + (x * LightmapConfig.DownscaleFactor);
                        float       ttY          = minY.Value + (y * LightmapConfig.DownscaleFactor);
                        CoordinateF pointOnPlane = (ttX - centerX) * group.uAxis + (ttY - centerY) * group.vAxis + targetFace.BoundingBox.Center;

                        /*Entity entity = new Entity(map.IDGenerator.GetNextObjectID());
                         * entity.Colour = Color.Pink;
                         * entity.Origin = new Coordinate(pointOnPlane);
                         * entity.UpdateBoundingBox();
                         * entity.SetParent(map.WorldSpawn);*/

                        int tX = (int)(writeX + x + (int)(minX - group.minTotalX) / LightmapConfig.DownscaleFactor);
                        int tY = (int)(writeY + y + (int)(minY - group.minTotalY) / LightmapConfig.DownscaleFactor);

                        CoordinateF luxelColor0    = new CoordinateF(r[0][x, y], g[0][x, y], b[0][x, y]);
                        CoordinateF luxelColor1    = new CoordinateF(r[1][x, y], g[1][x, y], b[1][x, y]);
                        CoordinateF luxelColor2    = new CoordinateF(r[2][x, y], g[2][x, y], b[2][x, y]);
                        CoordinateF luxelColorNorm = new CoordinateF(r[3][x, y], g[3][x, y], b[3][x, y]);

                        float dotToLight0    = Math.Max((lightPos - pointOnPlane).Normalise().Dot(targetFace.LightBasis0), 0.0f);
                        float dotToLight1    = Math.Max((lightPos - pointOnPlane).Normalise().Dot(targetFace.LightBasis1), 0.0f);
                        float dotToLight2    = Math.Max((lightPos - pointOnPlane).Normalise().Dot(targetFace.LightBasis2), 0.0f);
                        float dotToLightNorm = Math.Max((lightPos - pointOnPlane).Normalise().Dot(targetFace.Normal), 0.0f);

                        if (illuminated[x, y] && (pointOnPlane - lightPos).LengthSquared() < lightRange * lightRange)
                        {
#if TRUE
                            LineF lineTester = new LineF(lightPos, pointOnPlane);
                            for (int i = 0; i < applicableBlockerFaces.Count; i++)
                            {
                                LMFace      otherFace = applicableBlockerFaces[i];
                                CoordinateF hit       = otherFace.GetIntersectionPoint(lineTester);
                                if (hit != null && ((hit - leewayPoint).Dot(group.Plane.Normal) > 0.0f || (hit - pointOnPlane).LengthSquared() > LightmapConfig.DownscaleFactor * 2f))
                                {
                                    applicableBlockerFaces.RemoveAt(i);
                                    applicableBlockerFaces.Insert(0, otherFace);
                                    illuminated[x, y] = false;
                                    i++;
                                    break;
                                }
                            }
#endif
                        }
                        else
                        {
                            illuminated[x, y] = false;
                        }

                        if (illuminated[x, y])
                        {
                            float brightness = (lightRange - (pointOnPlane - lightPos).VectorMagnitude()) / lightRange;

                            if (light.Direction != null)
                            {
                                float directionDot = light.Direction.Dot((pointOnPlane - lightPos).Normalise());

                                if (directionDot < light.innerCos)
                                {
                                    if (directionDot < light.outerCos)
                                    {
                                        brightness = 0.0f;
                                    }
                                    else
                                    {
                                        brightness *= (directionDot - light.outerCos.Value) / (light.innerCos.Value - light.outerCos.Value);
                                    }
                                }
                            }

                            float brightness0    = dotToLight0 * brightness * brightness;
                            float brightness1    = dotToLight1 * brightness * brightness;
                            float brightness2    = dotToLight2 * brightness * brightness;
                            float brightnessNorm = dotToLightNorm * brightness * brightness;

                            brightness0    += ((float)rand.NextDouble() - 0.5f) * 0.005f;
                            brightness1    += ((float)rand.NextDouble() - 0.5f) * 0.005f;
                            brightness2    += ((float)rand.NextDouble() - 0.5f) * 0.005f;
                            brightnessNorm += ((float)rand.NextDouble() - 0.5f) * 0.005f;

                            r[0][x, y] += lightColor.Z * brightness0; if (r[0][x, y] > 1.0f)
                            {
                                r[0][x, y] = 1.0f;
                            }
                            if (r[0][x, y] < 0)
                            {
                                r[0][x, y] = 0;
                            }
                            g[0][x, y] += lightColor.Y * brightness0; if (g[0][x, y] > 1.0f)
                            {
                                g[0][x, y] = 1.0f;
                            }
                            if (g[0][x, y] < 0)
                            {
                                g[0][x, y] = 0;
                            }
                            b[0][x, y] += lightColor.X * brightness0; if (b[0][x, y] > 1.0f)
                            {
                                b[0][x, y] = 1.0f;
                            }
                            if (b[0][x, y] < 0)
                            {
                                b[0][x, y] = 0;
                            }

                            r[1][x, y] += lightColor.Z * brightness1; if (r[1][x, y] > 1.0f)
                            {
                                r[1][x, y] = 1.0f;
                            }
                            if (r[1][x, y] < 0)
                            {
                                r[1][x, y] = 0;
                            }
                            g[1][x, y] += lightColor.Y * brightness1; if (g[1][x, y] > 1.0f)
                            {
                                g[1][x, y] = 1.0f;
                            }
                            if (g[1][x, y] < 0)
                            {
                                g[1][x, y] = 0;
                            }
                            b[1][x, y] += lightColor.X * brightness1; if (b[1][x, y] > 1.0f)
                            {
                                b[1][x, y] = 1.0f;
                            }
                            if (b[1][x, y] < 0)
                            {
                                b[1][x, y] = 0;
                            }

                            r[2][x, y] += lightColor.Z * brightness2; if (r[2][x, y] > 1.0f)
                            {
                                r[2][x, y] = 1.0f;
                            }
                            if (r[2][x, y] < 0)
                            {
                                r[2][x, y] = 0;
                            }
                            g[2][x, y] += lightColor.Y * brightness2; if (g[2][x, y] > 1.0f)
                            {
                                g[2][x, y] = 1.0f;
                            }
                            if (g[2][x, y] < 0)
                            {
                                g[2][x, y] = 0;
                            }
                            b[2][x, y] += lightColor.X * brightness2; if (b[2][x, y] > 1.0f)
                            {
                                b[2][x, y] = 1.0f;
                            }
                            if (b[2][x, y] < 0)
                            {
                                b[2][x, y] = 0;
                            }

                            r[3][x, y] += lightColor.Z * brightnessNorm; if (r[3][x, y] > 1.0f)
                            {
                                r[3][x, y] = 1.0f;
                            }
                            if (r[3][x, y] < 0)
                            {
                                r[3][x, y] = 0;
                            }
                            g[3][x, y] += lightColor.Y * brightnessNorm; if (g[3][x, y] > 1.0f)
                            {
                                g[3][x, y] = 1.0f;
                            }
                            if (g[3][x, y] < 0)
                            {
                                g[3][x, y] = 0;
                            }
                            b[3][x, y] += lightColor.X * brightnessNorm; if (b[3][x, y] > 1.0f)
                            {
                                b[3][x, y] = 1.0f;
                            }
                            if (b[3][x, y] < 0)
                            {
                                b[3][x, y] = 0;
                            }

                            luxelColor0    = new CoordinateF(r[0][x, y], g[0][x, y], b[0][x, y]);
                            luxelColor1    = new CoordinateF(r[1][x, y], g[1][x, y], b[1][x, y]);
                            luxelColor2    = new CoordinateF(r[2][x, y], g[2][x, y], b[2][x, y]);
                            luxelColorNorm = new CoordinateF(r[3][x, y], g[3][x, y], b[3][x, y]);

                            if (tX >= 0 && tY >= 0 && tX < textureDims && tY < textureDims)
                            {
                                int offset = (tX + tY * textureDims) * Bitmap.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8;
                                if (luxelColor0.X + luxelColor0.Y + luxelColor0.Z > bitmaps[0][offset + 2] + bitmaps[0][offset + 1] + bitmaps[0][offset + 0])
                                {
                                    bitmaps[0][offset + 0] = luxelColor0.X;
                                    bitmaps[0][offset + 1] = luxelColor0.Y;
                                    bitmaps[0][offset + 2] = luxelColor0.Z;
                                }
                                if (luxelColor1.X + luxelColor1.Y + luxelColor1.Z > bitmaps[1][offset + 2] + bitmaps[1][offset + 1] + bitmaps[1][offset + 0])
                                {
                                    bitmaps[1][offset + 0] = luxelColor1.X;
                                    bitmaps[1][offset + 1] = luxelColor1.Y;
                                    bitmaps[1][offset + 2] = luxelColor1.Z;
                                }
                                if (luxelColor2.X + luxelColor2.Y + luxelColor2.Z > bitmaps[2][offset + 2] + bitmaps[2][offset + 1] + bitmaps[2][offset + 0])
                                {
                                    bitmaps[2][offset + 0] = luxelColor2.X;
                                    bitmaps[2][offset + 1] = luxelColor2.Y;
                                    bitmaps[2][offset + 2] = luxelColor2.Z;
                                }
                                if (luxelColorNorm.X + luxelColorNorm.Y + luxelColorNorm.Z > bitmaps[3][offset + 2] + bitmaps[3][offset + 1] + bitmaps[3][offset + 0])
                                {
                                    bitmaps[3][offset + 0] = luxelColorNorm.X;
                                    bitmaps[3][offset + 1] = luxelColorNorm.Y;
                                    bitmaps[3][offset + 2] = luxelColorNorm.Z;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 22
0
        public unsafe void BigAddAndQueryCallback(int count)
        {
#if DEBUG
            if (count > 1000)
            {
                Assert.Inconclusive("This would take too long under debug conditions due to validation.");
            }
#endif
            var boxes = new BoxF[count];

            var rng = RandomNumberGenerator.Create();

            fixed(BoxF *pBoxes = &boxes[0])
            rng.GetBytes(new Span <byte>(pBoxes, count * sizeof(BoxF)));

            for (var i = 0; i < count; ++i)
            {
                boxes[i].Normalize();
            }

            var dt = new BoxTree <int>((in int x) => boxes[x],
                                       capacity: count, growthFunc: x => throw new NotImplementedException(), locking: true);

            Assert.Multiple(() => {
                var sw = Stopwatch.StartNew();
                for (var i = 0; i < boxes.Length; ++i)
                {
                    Assert.True(dt.Add(i), $"Add {i}");
                }
                TestContext.Out.WriteLine($"Added {count} in {sw.ElapsedMilliseconds}ms");
            });

            var point = new Vector2(0, 0);

            var containers = Enumerable.Range(0, boxes.Length)
                             .Where(x => boxes[x].Contains(point))
                             .Distinct()
                             .OrderBy(x => x).ToArray();

            var procCount = Environment.ProcessorCount;

            if (procCount == 1)
            {
                Assert.Inconclusive("Could not query in parallel.");
            }

            TestContext.WriteLine($"Testing with {procCount} parallel instances.");

            Parallel.For(0, procCount, x => {
                var results = new List <int>(containers.Length);
                var sw      = Stopwatch.StartNew();
                dt.Query((ref int v) => {
                    results.Add(v);
                    return(true);
                }, point);
                TestContext.WriteLine($"{x}: Queried {results.Count} in {sw.ElapsedMilliseconds}ms");
                results.Sort(Comparer <int> .Default);

                //Assert.Multiple(() =>
                {
                    Assert.AreEqual(containers.Length, results.Count, "Length");
                    var l = Math.Min(containers.Length, results.Count);
                    for (var i = 0; i < l; ++i)
                    {
                        Assert.AreEqual(containers[i], results[i]);
                    }
                }
                //);
                {
                    Assert.AreEqual(containers.Length, results.Count, "Length");
                    var l = Math.Min(containers.Length, results.Count);
                    for (var i = 0; i < l; ++i)
                    {
                        Assert.AreEqual(containers[i], results[i]);
                    }
                }
            });

#if DEBUG
            //var debugView = dt.DebugAllocatedNodes;
#endif
        }
Exemplo n.º 23
0
        public bool Read(BundleEntry entry, ILoader loader = null)
        {
            Clear();

            MemoryStream  ms = entry.MakeStream();
            BinaryReader2 br = new BinaryReader2(ms);

            br.BigEndian = entry.Console;

            Min      = br.ReadVector3F();
            Unknown4 = br.ReadInt32();
            Max      = br.ReadVector3F();
            Unknown8 = br.ReadInt32();
            uint chunkPointerStart = br.ReadUInt32();
            uint boxListStart      = br.ReadUInt32();
            int  chunkCount        = br.ReadInt32();

            br.ReadUInt32();             // FileSize

            List <uint> chunkPointers = new List <uint>();

            // No Data
            if (chunkCount == 0)
            {
                br.Close();
                ms.Close();
                return(true);
            }

            br.BaseStream.Position = chunkPointerStart;

            for (int i = 0; i < chunkCount; i++)
            {
                chunkPointers.Add(br.ReadUInt32());
            }

            for (int i = 0; i < chunkCount; i++)
            {
                // Read Vertically

                long pos = boxListStart + 0x70 * (i / 4) + 4 * (i % 4);

                BoxF boundingBox = new BoxF();
                br.BaseStream.Position = pos;
                float minX = br.ReadSingle();
                br.BaseStream.Position += 12;
                float minY = br.ReadSingle();
                br.BaseStream.Position += 12;
                float minZ = br.ReadSingle();

                boundingBox.Min = new Vector3(minX, minY, minZ);

                br.BaseStream.Position += 12;
                float maxX = br.ReadSingle();
                br.BaseStream.Position += 12;
                float maxY = br.ReadSingle();
                br.BaseStream.Position += 12;
                float maxZ = br.ReadSingle();
                br.BaseStream.Position += 12;

                boundingBox.Max = new Vector3(maxX, maxY, maxZ);

                PolygonSoupBoundingBox box = new PolygonSoupBoundingBox(boundingBox, br.ReadInt32());

                BoundingBoxes.Add(box);
            }

            for (int i = 0; i < chunkPointers.Count; i++)
            {
                br.BaseStream.Position = chunkPointers[i];

                Chunks.Add(PolygonSoupChunk.Read(br));
            }

            br.Close();
            ms.Close();

            return(true);
        }
Exemplo n.º 24
0
 public bool Intersects(BoxF box)
 {
     return(Intersects(ref box));
 }
Exemplo n.º 25
0
 /// <summary>
 ///   Checks whether the two specified boxes at least partially intersect each other.
 /// </summary>
 /// <param name="first">
 ///   First box to check.
 /// </param>
 /// <param name="second">
 ///   Second box to check.
 /// </param>
 /// <returns>
 ///   <c>true</c>, if the two boxes intersect each other, and <c>false</c> otherwise.
 /// </returns>
 public static bool Intersects(this BoxF first, BoxF second)
 {
     return (first.MaxX > second.X && first.X < second.MaxX)
            && (first.MaxY > second.Y && first.Y < second.MaxY)
            && (first.MaxZ > second.Z && first.Z < second.MaxZ);
 }
Exemplo n.º 26
0
 public PolygonSoupBoundingBox(BoxF box, int terminator)
 {
     Box        = box;
     Terminator = terminator;
 }
Exemplo n.º 27
0
 public bool Contains(BoxF box)
 {
     return(Contains(ref box));
 }