private void CreateTessellatedShapeInternal(IFCImportShapeEditScope shapeEditScope, Transform scaledLcs) { TessellatedShapeBuilderScope tsBuilderScope = shapeEditScope.BuilderScope as TessellatedShapeBuilderScope; if (tsBuilderScope == null) { throw new InvalidOperationException("Expect a TessellatedShapeBuilderScope, but get a BrepBuilderScope instead"); } IList <XYZ> loopVertices = Bound.LoopVertices; int count = 0; if (loopVertices == null || ((count = loopVertices.Count) == 0)) { throw new InvalidOperationException("#" + Id + ": missing loop vertices, ignoring."); } if (count < 3) { throw new InvalidOperationException("#" + Id + ": too few loop vertices (" + count + "), ignoring."); } if (!Orientation) { loopVertices.Reverse(); } // Apply the transform IList <XYZ> transformedVertices = new List <XYZ>(); foreach (XYZ vertex in loopVertices) { transformedVertices.Add(scaledLcs.OfPoint(vertex)); } // Check that the loop vertices don't contain points that are very close to one another; // if so, throw the point away and hope that the TessellatedShapeBuilder can repair the result. // Warn in this case. If the entire boundary is bad, report an error and don't add the loop vertices. List <XYZ> validVertices = null; for (int pass = 0; pass < 2; pass++) { if (pass == 1 && !tsBuilderScope.RevertToMeshIfPossible()) { break; } IFCGeometryUtil.CheckAnyDistanceVerticesWithinTolerance(Id, shapeEditScope, transformedVertices, out validVertices); count = validVertices.Count; if (count >= 3 || !IsOuter) { break; } } // We are going to catch any exceptions if the loop is invalid. // We are going to hope that we can heal the parent object in the TessellatedShapeBuilder. bool bPotentiallyAbortFace = (count < 3); if (bPotentiallyAbortFace) { Importer.TheLog.LogComment(Id, "Too few distinct loop vertices (" + count + "), ignoring.", false); } else { bool maybeTryToTriangulate = tsBuilderScope.CanProcessDelayedFaceBoundary && (count == 4); bool tryToTriangulate = false; // Last check: check to see if the vertices are actually planar. // We are not going to be particularly fancy about how we pick the plane. if (count > 3) { XYZ planeNormal = null; XYZ firstPoint = validVertices[0]; XYZ secondPoint = validVertices[1]; XYZ firstDir = secondPoint - firstPoint; double bestLength = 0; for (int index = 2; index <= count; index++) { XYZ thirdPoint = validVertices[(index % count)]; XYZ currentPlaneNormal = firstDir.CrossProduct(thirdPoint - firstPoint); double planeNormalLength = currentPlaneNormal.GetLength(); if (planeNormalLength > 0.01) { planeNormal = currentPlaneNormal.Normalize(); break; } else if (maybeTryToTriangulate && (planeNormalLength > bestLength)) { planeNormal = currentPlaneNormal.Normalize(); bestLength = planeNormalLength; } firstPoint = secondPoint; secondPoint = thirdPoint; firstDir = secondPoint - firstPoint; } if (planeNormal == null) { // Even if we don't find a good normal, we will still see if the internal function can make sense of it. Importer.TheLog.LogComment(Id, "Bounded loop plane is likely non-planar, may triangulate.", false); } else { double vertexEps = IFCImportFile.TheFile.VertexTolerance; for (int index = 0; index < count; index++) { XYZ pointOnPlane = validVertices[index] - (validVertices[index] - firstPoint).DotProduct(planeNormal) * planeNormal; double distance = pointOnPlane.DistanceTo(validVertices[index]); if (distance > vertexEps * 10.0) { Importer.TheLog.LogComment(Id, "Bounded loop plane is non-planar, may triangulate.", false); tryToTriangulate = maybeTryToTriangulate; bPotentiallyAbortFace = !tryToTriangulate; break; } else if (distance > vertexEps) { if (!maybeTryToTriangulate) { Importer.TheLog.LogComment(Id, "Bounded loop plane is slightly non-planar, correcting.", false); validVertices[index] = pointOnPlane; } else { Importer.TheLog.LogComment(Id, "Bounded loop plane is slightly non-planar, will triangulate.", false); tryToTriangulate = maybeTryToTriangulate; } } } } } if (!bPotentiallyAbortFace) { if (tryToTriangulate) { tsBuilderScope.DelayedFaceBoundary = validVertices; } else { bPotentiallyAbortFace = !tsBuilderScope.AddLoopVertices(Id, validVertices); } } } if (bPotentiallyAbortFace && IsOuter) { tsBuilderScope.AbortCurrentFace(); } }
protected override void CreateShapeInternal(IFCImportShapeEditScope shapeEditScope, Transform lcs, Transform scaledLcs, string guid) { if (CoordIndex == null) { Importer.TheLog.LogError(Id, "Invalid coordinates for this triangulation, ignoring.", false); return; } using (BuilderScope bs = shapeEditScope.InitializeBuilder(IFCShapeBuilderType.TessellatedShapeBuilder)) { base.CreateShapeInternal(shapeEditScope, lcs, scaledLcs, guid); TessellatedShapeBuilderScope tsBuilderScope = bs as TessellatedShapeBuilderScope; tsBuilderScope.StartCollectingFaceSet(); // Create triangle face set from CoordIndex. We do not support the Normals yet at this point foreach (List <int> triIndex in CoordIndex) { // This is a defensive check in an unlikely situation that the index is larger than the data if (triIndex[0] > Coordinates.CoordList.Count || triIndex[1] > Coordinates.CoordList.Count || triIndex[2] > Coordinates.CoordList.Count) { continue; } // This is already triangulated, so no need to attempt triangulation here. tsBuilderScope.StartCollectingFace(GetMaterialElementId(shapeEditScope), false); IList <XYZ> loopVertices = new List <XYZ>(); for (int ii = 0; ii < 3; ++ii) { int actualVIdx = triIndex[ii] - 1; if (PnIndex != null) { actualVIdx = PnIndex[actualVIdx] - 1; } XYZ vv = Coordinates.CoordList[actualVIdx]; loopVertices.Add(vv); } IList <XYZ> transformedVertices = new List <XYZ>(); foreach (XYZ vertex in loopVertices) { transformedVertices.Add(scaledLcs.OfPoint(vertex)); } // Check triangle that is too narrow (2 vertices are within the tolerance IFCGeometryUtil.CheckAnyDistanceVerticesWithinTolerance(Id, shapeEditScope, transformedVertices, out List <XYZ> validVertices); if (validVertices.Count != transformedVertices.Count && tsBuilderScope.CanRevertToMesh()) { tsBuilderScope.RevertToMeshIfPossible(); IFCGeometryUtil.CheckAnyDistanceVerticesWithinTolerance(Id, shapeEditScope, transformedVertices, out validVertices); } // We are going to catch any exceptions if the loop is invalid. // We are going to hope that we can heal the parent object in the TessellatedShapeBuilder. bool bPotentiallyAbortFace = false; int count = validVertices.Count; if (validVertices.Count < 3) { Importer.TheLog.LogComment(Id, "Too few distinct loop vertices (" + count + "), ignoring.", false); bPotentiallyAbortFace = true; } else { if (!tsBuilderScope.AddLoopVertices(Id, validVertices)) { bPotentiallyAbortFace = true; } } tsBuilderScope.StopCollectingFace(!bPotentiallyAbortFace, false); } IList <GeometryObject> createdGeometries = tsBuilderScope.CreateGeometry(guid); if (createdGeometries != null) { foreach (GeometryObject createdGeometry in createdGeometries) { shapeEditScope.AddGeometry(IFCSolidInfo.Create(Id, createdGeometry)); } } } }