/// <summary>
        /// Crear esfera a partir de los puntos mas distintas
        /// </summary>
        private static SphereStruct sphereFromDistantPoints(Vector3[] pt)
        {
            // Find the most separated point pair defining the encompassing AABB
            int min, max;
            TgcBoundingSphere.mostSeparatedPointsOnAABB(pt, out min, out max);

            // Set up sphere to just encompass these two points
            SphereStruct s = new SphereStruct();
            s.center = (pt[min] + pt[max]) * 0.5f;
            s.radius = Vector3.Dot(pt[max] - s.center, pt[max] - s.center);
            s.radius = FastMath.Sqrt(s.radius);
            return s;
        }
 /// <summary>
 /// Given Sphere s and Point p, update s (if needed) to just encompass p
 /// </summary>
 private static void sphereOfSphereAndPt(ref SphereStruct s, Vector3 p)
 {
     // Compute squared distance between point and sphere center
     Vector3 d = p - s.center;
     float dist2 = Vector3.Dot(d, d);
     // Only update s if point p is outside it
     if (dist2 > s.radius * s.radius) {
         float dist = FastMath.Sqrt(dist2);
         float newRadius = (s.radius + dist) * 0.5f;
         float k = (newRadius - s.radius) / dist;
         s.radius = newRadius;
         s.center += d * k;
     }
 }
 /// <summary>
 /// Convertir a struct
 /// </summary>
 public SphereStruct toStruct()
 {
     SphereStruct sphereStruct = new SphereStruct();
     sphereStruct.center = center;
     sphereStruct.radius = radius;
     return sphereStruct;
 }
Beispiel #4
0
 public SphereModel(GameObject prefab, SphereData spheredata)
 {
     SphereData      = spheredata;
     SphereStruct    = spheredata.SphereStruct;
     SphereTransform = prefab.transform;
     SphereCollider  = prefab.gameObject.GetComponent <SphereCollider>();
 }
Beispiel #5
0
        /// <summary>
        /// Crea un BoundingSphere a partir de los vertices de un Mesh, utilizando el algoritmo de Ritter
        /// </summary>
        /// <param name="mesh">Mesh a partir del cual crear el BoundingSphere</param>
        /// <returns>BoundingSphere creado</returns>
        public static TgcBoundingSphere computeFromMesh(TgcMesh mesh)
        {
            Vector3[]    vertices = mesh.getVertexPositions();
            SphereStruct s        = TgcBoundingSphere.computeFromPoints(vertices);

            return(s.toClass());
        }
        /// <summary>
        ///     Convertir a struct
        /// </summary>
        public SphereStruct toStruct()
        {
            var sphereStruct = new SphereStruct();

            sphereStruct.center = Center;
            sphereStruct.radius = Radius;
            return(sphereStruct);
        }
Beispiel #7
0
        /// <summary>
        /// Convertir a struct
        /// </summary>
        public SphereStruct toStruct()
        {
            SphereStruct sphereStruct = new SphereStruct();

            sphereStruct.center = center;
            sphereStruct.radius = radius;
            return(sphereStruct);
        }
Beispiel #8
0
        /// <summary>
        /// Crear un BoundingSphere a partir de un conjunto de puntos, utilizando el algoritmo de Ritter:
        /// [Ritter, Jack. "An Efficient Bounding Sphere," in Andrew Glassner (ed.), Graphics Gems, Academic Press, pp. 301–303, 1990.]
        /// </summary>
        /// <param name="pt">Puntos a partir del cual calcular el BoundingSphere</param>
        /// <returns>BoundingSphere calculado</returns>
        public static SphereStruct computeFromPoints(Vector3[] pt)
        {
            //Get sphere encompassing two approximately most distant points
            SphereStruct s = sphereFromDistantPoints(pt);

            // Grow sphere to include all points
            for (int i = 0; i < pt.Length; i++)
            {
                TgcBoundingSphere.sphereOfSphereAndPt(ref s, pt[i]);
            }

            return(s);
        }
        /// <summary>
        ///     Crear esfera a partir de los puntos mas distintas
        /// </summary>
        private static SphereStruct sphereFromDistantPoints(TGCVector3[] pt)
        {
            // Find the most separated point pair defining the encompassing AABB
            int min, max;

            mostSeparatedPointsOnAABB(pt, out min, out max);

            // Set up sphere to just encompass these two points
            var s = new SphereStruct();

            s.center = (pt[min] + pt[max]) * 0.5f;
            s.radius = TGCVector3.Dot(pt[max] - s.center, pt[max] - s.center);
            s.radius = FastMath.Sqrt(s.radius);
            return(s);
        }
        /// <summary>
        ///     Given Sphere s and Point p, update s (if needed) to just encompass p
        /// </summary>
        private static void sphereOfSphereAndPt(ref SphereStruct s, TGCVector3 p)
        {
            // Compute squared distance between point and sphere center
            var d     = p - s.center;
            var dist2 = TGCVector3.Dot(d, d);

            // Only update s if point p is outside it
            if (dist2 > s.radius * s.radius)
            {
                var dist      = FastMath.Sqrt(dist2);
                var newRadius = (s.radius + dist) * 0.5f;
                var k         = (newRadius - s.radius) / dist;
                s.radius  = newRadius;
                s.center += d * k;
            }
        }