Exemplo n.º 1
0
    // Start is called before the first frame update
    void Start()
    {
        unitCirclePoint    = 3f * Mathf.PI / 2f;
        transform.position = new Vector3(Mathf.Cos(unitCirclePoint), 0f, Mathf.Sin(unitCirclePoint)) * radius;
        // transform.position = new Vector3(Mathf.Cos(unitCirclePoint)+1f, 0f, Mathf.Sin(unitCirclePoint))*.5f;



        myTracing = GetComponentInChildren <RayTracing>();
        rotation  = new Quaternion();
        //speed = 0;
    }
Exemplo n.º 2
0
        private IImageFunction getImageFunction(IRayScene sc, int width, int height)
        {
            IImageFunction imf = FormSupport.getImageFunction(sc, TextParam.Text);

            imf.Width  = width;
            imf.Height = height;

            RayTracing rt = imf as RayTracing;

            if (rt != null)
            {
                rt.DoShadows     = checkShadows.Checked;
                rt.DoReflections = checkReflections.Checked;
                rt.DoRefractions = checkRefractions.Checked;
            }

            return(imf);
        }
Exemplo n.º 3
0
    /// <summary>
    /// 模拟天空直射
    /// </summary>
    /// <param name="SolarDayAngle_Rad">太阳日角</param>
    /// <param name="SolarAltitude_Deg">太阳高度角</param>
    /// <param name="SolarAzimuth_Deg">太阳方位角</param>
    /// <param name="StartHour">开始的时间</param>
    /// <param name="EndHour">结束时间</param>
    /// <param name="_Octree">八叉树</param>
    /// <param name="d">光线之间的间隔,即光线密度</param>
    public static void DirectionLightSimulation(double SolarDayAngle_Rad, double SolarAltitude_Deg, double SolarAzimuth_Deg, double StartHour, double EndHour, Octree _Octree, float d)
    {
        Light[] Lights = DirectLights(SolarDayAngle_Rad, SolarAltitude_Deg, SolarAzimuth_Deg, _Octree, d);  //获取所有直射光线

        foreach (Light _Light in Lights)
        {
            Triangle HitTriangle;
            if (!RayTracing.CollisionDection(_Light, _Octree, out HitTriangle))
            {
                continue;                                                                   //如果该光线没有与三角面片相交,则遍历下一条光线
            }
            if (HitTriangle.Type != OrganType.Leaf)
            {
                continue;                                           //如果相交三角面片不为叶子,则遍历下一条光线
            }
            LeafIndex _LeafIndex = HitTriangle.Index as LeafIndex;
            _LeafIndex.DirectionEnergy += _Light.Energy;            //累加叶子的直射能量
        }
    }
Exemplo n.º 4
0
        private static ScreenBuffer DirectLighting(AABB view, Layer layer, Light[] lights, AABB[] obstacles)
        {
            var data  = SpriteCache.Get(layer.DataMapId);
            var color = SpriteCache.Get(layer.ColorMapId);

            var viewMin  = view.Min.Round();
            var viewSize = view.Size.Round();

            var sdl2Renderer = Renderer.Instance as SDL2Renderer;
            var result       = sdl2Renderer.CreateScreenBuffer(BufferEnum.DirectIllumination, viewSize);

            //for (var i = 0; i < result.Data.Length / 4; i++)
            Parallel.For(0, result.Data.Length / 4, (i, loopState) =>
            {
                var screen = new Vector2i(i % viewSize.X, i / viewSize.X);
                var wIndex = (viewMin.Y + screen.Y) * data.Size.X + viewMin.X + screen.X;
                var sIndex = ((viewSize.Y - screen.Y - 1) * viewSize.X + screen.X) * 4;

                if ((data.Data[wIndex] & Level.ShadeMask) == Level.ShadeMask)
                {
                    var tmpColor = new Color4f(color.Data[wIndex]);
                    if (tmpColor.A > 0)
                    {
                        tmpColor += RayTracing.DirectLight(screen + viewMin, lights, obstacles) * 0.5;
                    }

                    var finalColor          = new Color4b(tmpColor);
                    result.Data[sIndex + 0] = finalColor.A;
                    result.Data[sIndex + 1] = finalColor.B;
                    result.Data[sIndex + 2] = finalColor.G;
                    result.Data[sIndex + 3] = finalColor.R;
                }
                else
                {
                    // just alpha channel
                    result.Data[sIndex] = 0;
                }
            }
                         );

            return(result);
        }
