From 181d6c55b9654bdcdcdb6401aa4c4f37ac4fd4f3 Mon Sep 17 00:00:00 2001 From: Jifeng Date: Fri, 25 Jan 2019 21:21:18 +0800 Subject: [PATCH] obj splits performance --- src/Obj23dTiles/Obj23dTiles.csproj | 2 +- src/Obj2Gltf.Tests/ObjTests.cs | 1 - src/Obj2Gltf/Obj2Gltf.csproj | 2 +- src/Obj2Gltf/WaveFront/ObjModel.cs | 249 +++++++++++++++-------- src/ObjConvert.FrameworkTests/Program.cs | 4 +- 5 files changed, 163 insertions(+), 95 deletions(-) diff --git a/src/Obj23dTiles/Obj23dTiles.csproj b/src/Obj23dTiles/Obj23dTiles.csproj index 4eadc85..75a2056 100644 --- a/src/Obj23dTiles/Obj23dTiles.csproj +++ b/src/Obj23dTiles/Obj23dTiles.csproj @@ -12,7 +12,7 @@ https://github.com/arcplus/ObjConvert Copyright © Arcplus 2018 Convert wavefront obj file to 3d tiles, which can be used in cesium - 1.1.1 + 1.1.2 diff --git a/src/Obj2Gltf.Tests/ObjTests.cs b/src/Obj2Gltf.Tests/ObjTests.cs index 48631b4..7e02c4a 100644 --- a/src/Obj2Gltf.Tests/ObjTests.cs +++ b/src/Obj2Gltf.Tests/ObjTests.cs @@ -23,7 +23,6 @@ public void Test_LoadObj() [Fact] public void Test_Split() { - objFile = @"C:\Users\23105\Desktop\RevitModels\6F\model.obj"; Assert.True(System.IO.File.Exists(objFile), "obj file does not exist!"); using (var parser = new ObjParser(objFile)) { diff --git a/src/Obj2Gltf/Obj2Gltf.csproj b/src/Obj2Gltf/Obj2Gltf.csproj index 3562f4f..ad580d1 100644 --- a/src/Obj2Gltf/Obj2Gltf.csproj +++ b/src/Obj2Gltf/Obj2Gltf.csproj @@ -13,7 +13,7 @@ https://github.com/arcplus/ObjConvert Copyright © Arcplus 2018 Convert wavefront obj file to gltf/glb file - 1.1.1 + 1.1.2 1.1.0.0 diff --git a/src/Obj2Gltf/WaveFront/ObjModel.cs b/src/Obj2Gltf/WaveFront/ObjModel.cs index 31863fd..9a31794 100644 --- a/src/Obj2Gltf/WaveFront/ObjModel.cs +++ b/src/Obj2Gltf/WaveFront/ObjModel.cs @@ -78,7 +78,7 @@ public List Split(int level) var pnts = new List[boxes.Count]; var normals = new List[boxes.Count]; var uvs = new List[boxes.Count]; - for(var i = 0;i(); pnts[i] = new List(); @@ -87,15 +87,17 @@ public List Split(int level) } foreach(var g in Geometries) { - var index = GetBoxIndex(g, boxes); - var gg = AddGeo(g, pnts[index], normals[index], uvs[index]); + var geoBox = GetBoxIndex(g, boxes); + var index = geoBox.Index; + var gg = AddGeo(g, geoBox, pnts[index], normals[index], uvs[index]); geoes[index].Add(gg); } var objModels = new List(); for(var i = 0;i< geoes.Length;i++) { if (geoes[i].Count == 0) continue; - var m = new ObjModel { Geometries = geoes[i], Name = Name + "_" + objModels.Count, MatFilename = MatFilename, Materials = Materials }; + var m = new ObjModel { Geometries = geoes[i], Name = Name + "_" + objModels.Count, + MatFilename = MatFilename, Materials = Materials }; if (m.Vertices == null) m.Vertices = new List(); var ps = pnts[i]; foreach(var v in ps) @@ -119,7 +121,8 @@ public List Split(int level) return objModels; } - private static FaceVertex GetVertex(FaceVertex v, Dictionary pnts, Dictionary normals, Dictionary uvs) + private static FaceVertex GetVertex(FaceVertex v, Dictionary pnts, + Dictionary normals, Dictionary uvs) { var v1p = v.V; var v1n = v.N; @@ -139,92 +142,109 @@ private static FaceVertex GetVertex(FaceVertex v, Dictionary pnts, Dic return new FaceVertex(v1p, v1t, v1n); } - private Geometry AddGeo(Geometry g, List pnts, List normals, List uvs) + private Geometry AddGeo(Geometry g, GeomBox box, + List pnts, List normals, List uvs) { var gg = new Geometry { Id = g.Id }; - foreach(var f in g.Faces) + + var pntList = box.Pnts; // new List(); // + var normList = box.Norms; // new List(); // + var uvList = box.Uvs; // new List(); // + + //if (pntList.Count == 0) + //{ + // foreach (var f in g.Faces) + // { + // foreach (var t in f.Triangles) + // { + // var v1 = t.V1; + // if (!pntList.Contains(v1.V)) + // { + // pntList.Add(v1.V); + // } + // if (v1.N > 0 && !normList.Contains(v1.N)) + // { + // normList.Add(v1.N); + // } + // if (v1.T > 0 && !uvList.Contains(v1.T)) + // { + // uvList.Add(v1.T); + // } + // var v2 = t.V2; + // if (!pntList.Contains(v2.V)) + // { + // pntList.Add(v2.V); + // } + // if (v2.N > 0 && !normList.Contains(v2.N)) + // { + // normList.Add(v2.N); + // } + // if (v2.T > 0 && !uvList.Contains(v2.T)) + // { + // uvList.Add(v2.T); + // } + // var v3 = t.V3; + // if (!pntList.Contains(v3.V)) + // { + // pntList.Add(v3.V); + // } + // if (v3.N > 0 && !normList.Contains(v3.N)) + // { + // normList.Add(v3.N); + // } + // if (v3.T > 0 && !uvList.Contains(v3.T)) + // { + // uvList.Add(v3.T); + // } + // } + + // } + //} + + + var pntDict = new Dictionary(); + var normDict = new Dictionary(); + var uvDict = new Dictionary(); + + foreach (var p in pntList) { - var ff = new Face { MatName = f.MatName }; - var pntList = new List(); - var normList = new List(); - var uvList = new List(); - foreach(var t in f.Triangles) + var index = pnts.IndexOf(p); + if (index == -1) { - var v1 = t.V1; - if (!pntList.Contains(v1.V)) - { - pntList.Add(v1.V); - } - if (v1.N > 0 && !normList.Contains(v1.N)) - { - normList.Add(v1.N); - } - if (v1.T > 0 && !uvList.Contains(v1.T)) - { - uvList.Add(v1.T); - } - var v2 = t.V2; - if (!pntList.Contains(v2.V)) - { - pntList.Add(v2.V); - } - if (v2.N > 0 && !normList.Contains(v2.N)) - { - normList.Add(v2.N); - } - if (v2.T > 0 && !uvList.Contains(v2.T)) - { - uvList.Add(v2.T); - } - var v3 = t.V3; - if (!pntList.Contains(v3.V)) - { - pntList.Add(v3.V); - } - if (v3.N > 0 && !normList.Contains(v3.N)) - { - normList.Add(v3.N); - } - if (v3.T > 0 && !uvList.Contains(v3.T)) - { - uvList.Add(v3.T); - } + index = pnts.Count; + pnts.Add(p); } - var pntDict = new Dictionary(); - foreach(var p in pntList) - { - var index = pnts.IndexOf(p); - if (index == -1) - { - index = pnts.Count; - pnts.Add(p); - } - pntDict.Add(p, index + 1); - } - var normDict = new Dictionary(); - foreach(var n in normList) + pntDict.Add(p, index + 1); + } + + foreach (var n in normList) + { + var index = normals.IndexOf(n); + if (index == -1) { - var index = normals.IndexOf(n); - if (index == -1) - { - index = normals.Count; - normals.Add(n); - } - normDict.Add(n, index + 1); + index = normals.Count; + normals.Add(n); } - var uvDict = new Dictionary(); - foreach(var t in uvList) + normDict.Add(n, index + 1); + } + + foreach (var t in uvList) + { + var index = uvs.IndexOf(t); + if (index == -1) { - var index = uvs.IndexOf(t); - if (index == -1) - { - index = uvs.Count; - uvs.Add(t); - } - uvDict.Add(t, index + 1); + index = uvs.Count; + uvs.Add(t); } + uvDict.Add(t, index + 1); + } - foreach(var t in f.Triangles) + + foreach (var f in g.Faces) + { + var ff = new Face { MatName = f.MatName }; + + foreach (var t in f.Triangles) { var v1 = GetVertex(t.V1, pntDict, normDict, uvDict); var v2 = GetVertex(t.V2, pntDict, normDict, uvDict); @@ -239,22 +259,38 @@ private Geometry AddGeo(Geometry g, List pnts, List normals, List return gg; } - private int GetBoxIndex(Geometry g, IList boxes) + class GeomBox + { + public int Index { get; set; } = -1; + + public Vec3 Center { get; set; } + + public SortedSet Pnts { get; set; } + + public SortedSet Norms { get; set; } + + public SortedSet Uvs { get; set; } + } + + private GeomBox GetBoxIndex(Geometry g, IList boxes) { var gCenter = GetCenter(g); for(var i = 0;i(); + var ps = new SortedSet(); + var ns = new SortedSet(); + var ts = new SortedSet(); var sumX = 0.0; var sumY = 0.0; var sumZ = 0.0; @@ -269,7 +305,7 @@ private Vec3 GetCenter(Geometry g) sumY += v.Y; sumZ += v.Z; ps.Add(t.V1.V); - } + } if (!ps.Contains(t.V2.V)) { var v = Vertices[t.V2.V - 1]; @@ -277,7 +313,7 @@ private Vec3 GetCenter(Geometry g) sumY += v.Y; sumZ += v.Z; ps.Add(t.V2.V); - } + } if (!ps.Contains(t.V3.V)) { var v = Vertices[t.V3.V - 1]; @@ -286,13 +322,46 @@ private Vec3 GetCenter(Geometry g) sumZ += v.Z; ps.Add(t.V3.V); } + + + if (t.V1.N > 0 && !ns.Contains(t.V1.N)) + { + ns.Add(t.V1.N); + } + if (t.V1.T > 0 && !ts.Contains(t.V1.T)) + { + ts.Add(t.V1.T); + } + if (t.V2.N > 0 && !ns.Contains(t.V2.N)) + { + ns.Add(t.V2.N); + } + if (t.V2.T > 0 && !ts.Contains(t.V2.T)) + { + ts.Add(t.V2.T); + } + if (t.V3.N > 0 && !ns.Contains(t.V3.N)) + { + ns.Add(t.V3.N); + } + if (t.V3.T > 0 && !ts.Contains(t.V3.T)) + { + ts.Add(t.V3.T); + } + } } var x = sumX / ps.Count; var y = sumY / ps.Count; var z = sumZ / ps.Count; - return new Vec3(x, y, z); + return new GeomBox + { + Center = new Vec3(x, y, z), + Pnts = ps, + Norms = ns, + Uvs = ts + }; } public BoundingBox GetBounding() diff --git a/src/ObjConvert.FrameworkTests/Program.cs b/src/ObjConvert.FrameworkTests/Program.cs index 17f8bb8..ea237c2 100644 --- a/src/ObjConvert.FrameworkTests/Program.cs +++ b/src/ObjConvert.FrameworkTests/Program.cs @@ -20,8 +20,8 @@ static void Main(string[] args) static void TestSplitAndMerge() { var gisPosition = new GisPosition(); - var mobjZipFile2 = @"test.objr"; - Obj23dTilesTests.SplitObjAndMergeMTilesetsWithZip(mobjZipFile2, "test", gisPosition, 2); + var mobjZipFile2 = @"testsplitmerge.objr"; + Obj23dTilesTests.SplitObjAndMergeMTilesetsWithZip(mobjZipFile2, "testsplitmerge", gisPosition, 2); } static void Test3dTile()