admin 管理员组

文章数量: 1184232


2024年3月9日发(作者:2019年钢材价格一览表)

}

// Dictionary mapping the DataType enumeration to the corresponding keyword. private static Dictionary Keywords { get { return new Dictionary() { { t, "#" }, { , "g" }, { ingGroup, "s" }, { on, "v" }, { rd, "vt" }, { , "vn" }, { , "f" }, }; } }

///

/// Reads a WavefrontObject instance from the stream. /// /// /// Stream containing the OBJ file content. /// /// /// is null. /// /// /// Error while reading from the stream. /// public WavefrontObject Read(Stream stream) { if (stream == null) throw new ArgumentNullException("stream");

// Create the stream reader for the file var reader = new StreamReader(stream);

// Store the lines here var lines = new List();

// Store the current line here var current = ;

// Read the file line by line and normalize them while ((current = ne()) != null) (NormalizeLine(current));

// Create empty mesh instance var obj = new WavefrontObject();

// Iterate over all lines foreach (string line in lines) { // Get line type and content DataType type = GetType(line); string content = GetContent(line, type);

// Line is a position if (type == on) (ParseVector3(content));

// Line is a texture coordinate if (type == rd)

if (type == rd) (ParseVector2(content));

// Line is a normal vector if (type == ) (ParseVector3(content));

// Line is a mesh sub group if (type == ) (new WavefrontFaceGroup() { Name = content });

// Line is a polygon if (type == ) { // Create the default group for all faces outside a group if ( == 0) (new WavefrontFaceGroup());

// Add the face to the last group added ().(ParseFace(content)); } }

return obj; }

// Trim beginning and end and collapse all whitespace in a string to single space. private string NormalizeLine(string line) { return e((), @"s+", " "); }

// Get the type of data stored in the specified line. private DataType GetType(string line) { // Iterate over the keywords foreach (var item in Keywords) { var type = ; var keyword = ;

// Line starts with current keyword if (r().StartsWith(r() + " ")) { // Return current type return type; } }

// No type return ; }

// Remove the keyword from the start of the line and return the result. // Returns an empty string if the specified type was . private string GetContent(string line, DataType type) { // If empty return empty string, // else remove the keyword from the start return type == : ing(Keywords[type].Length).Trim(); }

// Create an array of floats of arbitary length from a string representation, // where the floats are spearated by whitespace.

// where the floats are spearated by whitespace. private static float[] ParseFloatArray(string str, int count) { var floats = new float[count];

var segments = (' ');

for (int i = 0; i < count; i++) { if (i < ) { try { floats[i] = (segments[i], antCulture); } catch { floats[i] = 0; } } }

return floats; }

// Parse a 3D vector from a string definition in the form of: 2.0 3.0 1.0 private Vector2 ParseVector2(string str) { var components = ParseFloatArray(str, 3);

var vec = new Vector2(components[0], components[1]);

return components[2] == 0 vec : vec / components[2]; }

// Parse a 3D vector from a string definition in the form of: 1.0 2.0 3.0 1.0 private Vector3 ParseVector3(string str) { var components = ParseFloatArray(str, 4);

var vec = new Vector3(components[0], components[1], components[2]);

return components[3] == 0 vec : vec / components[3]; }

// Parse a OBJ face from a string definition. private WavefrontFace ParseFace(string str) { // Split the face definition at whitespace var segments = (new Char[0], EmptyEntries);

var vertices = new List();

// Iterate over the segments foreach (string segment in segments) { // Parse and add the vertex (ParseVertex(segment)); }

// Create and return the face return new WavefrontFace()

{ Vertices = vertices, }; }

// Parse an OBJ vertex from a string definition in the forms of: // 1/2/3 // 1//3 // 1/2 // 1 private WavefrontVertex ParseVertex(string str) { // Split the string definition at the slash separator var segments = ('/');

// Store the vertex indices here var indices = new int[3];

// Iterate 3 times for (int i = 0; i < 3; i++) { // If no segment exists at the location or the segment can not be passed to an integer // Set the index to zero if ( <= i || !se(segments[i], out indices[i])) indices[i] = 0; }

// Create the new vertex return new WavefrontVertex() { Position = indices[0], Texcoord = indices[1], Normal = indices[2], }; } }

///

/// Class representing a Wavefront OBJ 3D mesh. /// public class WavefrontObject { public WavefrontObject() { Groups = new List(); Positions = new List(); Texcoords = new List(); Normals = new List(); }

// Lists containing the vertex components public List Positions { get; private set; } public List Texcoords { get; private set; } public List Normals { get; private set; }

// List of sub meshes public List Groups { get; private set; } }

///

/// Struct representing an Wavefront OBJ face group. /// /// /// Groups contain faces and subdivide a geometry into smaller objects. ///

public class WavefrontFaceGroup { public WavefrontFaceGroup() { Faces = new List(); }

// Name of the sub mesh public string Name { get; set; }

// A list of faces public List Faces { get; set; }

// Get the total number of triangles public int TriangleCount { get { var count = 0;

foreach (var face in Faces) count += leCount;

return count; } } }

///

/// A struct representing a Wavefront OBJ geometry face. /// /// /// A face is described through a list of OBJ vertices. /// It can consist of three or more vertices an can therefore be split up /// into one or more triangles. /// public struct WavefrontFace { public List Vertices { get; set; }

// Number of triangles the face (polygon) consists of public int TriangleCount { get { return - 2; } }

// Number of vertices public int VertexCount { get { return ; } } }

///

/// A struct representing an Wavefront OBJ vertex. /// /// /// OBJ vertices are indexed vertices so instead of vectors /// it has an index for the position, texture coordinate and normal. /// Each of those indices points to a location in a list of vectors. /// public struct WavefrontVertex

{ public WavefrontVertex(int position, int texcoord, int normal) : this() { Position = position; Texcoord = texcoord; Normal = normal; }

// Inidices of the vertex components public int Position { get; set; } public int Normal { get; set; } public int Texcoord { get; set; } }}  


本文标签: 钢材价格 作者 一览表