// transform input point pt1, (rotate "aX, aZ" and move to "center") public static void Transform(Mesh3D model, Point3D center, double aX, double aZ) { double angleX = 3.1415926f * aX / 180; double angleZ = 3.1415926f * aZ / 180; int vertNo = model.GetVertexNo(); for (int i = 0; i < vertNo; i++) { Point3D pt1 = model.GetPoint(i); // rotate from z-axis double x2 = pt1.X * Math.Cos(angleZ) + pt1.Z * Math.Sin(angleZ); double y2 = pt1.Y; double z2 = -pt1.X * Math.Sin(angleZ) + pt1.Z * Math.Cos(angleZ); double x3 = center.X + x2 * Math.Cos(angleX) - y2 * Math.Sin(angleX); double y3 = center.Y + x2 * Math.Sin(angleX) + y2 * Math.Cos(angleX); double z3 = center.Z + z2; model.SetPoint(i, x3, y3, z3); } }
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; }
// set the projection matrix public void CalculateProjectionMatrix(Mesh3D mesh, double scaleFactor) { CalculateProjectionMatrix(mesh.xMin, mesh.xMax, mesh.yMin, mesh.yMax, mesh.zMin, mesh.zMax, scaleFactor); }