protected static int ToPointsBuffer ( Point point, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int pointsCount = 0; if (point.Location.IsValid) { pointsCount = 1; vertexCount = 1; vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(point.Location))); } vb.Unmap(); ib = IndexPointsBuffer(pointsCount); } else { vertexFormatBits = 0; vb = null; vertexCount = 0; ib = null; } return(pointsCount); }
protected static DB3D.IndexBuffer ToWireframeBuffer(Mesh mesh, out int linesCount) { linesCount = (mesh.Faces.Count * 3) + mesh.Faces.QuadCount; if (linesCount > 0) { var ib = new DB3D.IndexBuffer(linesCount * DB3D.IndexLine.GetSizeInShortInts()); ib.Map(linesCount * DB3D.IndexLine.GetSizeInShortInts()); using (var istream = ib.GetIndexStreamLine()) { foreach (var face in mesh.Faces) { istream.AddLine(new DB3D.IndexLine(face.A, face.B)); istream.AddLine(new DB3D.IndexLine(face.B, face.C)); istream.AddLine(new DB3D.IndexLine(face.C, face.D)); if (face.IsQuad) { istream.AddLine(new DB3D.IndexLine(face.D, face.A)); } } } ib.Unmap(); return(ib); } return(null); }
public virtual bool Regen() { if (geometry != null) { if (!BeginRegen()) { return(false); } if (geometry.IsValid) { if (geometry is Mesh mesh) { vertexBuffer = ToVertexBuffer(mesh, part, out vertexFormatBits); vertexCount = part.VertexCount; triangleBuffer = ToTrianglesBuffer(mesh, part, out triangleCount); linesBuffer = ToEdgeBuffer(mesh, part, out linesCount); } else if (geometry is Curve curve) { using (var polyline = curve.ToPolyline(Revit.ShortCurveTolerance * Revit.ModelUnits, Revit.AngleTolerance * 100.0, Revit.ShortCurveTolerance * Revit.ModelUnits, 0.0)) { var pline = polyline.ToPolyline(); // Reduce too complex polylines. { var tol = Revit.VertexTolerance * Revit.ModelUnits; while (pline.Count > 0x4000) { tol *= 2.0; if (pline.ReduceSegments(tol) == 0) { break; } } } linesCount = ToPolylineBuffer(pline, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } } else if (geometry is Point point) { linesCount = -ToPointsBuffer(point, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } else if (geometry is PointCloud pointCloud) { linesCount = -ToPointsBuffer(pointCloud, part, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } vertexFormat = new DB3D.VertexFormat(vertexFormatBits); } geometry = null; EndRegen(); } return(true); }
void IDisposable.Dispose() { effectInstance?.Dispose(); effectInstance = null; if (linesBuffer != indexLinesBuffer && linesBuffer != indexPointsBuffer) { linesBuffer?.Dispose(); } linesBuffer = null; linesCount = 0; triangleBuffer?.Dispose(); triangleBuffer = null; triangleCount = 0; vertexFormat?.Dispose(); vertexFormat = null; vertexBuffer?.Dispose(); vertexBuffer = null; vertexCount = 0; }
protected static DB3D.IndexBuffer ToTrianglesBuffer ( Mesh mesh, Primitive.Part part, out int triangleCount ) { triangleCount = part.FaceCount; { var faces = mesh.Faces; for (int f = part.StartFaceIndex; f < part.EndFaceIndex; ++f) { if (faces[f].IsQuad) { triangleCount++; } } } if (triangleCount > 0) { var ib = new DB3D.IndexBuffer(triangleCount * DB3D.IndexTriangle.GetSizeInShortInts()); ib.Map(triangleCount * DB3D.IndexTriangle.GetSizeInShortInts()); using (var istream = ib.GetIndexStreamTriangle()) { var faces = mesh.Faces; for (int f = part.StartFaceIndex; f < part.EndFaceIndex; ++f) { var face = faces[f]; istream.AddTriangle(new DB3D.IndexTriangle(face.A - part.StartVertexIndex, face.B - part.StartVertexIndex, face.C - part.StartVertexIndex)); if (face.IsQuad) { istream.AddTriangle(new DB3D.IndexTriangle(face.C - part.StartVertexIndex, face.D - part.StartVertexIndex, face.A - part.StartVertexIndex)); } } } ib.Unmap(); return(ib); } return(null); }
static DB3D.IndexBuffer IndexPointsBuffer(int pointsCount) { Debug.Assert(pointsCount <= VertexThreshold); if (indexPointsBuffer == null) { indexPointsBuffer = new DB3D.IndexBuffer(VertexThreshold * DB3D.IndexPoint.GetSizeInShortInts()); indexPointsBuffer.Map(VertexThreshold * DB3D.IndexPoint.GetSizeInShortInts()); using (var istream = indexPointsBuffer.GetIndexStreamPoint()) { for (int vi = 0; vi < VertexThreshold; ++vi) { istream.AddPoint(new DB3D.IndexPoint(vi)); } } indexPointsBuffer.Unmap(); } Debug.Assert(indexPointsBuffer.IsValid()); return(indexPointsBuffer); }
protected static int ToPolylineBuffer ( Polyline polyline, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int linesCount = 0; if (polyline.SegmentCount > 0) { linesCount = polyline.SegmentCount; vertexCount = polyline.Count; vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(vertexCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(vertexCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { foreach (var v in polyline) { vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(v))); } } vb.Unmap(); ib = IndexLinesBuffer(vertexCount); } else { vertexFormatBits = 0; vb = null; vertexCount = 0; ib = null; } return(linesCount); }
protected static int ToPointsBuffer ( PointCloud pointCloud, Primitive.Part part, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int pointsCount = part.VertexCount; int normalCount = pointCloud.ContainsNormals ? pointsCount : 0; int colorsCount = pointCloud.ContainsColors ? pointsCount : 0; bool hasPoints = pointsCount > 0; bool hasNormals = normalCount == pointsCount; bool hasColors = colorsCount == pointsCount; if (hasPoints) { if (hasNormals) { if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionNormalColored; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionNormalColored()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; var c = new DB.ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A); vstream.AddVertex(new DB3D.VertexPositionNormalColored(RawEncoder.AsXYZ(point.Location), RawEncoder.AsXYZ(point.Normal), c)); } } vb.Unmap(); } else { vertexFormatBits = DB3D.VertexFormatBits.PositionNormal; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionNormal.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionNormal.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionNormal()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; vstream.AddVertex(new DB3D.VertexPositionNormal(RawEncoder.AsXYZ(point.Location), RawEncoder.AsXYZ(point.Normal))); } } vb.Unmap(); } } else { if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionColored; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionColored.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionColored.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionColored()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; var c = new DB.ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A); vstream.AddVertex(new DB3D.VertexPositionColored(RawEncoder.AsXYZ(point.Location), c)); } } vb.Unmap(); } else { vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(point.Location))); } } vb.Unmap(); } } ib = IndexPointsBuffer(pointsCount); } else { vertexFormatBits = 0; vb = null; ib = null; } vertexCount = pointsCount; return(pointsCount); }
protected static DB3D.IndexBuffer ToEdgeBuffer ( Mesh mesh, Primitive.Part part, out int linesCount ) { if (part.VertexCount != mesh.Vertices.Count) { if (part.VertexCount > 0) { linesCount = -part.VertexCount; return(IndexPointsBuffer(part.VertexCount)); } linesCount = 0; } else { var edgeIndices = new List <IndexPair>(); if (mesh.Ngons.Count > 0) { foreach (var ngon in mesh.Ngons) { var boundary = ngon.BoundaryVertexIndexList(); if ((boundary?.Length ?? 0) > 1) { for (int b = 0; b < boundary.Length - 1; ++b) { edgeIndices.Add(new IndexPair((int)boundary[b], (int)boundary[b + 1])); } edgeIndices.Add(new IndexPair((int)boundary[boundary.Length - 1], (int)boundary[0])); } } } else { var vertices = mesh.TopologyVertices; var edges = mesh.TopologyEdges; var edgeCount = edges.Count; for (int e = 0; e < edgeCount; ++e) { if (edges.IsEdgeUnwelded(e) || edges.GetConnectedFaces(e).Length < 2) { var pair = edges.GetTopologyVertices(e); pair.I = vertices.MeshVertexIndices(pair.I)[0]; pair.J = vertices.MeshVertexIndices(pair.J)[0]; edgeIndices.Add(pair); } } } linesCount = edgeIndices.Count; if (linesCount > 0) { var ib = new DB3D.IndexBuffer(linesCount * DB3D.IndexLine.GetSizeInShortInts()); ib.Map(linesCount * DB3D.IndexLine.GetSizeInShortInts()); using (var istream = ib.GetIndexStreamLine()) { foreach (var edge in edgeIndices) { Debug.Assert(0 <= edge.I && edge.I < part.VertexCount); Debug.Assert(0 <= edge.J && edge.J < part.VertexCount); istream.AddLine(new DB3D.IndexLine(edge.I, edge.J)); } } ib.Unmap(); return(ib); } } return(null); }