public void UpdateAvalonBridgeModelClrs(ABModel3D[] models)
 {
     int[] clrs = models[0].Geometry.VertexClrs;
     int cacheIdx = 0;
     for (int i = 0; i < clrs.Length; i += 6)
     {
         clrs[i] = clrs[i + 1] = clrs[i + 2] = clrs[i + 3] = clrs[i + 4] = clrs[i + 5] = colorSchema[clrSchemeIcache[cacheIdx++]].ToArgb();
     }
 }
        private ABModel3D ConvertMesh(ThreeDSMesh mesh, ABMaterial[] materials)
        {
            // convert vertices
            ABModel3D model = new ABModel3D();
            model.Geometry = new ABGeometry3D();
            if (mesh.verticies != null && mesh.verticies.Length > 0)
            {
                int numVerts = mesh.verticies.Length;
                model.Geometry.Vertices = new Vector3[numVerts];
                model.Geometry.Normals = new Vector3[numVerts];
                for (int vert = 0; vert < numVerts; vert++)
                {
                    model.Geometry.Vertices[vert] = new Vector3(mesh.verticies[vert].x,
                                                                mesh.verticies[vert].y,
                                                                mesh.verticies[vert].z);
                    model.Geometry.Normals[vert] = new Vector3(mesh.verticies[vert].nx,
                                                               mesh.verticies[vert].ny,
                                                               mesh.verticies[vert].nz);
                }
            }

            // uv map
            if (mesh.uvMap != null)
            {
                int numCoords = mesh.uvMap.Length;
                model.Geometry.TexCoords = new Vector2[numCoords];
                for (int coord = 0; coord < numCoords; coord++)
                {
                    model.Geometry.TexCoords[coord] = new Vector2(mesh.uvMap[coord].u,
                                                                  -mesh.uvMap[coord].v);
                }
            }
            model.Geometry.PrimType = Microsoft.DirectX.Direct3D.PrimitiveType.TriangleList;
            // indices
            if (mesh.groups != null)
            {
                model.Geometry.PrimIndices = new int[mesh.GetNumFaces() * 3];
                int idx = 0;
                foreach (ThreeDSMesh.FaceGroup group in mesh.groups)
                {
                    foreach (ThreeDSMesh.Face face in group.faces)
                    {
                        model.Geometry.PrimIndices[idx++] = face.p1;
                        model.Geometry.PrimIndices[idx++] = face.p2;
                        model.Geometry.PrimIndices[idx++] = face.p3;
                    }
                    if (group.mappings != null && group.mappings.Count > 0)
                    {
                        int numMatIndices = 0;
                        foreach (ThreeDSMesh.MaterialMapping mapping in group.mappings)
                        {
                            if (mapping.mappedFaces != null && mapping.mappedFaces.Length > 0)
                                numMatIndices++;
                        }
                        model.MaterialIndices = new ABMaterialIndex[numMatIndices];
                        int miIdx = 0;
                        foreach (ThreeDSMesh.MaterialMapping mapping in group.mappings)
                        {
                            if (mapping.mappedFaces != null && mapping.mappedFaces.Length > 0)
                            {
                                ABMaterialIndex matIndex = model.MaterialIndices[miIdx++] = new ABMaterialIndex();
                                matIndex.Indices = new int[mapping.mappedFaces.Length];
                                foreach (ABMaterial material in materials)
                                {
                                    if (material.Name == mapping.name)
                                        matIndex.Material = material;
                                }
                                for (int face = 0; face < mapping.mappedFaces.Length; face++)
                                {
                                    matIndex.Indices[face] = mapping.mappedFaces[face];
                                }
                            }
                        }
                    }
                }
            }

            return model;
        }
        public ABModel3D[] ToAvalonBridgeModel()
        {
            ABModel3D[] models = new ABModel3D[2];
            models[0] = new ABModel3D();
            
            // build geometry
            double xi = startPoint.X, yi, minZ = double.PositiveInfinity, maxZ = double.NegativeInfinity;

            int numPointsX = (int)((endPoint.X - startPoint.X) / density + 1);
            int numPointsY = (int)((endPoint.Y - startPoint.Y) / density + 1);

            ABGeometry3D geometry = models[0].Geometry = new ABGeometry3D();
            geometry.PrimType = PrimitiveType.TriangleList;
            int numVerts = numPointsX * numPointsY;

            int vIdx = 0;
            Vector3[] points = new Vector3[numVerts];
            for (int x = 0; x < numPointsX; x++)
            {
                yi = startPoint.Y;
                for (int y = 0; y < numPointsY; y++)
                {
                    double zz = function(xi, yi);
                    points[vIdx++] = new Vector3((float)xi, (float)zz, (float)yi);

                    yi += density;
                    if (minZ > zz) minZ = zz;
                    if (maxZ < zz) maxZ = zz;
                }
                xi += density;
            }
            //e1 = verts[vIdx-2].Position - verts[vIdx-3].Position;
            //e2 = verts[vIdx-1].Position - verts[vIdx-3].Position;
            //faceNormal = Vector3::Normalize(Vector3::Cross(e1, e2));
            
            // tri indices
            int numTris = (numPointsX - 1) * (numPointsY - 1) * 2;
            geometry.Vertices = new Vector3[numTris * 3];
            geometry.VertexClrs = new int[geometry.Vertices.Length];
            int tIdx = 0;
            vIdx = 0;
            float z1, z2;
            double cc = (maxZ - minZ) / (colorSchema.Length - 1.0);
            int cacheIdx = 0;
            clrSchemeIcache = new int[numTris];
            for (int x = 0; x < numPointsX - 1; x++)
            {
                for (int y = 0; y < numPointsY - 1; y++)
                {
                    z1 = points[tIdx].Y;
                    z2 = points[tIdx + 1].Y;
                    int clrIdx = clrSchemeIcache[cacheIdx++] = (int)(((z1 + z2) / 2.0 - minZ) / cc);
                    int triClr = colorSchema[clrIdx].ToArgb();

                    //Vector3 e1 = points[tIdx + 1] - points[tIdx];
                    //Vector3 e2 = points[tIdx + 1 + numPointsX] - points[tIdx];
                    //Vector3 faceNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx];
                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx + 1];
                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx + 1 + numPointsY];

                    //z1 = points[tIdx].Y;
                    //z2 = points[tIdx + 1 + numPointsX].Y;
                    //triClr = colorSchema[(int)(((z1 + z2) / 2.0 - minZ) / cc)].ToArgb();

                    //e1 = points[tIdx + 1 + numPointsX] - points[tIdx];
                    //e2 = points[tIdx + numPointsX] - points[tIdx];
                    //faceNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx];
                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx + 1 + numPointsY];
                    //geometry.Normals[vIdx] = faceNormal;
                    geometry.VertexClrs[vIdx] = triClr;
                    geometry.Vertices[vIdx++] = points[tIdx + numPointsY];

                    tIdx++;
                }
                tIdx++;
            }

            // now outlines
            models[1] = new ABModel3D();
            geometry = models[1].Geometry = new ABGeometry3D();
            geometry.PrimType = PrimitiveType.LineStrip;
            geometry.Vertices = new Vector3[numVerts * 2];
            geometry.Clr = Color.Black.ToArgb();

            // TODO: Use tri strips??
            vIdx = 0;
            int pos = 0;
            // do x strips
            for (int y = 0; y < numPointsY; y++)
            {
                for (int x = 0; x < numPointsX; x++)
                {
                    geometry.Vertices[vIdx] = points[pos];
                    pos += numPointsY;
                    vIdx++;
                }
                y++;
                if (y < numPointsY)
                {
                    pos -= numPointsY;
                    pos++;
                    for (int x = 0; x < numPointsX; x++)
                    {
                        geometry.Vertices[vIdx] = points[pos];
                        pos -= numPointsY;
                        vIdx++;
                    }
                    pos = y + 1;
                }
            }
            // do y strips
            pos = 0;
            for (int x = numPointsX - 1; x >= 0; x--)
            {
                pos = ((x + 1) * numPointsY) - 1;
                for (int y = 0; y < numPointsY; y++)
                {
                    geometry.Vertices[vIdx] = points[pos--];
                    vIdx++;
                }
                x--;
                if (x >= 0)
                {
                    pos -= numPointsY - 1;
                    for (int y = 0; y < numPointsY; y++)
                    {
                        //Vector3 p = points[pos++];
                        geometry.Vertices[vIdx++] = points[pos++];
                    }
                }
            }

            return models;
        }