コード例 #1
0
 static Vector3d newlen = new Vector3d(); // for calculating the radius of this poly
 public void CalcRadius()
 {
     newlen.Set(0, 0, 0);
     for (int c = 0; c < m_points.Length; c++)
     {
         newlen.x = m_center.x - m_points[c].x;
         newlen.y = m_center.y - m_points[c].y;
         newlen.z = m_center.z - m_points[c].z;
         if (newlen.Mag() >= m_radius)
         {
             m_radius = newlen.Mag();
         }
     }
 }
コード例 #2
0
        void CalculatePlaneEquation()
        {
            double   len;
            int      i;
            Vector3d ref1, norm, v1, v2;
            Point3d  vert1, vert2;

            ref1 = new Vector3d();
            norm = new Vector3d();
            v1   = new Vector3d();
            v2   = new Vector3d();

            for (i = 0; i < m_points.Length; i++)
            {
                vert1 = m_points[i];
                vert2 = m_points[(i + 1) % m_points.Length];

                v1.x = vert1.x;
                v1.y = vert1.y;
                v1.z = vert1.z;

                v2.x = vert2.x;
                v2.y = vert2.y;
                v2.z = vert2.z;

                norm.x += (v1.y - v2.y) * (v1.z + v2.z);
                norm.y += (v1.z - v2.z) * (v1.x + v2.x);
                norm.z += (v1.x - v2.x) * (v1.y + v2.y);
                ref1.x += v1.x;
                ref1.y += v1.y;
                ref1.z += v1.z;
            }

            len     = norm.Mag();
            plane.a = norm.x / len;
            plane.b = norm.y / len;
            plane.c = norm.z / len;
            len    *= m_points.Length;
            plane.d = -ref1.Dot(norm) / len;
        }
コード例 #3
0
        // split edge v1-v2. v3 is the last corner in the triangle and is used for computing normals
        int SplitEdge(int v1, int v2, Vector3d norm1, Vector3d norm2, out Vector3d norm12)
        {
            EdgeAmf edge = m_pointList[v1].FindEdge(v2);

            if (edge == null)
            {
                edge = m_pointList[v2].FindEdge(v1);
                if (edge != null)
                {
                    // swap verteces to match edge
                    int tv = v1;
                    v1 = v2;
                    v2 = tv;
                    Vector3d tnorm = norm1;
                    norm1 = norm2;
                    norm2 = tnorm;
                }
            }

            Vector3d t1, t2;
            PointAmf pamf1 = m_pointList[v1];
            PointAmf pamf2 = m_pointList[v2];
            Point3d  pt1   = pamf1.pt;
            Point3d  pt2   = pamf2.pt;
            PointAmf pamf;
            float    x, y, z;

            // calculate edge vector
            x = pt2.x - pt1.x;
            y = pt2.y - pt1.y;
            z = pt2.z - pt1.z;
            Vector3d edgeDir = new Vector3d(x, y, z);

            // first see if we have an edge for this segment
            if (edge != null)
            {
                // if this edge was already split, return result
                if (edge.v12 >= 0)
                {
                    norm12 = CalcCenterNormal(norm1, norm2, edge.t12);
                    return(edge.v12);
                }
                t1 = edge.t1;
                t2 = edge.t2;
            }
            else
            {
                t1 = GetTangetFromNormal(norm1, edgeDir);
                t2 = GetTangetFromNormal(norm2, edgeDir);
            }

            float d = edgeDir.Mag();

            // calculate mid point using Hermite interpolation
            x = 0.5f * pt1.x + 0.125f * t1.x * d + 0.5f * pt2.x - 0.125f * t2.x * d;
            y = 0.5f * pt1.y + 0.125f * t1.y * d + 0.5f * pt2.y - 0.125f * t2.y * d;
            z = 0.5f * pt1.z + 0.125f * t1.z * d + 0.5f * pt2.z - 0.125f * t2.z * d;

            pamf    = new PointAmf();
            pamf.pt = new Point3d(x, y, z);
            int v = m_pointList.Count;

            m_pointList.Add(pamf);

            // calculate new tanget and new normal
            x = -1.5f * pt1.x - 0.25f * t1.x * d + 1.5f * pt2.x - 0.25f * t2.x * d;
            y = -1.5f * pt1.y - 0.25f * t1.y * d + 1.5f * pt2.y - 0.25f * t2.y * d;
            z = -1.5f * pt1.z - 0.25f * t1.z * d + 1.5f * pt2.z - 0.25f * t2.z * d;
            Vector3d tanget = new Vector3d(x, y, z);

            tanget.Normalize();

            norm12 = CalcCenterNormal(norm1, norm2, tanget);

            if (edge == null)
            {
                //pamf.normal = GetNormalFromTanget(norm1, tanget);
                // create an edge for this segment
                edge    = new EdgeAmf();
                edge.v1 = v1;
                edge.v2 = v2;
                edge.t1 = t1;
                edge.t2 = t2;
                pamf1.AddEdge(edge);
            }
            edge.t12 = tanget;
            edge.v12 = m_pointList.Count - 1; // saves double computation

            //tanget.Normalize();
            // save 2 split edges
            EdgeAmf edge1 = new EdgeAmf();

            edge1.v1 = v1;
            edge1.v2 = v;
            edge1.t1 = t1;
            edge1.t2 = tanget;
            pamf1.AddEdge(edge1);

            EdgeAmf edge2 = new EdgeAmf();

            edge2.v1 = v;
            edge2.v2 = v2;
            edge2.t1 = tanget;
            edge2.t2 = t2;
            pamf.AddEdge(edge2);

            return(v);
        }
