using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using QuantumConcepts.Formats.StereoLithography;
namespace QuantumConcepts.Formats.StereoLithography
{
/// A representation of a facet which is defined by its location () and directionality ().
public class Facet : IEquatable, IEnumerable
{
/// Indicates the directionality of the .
public Normal Normal { get; set; }
/// Indicates the location of the .
public IList Vertices { get; set; }
/// Additional data attached to the facet.
/// Depending on the source of the STL, this could be used to indicate such things as the color of the . This functionality only exists in binary STLs.
public UInt16 AttributeByteCount { get; set; }
/// Creates a new, empty .
public Facet()
{
this.Vertices = new List();
}
/// Creates a new using the provided parameters.
/// The directionality of the .
/// The location of the .
/// Additional data to attach to the .
public Facet(Normal normal, IEnumerable vertices, UInt16 attributeByteCount)
: this()
{
this.Normal = normal;
this.Vertices = vertices.ToList();
this.AttributeByteCount = attributeByteCount;
}
/// Writes the as text to the .
/// The writer to which the will be written at the current position.
public void Write(StreamWriter writer)
{
writer.Write("\t");
writer.WriteLine(this.ToString());
writer.WriteLine("\t\touter loop");
//Write each vertex.
this.Vertices.ForEach(o => o.Write(writer));
writer.WriteLine("\t\tendloop");
writer.WriteLine("\tendfacet");
}
/// Writes the as binary to the .
/// The writer to which the will be written at the current position.
public void Write(BinaryWriter writer)
{
//Write the normal.
this.Normal.Write(writer);
//Write each vertex.
this.Vertices.ForEach(o => o.Write(writer));
//Write the attribute byte count.
writer.Write(this.AttributeByteCount);
}
/// Returns the string representation of this .
public override string ToString()
{
return "facet {0}".Interpolate(this.Normal);
}
/// Determines whether or not this instance is the same as the instance.
/// The to which to compare.
public bool Equals(Facet other)
{
return (this.Normal.Equals(other.Normal)
&& this.Vertices.Count == other.Vertices.Count
&& this.Vertices.All((i, o) => o.Equals(other.Vertices[i])));
}
/// Iterates through the collection.
public IEnumerator GetEnumerator()
{
return this.Vertices.GetEnumerator();
}
/// Iterates through the collection.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// Reads a single from the .
/// The reader which contains a to be read at the current position
public static Facet Read(StreamReader reader)
{
if (reader == null)
return null;
//Create the facet.
Facet facet = new Facet();
//Read the normal.
if ((facet.Normal = Normal.Read(reader)) == null)
return null;
//Skip the "outer loop".
reader.ReadLine();
//Read 3 vertices.
facet.Vertices = Enumerable.Range(0, 3).Select(o => Vertex.Read(reader)).ToList();
//Read the "endloop" and "endfacet".
reader.ReadLine();
reader.ReadLine();
return facet;
}
/// Reads a single from the .
/// The reader which contains a to be read at the current position
public static Facet Read(BinaryReader reader)
{
if (reader == null)
return null;
//Create the facet.
Facet facet = new Facet();
//Read the normal.
facet.Normal = Normal.Read(reader);
//Read 3 vertices.
facet.Vertices = Enumerable.Range(0, 3).Select(o => Vertex.Read(reader)).ToList();
//Read the attribute byte count.
facet.AttributeByteCount = reader.ReadUInt16();
return facet;
}
}
}