Exemplo n.º 5
0
        public Result rayTracing()
        {
            Result rt = this.validateCell();

            if (!rt.ok)
            {
                return(rt);
            }

            CellInfo cellInfo = new CellInfo(this.cellName, 0, 0);

            RayTracing interAnalysis = new RayTracing(cellInfo, 4, 2, false);
            string     msg           = interAnalysis.SingleRayAnalysis(this.direction, this.inclination);

            if (msg == "")
            {
                return(new Result(false, "射线到达建筑物顶面或经过若干次反射超出地图范围以致无法到达地面"));
            }
            return(new Result(true, msg));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Shoots single primary ray only..
        /// </summary>
        /// <param name="x">X-coordinate inside the raster image.</param>
        /// <param name="y">Y-coordinate inside the raster image.</param>
        private void singleSample(int x, int y)
        {
            if (imf == null)
            {
                imf  = FormSupport.getImageFunction(FormSupport.getScene());
                rend = null;
            }

            // determine output image size:
            int width = ImageWidth;

            if (width <= 0)
            {
                width = panel1.Width;
            }
            int height = ImageHeight;

            if (height <= 0)
            {
                height = panel1.Height;
            }
            imf.Width  = width;
            imf.Height = height;

            RayTracing rt = imf as RayTracing;

            if (rt != null)
            {
                rt.DoShadows     = checkShadows.Checked;
                rt.DoReflections = checkReflections.Checked;
                rt.DoRefractions = checkRefractions.Checked;
            }

            double[] color = new double[3];
            long     hash  = imf.GetSample(x + 0.5, y + 0.5, color);

            labelSample.Text = string.Format(CultureInfo.InvariantCulture, "Sample at [{0},{1}] = [{2:f},{3:f},{4:f}], {5:X}",
                                             x, y, color[0], color[1], color[2], hash);
        }
 internal Simulator(FileInfo CPFile, FileInfo RTFile, FileInfo constant, bool disableTrace,
                    int CPMemorySize, int RTMemorySize, FileInfo triangleFile,
                    FileInfo CPOutputFile, FileInfo RTOutputFile)
 {
     CPMem = new Memory(CPMemorySize);
     // traingleMem = new Memory(triangleFile == null ? 0 : (int)triangleFile.Length);
     // if (traingleMem != null)
     //     traingleMem.LoadToMemory(triangleFile.OpenRead());
     if (RTFile != null)
     {
         RTMem = new Memory((int)RTFile.Length);
         RTMem.LoadToMemory(RTFile.OpenRead());
     }
     if (CPFile != null)
     {
         CPMem.LoadToMemory(CPFile.OpenRead());
     }
     CP = new CommandProcessor(CPMem);
     RT = new RayTracing(RTMem, triangleFile, new Memory(RTMemorySize, (int)constant.Length), constant.OpenRead());
     this.disableTrace = disableTrace;
     this.RTOutputFile = RTOutputFile?.CreateText() ?? Console.Out;
     this.CPOutputFile = CPOutputFile?.CreateText() ?? Console.Out;
 }
Exemplo n.º 8
0
        public Result rayTracing()
        {
            Result rt = this.validateCell();

            if (!rt.ok)
            {
                return(rt);
            }

            CellInfo cellInfo = new CellInfo(this.cellName, 0, 0);

            LTE.Geometric.Point p = new Geometric.Point(this.longitude, this.latitude, 0);
            p = LTE.Utils.PointConvertByProj.Instance.GetGeoPoint(p);

            RayTracing interAnalysis = new RayTracing(cellInfo, 3, 2, false);
            string     msg           = interAnalysis.SingleRayAnalysis(p.X, p.Y, p.Z);

            if (msg == "")
            {
                return(new Result(false, "射线到达建筑物顶面或经过若干次反射超出地图范围以致无法到达地面"));
            }
            return(new Result(true, msg));
        }
Exemplo n.º 9
0
        // 根据强弱相间点对栅格打分
        private Result strongWeakLoc()
        {
            Pt.Sort(new StrongWeakPtCmp());

            // 加载加速结构
            RayTracing inter = new RayTracing();

            inter.SinglePrepare(leftBound, rightBound, downBound, upBound);

            // 从每个点发出360*360条射线,经过的格子分值加1,分数最高的格子为可能位置
            Dictionary <string, int> dic = new Dictionary <string, int>();

            for (int i = 0; i < Pt.Count && i < 20; i++)
            {
                for (int j = 0; j < 360; j++)
                {
                    for (int h = 3; h < 100; h += 3)
                    {
                        double angle = (double)(j) / 180.0 * Math.PI;
                        LTE.Geometric.Point    endPoint = new Geometric.Point(Pt[i].strong.X + Math.Cos(angle), Pt[i].strong.Y + Math.Sin(angle), (double)h);
                        LTE.Geometric.Vector3D dir      = LTE.Geometric.Vector3D.constructVector(Pt[i].strong, endPoint);
                        inter.SingleRayJudge(Pt[i].strong, dir, ref dic, true);

                        LTE.Geometric.Point    endPoint1 = new Geometric.Point(Pt[i].weak.X + Math.Cos(angle), Pt[i].weak.Y + Math.Sin(angle), (double)h);
                        LTE.Geometric.Vector3D dir1      = LTE.Geometric.Vector3D.constructVector(Pt[i].weak, endPoint1);
                        inter.SingleRayJudge(Pt[i].weak, dir1, ref dic, false);
                    }
                }
            }

            if (dic.Count == 0)
            {
                return(new Result(false, "强弱点对未推出合理结果"));
            }

            // 挑选分数最高的格子
            var maxKey    = (from d in dic orderby d.Value descending select d.Key).First();
            var allMaxKey = (from d in dic where d.Value == dic[maxKey] select d.Key).ToArray();

            int    cnt = 0;
            double X = 0, Y = 0, Z = 0;

            if (allMaxKey.Count() > 5)
            {
                foreach (string a in allMaxKey)  // 有多个分数最高的格子
                {
                    string[] num = a.Split(',');
                    GridHelper.getInstance().AccGridToXYZ(Convert.ToInt32(num[0]), Convert.ToInt32(num[1]), Convert.ToInt32(num[2]), ref X, ref Y, ref Z);

                    Pt3D pt = new Pt3D(X, Y, Z);
                    if (inBound(ref pt))
                    {
                        StrongWeakResult.Add(pt);
                    }
                }
            }
            else
            {
                var dicSort = from objDic in dic orderby objDic.Value descending select objDic;
                foreach (KeyValuePair <string, int> kvp in dicSort)
                {
                    string[] num = kvp.Key.Split(',');
                    GridHelper.getInstance().AccGridToXYZ(Convert.ToInt32(num[0]), Convert.ToInt32(num[1]), 0, ref X, ref Y, ref Z);

                    ++cnt;

                    Pt3D pt = new Pt3D(X, Y, Z);
                    if (inBound(ref pt))
                    {
                        StrongWeakResult.Add(pt);
                    }

                    if (cnt > 5)
                    {
                        break;
                    }
                }
            }
            return(new Result(true));
        }
Exemplo n.º 10
0
        /// <summary>
        /// [Re]-renders the whole image (in separate thread).
        /// </summary>
        private void RenderImage()
        {
            Cursor.Current = Cursors.WaitCursor;

            // determine output image size:
            int width = ImageWidth;

            if (width <= 0)
            {
                width = panel1.Width;
            }
            int height = ImageHeight;

            if (height <= 0)
            {
                height = panel1.Height;
            }

            Bitmap newImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            if (imf == null)
            {
                imf  = FormSupport.getImageFunction(FormSupport.getScene());
                rend = null;
            }
            imf.Width  = width;
            imf.Height = height;
            RayTracing rt = imf as RayTracing;

            if (rt != null)
            {
                rt.DoShadows     = checkShadows.Checked;
                rt.DoReflections = checkReflections.Checked;
                rt.DoRefractions = checkRefractions.Checked;
            }

            if (rend == null)
            {
                rend = FormSupport.getRenderer(imf);
            }
            rend.Width        = width;
            rend.Height       = height;
            rend.Adaptive     = 8;
            rend.ProgressData = progress;

            SupersamplingImageSynthesizer ss = rend as SupersamplingImageSynthesizer;

            if (ss != null)
            {
                ss.Supersampling = (int)numericSupersampling.Value;
                ss.Jittering     = checkJitter.Checked ? 1.0 : 0.0;
            }
            progress.SyncInterval = ((width * (long)height) > (2L << 20)) ? 30000L : 10000L;
            progress.Reset();
            CSGInnerNode.ResetStatistics();

            lock ( sw )
            {
                sw.Reset();
                sw.Start();
            }

            if (checkMultithreading.Checked && Environment.ProcessorCount > 1)
            {
                Thread[] pool = new Thread[Environment.ProcessorCount];
                int      t;
                for (t = 0; t < pool.Length; t++)
                {
                    pool[t] = new Thread(new ParameterizedThreadStart(this.RenderWorker));
                }
                for (t = pool.Length; --t >= 0;)
                {
                    pool[t].Start(new WorkerThreadInit(newImage, width, height, t, pool.Length));
                }

                for (t = 0; t < pool.Length; t++)
                {
                    pool[t].Join();
                    pool[t] = null;
                }
            }
            else
            {
                MT.InitThreadData();
                rend.RenderRectangle(newImage, 0, 0, width, height);
            }

            long elapsed;

            lock ( sw )
            {
                sw.Stop();
                elapsed = sw.ElapsedMilliseconds;
            }

            string msg = string.Format(CultureInfo.InvariantCulture,
                                       "{0:f1}s  [ {1}x{2}, f{3:#,#}, mt{4}, r{5:#,#}k, i{6:#,#}k, bb{7:#,#}k, t{8:#,#}k ]",
                                       1.0e-3 * elapsed, width, height, CSGInnerNode.countFaces,
                                       checkMultithreading.Checked ? Environment.ProcessorCount : 1,
                                       (Intersection.countRays + 500L) / 1000L,
                                       (Intersection.countIntersections + 500L) / 1000L,
                                       (CSGInnerNode.countBoundingBoxes + 500L) / 1000L,
                                       (CSGInnerNode.countTriangles + 500L) / 1000L);

            SetText(msg);
            Console.WriteLine("Rendering finished: " + msg);
            SetImage(newImage);

            Cursor.Current = Cursors.Default;

            StopRendering();
        }
Exemplo n.º 11
0
        private static ScreenBuffer AmbientOcclusion(AABB view, Layer layer, Light[] lights, AABB[] obstacles, int pixelSize)
        {
            var data  = SpriteCache.Get(layer.DataMapId);
            var color = SpriteCache.Get(layer.ColorMapId);

            var viewMin  = view.Min.Round();
            var viewSize = view.Size.Round();

            var sdl2Renderer = Renderer.Instance as SDL2Renderer;

            // ambient occlusion
            var aoSize         = viewSize / pixelSize;
            var aoBuffer       = sdl2Renderer.CreateScreenBuffer(BufferEnum.AmbientOcclusion1, aoSize);
            var aoBufferLength = aoBuffer.Data.Length / 4;

            Parallel.For(0, aoBufferLength, (i, loopState) =>
            {
                var aoPixel     = new Vector2i(i % aoSize.X, i / aoSize.X);
                var aoIndex     = ((aoSize.Y - aoPixel.Y - 1) * aoSize.X + aoPixel.X) * 4;
                var screenPixel = aoPixel * pixelSize + new Vector2i(pixelSize / 2);

                var pixelColor = RayTracing.AmbientOcclusion(viewMin + screenPixel, obstacles,
                                                             probeDistance: 128, rayCount: 4);

                var finalColor             = new Color4b(pixelColor);
                aoBuffer.Data[aoIndex + 0] = finalColor.A;
                aoBuffer.Data[aoIndex + 1] = finalColor.B;
                aoBuffer.Data[aoIndex + 2] = finalColor.G;
                aoBuffer.Data[aoIndex + 3] = finalColor.R;
            });

            // ambient occlusion & color
            var viewBuffer       = sdl2Renderer.CreateScreenBuffer(BufferEnum.AmbientOcclusion1, viewSize);
            var viewBufferLength = viewBuffer.Data.Length / 4;

            Parallel.For(0, viewBufferLength, (i, loopState) =>
            {
                var viewPixel = new Vector2i(i % viewSize.X, i / viewSize.X);
                var viewIndex = ((viewSize.Y - viewPixel.Y - 1) * viewSize.X + viewPixel.X) * 4;

                var colorIndex = (viewMin.Y + viewPixel.Y) * data.Size.X + viewMin.X + viewPixel.X;
                if (color.Data[colorIndex + 0] > 0)
                {
                    var aoPixel = viewPixel / pixelSize;
                    var aoIndex = ((aoSize.Y - aoPixel.Y - 1) * aoSize.X + aoPixel.X) * 4;

                    var pixelColor = new Color4f(color.Data[colorIndex]);
                    var aoColor    = new Color4f(new Color4b(
                                                     aoBuffer.Data[aoIndex + 3],
                                                     aoBuffer.Data[aoIndex + 2],
                                                     aoBuffer.Data[aoIndex + 1],
                                                     aoBuffer.Data[aoIndex + 0]).AsUint);

                    var finalColor = new Color4b(pixelColor * aoColor);
                    viewBuffer.Data[viewIndex + 0] = finalColor.A;
                    viewBuffer.Data[viewIndex + 1] = finalColor.B;
                    viewBuffer.Data[viewIndex + 2] = finalColor.G;
                    viewBuffer.Data[viewIndex + 3] = finalColor.R;
                }
                else
                {
                    // just alpha channel
                    viewBuffer.Data[viewIndex] = 0;
                }
            });

            return(viewBuffer);
        }