コード例 #4
0
        void CalculatePlaneEquation()
        {
            double           len;
                int             i;
                Vector3d          ref1, norm, v1, v2;
                Point3d          vert1, vert2;
                ref1 = new Vector3d();
                norm = new Vector3d();
                v1 = new Vector3d();
                v2 = new Vector3d();

                for(i=0; i < m_points.Length; i++)
                {
                        vert1 = m_points[i];
                        vert2 = m_points[(i+1) % m_points.Length];

                        v1.x = vert1.x;
                        v1.y = vert1.y;
                        v1.z = vert1.z;

                        v2.x = vert2.x;
                        v2.y = vert2.y;
                        v2.z = vert2.z;

                        norm.x += (v1.y - v2.y)*(v1.z + v2.z);
                        norm.y += (v1.z - v2.z)*(v1.x + v2.x);
                        norm.z += (v1.x - v2.x)*(v1.y + v2.y);
                        ref1.x += v1.x;
                        ref1.y += v1.y;
                        ref1.z += v1.z;
                }

                len = norm.Mag();
                plane.a = norm.x / len;
                plane.b = norm.y / len;
                plane.c = norm.z / len;
                len *= m_points.Length;
                plane.d = -ref1.Dot(norm) / len;
        }
コード例 #5
0
 public void CalcRadius()
 {
     Vector3d newlen = new Vector3d();
     newlen.Set(0, 0, 0, 0);
     for (int c = 0; c < m_points.Length; c++)
     {
         newlen.x = m_center.x - m_points[c].x;
         newlen.y = m_center.y - m_points[c].y;
         newlen.z = m_center.z - m_points[c].z;
         if(newlen.Mag() >= m_radius)
         {
             m_radius = newlen.Mag();
         }
     }
 }
コード例 #6
0
        public static bool IntersectSphere(Point3d start,Point3d end,ref Point3d intersect, Point3d center,double radius)
        {
            bool retval = false;
            double EO;//EO is distance from start of ray to center of sphere
            double d,disc,v;//v is length of direction ray
            Vector3d V,temp;//V is unit vector of the ray
            temp =new Vector3d();
            V = new Vector3d();

            temp.Set(center.x - start.x,center.y - start.y,	center.z - start.z,0);

            EO = temp.Mag(); // unnormalized length
            V.Set(end.x - start.x,end.y - start.y,end.z - start.z,0);
            v = V.Mag();// magnitude of direction vector
            V.Normalize();// normalize the direction vector
            disc = (radius*radius) - ((EO*EO) - (v*v));
            if(disc < 0.0f)
            {
                retval = false;// no intersection
            }
            else
            { // compute the intersection point
                retval = true;
                d = Math.Sqrt(disc);
                intersect.x = start.x + ((v-d)*V.x);
                intersect.y = start.y + ((v-d)*V.y);
                intersect.z = start.z + ((v-d)*V.z);
            }
            return retval;
        }
