Beispiel #1
0
    void Update()
    {
        //writeResult = false;

        if (chremote.nrCHremote == chlocal.nrCHlocal && chremote.nrCHremote != 0 && chlocal.nrCHlocal != 0 && chlocal.readyForIntersectionLocal && chremote.readyForIntersectionRemote && eval.localIsDemonstrator)
        {
            //child = chremote.nrCHremote - 1;
            Debug.Log("Initiate Intersection");
            try
            {
                child = chremote.nrCHremote - 1;

                /*if ()
                 * {
                 *
                 * }*/

                CSG A = CSG.fromMesh(localVolumesParent.transform.GetChild(child).GetComponent <MeshFilter>().mesh, localVolumesParent.transform.GetChild(child));
                CSG B = CSG.fromMesh(remoteVolumesParent.transform.GetChild(child).GetComponent <MeshFilter>().mesh, remoteVolumesParent.transform.GetChild(child));

                CSG result = null;
                result = A.intersect(B);

                Debug.Log(A.polygons.Count + ", " + B.polygons.Count + ", " + result.polygons.Count);

                GameObject newGo = Instantiate(meshLess, Vector3.zero, Quaternion.identity) as GameObject;
                newGo.transform.SetParent(IntParent.transform, false);
                intersectionMesh = result.toMesh();
                newGo.GetComponent <MeshFilter>().mesh = intersectionMesh;
                float volumeCSG = (VolumeOfMesh(intersectionMesh) * 1000000);
                Debug.Log("Volume from CSG Algorithm: " + volumeCSG + ".   %: " + volumeCSG / chlocal.volume * 100.0f);

                newGo.SetActive(false);



                ////////////////////////////////////////////////////////////////////////////////////
                ////////   PASS TO CONEX HULL /////////////////////

                //Parametros necessarios para o algoritmo de Convex Hull

                var calc    = new ConvexHullCalculator();
                var verts   = new List <Vector3>();
                var tris    = new List <int>();
                var normals = new List <Vector3>();


                Debug.Log("Intersection points Hash:" + result.intersectionPoints.Count);

                for (int i = 0; i < result.intersectionPoints.Count - 1; i++)
                {
                    float x = (float)Math.Ceiling(result.intersectionPoints.ToList()[i].x * 10000000) / 10000000;
                    float y = (float)Math.Ceiling(result.intersectionPoints.ToList()[i].y * 10000000) / 10000000;
                    float z = (float)Math.Ceiling(result.intersectionPoints.ToList()[i].z * 10000000) / 10000000;

                    Vector3 position = new UnityEngine.Vector3(x, y, z);

                    Instantiate(ball, position, Quaternion.identity);
                }

                if (result.intersectionPoints.Count >= 4)
                {
                    calc.GenerateHull(result.intersectionPoints.ToList(), true, ref verts, ref tris, ref normals);

                    //Create an initial transform that will evolve into our Convex Hull when altering the mesh

                    var initialHull = Instantiate(initialMeshCSG);
                    //initialHull = Instantiate(initialMesh);

                    initialHull.transform.SetParent(IntParent.transform, false);
                    initialHull.transform.position   = Vector3.zero;
                    initialHull.transform.rotation   = Quaternion.identity;
                    initialHull.transform.localScale = Vector3.one;

                    //Independentemente do tipo de mesh com que se começa (cubo, esfera..)
                    //a mesh é redefenida com as definiçoes abaixo

                    var mesh = new Mesh();
                    mesh.SetVertices(verts);
                    mesh.SetTriangles(tris, 0);
                    mesh.SetNormals(normals);

                    initialHull.GetComponent <MeshFilter>().sharedMesh   = mesh;
                    initialHull.GetComponent <MeshCollider>().sharedMesh = mesh;
                    /////////////////////////////////////////////////////////////////////

                    //Calcular o volume da mesh
                    volumeOfIntersection = VolumeOfMesh(mesh) * 1000000; //convert to cm3

                    //Compare the volume of intersection with the volume that the local pointed

                    percentageOfIntersection = volumeOfIntersection / chlocal.volume * 100.0f;
                }



                else
                {
                    volumeOfIntersection     = 0.0f;
                    percentageOfIntersection = 0.0f;
                }

                Debug.Log("Volume from CH Algorithm: " + volumeOfIntersection + ".   %: " + percentageOfIntersection);
                //Debug.Log("Percentage of Intersection: " + percentageOfIntersection.ToString("F0") + "%");

                percentageString = chlocal.volume.ToString() + '#' + chremote.volume.ToString() + '#' + volumeOfIntersection.ToString() + '#' + percentageOfIntersection.ToString("F1");
                writeResult      = true;

                Debug.Log(percentageString);

                initialMeshCSG.SetActive(false);
                localVolumesParent.transform.GetChild(child).gameObject.SetActive(false);
                remoteVolumesParent.transform.GetChild(child).gameObject.SetActive(false);



                chlocal.readyForIntersectionLocal   = false;
                chremote.readyForIntersectionRemote = false;

                Debug.Log("Intersection write = " + writeResult);
            }

            catch (System.ArgumentException)
            {
                //newGo.SetActive(false);

                initialMeshCSG.SetActive(false);
                localVolumesParent.transform.GetChild(child).gameObject.SetActive(false);
                remoteVolumesParent.transform.GetChild(child).gameObject.SetActive(false);


                chlocal.readyForIntersectionLocal   = false;
                chremote.readyForIntersectionRemote = false;

                /*foreach (Transform child in POINTS)
                 * {
                 *  Destroy(child.gameObject);
                 * }*/

                startEnd.getStartTime = true;
                percentageString      = "Error computing: Can't generate hull, points are coplanar!";
                writeResult           = true;
            }

            catch (UnityEngine.Assertions.AssertionException)
            {
                //newGo.SetActive(false);

                initialMeshCSG.SetActive(false);
                localVolumesParent.transform.GetChild(child).gameObject.SetActive(false);
                remoteVolumesParent.transform.GetChild(child).gameObject.SetActive(false);


                chlocal.readyForIntersectionLocal   = false;
                chremote.readyForIntersectionRemote = false;

                /*foreach (Transform child in POINTS)
                 * {
                 *  Destroy(child.gameObject);
                 * }*/

                startEnd.getStartTime = true;
                percentageString      = "Error computing: Assertion failed!";
                writeResult           = true;
            }

            //throw new System.ArgumentException("Can't generate hull, points are coplanar");
            //throw new UnityEngine.Assertions.AssertionException("Assertion failed", "");
        }

        /*else if (chremote.nrCHremote == chlocal.nrCHlocal && chremote.nrCHremote != 0 && chlocal.nrCHlocal != 0 && eval.localIsDemonstrator && (!chlocal.readyForIntersectionLocal || !chremote.readyForIntersectionRemote) )
         * {
         *  percentageString = "Error computing initial hulls: Assertion failed!";
         *  //writeResult = true;
         *
         *  Debug.Log("Stuck here");
         * }*/
    }
