Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
obj splits performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Jifeng committed Jan 25, 2019
1 parent 66f58d1 commit 181d6c5
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/Obj23dTiles/Obj23dTiles.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/arcplus/ObjConvert</RepositoryUrl>
<Copyright>Copyright © Arcplus 2018</Copyright>
<Description>Convert wavefront obj file to 3d tiles, which can be used in cesium</Description>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net45'">
Expand Down
1 change: 0 additions & 1 deletion src/Obj2Gltf.Tests/ObjTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Obj2Gltf/Obj2Gltf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/arcplus/ObjConvert</RepositoryUrl>
<Copyright>Copyright © Arcplus 2018</Copyright>
<Description>Convert wavefront obj file to gltf/glb file</Description>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
</PropertyGroup>

Expand Down
249 changes: 159 additions & 90 deletions src/Obj2Gltf/WaveFront/ObjModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<ObjModel> Split(int level)
var pnts = new List<int>[boxes.Count];
var normals = new List<int>[boxes.Count];
var uvs = new List<int>[boxes.Count];
for(var i = 0;i<geoes.Length;i++)
for (var i = 0; i < geoes.Length; i++)
{
geoes[i] = new List<Geometry>();
pnts[i] = new List<int>();
Expand All @@ -87,15 +87,17 @@ public List<ObjModel> 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<ObjModel>();
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<Vec3>();
var ps = pnts[i];
foreach(var v in ps)
Expand All @@ -119,7 +121,8 @@ public List<ObjModel> Split(int level)
return objModels;
}

private static FaceVertex GetVertex(FaceVertex v, Dictionary<int, int> pnts, Dictionary<int, int> normals, Dictionary<int, int> uvs)
private static FaceVertex GetVertex(FaceVertex v, Dictionary<int, int> pnts,
Dictionary<int, int> normals, Dictionary<int, int> uvs)
{
var v1p = v.V;
var v1n = v.N;
Expand All @@ -139,92 +142,109 @@ private static FaceVertex GetVertex(FaceVertex v, Dictionary<int, int> pnts, Dic
return new FaceVertex(v1p, v1t, v1n);
}

private Geometry AddGeo(Geometry g, List<int> pnts, List<int> normals, List<int> uvs)
private Geometry AddGeo(Geometry g, GeomBox box,
List<int> pnts, List<int> normals, List<int> uvs)
{
var gg = new Geometry { Id = g.Id };
foreach(var f in g.Faces)

var pntList = box.Pnts; // new List<int>(); //
var normList = box.Norms; // new List<int>(); //
var uvList = box.Uvs; // new List<int>(); //

//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<int, int>();
var normDict = new Dictionary<int, int>();
var uvDict = new Dictionary<int, int>();

foreach (var p in pntList)
{
var ff = new Face { MatName = f.MatName };
var pntList = new List<int>();
var normList = new List<int>();
var uvList = new List<int>();
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<int, int>();
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<int, int>();
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<int, int>();
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);
Expand All @@ -239,22 +259,38 @@ private Geometry AddGeo(Geometry g, List<int> pnts, List<int> normals, List<int>
return gg;
}

private int GetBoxIndex(Geometry g, IList<BoundingBox> boxes)
class GeomBox
{
public int Index { get; set; } = -1;

public Vec3 Center { get; set; }

public SortedSet<int> Pnts { get; set; }

public SortedSet<int> Norms { get; set; }

public SortedSet<int> Uvs { get; set; }
}

private GeomBox GetBoxIndex(Geometry g, IList<BoundingBox> boxes)
{
var gCenter = GetCenter(g);
for(var i = 0;i<boxes.Count;i++)
{
if (boxes[i].IsIn(gCenter))
if (boxes[i].IsIn(gCenter.Center))
{
return i;
gCenter.Index = i;
return gCenter;
}
}
return -1;
return gCenter;
}

private Vec3 GetCenter(Geometry g)
private GeomBox GetCenter(Geometry g)
{
var ps = new List<int>();
var ps = new SortedSet<int>();
var ns = new SortedSet<int>();
var ts = new SortedSet<int>();
var sumX = 0.0;
var sumY = 0.0;
var sumZ = 0.0;
Expand All @@ -269,15 +305,15 @@ 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];
sumX += v.X;
sumY += v.Y;
sumZ += v.Z;
ps.Add(t.V2.V);
}
}
if (!ps.Contains(t.V3.V))
{
var v = Vertices[t.V3.V - 1];
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/ObjConvert.FrameworkTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 181d6c5

Please sign in to comment.