コード例 #7
0
        // split edge v1-v2. v3 is the last corner in the triangle and is used for computing normals
        int SplitEdge(int v1, int v2, Vector3d norm1, Vector3d norm2, out Vector3d norm12)
        {
            EdgeAmf edge = m_pointList[v1].FindEdge(v2);
            if (edge == null)
            {
                edge = m_pointList[v2].FindEdge(v1);
                if (edge != null)
                {
                    // swap verteces to match edge
                    int tv = v1;
                    v1 = v2;
                    v2 = tv;
                    Vector3d tnorm = norm1;
                    norm1 = norm2;
                    norm2 = tnorm;
                }
            }

            Vector3d t1, t2;
            PointAmf pamf1 = m_pointList[v1];
            PointAmf pamf2 = m_pointList[v2];
            Point3d pt1 = pamf1.pt;
            Point3d pt2 = pamf2.pt;
            PointAmf pamf;
            float x, y, z;

            // calculate edge vector
            x = pt2.x - pt1.x;
            y = pt2.y - pt1.y;
            z = pt2.z - pt1.z;
            Vector3d edgeDir = new Vector3d(x, y, z);

            // first see if we have an edge for this segment
            if (edge != null)
            {
                // if this edge was already split, return result
                if (edge.v12 >= 0)
                {
                    norm12 = CalcCenterNormal(norm1, norm2, edge.t12);
                    return edge.v12;
                }
                t1 = edge.t1;
                t2 = edge.t2;
            }
             else
            {
                t1 = GetTangetFromNormal(norm1, edgeDir);
                t2 = GetTangetFromNormal(norm2, edgeDir);
            }

            float d = edgeDir.Mag();

            // calculate mid point using Hermite interpolation
            x = 0.5f * pt1.x + 0.125f * t1.x * d + 0.5f * pt2.x - 0.125f * t2.x * d;
            y = 0.5f * pt1.y + 0.125f * t1.y * d + 0.5f * pt2.y - 0.125f * t2.y * d;
            z = 0.5f * pt1.z + 0.125f * t1.z * d + 0.5f * pt2.z - 0.125f * t2.z * d;

            pamf = new PointAmf();
            pamf.pt = new Point3d(x, y, z);
            int v = m_pointList.Count;
            m_pointList.Add(pamf);

            // calculate new tanget and new normal
            x = -1.5f * pt1.x - 0.25f * t1.x * d + 1.5f * pt2.x - 0.25f * t2.x * d;
            y = -1.5f * pt1.y - 0.25f * t1.y * d + 1.5f * pt2.y - 0.25f * t2.y * d;
            z = -1.5f * pt1.z - 0.25f * t1.z * d + 1.5f * pt2.z - 0.25f * t2.z * d;
            Vector3d tanget = new Vector3d(x, y, z);
            tanget.Normalize();

            norm12 = CalcCenterNormal(norm1, norm2, tanget);
            
            if (edge == null)
            {
                //pamf.normal = GetNormalFromTanget(norm1, tanget);
                // create an edge for this segment
                edge = new EdgeAmf();
                edge.v1 = v1;
                edge.v2 = v2;
                edge.t1 = t1;
                edge.t2 = t2;
                pamf1.AddEdge(edge);
            }
            edge.t12 = tanget;
            edge.v12 = m_pointList.Count - 1; // saves double computation 

            //tanget.Normalize();
            // save 2 split edges
            EdgeAmf edge1 = new EdgeAmf();
            edge1.v1 = v1;
            edge1.v2 = v;
            edge1.t1 = t1;
            edge1.t2 = tanget;
            pamf1.AddEdge(edge1);

            EdgeAmf edge2 = new EdgeAmf();
            edge2.v1 = v;
            edge2.v2 = v2;
            edge2.t1 = tanget;
            edge2.t2 = t2;
            pamf.AddEdge(edge2);

            return v;
        }