/// <summary>
        /// Create a shape message.
        /// </summary>
        /// <param name="id_">Shape id</param>
        /// <param name="vertices">Vertices that make up the shape</param>
        /// <param name="indices">Indices indicating in which order to draw the vertices</param>
        /// <returns>The created shape</returns>
        public static Shape_ CreateShape_(Int32 id_, ICollection <Vector3> vertices,
                                          ICollection <UInt32> indices, UnityEngine.Transform transform = null)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("vertices");
            }

            if (indices == null)
            {
                throw new ArgumentNullException("indices");
            }

            if (vertices.Count < 3)
            {
                Debug.LogWarning("Creating a shape with less than 3 vertices.");
            }

            if (indices.Count < 3)
            {
                Debug.LogWarning("Creating a shape with less than 3 indices.");
            }

            Shape_ result = new Shape_
            {
                id = id_,
            };

            foreach (var uVertex in vertices)
            {
                result.vertices.Add(UnityMessageBuilder.CreateVector(uVertex));
            }

            foreach (UInt32 index in indices)
            {
                result.indices.Add(index);
            }

            if (transform != null)
            {
                result.transform = CreateTransform_(transform);
            }

            return(result);
        }
        // Затем напишем метод, который работает с любыми Shape'ами и выдает площадь фигуры.
        // В данном методе происходит проверка на тип переданной фигуры и в зависимости от него используется
        // та или иная формула.
        //
        // Подобные методы довольно часто встречаются, когда мы не хотим заносить сложные зависимости в наши
        // классы и пользоваться полиморфизмом. Например, данный метод может обращаться к серверу для расчета площади и т.п.
        // Не хотелось бы усложнять класс Shape и его наследников лишними подробностями.
        public double GetArea_(Shape_ shape)
        {
            var rectangle = shape as Rectangle_;

            if (rectangle != null)
            {
                return(rectangle.Height * rectangle.Width);
            }

            var circle = shape as Circle_;

            if (circle != null)
            {
                return(Math.PI * Math.Pow(circle.Radius, 2));
            }

            throw new NotSupportedException(shape.GetType().Name);
        }
Example #3
0
        public static void CreateShape_()
        {
            int id = 42;

            List <Vector3> vecs = new List <Vector3>();

            vecs.Add(new Vector3(1, 1, 1));
            vecs.Add(new Vector3(2, 2, 2));
            vecs.Add(new Vector3(3, 3, 3));

            List <UInt32> indices = new List <UInt32>();

            indices.Add(1);
            indices.Add(0);
            indices.Add(2);

            Shape_ result = UnityMessageBuilder.CreateShape_(id, vecs, indices);

            Assert.AreEqual(result.id, id);

            Assert.AreEqual(result.vertices.Count, vecs.Count);
            for (int i = 0; i < result.vertices.Count; i++)
            {
                var protoVec = result.vertices[i];
                var unityVec = vecs[i];

                Assert.AreEqual(unityVec, protoVec.ToUnityVector());
            }

            Assert.AreEqual(indices.Count, result.indices.Count);
            for (int i = 0; i < result.indices.Count; i++)
            {
                Assert.AreEqual(indices[i], result.indices[i]);
            }

            result.vertices.RemoveAt(0);
            Assert.AreEqual(vecs.Count - 1, result.vertices.Count);
            Assert.AreEqual(vecs[1], result.vertices[0].ToUnityVector());

            result.indices.RemoveAt(0);
            Assert.AreEqual(indices.Count - 1, result.indices.Count);
            Assert.AreEqual(indices[1], result.indices[0]);
        }