Beispiel #2
0
        } // BoundsIntersectsCloud

        // area selection from multiple clouds, returns list of collectedpoints struct (which contains indexes to cloud and the actual point)
        public List<CollectedPoint> ConvexHullSelectPoints(GameObject go, List<Vector3> area)
        {
            // build area bounds
            var areaBounds = new Bounds();
            for (int i = 0, len = area.Count; i < len; i++)
            {
                areaBounds.Encapsulate(area[i]);
            }

            // check bounds
            //PointCloudTools.DrawBounds(areaBounds, 100);

            // build area hull
            var calc = new ConvexHullCalculator();

            var verts = new List<Vector3>();
            var tris = new List<int>();
            var normals = new List<Vector3>();
            var mf = go.GetComponent<MeshFilter>();
            if (go == null) Debug.LogError("Missing MeshFilter from " + go.name, go);
            var mesh = new Mesh();
            mf.sharedMesh = mesh;

            calc.GenerateHull(area, false, ref verts, ref tris, ref normals);

            mesh.Clear();
            mesh.SetVertices(verts);
            mesh.SetTriangles(tris, 0);
            mesh.SetNormals(normals);

            var results = new List<CollectedPoint>();
            // all clouds
            for (int cloudIndex = 0, length2 = clouds.Count; cloudIndex < length2; cloudIndex++)
            {
                // exit if outside whole cloud bounds
                if (clouds[cloudIndex].bounds.Intersects(areaBounds) == false) return null;

                // check all nodes from this cloud
                for (int nodeIndex = 0; nodeIndex < clouds[cloudIndex].nodes.Length; nodeIndex++)
                {
                    // early exit if bounds doesnt hit this node?
                    if (clouds[cloudIndex].nodes[nodeIndex].bounds.Intersects(areaBounds) == false) continue;

                    // loop points
                    for (int j = 0, l = clouds[cloudIndex].nodes[nodeIndex].points.Count; j < l; j++)
                    {
                        // check all points from that node
                        int pointIndex = clouds[cloudIndex].nodes[nodeIndex].points[j];
                        // get actual point
                        Vector3 p = viewers[clouds[cloudIndex].viewerIndex].points[pointIndex];

                        // check if inside hull
                        if (IsPointInsideMesh(mesh, p))
                        {
                            var temp = new CollectedPoint();
                            temp.cloudIndex = cloudIndex;
                            temp.pointIndex = pointIndex;
                            results.Add(temp);
                        }
                    }

                }

            } // for clouds
            return results;
        }
