Exemple #1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Get inputs.
            string inputRobotName       = "";
            Mesh   inputEndeffectorMesh = new Mesh();
            int    inputRobotID         = 0;
            Plane  inputPlane           = this.defaultPlane;

            if (!DA.GetData(0, ref inputRobotName))
            {
                RobotOptionList();
            }
            DA.GetData(1, ref inputEndeffectorMesh);
            DA.GetData(2, ref inputRobotID);
            DA.GetData(3, ref inputPlane);
            //////////////////////////////////////////////////////
            // Process data.
            inputEndeffectorMesh.Weld(Math.PI);
            EndeffectorData endEffector = new EndeffectorData(MeshUtilities.EncodeMesh(inputEndeffectorMesh));
            RobotData       robot       = new RobotData(inputRobotID, inputRobotName, endEffector, EncodeUtilities.EncodePlane(inputPlane));

            UniversalDebug("Robot Object Created.");
            //////////////////////////////////////////////////////
            // Output.
            DA.SetData(0, robot);
                        #if DEBUG
            DA.SetData(1, this.debugMessages[this.debugMessages.Count - 1]);
                        #endif
        }
        // Collect environment meshes
        public List <byte[]> EncodeEnvironmentMesh(out string currentMessage)
        {
            currentMessage = string.Empty;
            List <byte[]> data = new List <byte[]>();

            FindMeshes();
            if (this.scannedEnvironment.Count > 0)
            {
                // Combine meshes
                CombineInstance[] combineStructure = new CombineInstance[this.scannedEnvironment.Count];
                int i = 0;
                foreach (MeshRenderer meshRenderer in this.scannedEnvironment)
                {
                    MeshFilter meshFilter = meshRenderer.gameObject.GetComponent <MeshFilter>();
                    combineStructure[i].mesh      = meshFilter.sharedMesh;
                    combineStructure[i].transform = meshRenderer.transform.localToWorldMatrix;
                    i++;
                }
                Mesh mesh = new Mesh();
                mesh.CombineMeshes(combineStructure);

                // Encode mesh
                MeshData meshData  = MeshUtilities.EncodeMesh(mesh);
                byte[]   localData = EncodeUtilities.EncodeData("ENVIRONMENT", meshData, out string currentLocalMessage);
                data.Add(localData);
                                #if DEBUG
                DebugUtilities.UniversalDebug(this.sourceName, "Mesh Encoding: " + currentLocalMessage);
                                #endif
                currentMessage = currentLocalMessage;

                // // Encode meshes separately
                // // {
                // //   MeshRenderer meshRenderer = this.scannedEnvironment[0];
                // foreach (MeshRenderer meshRenderer in this.scannedEnvironment) {
                //  MeshFilter meshFilter = meshRenderer.gameObject.GetComponent<MeshFilter>();
                //  MeshData meshData = MeshUtilities.EncodeMesh(meshFilter.sharedMesh);
                //  byte[] localData = EncodeUtilities.EncodeData("ENVIRONMENT", meshData, out string currentLocalMessage);
                //  #if DEBUG
                //  DebugUtilities.UniversalDebug(this.sourceName, "Mesh Encoding: " + currentLocalMessage);
                //  #endif
                //  data.Add(localData);
                //  currentMessage += currentLocalMessage;
                // }
            }
            else
            {
                                #if DEBUG
                DebugUtilities.UniversalDebug(this.sourceName, "No meshes found.");
                                #endif
            }

            return(data);
        }
        private void CreateMesh(EndeffectorData endEffector, MeshFilter tool)           //, double[] tcp
        {
            List <Vector3> vertices  = new List <Vector3>();
            List <int>     triangles = new List <int>();

            if (endEffector.vertices != null)
            {
                                #if DEBUG
                Debug.Log("Robot: Adding Verticies . . . !");
                                #endif
                for (int j = 0; j < endEffector.vertices.Count; j++)
                {
                    vertices.Add(new Vector3(endEffector.vertices[j][0],
                                             endEffector.vertices[j][1],
                                             endEffector.vertices[j][2]));
                }
            }

            if (endEffector.faces != null)
            {
                                #if DEBUG
                Debug.Log("Robot: Adding Faces . . . !");
                                #endif
                for (int j = 0; j < endEffector.faces.Count; j++)
                {
                    triangles.Add(endEffector.faces[j][1]);
                    triangles.Add(endEffector.faces[j][2]);
                    triangles.Add(endEffector.faces[j][3]);

                    if ((endEffector.faces[j][0] == 1))
                    {
                        triangles.Add(endEffector.faces[j][1]);
                        triangles.Add(endEffector.faces[j][3]);
                        triangles.Add(endEffector.faces[j][4]);
                    }
                }
            }
            tool.mesh = MeshUtilities.DecodeMesh(vertices, triangles);
            tool.transform.Rotate(0, 180, -90);
        }
