private void Update(EvaluationContext context) { var path = Path.GetValue(context); var mesh = ObjMesh.LoadFromFile(path); if (mesh == null) { Log.Warning($"Can't read file {path}"); return; } // Prepare sorting var sortedVertexIndices = Enumerable.Range(0, mesh.Positions.Count).ToList(); var sorting = Sorting.GetValue(context); if (sorting != (int)ObjMesh.SortDirections.Ignore) { var sortAxisIndex = _sortAxisAndDirections[sorting][0]; var sortDirection = _sortAxisAndDirections[sorting][1]; sortedVertexIndices.Sort((v1, v2) => mesh.Positions[v1][sortAxisIndex].CompareTo(mesh.Positions[v2][sortAxisIndex]) * sortDirection); } // Export var exportMode = (Modes)Mode.GetValue(context); switch (exportMode) { case Modes.AllVertices: { //var list = new StructuredList<Point>(pointCount); Log.Warning("Object mode not implemented", SymbolChildId); break; } case Modes.Vertices_ColorInOrientation: case Modes.Vertices_GrayscaleInOrientation: case Modes.Vertices_GrayscaleAsW: { if (mesh.Colors.Count == 0) { Log.Warning($"{path} doesn't contain colors definitions. You can use MeshLab to export such files.", SymbolChildId); } if (mesh.Positions.Count == 0) { Log.Warning($"{path} doesn't contain vertex definitions.", SymbolChildId); } try { _points = new StructuredList <Point>(mesh.Positions.Count); for (var vertexIndex = 0; vertexIndex < mesh.Positions.Count; vertexIndex++) { var sortedVertexIndex = sortedVertexIndices[vertexIndex]; var c = (sortedVertexIndex >= mesh.Colors.Count) ? SharpDX.Vector4.One : mesh.Colors[sortedVertexIndex]; if (exportMode == Modes.Vertices_GrayscaleAsW) { _points.TypedElements[vertexIndex] = new Point() { Position = new Vector3( mesh.Positions[sortedVertexIndex].X, mesh.Positions[sortedVertexIndex].Y, mesh.Positions[sortedVertexIndex].Z), Orientation = Quaternion.Identity, W = (c.X + c.Y + c.Z) / 3, }; } else if (exportMode == Modes.Vertices_GrayscaleInOrientation) { var gray = (c.X + c.Y + c.Z) / 3; _points.TypedElements[vertexIndex] = new Point() { Position = new Vector3( mesh.Positions[sortedVertexIndex].X, mesh.Positions[sortedVertexIndex].Y, mesh.Positions[sortedVertexIndex].Z), Orientation = new Quaternion(gray, gray, gray, 1), W = 1, }; } else { _points.TypedElements[vertexIndex] = new Point() { Position = new Vector3( mesh.Positions[sortedVertexIndex].X, mesh.Positions[sortedVertexIndex].Y, mesh.Positions[sortedVertexIndex].Z), Orientation = new Quaternion(c.X, c.Y, c.Z, c.W), W = 1 }; } } Log.Debug($"loaded {path} with and {mesh.Colors.Count} colored points"); } catch (Exception e) { Log.Error("Reading vertices failed " + e); } break; } case Modes.LinesVertices: { try { int segmentCount = 0; int vertexCount = 0; int lastVertexIndex = -1; foreach (var line in mesh.Lines) { vertexCount++; if (line.V0 != lastVertexIndex) { segmentCount++; } lastVertexIndex = line.V2; } int countIncludingSeparators = vertexCount + segmentCount * 2; _points = new StructuredList <Point>(countIncludingSeparators); var pointIndex = 0; lastVertexIndex = -1; foreach (var line in mesh.Lines) { if (pointIndex > 0 && line.V0 != lastVertexIndex) { _points.TypedElements[pointIndex++] = new Point() { Position = new Vector3( mesh.Positions[sortedVertexIndices[lastVertexIndex]].X, mesh.Positions[sortedVertexIndices[lastVertexIndex]].Y, mesh.Positions[sortedVertexIndices[lastVertexIndex]].Z), W = 1 }; _points.TypedElements[pointIndex++] = Point.Separator(); } _points.TypedElements[pointIndex++] = new Point() { Position = new Vector3( mesh.Positions[sortedVertexIndices[line.V0]].X, mesh.Positions[sortedVertexIndices[line.V0]].Y, mesh.Positions[sortedVertexIndices[line.V0]].Z), W = 1 }; lastVertexIndex = line.V2; } _points.TypedElements[pointIndex] = Point.Separator(); Log.Debug($"loaded {path} with {segmentCount} segments and {vertexCount} points"); } catch (Exception e) { Log.Error("Reading vertices failed " + e); } break; } } Points.Value = _points; }
private void Update(EvaluationContext context) { var resourceManager = ResourceManager.Instance(); string path = Path.GetValue(context); var mesh = ObjMesh.LoadFromFile(path); if (mesh == null) { Log.Warning($"Could not load {path}"); return; } float areaSum = 0.0f; int numVertexEntries = mesh.Faces.Count; var faceBufferData = new FaceEntry[numVertexEntries]; for (int i = 0, faceIndex = 0; faceIndex < mesh.Faces.Count; faceIndex++) { var face = mesh.Faces[faceIndex]; // calc area of triangle Vector3 v0 = mesh.Positions[face.V0]; Vector3 v1 = mesh.Positions[face.V1]; Vector3 v2 = mesh.Positions[face.V2]; Vector3 baseDir = (v1 - v0); float a = baseDir.Length(); baseDir.Normalize(); Vector3 heightStart = v0 + Vector3.Dot(v2 - v0, baseDir) * baseDir; float b = (v2 - heightStart).Length(); float faceArea = a * b * 0.5f; areaSum += faceArea; faceBufferData[i].Pos0 = v0; faceBufferData[i].Pos1 = v1; faceBufferData[i].Pos2 = v2; faceBufferData[i].Normal0 = mesh.Normals[face.V0n]; faceBufferData[i].Normal1 = mesh.Normals[face.V1n]; faceBufferData[i].Normal2 = mesh.Normals[face.V2n]; faceBufferData[i].TexCoord0 = mesh.TexCoords[face.V0t]; faceBufferData[i].TexCoord1 = mesh.TexCoords[face.V1t]; faceBufferData[i].TexCoord2 = mesh.TexCoords[face.V2t]; faceBufferData[i].FaceArea = faceArea; i++; } // normalize face area to 1 float sumReci = 1.0f / areaSum; float cdf = 0.0f; for (int i = 0; i < faceBufferData.Length; i++) { cdf += faceBufferData[i].FaceArea * sumReci; faceBufferData[i].Cdf = cdf; } int stride = 108; resourceManager.SetupStructuredBuffer(faceBufferData, stride * numVertexEntries, stride, ref Buffer); Buffer.DebugName = nameof(PointCloudFromObj); resourceManager.CreateStructuredBufferSrv(Buffer, ref PointCloudSrv.Value); }
private void Update(EvaluationContext context) { var path = Path.GetValue(context); if (path != _lastFilePath || SortVertices.DirtyFlag.IsDirty) { var vertexSorting = SortVertices.GetValue(context); _description = System.IO.Path.GetFileName(path); if (UseGPUCaching.GetValue(context)) { if (MeshBufferCache.TryGetValue(path, out var cachedBuffer)) { Data.Value = cachedBuffer.DataBuffers; return; } } var mesh = ObjMesh.LoadFromFile(path); if (mesh == null || mesh.DistinctDistinctVertices.Count == 0) { Log.Warning($"Can't read file {path}"); return; } mesh.UpdateVertexSorting((ObjMesh.SortDirections)vertexSorting); _lastFilePath = path; var resourceManager = ResourceManager.Instance(); var newData = new DataSet(); var reversedLookup = new int[mesh.DistinctDistinctVertices.Count]; // Create Vertex buffer { var verticesCount = mesh.DistinctDistinctVertices.Count; if (newData.VertexBufferData.Length != verticesCount) { newData.VertexBufferData = new PbrVertex[verticesCount]; } for (var vertexIndex = 0; vertexIndex < verticesCount; vertexIndex++) { var sortedVertexIndex = mesh.SortedVertexIndices[vertexIndex]; var sortedVertex = mesh.DistinctDistinctVertices[sortedVertexIndex]; reversedLookup[sortedVertexIndex] = vertexIndex; newData.VertexBufferData[vertexIndex] = new PbrVertex { Position = mesh.Positions[sortedVertex.PositionIndex], Normal = mesh.Normals[sortedVertex.NormalIndex], Tangent = mesh.VertexTangents[sortedVertexIndex], Bitangent = mesh.VertexBinormals[sortedVertexIndex], Texcoord = mesh.TexCoords[sortedVertex.TextureCoordsIndex], Selection = 1, }; } newData.VertexBufferWithViews.Buffer = newData.VertexBuffer; resourceManager.SetupStructuredBuffer(newData.VertexBufferData, PbrVertex.Stride * verticesCount, PbrVertex.Stride, ref newData.VertexBuffer); resourceManager.CreateStructuredBufferSrv(newData.VertexBuffer, ref newData.VertexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(newData.VertexBuffer, UnorderedAccessViewBufferFlags.None, ref newData.VertexBufferWithViews.Uav); } // Create Index buffer { var faceCount = mesh.Faces.Count; if (newData.IndexBufferData.Length != faceCount) { newData.IndexBufferData = new SharpDX.Int3[faceCount]; } for (var faceIndex = 0; faceIndex < faceCount; faceIndex++) { var face = mesh.Faces[faceIndex]; var v1Index = mesh.GetVertexIndex(face.V0, face.V0n, face.V0t); var v2Index = mesh.GetVertexIndex(face.V1, face.V1n, face.V1t); var v3Index = mesh.GetVertexIndex(face.V2, face.V2n, face.V2t); newData.IndexBufferData[faceIndex] = new SharpDX.Int3(reversedLookup[v1Index], reversedLookup[v2Index], reversedLookup[v3Index]); } newData.IndexBufferWithViews.Buffer = newData.IndexBuffer; const int stride = 3 * 4; resourceManager.SetupStructuredBuffer(newData.IndexBufferData, stride * faceCount, stride, ref newData.IndexBuffer); resourceManager.CreateStructuredBufferSrv(newData.IndexBuffer, ref newData.IndexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(newData.IndexBuffer, UnorderedAccessViewBufferFlags.None, ref newData.IndexBufferWithViews.Uav); } if (UseGPUCaching.GetValue(context)) { MeshBufferCache[path] = newData; } _data = newData; } _data.DataBuffers.VertexBuffer = _data.VertexBufferWithViews; _data.DataBuffers.IndicesBuffer = _data.IndexBufferWithViews; Data.Value = _data.DataBuffers; }
private void Update(EvaluationContext context) { var path = Path.GetValue(context); if (path != _lastFilePath) { _description = System.IO.Path.GetFileName(path); var mesh = ObjMesh.LoadFromFile(path); if (mesh == null) { Log.Error($"Failed to extract edge line points from obj {path}", SymbolChildId); return; } var hashSet = new HashSet <int>(); foreach (var f in mesh.Faces) { InsertVertexPair(f.V0, f.V1); InsertVertexPair(f.V1, f.V2); InsertVertexPair(f.V2, f.V0); } void InsertVertexPair(int from, int to) { if (from < to) { var tmp = from; from = to; to = tmp; } var combined = (to << 16) + from; hashSet.Add(combined); } var count = hashSet.Count; //_pointList = new T3.Core.DataTypes.Point[count * 3]; _pointList.SetLength(count * 3); var index = 0; foreach (var pair in hashSet) { var fromIndex = pair & 0xffff; var toIndex = pair >> 16; var pFrom = mesh.Positions[fromIndex]; _pointList.TypedElements[index] = new Point() { Position = new Vector3(pFrom.X, pFrom.Y, pFrom.Z), Orientation = Quaternion.Identity, W = 1 }; index++; var pTo = mesh.Positions[toIndex]; _pointList.TypedElements[index] = new Point() { Position = new Vector3(pTo.X, pTo.Y, pTo.Z), Orientation = Quaternion.Identity, W = 1 }; index++; _pointList.TypedElements[index] = Point.Separator(); index++; } _lastFilePath = path; } Data.Value = _pointList; }