Beispiel #3
0
    //public bool readyRemote = true;
    //public bool readylocal = true;

    //public IntersectionCSGTry intersectionCSG;
    void Start()
    {
        //Intersection through CSG

        //return;


        CSG A = CSG.fromMesh(a.GetComponent <MeshFilter>().mesh, a.transform);
        CSG B = CSG.fromMesh(b.GetComponent <MeshFilter>().mesh, b.transform);

        //CSG A = CSG.fromMesh(a.GetComponent<MeshFilter>().mesh, a.transform.GetChild(0));
        //CSG B = CSG.fromMesh(b.GetComponent<MeshFilter>().mesh, b.transform.GetChild(0));

        Debug.Log("Antes Interseção");

        CSG result = A.intersect(B);

        //Debug.Log("Depois Interseção");

        intersectionPoints = CSGtoListOfPoints(result.polygons);

        //Debug.Log("Depois dos pontos: " + intersectionPoints.Count + "pontos");

        //Parametros necessarios para o algoritmo de Convex Hull

        var calc    = new ConvexHullCalculator();
        var verts   = new List <Vector3>();
        var tris    = new List <int>();
        var normals = new List <Vector3>();

        //Debug.Log("Antes da CH" );

        for (int pi = 0; pi < intersectionPoints.Count; pi++)
        {
            Instantiate(ball, intersectionPoints[pi], Quaternion.identity, POINTS);
            //ballsList.Add((Instantiate(ball, intersectionPoints[pi], Quaternion.identity)).position);
            //Debug.Log("Tau");
        }

        for (int pi = 0; pi < POINTS.childCount; pi++)
        {
            ballsList.Add(POINTS.GetChild(pi).position);
        }



        calc.GenerateHull(ballsList, true, ref verts, ref tris, ref normals);

        //Debug.Log("Depois da CH");

        //Create an initial transform that will evolve into our Convex Hull when altering the mesh

        var initialHull = Instantiate(initialMeshCSG);

        initialHull.transform.SetParent(transform, false);
        initialHull.transform.position   = Vector3.zero;
        initialHull.transform.rotation   = Quaternion.identity;
        initialHull.transform.localScale = Vector3.one;

        //Independentemente do tipo de mesh com que se começa (cubo, esfera..)
        //a mesh é redefenida com as definiçoes abaixo

        var mesh = new Mesh();

        mesh.SetVertices(verts);
        mesh.SetTriangles(tris, 0);
        mesh.SetNormals(normals);

        initialHull.GetComponent <MeshFilter>().sharedMesh   = mesh;
        initialHull.GetComponent <MeshCollider>().sharedMesh = mesh;

        //Calcular o volume da CH

        volume = VolumeOfMesh(mesh) * 1000000; //convert to cm3
        Debug.Log("Volume: " + volume);

        //Limpar os pontos antigos da lista para o proximo convex hull e
        //informar o programa de que já realizou esta função

        //intersectionCSG.intersectionPoints.Clear();

        initialMeshCSG.SetActive(false);
        a.SetActive(false);
        b.SetActive(false);
    }