Example #4
0
        public static void SetShapeUpdateInfo()
        {
            List <Vector3> vert1 = new List <Vector3>();
            List <Vector3> vert2 = new List <Vector3>();
            List <Vector3> vert3 = new List <Vector3>();
            List <Vector3> vert4 = new List <Vector3>();

            vert1.Add(new Vector3(1, 1, 1));
            vert1.Add(new Vector3(1, 2, 2));
            vert1.Add(new Vector3(1, 3, 3));

            vert2.Add(new Vector3(2, 1, 1));
            vert2.Add(new Vector3(2, 2, 2));
            vert2.Add(new Vector3(2, 3, 3));

            vert3.Add(new Vector3(3, 1, 1));
            vert3.Add(new Vector3(3, 2, 2));
            vert3.Add(new Vector3(3, 3, 3));

            vert4.Add(new Vector3(4, 1, 1));
            vert4.Add(new Vector3(4, 2, 2));
            vert4.Add(new Vector3(4, 3, 3));


            List <UInt32> indices = new List <UInt32>();

            indices.Add(0);
            indices.Add(1);
            indices.Add(2);

            Shape_ shape1 = UnityMessageBuilder.CreateShape_(1, vert1, indices);
            Shape_ shape2 = UnityMessageBuilder.CreateShape_(2, vert2, indices);
            Shape_ shape3 = UnityMessageBuilder.CreateShape_(3, vert3, indices);
            Shape_ shape4 = UnityMessageBuilder.CreateShape_(4, vert3, indices);

            List <Shape_> changedShapes = new List <Shape_>();
            List <Shape_> newShapes     = new List <Shape_>();

            changedShapes.Add(shape1);
            changedShapes.Add(shape2);
            newShapes.Add(shape3);
            newShapes.Add(shape4);

            Message result = new Message();

            result.SetShapeUpdateInfo(changedShapes, newShapes);

            Assert.AreEqual(changedShapes.Count, result.shapeUpdateInfo.changedShapes.Count);
            Assert.AreEqual(newShapes.Count, result.shapeUpdateInfo.newShapes.Count);

            for (int i = 0; i < changedShapes.Count; i++)
            {
                Assert.AreEqual(changedShapes[i].id, result.shapeUpdateInfo.changedShapes[i].id);
                Assert.AreEqual(changedShapes[i].vertices.Count, result.shapeUpdateInfo.changedShapes[i].vertices.Count);

                for (int j = 0; j < changedShapes[i].vertices.Count; j++)
                {
                    Assert.AreEqual(changedShapes[i].vertices[j].ToUnityVector(),
                                    result.shapeUpdateInfo.changedShapes[i].vertices[j].ToUnityVector());
                }
            }

            for (int i = 0; i < newShapes.Count; i++)
            {
                Assert.AreEqual(newShapes[i].id, result.shapeUpdateInfo.newShapes[i].id);
                Assert.AreEqual(newShapes[i].vertices.Count, result.shapeUpdateInfo.newShapes[i].vertices.Count);

                for (int j = 0; j < newShapes[i].vertices.Count; j++)
                {
                    Assert.AreEqual(newShapes[i].vertices[j].ToUnityVector(),
                                    result.shapeUpdateInfo.newShapes[i].vertices[j].ToUnityVector());
                }
            }
        }
        public void AcceptConnection()
        {
            if (EditorPrefs.GetBool("test_tcp_asio") == false)
            {
                Debug.LogWarning("[TEST_TCPAsioConnection.AcceptConnection] Test is disabled, you can enable this test via the menu option \"Testing\"");
                Assert.That(true);
                return;
            }

            Debug.LogWarning("[TEST_TCPAsioConnection.AcceptConnection] This test will only pass when a tcp client is manually connected to the listener started in this test.");

            _subscriber = new TestIncomingDataLinkSubscriberCopy();

            _listener = new TCPDataLinkListener <ProtoBufPresentation>(_subscriber);
            //use public IP address not localhost/127.0.0.1
            //use eduroam(same network)
            try
            {
                Assert.True(_listener.Start("145.93.45.16", 1234));
            }
            catch (SocketException ex)
            {
                Assert.That(false, "Invalid ip address\n" + ex.Message);
            }


            int sleepCount = 0;

            while (!_subscriber.Connected)
            {
                if (sleepCount > 500)
                {
                    Assert.True(false, "Failed to connect within timeout.");
                    break;
                }

                Thread.Sleep(20);

                sleepCount++;
            }
            Debug.Log("Actually connected");
            //Thread.Sleep(8000);

            Assert.IsTrue(_subscriber.Connected);
            Assert.NotNull(_subscriber.DataLink);
            Assert.IsTrue(_subscriber.DataLink.Connected());

            if (_subscriber.Connected)
            {
                Thread.Sleep(3000);
                Communicator          comm = new Communicator(_subscriber.DataLink, new ProtoBufPresentation());
                Communication.Message m    = new Message
                {
                    messageType   = Communication.MessageType_.ShapeUpdate,
                    messageTarget = Communication.MessageTarget_.Robot,
                    id            = 5
                };

                Shape_ sh = new Shape_();
                sh.id = 15;
                Vector3_ v = new Vector3_
                {
                    x = 3.0f,
                    y = 3.0f,
                    z = 4.0f
                };

                Vector3_ v1 = new Vector3_
                {
                    x = -1.0f,
                    y = -3.0f,
                    z = -5.5f
                };
                sh.vertices.Add(v);
                sh.vertices.Add(v1);

                Shape_ sh2 = new Shape_();
                sh2.id = 2;
                Vector3_ v2 = new Vector3_
                {
                    x = 3.4f,
                    y = 3.1f,
                    z = 4.7f
                };
                sh2.vertices.Add(v2);

                m.shapeUpdateInfo = new ShapeUpdateInfo_();
                //m.shapeUpdateInfo.changedShapes = new List<Shape_>();
                m.shapeUpdateInfo.changedShapes.Add(sh);
                m.shapeUpdateInfo.changedShapes.Add(sh2);
                m.customMessage      = new CustomMessage_();
                m.customMessage.key  = "asdf";
                m.customMessage.data = "asdf data";

                Assert.IsTrue(comm.SendCommand(m));
            }
        }