using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
namespace ScoOteRLoAdeR
{
struct Point
{
public List<double> Data;
public float NormalX, NormalY, NormalZ;
public Point(int DummyVariableForSatisfactionOfStupidCGarbageCompiler)
{
Data = new List<double>();
NormalX = NormalY = NormalZ = 0.0f;
}
}
struct Edge
{
public int Point1Index, Point2Index;
}
struct Face
{
public int Edge1Index, Edge2Index, Edge3Index;
}
struct Polygon
{
public int Face1Index, Face2Index;
}
struct Element
{
public List<int> PolygonsIndex;
public List<int> CubesIndex;
public string ZoneHeader;
public Element(int DummyVariableForSatisfactionOfStupidCGarbageCompiler)
{
PolygonsIndex = new List<int>();
CubesIndex = new List<int>();
ZoneHeader = "";
}
}
struct TriangulatedPolygon
{
public int Vertex1Index, Vertex2Index, Vertex3Index, Color;
}
struct TetrahardonatedCube
{
public int Vertex1Index, Vertex2Index, Vertex3Index, Vertex4Index;
}
struct BoundValues
{
public List<double> data;
public BoundValues(int DummyVariableForSatisfactionOfStupidCGarbageCompiler)
{
data = new List<double>();
}
}
enum type
{
Point,
Block
}
class ScoOteRParser
{
public BoundValues MaxValue;
public BoundValues MinValue;
protected List<Point> Points;
protected List<Edge> Edges;
protected List<Face> Faces;
protected List<TriangulatedPolygon> Facelets;
protected List<TetrahardonatedCube> Tetrahardons;
protected List<Polygon> Polygons;
protected List<MarchedCube> Cubes;
protected List<Element> Elements;
protected List<string> Variables;
private FileStream stream;
private StreamReader streamReader;
private string Title;
public ScoOteRParser()
{
ResetData();
}
public bool Load(string FileName)
{
stream = new FileStream(FileName, FileMode.Open);
streamReader = new StreamReader(stream);
string temp = ReadLine();
if (temp.Substring(0, 5) == "TITLE" || temp[0] == '"')
{
string[] tokens = temp.Split('"');
if (tokens.Length == 1)
Title = tokens[0];
else
Title = tokens[1];
temp = ReadLine();
}
if (temp.Substring(0, 9) == "VARIABLES")
{
temp = temp.Replace(" ", "");
string[] tokens = temp.Split('"');
Variables = new List<string>();
for (int i = 1; i < tokens.Length; i += 2)
{
Variables.Add(tokens[i]);
MaxValue.data.Add(double.MinValue);
MinValue.data.Add(double.MaxValue);
}
if (Variables.IndexOf("Z") == -1)
{
MaxValue.data.Add(double.MinValue);
MinValue.data.Add(double.MaxValue);
}
}
else
{
System.Windows.Forms.MessageBox.Show("Error Loading File\n The file might be corrupted or in the wrong format");
return false;
}
temp = ReadLine();
do
{
temp = temp.Replace(" ", "");
int TypeIndex = temp.IndexOf("F=");
string[] tokens = temp.Substring(TypeIndex).Split('=');
string ZoneHeader = temp.Remove(0, 4);
if (tokens[1].Substring(0, 5) == "POINT")
{
if (!ReadUnstructuredZone(ZoneHeader,type.Point))
return false;
}
else if (tokens[1].Substring(0, 5) == "BLOCK")
{
if (!ReadUnstructuredZone(ZoneHeader, type.Block))
return false;
}
else if (tokens[1].Substring(0, 7) == "FEPOINT")
{
if (!ReadFiniteElementZone(ZoneHeader,type.Point))
return false;
}
else if (tokens[1].Substring(0, 7) == "FEBLOCK")
{
if (!ReadFiniteElementZone(ZoneHeader,type.Block))
return false;
}
temp = ReadLine();
}
while(temp != null && temp.Substring(0,4) == "ZONE");
if (Variables.IndexOf("Z") == -1)
Variables.Insert(2, "Z");
streamReader.Close();
stream.Close();
return true;
}
#region Data Relation extraction functions
private void ReadPoints(int PointsNum)
{
string[] Tokens;
for (int LinesCounter = 0; LinesCounter < PointsNum; LinesCounter++)
{
Point p = new Point(13);
string temp = ReadLine();
Tokens = temp.Split(' ');
int VariablesCounter = 0;
foreach (string str in Tokens)
{
if (str == "")
continue;
if (VariablesCounter == 2 && Variables.IndexOf("Z") == -1)
p.Data.Add(0.0);
if (VariablesCounter == 2 && Variables.IndexOf("Z") != -1)
p.Data.Add(double.Parse(str) * -1);
else
p.Data.Add(double.Parse(str));
VariablesCounter++;
}
Points.Add(p);
CheckMaxMin(p);
}
}
private void ReadBlock(int PointsNum)
{
string[] Tokens;
for (int i = 0; i < PointsNum; i++)
{
Point p = new Point(13);
Points.Add(p);
}
for (int VariablesCounter = 0; VariablesCounter < Variables.Count; VariablesCounter++)
{
int CurrentPointsCount = 0;
if (VariablesCounter == 2 && Variables.IndexOf("Z") == -1)
for (int i = 0; i < Points.Count; i++)
Points[i].Data.Add(0.0);
else
while (CurrentPointsCount < PointsNum)
{
string temp = ReadLine();
Tokens = temp.Split(' ');
foreach (string str in Tokens)
{
if (str == "")
continue;
if (VariablesCounter == 2 && Variables.IndexOf("Z") != -1)
Points[CurrentPointsCount].Data.Add(double.Parse(str) * -1);
else
Points[CurrentPointsCount].Data.Add(double.Parse(str));
CurrentPointsCount++;
}
}
}
foreach(Point p in Points)
CheckMaxMin(p);
}
private bool ReadUnstructuredZone(string ZoneHeader, type Type)
{
int Maxi = 0;
int Maxj = 0;
int Maxk = 0;
int FirstPoint = Points.Count;
Element element = new Element(13);
element.ZoneHeader = ZoneHeader;
#region Parsing Zone Header
string[] Tokens = ZoneHeader.Split(',');
foreach (string str in Tokens)
{
if (str == "")
continue;
if (str[0] == 'I')
{
Maxi = int.Parse(str.Substring(2));
}
else if (str[0] == 'J')
{
Maxj = int.Parse(str.Substring(2));
}
else if (str[0] == 'K')
{
Maxk = int.Parse(str.Substring(2));
}
}
#endregion
#region Reading point
Maxk = (Maxk == 0) ? 1 : Maxk;
int PointsNum = Maxi * Maxj * Maxk;
if (Type == type.Point)
{
ReadPoints(PointsNum);
}
else
{
ReadBlock(PointsNum);
}
#endregion
#region Constructing DataStructure
for (int k = 0; k < Maxk ; k++)
{
for (int j = 0; j < Maxj - 1; j++)
{
for (int i = 0; i < Maxi - 1; i++)
{
Face face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
Polygon poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
#region Generating The Cube Relation
if (Maxk > 1 && k < Maxk - 1)
{
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)));
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)));
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j));
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)));
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)));
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j));
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)));
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1);
face.Edge2Index = AddEdge(i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)), i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
face.Edge3Index = AddEdge(i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1, i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
poly = new Polygon();
poly.Face1Index = Faces.Count - 2;
poly.Face2Index = Faces.Count - 1;
Polygons.Add(poly);
element.PolygonsIndex.Add(Polygons.Count - 1);
MarchedCube c = new MarchedCube();
c.Vertex1 = i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j);
c.Vertex2 = i + FirstPoint + (Maxi * Maxj * k) + (Maxi * j) + 1;
c.Vertex3 = i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j) + 1;
c.Vertex4 = i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * j);
c.Vertex5 = i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1));
c.Vertex6 = i + FirstPoint + (Maxi * Maxj * k) + (Maxi * (j + 1)) + 1;
c.Vertex7 = i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1)) + 1;
c.Vertex8 = i + FirstPoint + (Maxi * Maxj * (k + 1)) + (Maxi * (j + 1));
Cubes.Add(c);
element.CubesIndex.Add(Cubes.Count - 1);
}
#endregion
}
}
}
#endregion
Elements.Add(element);
return true;
}
private bool ReadFiniteElementZone(string ZoneHeader, type Type)
{
int FirstPoint = Points.Count;
int PointsNum = 0;
int PolygonsNum = 0;
string PolygonType = "";
Element element = new Element(13);
element.ZoneHeader = ZoneHeader;
#region Parsing Zone Header
string[] Tokens = ZoneHeader.Split(',');
foreach (string str in Tokens)
{
if (str == "")
continue;
if (str[0] == 'N')
{
PointsNum = int.Parse(str.Substring(2));
}
else if (str.Substring(0, 2) == "ET")
{
PolygonType = str.Substring(3);
}
else if (str[0] == 'E' )
{
PolygonsNum = int.Parse(str.Substring(2));
}
}
#endregion
#region Reading point
if (Type == type.Point)
{
ReadPoints(PointsNum);
}
else
{
ReadBlock(PointsNum);
}
#endregion
#region Constructing DataStructure
Polygon polygon = new Polygon();
for (int PolygonCount = 0; PolygonCount < PolygonsNum; PolygonCount++)
{
string temp = ReadLine();
if (PolygonType == "TRIANGLE")
{
int[] points = new int[3];
Tokens = temp.Split(' ');
int index = 0;
foreach (string str in Tokens)
{
if (str == "")
continue;
points[index] = int.Parse(str) - 1 + FirstPoint;
index++;
}
Face face = new Face();
face.Edge1Index = AddEdge(points[0], points[1]);
face.Edge2Index = AddEdge(points[0], points[2]);
face.Edge3Index = AddEdge(points[1], points[2]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
if (PolygonCount % 2 == 0)
{
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 1;
}
else
{
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
}
}
else if (PolygonType == "QUADRILATERAL" || PolygonType == "TETRAHEDRON")
{
int[] points = new int[4];
Tokens = temp.Split(' ');
int index = 0;
foreach (string str in Tokens)
{
if (str == "")
continue;
points[index] = int.Parse(str) - 1;
index++;
}
Face face = new Face();
face.Edge1Index = AddEdge(points[0], points[1]);
face.Edge2Index = AddEdge(points[0], points[2]);
face.Edge3Index = AddEdge(points[1], points[2]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[2]);
face.Edge2Index = AddEdge(points[0], points[3]);
face.Edge3Index = AddEdge(points[2], points[3]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
}
else if (PolygonType == "BRICK")
{
int[] points = new int[8];
Tokens = temp.Split(' ');
int index = 0;
foreach (string str in Tokens)
{
if (str == "")
continue;
points[index] = int.Parse(str) - 1;
index++;
}
#region Creating the 6 faces of the brick
Face face = new Face();
face.Edge1Index = AddEdge(points[0], points[1]);
face.Edge2Index = AddEdge(points[0], points[2]);
face.Edge3Index = AddEdge(points[1], points[2]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[2]);
face.Edge2Index = AddEdge(points[0], points[3]);
face.Edge3Index = AddEdge(points[2], points[3]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[4], points[5]);
face.Edge2Index = AddEdge(points[4], points[6]);
face.Edge3Index = AddEdge(points[5], points[6]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[4], points[6]);
face.Edge2Index = AddEdge(points[4], points[7]);
face.Edge3Index = AddEdge(points[6], points[7]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[3], points[2]);
face.Edge2Index = AddEdge(points[3], points[6]);
face.Edge3Index = AddEdge(points[2], points[6]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[3], points[6]);
face.Edge2Index = AddEdge(points[3], points[7]);
face.Edge3Index = AddEdge(points[6], points[7]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[1]);
face.Edge2Index = AddEdge(points[0], points[5]);
face.Edge3Index = AddEdge(points[1], points[5]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[5]);
face.Edge2Index = AddEdge(points[0], points[4]);
face.Edge3Index = AddEdge(points[5], points[4]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[1], points[5]);
face.Edge2Index = AddEdge(points[1], points[6]);
face.Edge3Index = AddEdge(points[5], points[6]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[1], points[6]);
face.Edge2Index = AddEdge(points[1], points[2]);
face.Edge3Index = AddEdge(points[6], points[2]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[4]);
face.Edge2Index = AddEdge(points[0], points[7]);
face.Edge3Index = AddEdge(points[4], points[7]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
face = new Face();
face.Edge1Index = AddEdge(points[0], points[7]);
face.Edge2Index = AddEdge(points[0], points[3]);
face.Edge3Index = AddEdge(points[7], points[3]);
Faces.Add(face);
SetFaceNormals(Faces.Count - 1);
polygon = new Polygon();
polygon.Face1Index = Faces.Count - 2;
polygon.Face2Index = Faces.Count - 1;
Polygons.Add(polygon);
element.PolygonsIndex.Add(Polygons.Count - 1);
MarchedCube c = new MarchedCube();
c.Vertex1 = points[0];
c.Vertex2 = points[1];
c.Vertex3 = points[2];
c.Vertex4 = points[3];
c.Vertex5 = points[4];
c.Vertex6 = points[5];
c.Vertex7 = points[6];
c.Vertex8 = points[7];
Cubes.Add(c);
element.CubesIndex.Add(Cubes.Count - 1);
#endregion
}
}
#endregion
Elements.Add(element);
return true;
}
#endregion
public virtual void Triangulate()
{
foreach (Element e in Elements)
{
if (e.ZoneHeader.Contains("TRIANGLE"))
{
for (int i = 0; i < e.PolygonsIndex.Count; i++)
{
TriangulatedPolygon triangle = new TriangulatedPolygon();
triangle.Vertex1Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face1Index].Edge1Index].Point1Index;
triangle.Vertex2Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face1Index].Edge1Index].Point2Index;
triangle.Vertex3Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face1Index].Edge2Index].Point2Index;
Facelets.Add(triangle);
triangle = new TriangulatedPolygon();
triangle.Vertex1Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face2Index].Edge1Index].Point1Index;
triangle.Vertex2Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face2Index].Edge1Index].Point2Index;
triangle.Vertex3Index = Edges[Faces[Polygons[e.PolygonsIndex[i]].Face2Index].Edge2Index].Point2Index;
Facelets.Add(triangle);
}
}
else
{
for (int i = 0; i < e.PolygonsIndex.Count; i++)
{
int[] points = GetPolygonIndices(e.PolygonsIndex[i]);
if (points == null)
continue;
Point p = new Point(13);
for (int vars = 0; vars < Variables.Count; vars++)
{
p.Data.Add((double)(Points[points[0]].Data[vars] + Points[points[1]].Data[vars]
+ Points[points[2]].Data[vars] + Points[points[3]].Data[vars]) / 4);
}
p.NormalX = Points[points[0]].NormalX;
p.NormalY = Points[points[0]].NormalY;
p.NormalZ = Points[points[0]].NormalZ;
Points.Add(p);
TriangulatedPolygon[] triangles = new TriangulatedPolygon[4];
triangles[0] = new TriangulatedPolygon();
triangles[0].Vertex1Index = points[0];
triangles[0].Vertex2Index = points[1];
triangles[0].Vertex3Index = Points.Count - 1;
triangles[1] = new TriangulatedPolygon();
triangles[1].Vertex1Index = points[1];
triangles[1].Vertex2Index = points[2];
triangles[1].Vertex3Index = Points.Count - 1;
triangles[2] = new TriangulatedPolygon();
triangles[2].Vertex1Index = points[2];
triangles[2].Vertex2Index = points[3];
triangles[2].Vertex3Index = Points.Count - 1;
triangles[3] = new TriangulatedPolygon();
triangles[3].Vertex1Index = points[3];
triangles[3].Vertex2Index = points[0];
triangles[3].Vertex3Index = Points.Count - 1;
Facelets.AddRange(triangles);
}
}
}
}
public void Tetrahardonate()
{
Tetrahardons = new List<TetrahardonatedCube>();
for (int i = 0 ; i < Cubes.Count; i++)
{
Point temp = new Point(13);
int [] points = GetCubeIndicies(i);
for (int vars = 0; vars < Variables.Count; vars ++)
{
temp.Data.Add((Points[points[0]].Data[vars] + Points[points[1]].Data[vars]
+ Points[points[2]].Data[vars] + Points[points[3]].Data[vars]
+ Points[points[4]].Data[vars] + Points[points[5]].Data[vars]
+ Points[points[6]].Data[vars] + Points[points[7]].Data[vars]) / 8);
}
Points.Add(temp);
TetrahardonatedCube t = new TetrahardonatedCube();
t.Vertex1Index = points[0];
t.Vertex2Index = points[1];
t.Vertex3Index = points[2];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[0];
t.Vertex2Index = points[2];
t.Vertex3Index = points[3];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[0];
t.Vertex2Index = points[3];
t.Vertex3Index = points[4];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[3];
t.Vertex2Index = points[4];
t.Vertex3Index = points[7];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[4];
t.Vertex2Index = points[5];
t.Vertex3Index = points[6];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[4];
t.Vertex2Index = points[6];
t.Vertex3Index = points[7];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[1];
t.Vertex2Index = points[2];
t.Vertex3Index = points[5];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[2];
t.Vertex2Index = points[5];
t.Vertex3Index = points[6];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[0];
t.Vertex2Index = points[1];
t.Vertex3Index = points[5];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[0];
t.Vertex2Index = points[5];
t.Vertex3Index = points[4];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[3];
t.Vertex2Index = points[2];
t.Vertex3Index = points[6];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
t = new TetrahardonatedCube();
t.Vertex1Index = points[3];
t.Vertex2Index = points[6];
t.Vertex3Index = points[7];
t.Vertex4Index = Points.Count - 1;
Tetrahardons.Add(t);
}
}
public List<TriangulatedPolygon> FACELETS
{
get { return Facelets; }
}
public List<TetrahardonatedCube> TETRAHARDONS
{
get { return Tetrahardons; }
}
public List<MarchedCube> CUBES
{
get { return Cubes; }
}
public List<Point> POINTS
{
get { return Points; }
}
#region HelperFunctions
private void ResetData()
{
Points = new List<Point>();
Edges = new List<Edge>();
Faces = new List<Face>();
Facelets = new List<TriangulatedPolygon>();
Polygons = new List<Polygon>();
Cubes = new List<MarchedCube>();
Elements = new List<Element>();
MaxValue = new BoundValues(13);
MinValue = new BoundValues(13);
}
private string ReadLine()
{
string str = streamReader.ReadLine();
Zeft: while (str == "" || str == string.Empty)
{
str = streamReader.ReadLine();
}
while (str != null && str[0] == ' ')
{
str = str.Remove(0, 1);
if (str == "")
goto Zeft;
}
return (str != null )?str.ToUpper():null;
}
private void CheckMaxMin(Point p)
{
if (p.Data[0] >= MaxValue.data[0] && p.Data[1] >= MaxValue.data[1] && p.Data[2] >= MaxValue.data[2])
{
MaxValue.data[0] = p.Data[0];
MaxValue.data[1] = p.Data[1];
MaxValue.data[2] = p.Data[2];
}
if (p.Data[0] <= MinValue.data[0] && p.Data[1] <= MinValue.data[1] && p.Data[2] <= MinValue.data[2])
{
MinValue.data[0] = p.Data[0];
MinValue.data[1] = p.Data[1];
MinValue.data[2] = p.Data[2];
}
for (int i = 3; i < p.Data.Count; i++)
{
if (p.Data[i] >= MaxValue.data[i])
MaxValue.data[i] = p.Data[i];
if (p.Data[i] <= MinValue.data[i])
MinValue.data[i] = p.Data[i];
}
}
private int AddEdge(int Index1, int Index2)
{
Edge e = new Edge();
e.Point1Index = Index1;
e.Point2Index = Index2;
int index = Edges.IndexOf(e);
if (index == -1)
{
Edges.Add(e);
return Edges.Count - 1;
}
return index;
}
private int[] GetPolygonIndices(int PolygonIndex)
{
int[] temp = new int[4];
temp[0] = Edges[Faces[Polygons[PolygonIndex].Face1Index].Edge1Index].Point1Index;
temp[1] = Edges[Faces[Polygons[PolygonIndex].Face1Index].Edge1Index].Point2Index;
temp[2] = Edges[Faces[Polygons[PolygonIndex].Face1Index].Edge2Index].Point2Index;
temp[3] = Edges[Faces[Polygons[PolygonIndex].Face2Index].Edge3Index].Point2Index;
return temp;
}
private int[] GetCubeIndicies(int CubeIndex)
{
int[] CubeIndecies = new int[8];
CubeIndecies[0] = Cubes[CubeIndex].Vertex1;
CubeIndecies[1] = Cubes[CubeIndex].Vertex2;
CubeIndecies[2] = Cubes[CubeIndex].Vertex3;
CubeIndecies[3] = Cubes[CubeIndex].Vertex4;
CubeIndecies[4] = Cubes[CubeIndex].Vertex5;
CubeIndecies[5] = Cubes[CubeIndex].Vertex6;
CubeIndecies[6] = Cubes[CubeIndex].Vertex7;
CubeIndecies[7] = Cubes[CubeIndex].Vertex8;
return CubeIndecies;
}
private void SetFaceNormals(int FaceIndex)
{
Point p1 = Points[Edges[Faces[FaceIndex].Edge1Index].Point1Index];
Point p2 = Points[Edges[Faces[FaceIndex].Edge2Index].Point2Index];
Point p3 = Points[Edges[Faces[FaceIndex].Edge3Index].Point1Index];
if (p1.NormalX == 0 && p1.NormalY == 0 && p1.NormalZ == 0)
{
Vector3 v1 = new Vector3((float)(p2.Data[0] - p1.Data[0]),
(float)(p2.Data[1] - p1.Data[1]),
(float)(p2.Data[2] - p1.Data[2]));
Vector3 v2 = new Vector3((float)(p3.Data[0] - p1.Data[0]),
(float)(p3.Data[1] - p1.Data[1]),
(float)(p3.Data[2] - p1.Data[2]));
v1 = Vector3.Cross(v2, v1);
v1.Normalize();
p1.NormalX = v1.X;
p1.NormalY = v1.Y;
p1.NormalZ = v1.Z;
Points[Edges[Faces[FaceIndex].Edge1Index].Point1Index] = p1;
}
if (p2.NormalX == 0 && p2.NormalY == 0 && p2.NormalZ == 0)
{
Vector3 v1 = new Vector3((float)(p1.Data[0] - p2.Data[0]),
(float)(p1.Data[1] - p2.Data[1]),
(float)(p1.Data[2] - p2.Data[2]));
Vector3 v2 = new Vector3((float)(p3.Data[0] - p2.Data[0]),
(float)(p3.Data[1] - p2.Data[1]),
(float)(p3.Data[2] - p2.Data[2]));
v1 = Vector3.Cross(v1, v2);
v1.Normalize();
p2.NormalX = v1.X;
p2.NormalY = v1.Y;
p2.NormalZ = v1.Z;
Points[Edges[Faces[FaceIndex].Edge2Index].Point2Index] = p2;
}
if (p3.NormalX == 0 && p3.NormalY == 0 && p3.NormalZ == 0)
{
Vector3 v1 = new Vector3((float)(p1.Data[0] - p3.Data[0]),
(float)(p1.Data[1] - p3.Data[1]),
(float)(p1.Data[2] - p3.Data[2]));
Vector3 v2 = new Vector3((float)(p2.Data[0] - p3.Data[0]),
(float)(p2.Data[1] - p3.Data[1]),
(float)(p2.Data[2] - p3.Data[2]));
v1 = Vector3.Cross(v2, v1);
v1.Normalize();
p3.NormalX = v1.X;
p3.NormalY = v1.Y;
p3.NormalZ = v1.Z;
Points[Edges[Faces[FaceIndex].Edge3Index].Point1Index] = p3;
}
}
protected void InverseNormals()
{
for (int i = 0; i < Points.Count; i++)
{
Point p = Points[i];
p.NormalX *= -1;
p.NormalY *= -1;
p.NormalZ *= -1;
Points[i] = p;
}
}
#endregion
}
}
when i am running the proect it works, but when i choose file (.poly) to load in application error(
Index and length must refer to a location within the string.
Parameter name: length ) appears
|