Ejemplo n.º 1
0
        public Vector3D GetTriangleNormal(int n)
        {
            Triangle3D tri = GetTriangle(n);
            Point3D    pt0 = GetPoint(tri.n0);
            Point3D    pt1 = GetPoint(tri.n1);
            Point3D    pt2 = GetPoint(tri.n2);

            double dx1 = pt1.X - pt0.X;
            double dy1 = pt1.Y - pt0.Y;
            double dz1 = pt1.Z - pt0.Z;

            double dx2 = pt2.X - pt0.X;
            double dy2 = pt2.Y - pt0.Y;
            double dz2 = pt2.Z - pt0.Z;

            double vx = dy1 * dz2 - dz1 * dy2;
            double vy = dz1 * dx2 - dx1 * dz2;
            double vz = dx1 * dy2 - dy1 * dx2;

            double length = Math.Sqrt(vx * vx + vy * vy + vz * vz);

            return(new Vector3D(vx / length, vy / length, vz / length));
        }
Ejemplo n.º 2
0
        private void SetModel(ArrayList meshs, Material backMaterial)
        {
            int meshNo = meshs.Count;

            if (meshNo == 0)
            {
                return;
            }

            MeshGeometry3D triangleMesh = new MeshGeometry3D();
            int            totalVertNo  = 0;

            for (int j = 0; j < meshNo; j++)
            {
                Mesh3D mesh   = (Mesh3D)meshs[j];
                int    vertNo = mesh.GetVertexNo();
                int    triNo  = mesh.GetTriangleNo();
                if ((vertNo <= 0) || (triNo <= 0))
                {
                    continue;
                }

                double[] vx = new double[vertNo];
                double[] vy = new double[vertNo];
                double[] vz = new double[vertNo];
                for (int i = 0; i < vertNo; i++)
                {
                    vx[i] = vy[i] = vz[i] = 0;
                }

                // get normal of each vertex
                for (int i = 0; i < triNo; i++)
                {
                    Triangle3D tri = mesh.GetTriangle(i);
                    Vector3D   vN  = mesh.GetTriangleNormal(i);
                    int        n0  = tri.n0;
                    int        n1  = tri.n1;
                    int        n2  = tri.n2;

                    vx[n0] += vN.X;
                    vy[n0] += vN.Y;
                    vz[n0] += vN.Z;
                    vx[n1] += vN.X;
                    vy[n1] += vN.Y;
                    vz[n1] += vN.Z;
                    vx[n2] += vN.X;
                    vy[n2] += vN.Y;
                    vz[n2] += vN.Z;
                }
                for (int i = 0; i < vertNo; i++)
                {
                    double length = Math.Sqrt(vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]);
                    if (length > 1e-20)
                    {
                        vx[i] /= length;
                        vy[i] /= length;
                        vz[i] /= length;
                    }
                    triangleMesh.Positions.Add(mesh.GetPoint(i));
                    Color color = mesh.GetColor(i);
                    Point mapPt = mapping.GetMappingPosition(color);
                    triangleMesh.TextureCoordinates.Add(new Point(mapPt.X, mapPt.Y));
                    triangleMesh.Normals.Add(new Vector3D(vx[i], vy[i], vz[i]));
                }

                for (int i = 0; i < triNo; i++)
                {
                    Triangle3D tri = mesh.GetTriangle(i);
                    int        n0  = tri.n0;
                    int        n1  = tri.n1;
                    int        n2  = tri.n2;

                    triangleMesh.TriangleIndices.Add(totalVertNo + n0);
                    triangleMesh.TriangleIndices.Add(totalVertNo + n1);
                    triangleMesh.TriangleIndices.Add(totalVertNo + n2);
                }
                totalVertNo += vertNo;
            }
            //Material material = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
            Material material = mapping.material;

            GeometryModel3D triangleModel = new GeometryModel3D(triangleMesh, material);

            triangleModel.Transform = new Transform3DGroup();
            if (backMaterial != null)
            {
                triangleModel.BackMaterial = backMaterial;
            }

            Content = triangleModel;
        }
Ejemplo n.º 3
0
 public void SetTriangle(int i, int m0, int m1, int m2)
 {
     tris[i] = new Triangle3D(m0, m1, m2);
 }
Ejemplo n.º 4
0
 public void SetTriangle(int n, Triangle3D triangle)
 {
     tris[n] = triangle;
 }