Пример #1
0
            public static int[] GetMineNeighbourIds(int i)
            {
                i = SanitizeCellIndex(i);
                bool cond_v, cond_h;

                int[]         output = new int[8];
                AdjacentCells edges  = GetAdjacentCells(i);
                MineInfo      mine   = Data.MineField[i];

                cond_h    = edges.HasFlag(AdjacentCells.Right);
                output[0] = cond_h ? i + 1 : -1;
                cond_v    = edges.HasFlag(AdjacentCells.Bottom);
                output[1] = cond_v && cond_h ? i + Data.MineFieldWidth + 1 : -1;
                output[2] = cond_v ? i + Data.MineFieldWidth : -1;
                cond_h    = edges.HasFlag(AdjacentCells.Left);
                output[3] = cond_v && cond_h ? i + Data.MineFieldWidth - 1 : -1;
                output[4] = cond_h ? i - 1 : -1;
                cond_v    = edges.HasFlag(AdjacentCells.Top);
                output[5] = cond_v && cond_h ? i - Data.MineFieldWidth - 1 : -1;
                output[6] = cond_v ? i - Data.MineFieldWidth : -1;
                cond_h    = edges.HasFlag(AdjacentCells.Right);
                output[7] = cond_v && cond_h ? i - Data.MineFieldWidth + 1 : -1;

                return(output);
            }
Пример #2
0
 void IGXDLMSBase.Load(GXXmlReader reader)
 {
     Operator            = reader.ReadElementContentAsString("Operator");
     Status              = (GsmStatus)reader.ReadElementContentAsInt("Status");
     CircuitSwitchStatus = (GsmCircuitSwitchStatus)reader.ReadElementContentAsInt("CircuitSwitchStatus");
     PacketSwitchStatus  = (GsmPacketSwitchStatus)reader.ReadElementContentAsInt("PacketSwitchStatus");
     if (reader.IsStartElement("CellInfo", true))
     {
         CellInfo.CellId        = (UInt16)reader.ReadElementContentAsInt("CellId");
         CellInfo.LocationId    = (UInt16)reader.ReadElementContentAsInt("LocationId");
         CellInfo.SignalQuality = (byte)reader.ReadElementContentAsInt("SignalQuality");
         CellInfo.Ber           = (byte)reader.ReadElementContentAsInt("Ber");
         reader.ReadEndElement("CellInfo");
     }
     AdjacentCells.Clear();
     if (reader.IsStartElement("AdjacentCells", true))
     {
         while (reader.IsStartElement("Item", true))
         {
             AdjacentCell it = new Objects.AdjacentCell();
             it.CellId        = (UInt32)reader.ReadElementContentAsInt("CellId");
             it.SignalQuality = (byte)reader.ReadElementContentAsInt("SignalQuality");
             AdjacentCells.Add(it);
         }
         reader.ReadEndElement("AdjacentCells");
     }
     CaptureTime = new GXDateTime(reader.ReadElementContentAsString("CaptureTime"), System.Globalization.CultureInfo.InvariantCulture);
 }