Exemple #4
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //CheckType();
            // Get inputs.
            string       message;
            List <Mesh>  inputMeshes = new List <Mesh>();
            List <Color> inputColor  = new List <Color>();
            Connection   connect     = null;

            if (!DA.GetDataList(0, inputMeshes))
            {
                return;
            }
            DA.GetDataList(1, inputColor);
            if (!DA.GetData(2, ref connect))
            {
                return;
            }
            // Check inputs.
            if ((inputColor.Count > 1) && (inputColor.Count != inputMeshes.Count))
            {
                message = (inputColor.Count > inputMeshes.Count) ?
                          "The number of Colors does not match the number of Mesh objects. Extra colors will be ignored." :
                          "The number of Colors does not match the number of Mesh objects. The last color will be repeated.";
                UniversalDebug(message, GH_RuntimeMessageLevel.Warning);
            }
            ////////////////////////////////////////////////////////////////////

            // If connection open start acting.
            if (connect.status)
            {
                // Encode mesh data.
                List <MeshData> inputMeshData = new List <MeshData> {
                };
                for (int i = 0; i < inputMeshes.Count; i++)
                {
                    Color currentColor = inputColor[Math.Min(i, inputColor.Count)];
                    inputMeshData.Add(MeshUtilities.EncodeMesh(inputMeshes[i], currentColor));
                }
                // Send mesh data.
                byte[] bytes = EncodeUtilities.EncodeData("MESHSTREAMING", inputMeshData, out string currentMessage);
                if (this.lastMessage != currentMessage)
                {
                    bool success = false;
                    if (this.sourceType == SourceType.TCP)
                    {
                        connect.tcpSender.Send(bytes);
                        success = connect.tcpSender.success;
                        message = connect.tcpSender.debugMessages[connect.tcpSender.debugMessages.Count - 1];
                    }
                    else
                    {
                        connect.udpSender.Send(bytes);
                        success = connect.udpSender.success;
                        message = connect.udpSender.debugMessages[connect.udpSender.debugMessages.Count - 1];
                    }
                    if (success)
                    {
                        this.lastMessage = currentMessage;
                    }
                    UniversalDebug(message, (success) ? GH_RuntimeMessageLevel.Remark : GH_RuntimeMessageLevel.Error);
                }
            }
            else
            {
                this.lastMessage = string.Empty;
                UniversalDebug("Set 'Send' on true in HoloFab 'HoloConnect'", GH_RuntimeMessageLevel.Warning);
            }

            // Output.
                        #if DEBUG
            DA.SetData(0, this.debugMessages[this.debugMessages.Count - 1]);
                        #endif
        }
        // Decode Received Data.
        public void ProcessMesh(List <MeshData> receivedMeshes, SourceType sourceType)
        {
                        #if DEBUG
            Debug.Log("Mesh: Received Count: " + receivedMeshes.Count + ". Type: " + (int)sourceType);
                        #endif

            // Check for C-plane
            if (!ObjectManager.instance.CheckCPlane())
            {
                return;
            }

            // Find relevant objects based on source to update
            GameObject[] goRelevantMeshes;
            string       currentTag;
            GameObject   currentPrefab;
            switch (sourceType)
            {
            case (SourceType.TCP):
                                 #if DEBUG
                Debug.Log("Mesh: TCP");
                                 #endif
                currentPrefab = this.goPrefabMesh;
                currentTag    = this.tagMesh;
                break;

            case (SourceType.UDP):
                                 #if DEBUG
                Debug.Log("Mesh: UDP");
                                 #endif
                currentPrefab = this.goPrefabMeshPlus;
                currentTag    = this.tagMeshPlus;
                break;

            default:
                return;
            }
            goRelevantMeshes = GameObject.FindGameObjectsWithTag(currentTag);

            List <Vector3> currentVertices = new List <Vector3>();
            List <int>     currentFaces    = new List <int>();
            List <Color>   currentColors   = new List <Color>();
            // Loop through all received meshes.
            for (int i = 0; i < receivedMeshes.Count; i++)
            {
                // Condition Vertex based data (points and colors). // Later: normals etc.
                for (int j = 0; j < receivedMeshes[i].vertices.Count; j++)
                {
                    currentVertices.Add(new Vector3(receivedMeshes[i].vertices[j][0],
                                                    receivedMeshes[i].vertices[j][1],
                                                    receivedMeshes[i].vertices[j][2]));
                    int colorIndex = (receivedMeshes[i].colors.Count == receivedMeshes[i].vertices.Count) ? j : 0;
                    currentColors.Add(new Color((float)receivedMeshes[i].colors[colorIndex][1] / 255.0f,
                                                (float)receivedMeshes[i].colors[colorIndex][2] / 255.0f,
                                                (float)receivedMeshes[i].colors[colorIndex][3] / 255.0f,
                                                1.0f));
                }
                float alpha = (float)receivedMeshes[i].colors[0][0] / 255.0f;
                // Condition Faces.
                for (int j = 0; j < receivedMeshes[i].faces.Count; j++)
                {
                    currentFaces.Add(receivedMeshes[i].faces[j][1]);
                    currentFaces.Add(receivedMeshes[i].faces[j][2]);
                    currentFaces.Add(receivedMeshes[i].faces[j][3]);
                    // If quad - add second face.
                    if ((receivedMeshes[i].faces[j][0] == 1))
                    {
                        currentFaces.Add(receivedMeshes[i].faces[j][1]);
                        currentFaces.Add(receivedMeshes[i].faces[j][3]);
                        currentFaces.Add(receivedMeshes[i].faces[j][4]);
                    }
                }

                // Set Values
                GameObject meshInstance;
                if (i < goRelevantMeshes.Length)
                {
                                        #if DEBUG
                    Debug.Log("Mesh: Updating old Mesh");
                                        #endif
                    meshInstance = goRelevantMeshes[i];
                }
                else
                {
                                        #if DEBUG
                    Debug.Log("Mesh: Adding new Mesh");
                                        #endif
                    meshInstance = Instantiate(currentPrefab, ObjectManager.instance.cPlane.transform.position, ObjectManager.instance.cPlane.transform.rotation, ObjectManager.instance.cPlane.transform);
                }
                meshInstance.GetComponent <Renderer>().material.SetFloat("_ShadowStrength", 1.0f);
                meshInstance.GetComponent <Renderer>().material.SetFloat("_Alpha", alpha);
                meshInstance.GetComponent <MeshFilter>().mesh = MeshUtilities.DecodeMesh(currentVertices, currentFaces, currentColors);
            }

            // Delete Extraneous Meshes.
            if (goRelevantMeshes.Length > receivedMeshes.Count)
            {
                                #if DEBUG
                Debug.Log("Mesh: Meshes extra deleting.");
                                #endif
                DeleteMeshes(sourceType, receivedMeshes.Count);
            }
        }