Пример #3
0
    public void Edges()
    {
        //makes list of edges for each cell and removes any that are too small

        for (int a = 0; a < cells.Count; a++)
        {
            //hold edge info in wall script in cell
            if (cells[a].GetComponent <AdjacentCells>() == null)
            {
                cells[a].AddComponent <AdjacentCells>();
            }

            AdjacentCells aJ = cells[a].GetComponent <AdjacentCells>();

            //this will figure out edges and save them on the script

            aJ.Edges();
        }

        //now we have worked out all edges, find adjacent edges
        for (int a = 0; a < cells.Count; a++)
        {
            //  cells[a].GetComponent<AdjacentCells>().FindSharedEdges(); //using?
        }
    }
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         LogicalName = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 2)
     {
         Operator = (string)e.Value;
     }
     else if (e.Index == 3)
     {
         Status = (GsmStatus)e.Value;
     }
     else if (e.Index == 4)
     {
         CircuitSwitchStatus = (GsmCircuitSwitchStatus)e.Value;
     }
     else if (e.Index == 5)
     {
         PacketSwitchStatus = (GsmPacketSwitchStatus)e.Value;
     }
     else if (e.Index == 6)
     {
         if (e.Value != null)
         {
             object[] tmp = (object[])e.Value;
             CellInfo.CellId        = (string)tmp[0];
             CellInfo.LocationId    = (ushort)tmp[1];
             CellInfo.SignalQuality = (byte)tmp[2];
             CellInfo.Ber           = (byte)tmp[3];
         }
     }
     else if (e.Index == 7)
     {
         AdjacentCells.Clear();
         if (e.Value != null)
         {
             foreach (object it in (object[])e.Value)
             {
                 object[]     tmp = (object[])it;
                 AdjacentCell ac  = new Objects.AdjacentCell();
                 ac.CellId        = (string)tmp[0];
                 ac.SignalQuality = (byte)tmp[1];
                 AdjacentCells.Add(ac);
             }
         }
     }
     else if (e.Index == 8)
     {
         CaptureTime = (GXDateTime)e.Value;
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
Пример #5
0
    public static void CalculateAdjacents(List <GameObject> cells, GameObject cell, float tolerance)
    {
        //work out which cells are adjacent tocell, save in a list

        List <GameObject> adjacents = new List <GameObject>();

        Vector3[] thisVertices = cell.GetComponent <MeshFilter>().mesh.vertices;
        for (int j = 0; j < cells.Count; j++)
        {
            //don't check own cell
            if (cell == cells[j])
            {
                continue;
            }

            Vector3[] otherVertices = cells[j].GetComponent <MeshFilter>().mesh.vertices;
            int       matches       = 0;

            for (int a = 0; a < thisVertices.Length; a++)
            {
                for (int b = 0; b < otherVertices.Length; b++)
                {
                    //if we have a match, add "other" cell to a list of adjacents for this cell
                    if (Vector3.Distance(thisVertices[a], otherVertices[b]) <= tolerance) //opt0- think this is ok as ==
                    {
                        //adjacents.Add(cells[j]); //making so we need two points for an adjacent cell

                        //force out of the loops
                        //a = thisVertices.Length;


                        matches++;
                    }
                }
            }

            if (matches > 1)//means if cell mathces one ponton a corner, we ignore. it has to be a solid edge
            {
                adjacents.Add(cells[j]);
            }
        }

        AdjacentCells aJ = null;

        if (cell.GetComponent <AdjacentCells>() == null)
        {
            aJ = cell.AddComponent <AdjacentCells>();
        }
        else
        {
            aJ = cell.GetComponent <AdjacentCells>();
        }

        aJ.adjacentCells = adjacents;
    }
Пример #6
0
            public static AdjacentCells GetAdjacentCells(int i)
            {
                i = SanitizeCellIndex(i);
                AdjacentCells output = 0;

                output |= i % Data.MineFieldWidth < Data.MineFieldWidth - 1 ? AdjacentCells.Right : 0;
                output |= i < (Data.MineFieldHeight - 1) * Data.MineFieldWidth ? AdjacentCells.Bottom : 0;
                output |= i % Data.MineFieldWidth > 0 ? AdjacentCells.Left : 0;
                output |= i >= Data.MineFieldWidth ? AdjacentCells.Top : 0;
                return(output);
            }
Пример #7
0
    public void CalculateAdjacents()
    {
        //work out which cells are adjacent to each cell, save in a list
        for (int i = 0; i < cells.Count; i++)
        {
            //set layer here
            cells[i].layer = LayerMask.NameToLayer("Cells");

            AdjacentCells.CalculateAdjacents(cells, cells[i], 0.1f);
        }
    }
Пример #8
0
    List <GameObject> Cells(List <List <Vector3> > splitVertices)
    {
        List <GameObject> cells = new List <GameObject>();

        //use passed list to make game objects with renderers etc
        //MeshGenerator mG = GameObject.FindGameObjectWithTag("Code").GetComponent<MeshGenerator>();
        for (int i = 0; i < splitVertices.Count; i++)
        {
            GameObject cell = new GameObject();
            cell.transform.position = transform.position;
            cell.name             = "Split Cell " + i.ToString();
            cell.transform.parent = meshGenerator.transform;

            cell.layer = LayerMask.NameToLayer("Cells");

            MeshRenderer mR = cell.AddComponent <MeshRenderer>();
            mR.sharedMaterial = Resources.Load("Ground") as Material;

            MeshFilter mF = cell.AddComponent <MeshFilter>();

            //find center point of passed points. Cell needs set up like this for building algorithm
            Vector3 centroid = Vector3.zero;
            for (int j = 0; j < splitVertices[i].Count; j++)
            {
                centroid += splitVertices[i][j];
            }
            centroid /= splitVertices[i].Count;

            //add centre first
            splitVertices[i].Insert(0, centroid);

            List <int> triangles = new List <int>();
            //now triangulate cell
            for (int j = 0; j < splitVertices[i].Count; j++)
            {
                if (j == 0)
                {
                    continue;
                }

                if (j < splitVertices[i].Count - 1)
                {
                    triangles.Add(j);
                    triangles.Add(j + 1);
                    triangles.Add(0);
                }
                else
                {
                    //finish loop by attaching to the first index
                    triangles.Add(j);
                    triangles.Add(1);
                    triangles.Add(0);
                }
            }

            //add to mesh
            Mesh mesh = new Mesh();
            mesh.vertices  = splitVertices[i].ToArray();
            mesh.triangles = triangles.ToArray();

            mF.mesh = mesh;

            //add aedge info and work out adjacents
            AdjacentCells aJ = cell.AddComponent <AdjacentCells>();
            aJ.Edges();

            // MergeCell.CalculateAdjacents(meshGenerator.cells,cell, meshGenerator.minEdgeSize);

            cells.Add(cell);;
        }



        return(cells);
    }
Пример #9
0
    List <List <Vector3> > LongestSplit(out List <List <int> > edgesSortedByLength, GameObject toSplit)
    {
        // GetComponent<MeshRenderer>().enabled = true;

        //we will return a list of vector3a for each split (2)
        List <List <Vector3> > splitVertices = new List <List <Vector3> >();

        Mesh mesh = toSplit.GetComponent <MeshFilter>().mesh;

        Vector3[] vertices = mesh.vertices;

        //finds the two longest edges, then creates a line between them
        List <List <int> > edges = new List <List <int> >();

        if (toSplit.GetComponent <AdjacentCells>() == null)
        {
            AdjacentCells aJ = toSplit.AddComponent <AdjacentCells>();
            aJ.Edges();
            edges = aJ.edges;
        }
        else
        {
            edges = toSplit.GetComponent <AdjacentCells>().edges;
        }
        //order by length
        List <List <int> > edgesByLength = new List <List <int> >(edges);

        edgesByLength.Sort(delegate(List <int> a, List <int> b)
        {
            return(Vector3.Distance(vertices[a[0]], vertices[a[1]])
                   .CompareTo(
                       Vector3.Distance(vertices[b[0]], vertices[b[1]])));
        });
        //make largest number first in list
        edgesByLength.Reverse();

        // Debug.DrawLine(vertices[edgesByLength[0][0]], vertices[edgesByLength[0][1]], Color.cyan);
        //Debug.DrawLine(vertices[edgesByLength[1][0]], vertices[edgesByLength[1][1]]);

        //now choose where to split these edges
        Vector3 split0 = Vector3.Lerp(vertices[edgesByLength[0][0]], vertices[edgesByLength[0][1]], .5f); //random?
        Vector3 split1 = Vector3.Lerp(vertices[edgesByLength[1][0]], vertices[edgesByLength[1][1]], .5f);

        //Debug.DrawLine(split0, split1, Color.red);

        //now we need to make two polygons out of this
        //start at [1]

        //find longest edgea in edge list
        int edge0Index = 0;
        int edge1Index = 0;

        for (int i = 0; i < edges.Count; i++)
        {
            if (edges[i] == edgesByLength[0])
            {
                edge0Index = i;
            }

            if (edges[i] == edgesByLength[1])
            {
                edge1Index = i;
            }
        }

        //now start from the end of the split and then work our way round the edges until we get back to the start of the split



        //using  for loop to switch between target edges
        for (int a = 0; a < 2; a++)
        {
            List <Vector3> list0 = new List <Vector3>();

            list0.Add(split0);
            list0.Add(split1);

            //have to reverse when going other way
            if (a == 1)
            {
                list0.Reverse();
            }

            //where to start on polygon changes on what side we are doing
            int start = edge1Index;
            if (a == 1)
            {
                start = edge0Index;
            }

            for (int i = start; i < 1000; i++) // a while loop with an upper safety number is used for debuggin so it doesn't stick forever
            {
                int index = i;
                if (index > edges.Count - 1)
                {
                    index -= edges.Count;
                }

                //continue round until we hit our other longest pair
                if (edges[index] == edgesByLength[a])
                {
                    break;
                }

                if (i == 999)
                {
                    Debug.Log("Problem");
                }

                list0.Add(vertices[edges[index][1]]);
            }

            splitVertices.Add(list0);
        }

        for (int i = 0; i < splitVertices.Count; i++)
        {
            for (int j = 0; j < splitVertices[i].Count; j++)
            {
                // GameObject c = GameObject.CreatePrimitive(PrimitiveType.Cube);
                // c.transform.position = splitVertices[i][j];
                // c.transform.parent = transform;
                //c.name = i.ToString();
            }
        }

        edgesSortedByLength = edgesByLength;
        return(splitVertices);
    }
Пример #10
0
        void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            switch (e.Index)
            {
            case 1:
                LogicalName = GXCommon.ToLogicalName(e.Value);
                break;

            case 2:
                if (e.Value is byte[])
                {
                    Operator = ASCIIEncoding.ASCII.GetString((byte[])e.Value);
                }
                else
                {
                    Operator = (string)e.Value;
                }
                break;

            case 3:
                Status = (GsmStatus)Convert.ToByte(e.Value);
                break;

            case 4:
                CircuitSwitchStatus = (GsmCircuitSwitchStatus)Convert.ToByte(e.Value);
                break;

            case 5:
                PacketSwitchStatus = (GsmPacketSwitchStatus)Convert.ToByte(e.Value);
                break;

            case 6:
                if (e.Value != null)
                {
                    List <object> tmp;
                    if (e.Value is List <object> )
                    {
                        tmp = (List <object>)e.Value;
                    }
                    else
                    {
                        tmp = new List <object>((object[])e.Value);
                    }
                    CellInfo.CellId        = Convert.ToUInt32(tmp[0]);
                    CellInfo.LocationId    = (UInt16)tmp[1];
                    CellInfo.SignalQuality = (byte)tmp[2];
                    CellInfo.Ber           = (byte)tmp[3];
                    if (Version > 0)
                    {
                        CellInfo.MobileCountryCode = (UInt16)tmp[4];
                        CellInfo.MobileNetworkCode = (UInt16)tmp[5];
                        CellInfo.ChannelNumber     = (UInt32)tmp[6];
                    }
                }
                break;

            case 7:
                AdjacentCells.Clear();
                if (e.Value != null)
                {
                    foreach (object tmp in (IEnumerable <object>)e.Value)
                    {
                        List <object> it;
                        if (tmp is List <object> )
                        {
                            it = (List <object>)tmp;
                        }
                        else
                        {
                            it = new List <object>((object[])tmp);
                        }
                        AdjacentCell ac = new Objects.AdjacentCell();
                        ac.CellId        = Convert.ToUInt32(it[0]);
                        ac.SignalQuality = (byte)it[1];
                        AdjacentCells.Add(ac);
                    }
                }
                break;

            case 8:
                if (e.Value is byte[])
                {
                    e.Value = GXDLMSClient.ChangeType((byte[])e.Value, DataType.DateTime, settings.UseUtc2NormalTime);
                }
                else if (e.Value is string)
                {
                    e.Value = new GXDateTime((string)e.Value);
                }
                CaptureTime = (GXDateTime)e.Value;
                break;

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
        }
Пример #11
0
        void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            switch (e.Index)
            {
            case 1:
                LogicalName = GXCommon.ToLogicalName(e.Value);
                break;

            case 2:
                if (e.Value is byte[])
                {
                    Operator = ASCIIEncoding.ASCII.GetString((byte[])e.Value);
                }
                else
                {
                    Operator = (string)e.Value;
                }
                break;

            case 3:
                Status = (GsmStatus)(byte)e.Value;
                break;

            case 4:
                CircuitSwitchStatus = (GsmCircuitSwitchStatus)(byte)e.Value;
                break;

            case 5:
                PacketSwitchStatus = (GsmPacketSwitchStatus)(byte)e.Value;
                break;

            case 6:
                if (e.Value != null)
                {
                    object[] tmp = (object[])e.Value;
                    CellInfo.CellId        = (UInt16)tmp[0];
                    CellInfo.LocationId    = (UInt16)tmp[1];
                    CellInfo.SignalQuality = (byte)tmp[2];
                    CellInfo.Ber           = (byte)tmp[3];
                }
                break;

            case 7:
                AdjacentCells.Clear();
                if (e.Value != null)
                {
                    foreach (object it in (object[])e.Value)
                    {
                        object[]     tmp = (object[])it;
                        AdjacentCell ac  = new Objects.AdjacentCell();
                        ac.CellId        = (string)tmp[0];
                        ac.SignalQuality = (byte)tmp[1];
                        AdjacentCells.Add(ac);
                    }
                }
                break;

            case 8:
                if (e.Value is byte[])
                {
                    CaptureTime = (GXDateTime)GXDLMSClient.ChangeType((byte[])e.Value, DataType.DateTime);
                }
                else
                {
                    CaptureTime = (GXDateTime)e.Value;
                }
                break;

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
        }