/// <summary> /// Creates a new mesh where no vertices are shared. /// </summary> /// <param name="input"> /// The input mesh. /// </param> /// <returns> /// A new mesh. /// </returns> public static MeshGeometry3D NoSharedVertices(this MeshGeometry3D input) { var p = new Point3DCollection(); var ti = new Int32Collection(); Vector3DCollection n = null; if (input.Normals != null) { n = new Vector3DCollection(); } PointCollection tc = null; if (input.TextureCoordinates != null) { tc = new PointCollection(); } for (var i = 0; i < input.TriangleIndices.Count; i += 3) { var i0 = i; var i1 = i + 1; var i2 = i + 2; var index0 = input.TriangleIndices[i0]; var index1 = input.TriangleIndices[i1]; var index2 = input.TriangleIndices[i2]; var p0 = input.Positions[index0]; var p1 = input.Positions[index1]; var p2 = input.Positions[index2]; p.Add(p0); p.Add(p1); p.Add(p2); ti.Add(i0); ti.Add(i1); ti.Add(i2); if (n != null && input.Normals.Count > 0) { n.Add(input.Normals[index0]); n.Add(input.Normals[index1]); n.Add(input.Normals[index2]); } if (tc != null) { tc.Add(input.TextureCoordinates[index0]); tc.Add(input.TextureCoordinates[index1]); tc.Add(input.TextureCoordinates[index2]); } } #if SHARPDX return(new MeshGeometry3D { Positions = p, TriangleIndices = new IntCollection(ti), Normals = n, TextureCoordinates = tc }); #else return(new MeshGeometry3D { Positions = p, TriangleIndices = ti, Normals = n, TextureCoordinates = tc }); #endif }
private void ClearView() { // floor plane grid var Maxreset = 100; var Minreset = -100; EffectsManager.Dispose(); EffectsManager = new DefaultEffectsManager(); var points = new PointGeometry3D(); var col = new Color4Collection(); // gradient na kolory var ptPos = new Vector3Collection(); // pozycje punktów var ptIdx = new IntCollection(); // indeksy punktów arrows = lines = new LineBuilder(); points.Positions = ptPos; points.Indices = ptIdx; points.Colors = col; PointsColor = Colors.White; // <- nie pamiętam po co to tutaj ale bez tego nie działa Points = points; PointsTransform = new Media3D.TranslateTransform3D(0, 0, Minreset); CreateGrid(Maxreset, Minreset, Maxreset, Maxreset, Minreset); Grid = lines.ToLineGeometry3D(); GridColor = new Color4(153 / 255.0f, 204 / 255.0f, 255 / 255.0f, (float)0.3).ToColor();; GridTransform = new Media3D.TranslateTransform3D(0, 0, Minreset); // strzałki CreateArrows(Maxreset, Minreset, Maxreset, Minreset, Maxreset, Minreset); Arrow = arrows.ToLineGeometry3D(); ArrowColor = new Color4(0, 255 / 255.0f, 255 / 255.0f, (float)0.5).ToColor();; ArrowTransform = new Media3D.TranslateTransform3D(0, 0, Minreset); }
/// <summary> /// Calculates the normal vectors. /// </summary> /// <param name="positions"> /// The positions. /// </param> /// <param name="triangleIndices"> /// The triangle indices. /// </param> /// <returns> /// Collection of normal vectors. /// </returns> public static Vector3DCollection CalculateNormals(IList <Point3D> positions, IList <int> triangleIndices) { var normals = new Vector3DCollection(positions.Count); for (var i = 0; i < positions.Count; i++) { normals.Add(new Vector3D()); } for (var i = 0; i < triangleIndices.Count; i += 3) { var index0 = triangleIndices[i]; var index1 = triangleIndices[i + 1]; var index2 = triangleIndices[i + 2]; var p0 = positions[index0]; var p1 = positions[index1]; var p2 = positions[index2]; var u = p1 - p0; var v = p2 - p0; var w = SharedFunctions.CrossProduct(ref u, ref v); w.Normalize(); normals[index0] += w; normals[index1] += w; normals[index2] += w; } for (var i = 0; i < normals.Count; i++) { var n = normals[i]; n.Normalize(); normals[i] = n; } return(normals); }
public BillboardSingleText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); TextInfo = new TextInfo(); }
public BillboardText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); this.TextInfo = new List <TextInfo>(); var assembly = Assembly.GetExecutingAssembly(); //Read the texture description using (var texDescriptionStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.fnt")) { bmpFont = new BitmapFont(); bmpFont.Load(texDescriptionStream); } //Read the texture using (var texImageStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.png")) { var image = new BitmapImage(); image.BeginInit(); image.StreamSource = texImageStream; image.EndInit(); Texture = image; } }
public static Tuple <Vector3Collection, Color4Collection> BPtoPointCloud(Vector3Collection imageSpace, List <LMData> lmDataList, double angleThreshold = 5, double minCountPercent = 0) { if (minCountPercent > 1 || minCountPercent < 0) { throw new ArgumentOutOfRangeException(nameof(minCountPercent)); } Vector3Collection vector3sOut = new Vector3Collection(); Color4Collection color4sOut = new Color4Collection(); if (imageSpace.Count == 0 || lmDataList.Count == 0) { var tupleOut1 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut); return(tupleOut1); } int[] counts = new int[imageSpace.Count]; var templmlist = lmDataList.ToArray(); foreach (var lmData in templmlist) { if (lmData.Type == LMData.InteractionType.Compton) { Parallel.For(0, imageSpace.Count, i => { if (IsEffectedBPPoint(lmData.ScatterLMDataInfos[0].RelativeInteractionPoint3D, lmData.ScatterLMDataInfos[0].InteractionEnergy, lmData.AbsorberLMDataInfos[0].RelativeInteractionPoint3D, lmData.AbsorberLMDataInfos[0].InteractionEnergy, imageSpace[i].ToPoint3D(), angleThreshold)) { counts[i]++; } }); } } int maxCount = counts.Max(); int minCount = Convert.ToInt32(Math.Round(maxCount * minCountPercent)); if (maxCount < 5) { var tupleOut2 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut); return(tupleOut2); } for (int i = 0; i < imageSpace.Count; i++) { if (counts[i] > minCount) { vector3sOut.Add(imageSpace[i]); color4sOut.Add(ColorScaleJet(counts[i], minCount, maxCount)); } } var tupleOut3 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut); return(tupleOut3); }
public void MultipleTilesSelected_TwoByTwoByTwoGrid_OutIn() { Vector3Collection selectedTiles = ScriptableObject.CreateInstance <Vector3Collection>(); selectedTiles.Add(new Vector3(1f, 1f, 1f)); TileSelection tileSelection = new TileSelectionBuilder().AddSelectedTiles(selectedTiles) .Build(); tileSelection.OnUpdate(new Vector3(0f, 0f, 0f)); Assert.AreEqual(8, selectedTiles.Count); Assert.AreEqual(new Vector3(1f, 1f, 1f), selectedTiles[0]); Assert.AreEqual(new Vector3(1f, 1f, 0f), selectedTiles[1]); Assert.AreEqual(new Vector3(1f, 0f, 1f), selectedTiles[2]); Assert.AreEqual(new Vector3(1f, 0f, 0f), selectedTiles[3]); Assert.AreEqual(new Vector3(0f, 1f, 1f), selectedTiles[4]); Assert.AreEqual(new Vector3(0f, 1f, 0f), selectedTiles[5]); Assert.AreEqual(new Vector3(0f, 0f, 1f), selectedTiles[6]); Assert.AreEqual(new Vector3(0f, 0f, 0f), selectedTiles[7]); tileSelection.OnUpdate(new Vector3(1f, 1f, 1f)); Assert.AreEqual(1, selectedTiles.Count); Assert.AreEqual(new Vector3(1f, 1f, 1f), selectedTiles[0]); }
/// <summary> /// Create a Mesh, with found props /// </summary> /// <param name="positions"></param> /// <param name="textureCoordinates"></param> /// <param name="triangleIndices"></param> /// <param name="normals"></param> /// <param name="tangents"></param> /// <param name="bitangents"></param> /// <param name="material"></param> /// <param name="transforms"></param> private void CreateMesh(Vector3Collection positions, Vector2Collection textureCoordinates, IntCollection triangleIndices, List <Matrix> transforms, out Vector3Collection normals, out Vector3Collection tangents, out Vector3Collection bitangents, MaterialCore material) { ComputeNormals(positions, triangleIndices, out normals); if (textureCoordinates == null) { textureCoordinates = new Vector2Collection(); foreach (var pos in positions) { textureCoordinates.Add(Vector2.One); } } MeshBuilder.ComputeTangents(positions, normals, textureCoordinates, triangleIndices, out tangents, out bitangents); MeshGeometry3D mesh = new MeshGeometry3D() { Positions = positions, Normals = normals, TextureCoordinates = textureCoordinates, Indices = triangleIndices, Tangents = tangents, BiTangents = bitangents }; Object3D ob3d = new Object3D(); ob3d.Geometry = mesh; ob3d.Material = material; ob3d.Transform = transforms; ob3d.Name = "Default"; this.obGroup.Add(ob3d); }
public BillboardText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); TextInfo = new List <TextInfo>(); }
/// <summary> /// Stolen from MeshBuilder class, maybe make this static method there public... /// </summary> /// <param name="positions"></param> /// <param name="triangleIndices"></param> /// <param name="normals"></param> private static void ComputeNormals(Vector3Collection positions, IntCollection triangleIndices, out Vector3Collection normals) { normals = new Vector3Collection(positions.Count); normals.AddRange(Enumerable.Repeat(Vector3.Zero, positions.Count)); for (int t = 0; t < triangleIndices.Count; t += 3) { var i1 = triangleIndices[t]; var i2 = triangleIndices[t + 1]; var i3 = triangleIndices[t + 2]; var v1 = positions[i1]; var v2 = positions[i2]; var v3 = positions[i3]; var p1 = v2 - v1; var p2 = v3 - v1; var n = Vector3.Cross(p1, p2); // angle p1.Normalize(); p2.Normalize(); var a = (float)Math.Acos(Vector3.Dot(p1, p2)); n.Normalize(); normals[i1] += (a * n); normals[i2] += (a * n); normals[i3] += (a * n); } for (int i = 0; i < normals.Count; i++) { var n = normals[i]; n.Normalize(); normals[i] = n; } }
private static List <MeshGeometryModel3D> GenerateMeshGeometryModels(MGEOFile mgeo) { List <MeshGeometryModel3D> models = new List <MeshGeometryModel3D>(mgeo.Objects.Count); foreach (MGEOObject mgeoModel in mgeo.Objects) { IntCollection indices = new IntCollection(mgeoModel.Indices.Select(x => (int)x).AsEnumerable()); Vector3Collection vertices = new Vector3Collection(mgeoModel.Vertices.Count); Vector2Collection uvs = new Vector2Collection(mgeoModel.Vertices.Count); foreach (MGEOVertex vertex in mgeoModel.Vertices) { vertices.Add(new dxVector3(vertex.Position.X, vertex.Position.Y, vertex.Position.Z)); uvs.Add(new dxVector2(vertex.DiffuseUV.X, vertex.DiffuseUV.Y)); } MeshGeometry3D meshGeometry = new MeshGeometry3D() { Indices = indices, Positions = vertices, TextureCoordinates = uvs }; MeshGeometryModel3D model = new MeshGeometryModel3D() { Geometry = meshGeometry, Material = DiffuseMaterials.Pearl, Name = mgeoModel.Name }; models.Add(model); } return(models); }
public IEnumerator NoUpdateAfterSelectionStops() { Vector3Collection selectedTiles = ScriptableObject.CreateInstance <Vector3Collection>(); selectedTiles.Add(new Vector3(0, 0, 0)); selectedTiles.Add(new Vector3(0, 0, -1)); selectedTiles.Add(new Vector3(-1, 0, 0)); selectedTiles.Add(new Vector3(-1, 0, -1)); TileSelectionHighlighter tileSelectionHighlighter = new TileSelectionHighlighterBuilder().AddSelectedTiles(selectedTiles) .Build(); tileSelectionHighlighter.BeginSelection(Vector3.zero); yield return(null); selectedTiles.Add(new Vector3(1, 0, 1)); selectedTiles.Add(new Vector3(1, 0, 2)); selectedTiles.Add(new Vector3(1, 0, 3)); tileSelectionHighlighter.EndSelection(new Vector3(-1, 0, -1)); yield return(null); Assert.AreEqual(new Vector3(-1, 0, -1), tileSelectionHighlighter.Transform.position); Assert.AreEqual(new Vector3(2, 2, 1), tileSelectionHighlighter.Transform.localScale); }
public IEnumerator TransformedScaledToSizeOfSelectedTiles_Multiple_Negative() { Vector3Collection selectedTiles = ScriptableObject.CreateInstance <Vector3Collection>(); selectedTiles.Add(new Vector3(0, 0, 0)); selectedTiles.Add(new Vector3(0, 0, -1)); selectedTiles.Add(new Vector3(0, 0, -2)); selectedTiles.Add(new Vector3(-1, 0, 0)); selectedTiles.Add(new Vector3(-1, 0, -1)); selectedTiles.Add(new Vector3(-1, 0, -2)); selectedTiles.Add(new Vector3(-2, 0, 0)); selectedTiles.Add(new Vector3(-2, 0, -1)); selectedTiles.Add(new Vector3(-2, 0, -2)); TileSelectionHighlighter tileSelectionHighlighter = new TileSelectionHighlighterBuilder().AddSelectedTiles(selectedTiles) .Build(); tileSelectionHighlighter.BeginSelection(Vector3.zero); yield return(null); Assert.AreEqual(new Vector3(3, 3, 1), tileSelectionHighlighter.Transform.localScale); }
public MeshModel(Vector3Collection positions, Vector3Collection normals, IntCollection triangleIndices, Vector2Collection textureCoordinates) { Positions = positions; Normals = normals; TriangleIndices = triangleIndices; TextureCoordinates = textureCoordinates; }
void DrawBPPointCloudToSLAMPointCloud() { Stopwatch sw = new Stopwatch(); sw.Start(); VMStatus = $"Reconing......SLAM! {elapsedTime} ms"; var vc = new Vector3Collection(); var cc = new Color4Collection(); var tempposeVect = new List <double[]>(); var tempColorVect = new List <double[]>(); RealsenseControl.GetReconSLAMPointCloud(ref tempposeVect, ref tempColorVect); for (int i = 0; i < tempposeVect.Count; i++) { vc.Add(new Vector3(Convert.ToSingle(tempposeVect[i][0]), Convert.ToSingle(tempposeVect[i][1]), Convert.ToSingle(tempposeVect[i][2]))); cc.Add(new Color4(Convert.ToSingle(tempColorVect[i][0]), Convert.ToSingle(tempColorVect[i][1]), Convert.ToSingle(tempColorVect[i][2]), 0.5f)); } SLAMReconPointCloud = new PointGeometry3D() { Positions = vc, Colors = cc }; sw.Stop(); elapsedTime = sw.ElapsedMilliseconds; VMStatus = "Reconing......SLAM Done!"; }
protected BillboardSingleImage3D() { Positions = new Vector3Collection(6); Colors = new Color4Collection(6); TextureCoordinates = new Vector2Collection(6); MaskColor = Color.Transparent; }
public BillboardText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); this.TextInfo = new List<TextInfo>(); var assembly = Assembly.GetExecutingAssembly(); //Read the texture description using(var texDescriptionStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.fnt")) { bmpFont = new BitmapFont(); bmpFont.Load(texDescriptionStream); } //Read the texture using(var texImageStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.png")) { var image = new BitmapImage(); image.BeginInit(); image.StreamSource = texImageStream; image.EndInit(); Texture = image; } }
public static MeshGeometry3D Merge(params MeshGeometry3D[] meshes) { var positions = new Vector3Collection(); var indices = new IntCollection(); var normals = meshes.All(x => x.Normals != null) ? new Vector3Collection() : null; var colors = meshes.All(x => x.Colors != null) ? new Color4Collection() : null; var textureCoods = meshes.All(x => x.TextureCoordinates != null) ? new Vector2Collection() : null; var tangents = meshes.All(x => x.Tangents != null) ? new Vector3Collection() : null; var bitangents = meshes.All(x => x.BiTangents != null) ? new Vector3Collection() : null; int index = 0; foreach (var part in meshes) { positions.AddRange(part.Positions); indices.AddRange(part.Indices.Select(x => x + index)); index += part.Positions.Count; } if (normals != null) { normals = new Vector3Collection(meshes.SelectMany(x => x.Normals)); } if (colors != null) { colors = new Color4Collection(meshes.SelectMany(x => x.Colors)); } if (textureCoods != null) { textureCoods = new Vector2Collection(meshes.SelectMany(x => x.TextureCoordinates)); } if (tangents != null) { tangents = new Vector3Collection(meshes.SelectMany(x => x.Tangents)); } if (bitangents != null) { bitangents = new Vector3Collection(meshes.SelectMany(x => x.BiTangents)); } var mesh = new MeshGeometry3D() { Positions = positions, Indices = indices, }; mesh.Normals = normals; mesh.Colors = colors; mesh.TextureCoordinates = textureCoods; mesh.Tangents = tangents; mesh.BiTangents = bitangents; return(mesh); }
public static void AssertContains(this Vector3Collection collection, params double[][] points) { Assert.AreEqual(points.Length, collection.Count, "Expected to find {0} points in collection", points.Length); foreach (var point in points) { Assert.IsTrue(collection.Contains(point), "Expected collection to contain point [{0},{1},{2}]", point[0], point[1], point[2]); } }
public BillboardText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); TextInfo = new List<TextInfo>(); Initialize(); }
public BillboardSingleText3D(float width, float height) { Positions = new Vector3Collection(12); Colors = new Color4Collection(12); TextureCoordinates = new Vector2Collection(12); TextInfo = new TextInfo(); Width = width; Height = height; predefinedSize = true; }
public static (Vector3Collection, Color4Collection) BPtoPointCloud2Pi(Vector3Collection imageSpace, List <LMData> lmDataList, double angleThreshold = 5, double minCountPercent = 0) { if (minCountPercent > 1 || minCountPercent < 0) { throw new ArgumentOutOfRangeException(nameof(minCountPercent)); } Vector3Collection vector3sOut = new Vector3Collection(); Color4Collection color4sOut = new Color4Collection(); if (imageSpace.Count == 0 || lmDataList.Count == 0) { return(vector3sOut, color4sOut); } int[] counts = new int[imageSpace.Count]; foreach (var lmData in lmDataList) { if (lmData.Type == LMData.InteractionType.Compton) { for (int i = 0; i < imageSpace.Count; ++i) { if (IsEffectedBPPoint2Pi(lmData.ScatterLMDataInfo.TransformedInteractionPoint3D, lmData.ScatterLMDataInfo.InteractionEnergy, lmData.AbsorberLMDataInfo.TransformedInteractionPoint3D, lmData.AbsorberLMDataInfo.InteractionEnergy, imageSpace[i].ToPoint3D(), lmData.DeviceTransformMatrix, angleThreshold)) { counts[i]++; } } } } int maxCount = counts.Max(); int minCount = Convert.ToInt32(Math.Round(maxCount * minCountPercent)); if (maxCount < 5) { return(vector3sOut, color4sOut); } for (int i = 0; i < imageSpace.Count; i++) { if (counts[i] > minCount) { vector3sOut.Add(imageSpace[i]); color4sOut.Add(ColorScaleJet(counts[i], minCount, maxCount)); } } return(vector3sOut, color4sOut); }
/// <summary> /// Simplifies the specified mesh. /// </summary> /// <param name="mesh"> /// The mesh. /// </param> /// <param name="eps"> /// The tolerance. /// </param> /// <returns> /// A simplified mesh. /// </returns> public static MeshGeometry3D Simplify(this MeshGeometry3D mesh, DoubleOrSingle eps) { // Find common positions var dict = new Dictionary <int, int>(); // map position index to first occurence of same position for (var i = 0; i < mesh.Positions.Count; i++) { for (var j = i + 1; j < mesh.Positions.Count; j++) { if (dict.ContainsKey(j)) { continue; } var v = mesh.Positions[i] - mesh.Positions[j]; var l2 = SharedFunctions.LengthSquared(ref v); if (l2 < eps) { dict.Add(j, i); } } } var p = new Point3DCollection(); var ti = new Int32Collection(); // create new positions array var newIndex = new Dictionary <int, int>(); // map old index to new index for (var i = 0; i < mesh.Positions.Count; i++) { if (!dict.ContainsKey(i)) { newIndex.Add(i, p.Count); p.Add(mesh.Positions[i]); } } // Update triangle indices foreach (var index in mesh.TriangleIndices) { int j; ti.Add(dict.TryGetValue(index, out j) ? newIndex[j] : newIndex[index]); } #if SHARPDX var result = new MeshGeometry3D { Positions = p, TriangleIndices = new IntCollection(ti), }; #else var result = new MeshGeometry3D { Positions = p, TriangleIndices = ti }; #endif return(result); }
public MeshModel(Vector3Collection positions, Vector3Collection normals, IntCollection triangleIndices, bool calculateTextureCoordinates) { Positions = positions; Normals = normals; TriangleIndices = triangleIndices; if (calculateTextureCoordinates) { CalculateTextureCoordinates(); } }
public static IEnumerable <double> ToEnumerable(this Vector3Collection collection) { foreach (var v in collection) { yield return(v.X); yield return(v.Y); yield return(v.Z); } }
/// <summary> /// Add axis in positive, 0.1 m /// </summary> private void AddAxisPoints() { Vector3Collection vector3s = new Vector3Collection(); Color4Collection color4s = new Color4Collection(); vector3s.Add(new SharpDX.Vector3()); color4s.Add(new SharpDX.Color4(0, 0, 0, 1)); for (int i = 1; i < 50; i++) { if (i % 10 == 0) { vector3s.Add(new SharpDX.Vector3() { X = 0.1f * i }); color4s.Add(new SharpDX.Color4(0, 1, 1, 1)); vector3s.Add(new SharpDX.Vector3() { Y = 0.1f * i }); color4s.Add(new SharpDX.Color4(1, 0, 1, 1)); vector3s.Add(new SharpDX.Vector3() { Z = 0.1f * i }); color4s.Add(new SharpDX.Color4(1, 1, 0, 1)); } else { vector3s.Add(new SharpDX.Vector3() { X = 0.1f * i }); color4s.Add(new SharpDX.Color4(1, 0, 0, 1)); vector3s.Add(new SharpDX.Vector3() { Y = 0.1f * i }); color4s.Add(new SharpDX.Color4(0, 1, 0, 1)); vector3s.Add(new SharpDX.Vector3() { Z = 0.1f * i }); color4s.Add(new SharpDX.Color4(0, 0, 1, 1)); } } AxisPoint = new PointGeometry3D() { Positions = vector3s, Colors = color4s }; }
private void CreateWireFrameModels(IFCItem item, Vector3 center) { while (item != null) { if (item.ifcID != IntPtr.Zero && item.noVerticesForWireFrame != 0 && item.noPrimitivesForWireFrame != 0) { var geo = new LineGeometry3D(); geo.Positions = new Vector3Collection(); geo.Indices = new IntCollection(); var points = new Vector3Collection(); if (item.verticesForWireFrame != null) { for (int i = 0; i < item.noVerticesForWireFrame; i++) { points.Add(new Vector3((item.verticesForWireFrame[3 * i + 0] - center.X), (item.verticesForWireFrame[3 * i + 1] - center.Y), (item.verticesForWireFrame[3 * i + 2] - center.Z))); geo.Positions.Add(new Vector3((item.verticesForWireFrame[3 * i + 0] - center.X), (item.verticesForWireFrame[3 * i + 1] - center.Y), (item.verticesForWireFrame[3 * i + 2] - center.Z))); } } if (item.indicesForWireFrameLineParts != null) { for (int i = 0; i < item.noPrimitivesForWireFrame; i++) { var idx = item.indicesForWireFrameLineParts[2 * i + 0]; geo.Indices.Add(idx); idx = item.indicesForWireFrameLineParts[2 * i + 1]; geo.Indices.Add(idx); } } else { for (int i = 0, count = points.Count; i < count; i++) { geo.Indices.Add(i); geo.Indices.Add((i + 1) % count); } } LineGeometryModel3D line = new LineGeometryModel3D(); line.Geometry = geo; line.Color = _defaultLineColor; line.Thickness = 0.5; item.Wireframe = line; line.Tag = item.ifcType + ":" + item.ifcID; model.Add(line); } CreateFaceModels(item.child, center); item = item.next; } }
private static Vector3Collection GetPositions(DMesh3 mesh) { var positions = new Vector3Collection(mesh.VerticesRefCounts.count); var vertices = mesh.VerticesBuffer; foreach (int vId in mesh.VerticesRefCounts) { int i = vId * 3; positions.Add(new SharpDX.Vector3((float)vertices[i], (float)vertices[i + 1], (float)vertices[i + 2])); } return(positions); }
private static Vector3Collection GetNormals(DMesh3 mesh) { var normalsList = new Vector3Collection(mesh.VerticesRefCounts.count); var normals = mesh.NormalsBuffer; foreach (int vId in mesh.VerticesRefCounts) { int i = vId * 3; normalsList.Add(new SharpDX.Vector3(normals[i], normals[i + 1], normals[i + 2])); } return(normalsList); }
private Vector3Collection ReadVertexList(BinaryReader reader) { int size = reader.ReadUInt16(); var pts = new Vector3Collection(); for (int i = 0; i < size; i++) { float x = reader.ReadSingle(); float y = reader.ReadSingle(); float z = reader.ReadSingle(); pts.Add(new Vector3(x, y, z)); } return(pts); }
public void SingleTileSelectedWhenInitialPointDoesntChange() { Vector3Collection selectedTiles = ScriptableObject.CreateInstance <Vector3Collection>(); selectedTiles.Add(new Vector3(1f, 0f, 11f)); TileSelection tileSelection = new TileSelectionBuilder().AddSelectedTiles(selectedTiles) .Build(); tileSelection.OnUpdate(new Vector3(1f, 0f, 11f)); Assert.AreEqual(1, selectedTiles.Count); Assert.AreEqual(new Vector3(1f, 0f, 11f), selectedTiles[0]); }
/// <summary> /// Create the grid /// </summary> private void DrawGrid() { Grid = new LineGeometry3D(); var positions = new Vector3Collection(); var indices = new IntCollection(); var colors = new Color4Collection(); for (var i = 0; i < 10; i += 1) { for (var j = 0; j < 10; j += 1) { DrawGridPatch(positions, indices, colors, -50 + i * 10, -50 + j * 10); } } Grid.Positions = positions; Grid.Indices = indices; Grid.Colors = colors; Axes = new LineGeometry3D(); var axesPositions = new Vector3Collection(); var axesIndices = new IntCollection(); var axesColors = new Color4Collection(); // Draw the coordinate axes axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(50, 0, 0)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Red); axesColors.Add(SharpDX.Color.Red); axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(0, 5, 0)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Blue); axesColors.Add(SharpDX.Color.Blue); axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(0, 0, -50)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Green); axesColors.Add(SharpDX.Color.Green); Axes.Positions = axesPositions; Axes.Indices = axesIndices; Axes.Colors = axesColors; }
private void LoadThreadFunction() { var pt = new Potree(new Uri(Filename)); var points = new PointGeometry3D(); var col = new Color4Collection(); var ptPos = new Vector3Collection(); var ptIdx = new IntCollection(); var j = 0; foreach (var p in pt.Compilation.Points) { ptPos.Add(new Vector3(p.x, p.y, p.z)); ptIdx.Add(j); col.Add(new Color4(p.r/255f, p.g / 255f, p.b / 255f, p.a / 255f)); j++; } var additionalTurns = 0; if ((pt.Compilation.Points.Count / 3) * 3 != pt.Compilation.Points.Count) { additionalTurns = ((pt.Compilation.Points.Count / 3 + 1) * 3) - pt.Compilation.Points.Count; } for (int i = 0; i < additionalTurns; i++) { ptIdx.Add(ptPos.Count); ptPos.Add(ptPos[ptPos.Count - 1]); col.Add(col[col.Count - 1]); } points.Positions = ptPos; points.Indices = ptIdx; points.Colors = col; Data = points; }
public BillboardText3D() { Positions = new Vector3Collection(); Colors = new Color4Collection(); TextureCoordinates = new Vector2Collection(); this.TextInfo = new List<TextInfo>(); var assembly = Assembly.GetExecutingAssembly(); var texDescriptionFilePath = Path.GetTempFileName(); var texImageFilePath = Path.GetTempFileName(); //Read the texture description var texDescriptionStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.fnt"); using (var fileStream = File.Create(texDescriptionFilePath)) { texDescriptionStream.CopyTo(fileStream); } bmpFont = BitmapFontLoader.LoadFontFromFile(texDescriptionFilePath); //Read the texture var texImageStream = assembly.GetManifestResourceStream("HelixToolkit.Wpf.SharpDX.Textures.arial.png"); using (var fileStream = File.Create(texImageFilePath)) { texImageStream.CopyTo(fileStream); } Texture = new BitmapImage(new Uri(texImageFilePath)); //Cleanup the temp files if (File.Exists(texDescriptionFilePath)) { File.Delete(texDescriptionFilePath); } }
/// <summary> /// Create the grid /// </summary> private void DrawGrid() { Grid = new LineGeometry3D(); var positions = new Vector3Collection(); var indices = new IntCollection(); var colors = new Color4Collection(); for(var i= 0; i < 10; i += 1) { for (var j = 0; j < 10; j += 1) { DrawGridPatch(positions, indices, colors, -50 + i * 10, -50 + j * 10); } } Grid.Positions = positions; Grid.Indices = indices; Grid.Colors = colors; Axes = new LineGeometry3D(); var axesPositions = new Vector3Collection(); var axesIndices = new IntCollection(); var axesColors = new Color4Collection(); // Draw the coordinate axes axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(50, 0, 0)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Red); axesColors.Add(SharpDX.Color.Red); axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(0, 5, 0)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Blue); axesColors.Add(SharpDX.Color.Blue); axesPositions.Add(new Vector3()); axesIndices.Add(axesPositions.Count - 1); axesPositions.Add(new Vector3(0, 0, -50)); axesIndices.Add(axesPositions.Count - 1); axesColors.Add(SharpDX.Color.Green); axesColors.Add(SharpDX.Color.Green); Axes.Positions = axesPositions; Axes.Indices = axesIndices; Axes.Colors = axesColors; }
private void CreateWireFrameModels(IFCItem item, Vector3D center) { while (item != null) { if (item.ifcID != IntPtr.Zero && item.noVerticesForWireFrame != 0 && item.noPrimitivesForWireFrame != 0) { var points = new Vector3Collection(); Vector3Collection positions; if (item.verticesForWireFrame != null) { for (int i = 0; i < item.noVerticesForWireFrame; i++) { points.Add(new Vector3((float)(item.verticesForWireFrame[3 * i + 0] - center.X), (float)(item.verticesForWireFrame[3 * i + 1] - center.Y), (float)(item.verticesForWireFrame[3 * i + 2] - center.Z))); } } if (item.indicesForWireFrameLineParts != null) { positions = new Vector3Collection(); for (int i = 0; i < item.noPrimitivesForWireFrame; i++) { var idx = item.indicesForWireFrameLineParts[2 * i + 0]; positions.Add(points[idx]); idx = item.indicesForWireFrameLineParts[2 * i + 1]; positions.Add(points[idx]); } } else { positions = points; } var lineBuilder = new LineBuilder(); lineBuilder.Add(false,positions.ToArray()); LineGeometryModel3D line = new LineGeometryModel3D(); line.Geometry = lineBuilder.ToLineGeometry3D(); line.Color = new SharpDX.Color(0,0,0,0); item.Wireframe = line; model.Add(line as Element3D); } CreateFaceModels(item.child, center); item = item.next; } }
public static PointGeometry3D ConvertToPointGeometry3D(Points[] points) { var geometry = new PointGeometry3D(); var col = new Color4Collection(); var ptPos = new Vector3Collection(); var ptIdx = new IntCollection(); var ptNormals = new Vector3Collection(); var additionalTurns = 0; foreach (var point in points) { ptIdx.Add(ptPos.Count); ptPos.Add(new Vector3(point.x, point.y, point.z)); col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255))); ptNormals.Add(new Vector3(0, 1, 0)); } if ((points.Length / 3) * 3 != points.Length) { additionalTurns = ((points.Length / 3 + 1) * 3) - points.Length; } for (int i = 0; i < additionalTurns; i++) { ptIdx.Add(ptPos.Count); ptPos.Add(ptPos[ptPos.Count - 1]); col.Add(col[col.Count - 1]); ptNormals.Add(ptNormals[ptNormals.Count - 1]); } geometry.Positions = ptPos; geometry.Indices = ptIdx; geometry.Colors = col; return geometry; }
public static void Draw(IFCItem element, ref List<IfcMeshGeometryModel3D> modelList) { while (element != null) { if (element.child != null) { Draw(element.child, ref modelList); } if (!(element.noPrimitivesForFaces > 0)) { element = element.next; continue; } MeshGeometry3D geometry = new MeshGeometry3D() { Positions = new Vector3Collection(), Normals = new Vector3Collection(), Indices = new IntCollection(), TextureCoordinates = new Vector2Collection() }; var positions = new Vector3Collection(); var normals = new Vector3Collection(); //ToDo: Depending on if the model is modelled in meter or millimeter this divident has to be changed var divident = 1000; for (int i = 0; i < element.noVerticesForFaces; i++) { var offset = i * 6; positions.Add(new SharpDX.Vector3( element.verticesForFaces[offset + 0] / divident, element.verticesForFaces[offset + 2] / divident, -element.verticesForFaces[offset + 1] / divident )); normals.Add(new SharpDX.Vector3( element.verticesForFaces[offset + 3], element.verticesForFaces[offset + 5], -element.verticesForFaces[offset + 4] )); } for (int i = 0; i < element.indicesForFaces.Length; i++) { geometry.Indices.Add(i); geometry.Positions.Add(positions[(int)element.indicesForFaces[i]]); geometry.Normals.Add(normals[(int)element.indicesForFaces[i]]); //just needs to be filled with something geometry.TextureCoordinates.Add(new Vector2(0,0)); } modelList.Add(new IfcMeshGeometryModel3D() { Geometry = geometry, IfcName = element.name }); element = element.next; } }
public static MeshGeometry3D Merge(params MeshGeometry3D[] meshes) { var positions = new Vector3Collection(); var indices = new IntCollection(); var normals = meshes.All(x => x.Normals != null) ? new Vector3Collection() : null; var colors = meshes.All(x => x.Colors != null) ? new Color4Collection() : null; var textureCoods = meshes.All(x => x.TextureCoordinates != null) ? new Vector2Collection() : null; var tangents = meshes.All(x => x.Tangents != null) ? new Vector3Collection() : null; var bitangents = meshes.All(x => x.BiTangents != null) ? new Vector3Collection() : null; int index = 0; foreach (var part in meshes) { for (int i = 0; i < part.Positions.Count; i++) { positions.Add(part.Positions[i]); } for (int i = 0; i < part.Indices.Count; i++) { indices.Add(index + part.Indices[i]); } index += part.Indices.Count; } if (normals != null) { normals = new Vector3Collection(meshes.SelectMany(x => x.Normals)); } if (colors != null) { colors = new Color4Collection(meshes.SelectMany(x => x.Colors)); } if (textureCoods != null) { textureCoods = new Vector2Collection(meshes.SelectMany(x => x.TextureCoordinates)); } if (tangents != null) { tangents = new Vector3Collection(meshes.SelectMany(x => x.Tangents)); } if (bitangents != null) { bitangents = new Vector3Collection(meshes.SelectMany(x => x.BiTangents)); } var mesh = new MeshGeometry3D() { Positions = positions, Indices = indices, }; mesh.Normals = normals; mesh.Colors = colors; mesh.TextureCoordinates = textureCoods; mesh.Tangents = tangents; mesh.BiTangents = bitangents; return mesh; }
/// <summary> /// Chamfers the specified corner (experimental code). /// </summary> /// <param name="p"> /// The corner point. /// </param> /// <param name="d"> /// The chamfer distance. /// </param> /// <param name="eps"> /// The corner search limit distance. /// </param> /// <param name="chamferPoints"> /// If this parameter is provided, the collection will be filled with the generated chamfer points. /// </param> public void ChamferCorner(Vector3 p, double d, double eps = 1e-6, IList<Vector3> chamferPoints = null) { this.NoSharedVertices(); this.normals = null; this.textureCoordinates = null; var cornerNormal = this.FindCornerNormal(p, eps); var newCornerPoint = p - (cornerNormal * (float)d); int index0 = this.positions.Count; this.positions.Add(newCornerPoint); var plane = new Plane3D(newCornerPoint, cornerNormal); int ntri = this.triangleIndices.Count; for (int i = 0; i < ntri; i += 3) { int i0 = i; int i1 = i + 1; int i2 = i + 2; var p0 = this.positions[this.triangleIndices[i0]]; var p1 = this.positions[this.triangleIndices[i1]]; var p2 = this.positions[this.triangleIndices[i2]]; var d0 = (p - p0).LengthSquared(); var d1 = (p - p1).LengthSquared(); var d2 = (p - p2).LengthSquared(); var mind = Math.Min(d0, Math.Min(d1, d2)); if (mind > eps) { continue; } if (d1 < eps) { i0 = i + 1; i1 = i + 2; i2 = i; } if (d2 < eps) { i0 = i + 2; i1 = i; i2 = i + 1; } p0 = this.positions[this.triangleIndices[i0]]; p1 = this.positions[this.triangleIndices[i1]]; p2 = this.positions[this.triangleIndices[i2]]; // p0 is the corner vertex (at index i0) // find the intersections between the chamfer plane and the two edges connected to the corner var line1 = new Ray(p0, p1 - p0); var line2 = new Ray(p0, p2 - p0); Vector3 p01, p02; if (!plane.Intersects(ref line1, out p01)) { continue; } if (!plane.Intersects(ref line2, out p02)) { continue; } if (chamferPoints != null) { // add the chamfered points if (!chamferPoints.Contains(p01)) { chamferPoints.Add(p01); } if (!chamferPoints.Contains(p02)) { chamferPoints.Add(p02); } } int i01 = i0; // change the original triangle to use the first chamfer point this.positions[this.triangleIndices[i01]] = p01; int i02 = this.positions.Count; this.positions.Add(p02); // add a new triangle for the other chamfer point this.triangleIndices.Add(i01); this.triangleIndices.Add(i2); this.triangleIndices.Add(i02); // add a triangle connecting the chamfer points and the new corner point this.triangleIndices.Add(index0); this.triangleIndices.Add(i01); this.triangleIndices.Add(i02); } this.NoSharedVertices(); }
public MainViewModel() { // titles Title = "Simple Demo"; SubTitle = "WPF & SharpDX"; // camera setup Camera = new PerspectiveCamera { Position = new Point3D(3, 3, 5), LookDirection = new Vector3D(-3, -3, -5), UpDirection = new Vector3D(0, 1, 0), FarPlaneDistance = 5000000 }; // Create a custom render techniques manager that // only supports Phong and Blinn RenderTechniquesManager = new CustomRenderTechniquesManager(); RenderTechnique = RenderTechniquesManager.RenderTechniques["RenderCustom"]; EffectsManager = new CustomEffectsManager(RenderTechniquesManager); // setup lighting AmbientLightColor = new Color4(0.1f, 0.1f, 0.1f, 1.0f); DirectionalLightColor = Color.White; DirectionalLightDirection = new Vector3(-2, -5, -2); // floor plane grid Grid = LineBuilder.GenerateGrid(); GridColor = Color.Black; GridTransform = new Media3D.TranslateTransform3D(-5, -1, -5); // scene model3d var b1 = new MeshBuilder(); b1.AddSphere(new Vector3(0, 0, 0), 0.5); b1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2, BoxFaces.All); var meshGeometry = b1.ToMeshGeometry3D(); meshGeometry.Colors = new Color4Collection(meshGeometry.TextureCoordinates.Select(x => x.ToColor4())); Model = meshGeometry; // lines model3d var e1 = new LineBuilder(); e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2); Lines = e1.ToLineGeometry3D(); // model transform Model1Transform = new Media3D.TranslateTransform3D(0, 0, 0); Model2Transform = new Media3D.TranslateTransform3D(-2, 0, 0); Model3Transform = new Media3D.TranslateTransform3D(+2, 0, 0); // model materials RedMaterial = PhongMaterials.Red; GreenMaterial = PhongMaterials.Green; BlueMaterial = PhongMaterials.Blue; Points = new PointGeometry3D(); var ptPos = new Vector3Collection(); var ptIdx = new IntCollection(); Text = new BillboardText3D(); for (int x = -5; x <= 5; x++) { for (int y = -5; y <= 5; y++) { ptIdx.Add(ptPos.Count); ptPos.Add(new Vector3(x, -1, y)); Text.TextInfo.Add(new TextInfo(string.Format("{0}:{1}", x, y), new Vector3(x, -1, y))); } } Points.Positions = ptPos; Points.Indices = ptIdx; }
public MainViewModel() { // titles this.Title = "Simple Demo"; this.SubTitle = "WPF & SharpDX"; // camera setup this.Camera = new PerspectiveCamera { Position = new Point3D(3, 3, 5), LookDirection = new Vector3D(-3, -3, -5), UpDirection = new Vector3D(0, 1, 0), FarPlaneDistance = 5000000 }; // default render technique this.RenderTechnique = Techniques.RenderBlinn; // setup lighting this.AmbientLightColor = new Color4(0.1f, 0.1f, 0.1f, 1.0f); this.DirectionalLightColor = Color.White; this.DirectionalLightDirection = new Vector3(-2, -5, -2); // floor plane grid this.Grid = LineBuilder.GenerateGrid(); this.GridColor = SharpDX.Color.Black; this.GridTransform = new Media3D.TranslateTransform3D(-5, -1, -5); // scene model3d var b1 = new MeshBuilder(); b1.AddSphere(new Vector3(0, 0, 0), 0.5); b1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2, BoxFaces.All); var meshGeometry = b1.ToMeshGeometry3D(); meshGeometry.Colors = new Color4Collection(meshGeometry.TextureCoordinates.Select(x => x.ToColor4())); this.Model = meshGeometry; // lines model3d var e1 = new LineBuilder(); e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2); this.Lines = e1.ToLineGeometry3D(); // model trafos this.Model1Transform = new Media3D.TranslateTransform3D(0, 0, 0); this.Model2Transform = new Media3D.TranslateTransform3D(-2, 0, 0); this.Model3Transform = new Media3D.TranslateTransform3D(+2, 0, 0); // model materials this.RedMaterial = PhongMaterials.Red; this.GreenMaterial = PhongMaterials.Green; this.BlueMaterial = PhongMaterials.Blue; //var diffColor = this.RedMaterial.DiffuseColor; //diffColor.Alpha = 0.5f; //this.RedMaterial.DiffuseColor = diffColor; Points = new PointGeometry3D(); var ptPos = new Vector3Collection(); var ptIdx = new IntCollection(); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { for (int z = 0; z < 10; z++) { ptIdx.Add(ptPos.Count); ptPos.Add(new Vector3(x, y, z)); } } } Points.Positions = ptPos; Points.Indices = ptIdx; Text = new BillboardText3D(); for (var i = 0; i < 50; i++) { for (var j = 0; j < 50; j++) { Text.TextInfo.Add(new TextInfo("Hello World", new Vector3(i,j,0))); } } }
/// <summary> /// /// </summary> public LineBuilder() { positions = new Vector3Collection(); // textureCoordinates = new List<Point>(); lineListIndices = new IntCollection(); }
/// <summary> /// Initializes a new instance of the <see cref="MeshBuilder"/> class. /// </summary> /// <remarks> /// Normal and texture coordinate generation are included. /// </remarks> public MeshBuilder(bool generateNormals = true, bool generateTexCoords = true, bool tangentSpace = false) { this.positions = new Vector3Collection(); this.triangleIndices = new IntCollection(); if (generateNormals) { this.normals = new Vector3Collection(); } if (generateTexCoords) { this.textureCoordinates = new Vector2Collection(); } if (tangentSpace) { this.tangents = new Vector3Collection(); this.bitangents = new Vector3Collection(); } }
private void CreateFaceModels(IFCItem item, Vector3 center) { while (item != null) { if (item.ifcID != IntPtr.Zero && item.noVerticesForFaces != 0 && item.noPrimitivesForFaces != 0) { var positions = new Vector3Collection(); var normals = new Vector3Collection(); if (item.verticesForFaces != null) { for (int i = 0; i < item.noVerticesForFaces; i++) { var point = new Vector3(item.verticesForFaces[6 * i + 0] - center.X, item.verticesForFaces[6 * i + 1] - center.Y, item.verticesForFaces[6 * i + 2] - center.Z); var normal = new Vector3(item.verticesForFaces[6 * i + 3], item.verticesForFaces[6 * i + 4], item.verticesForFaces[6 * i + 5]); positions.Add(point); normals.Add(normal); } Debug.Assert(item.verticesForFaces.Length == item.noVerticesForFaces * 6); } var indices = new IntCollection(); if (item.indicesForFaces != null) { for (int i = 0; i < 3 * item.noPrimitivesForFaces; i++) { indices.Add(item.indicesForFaces[i]); } } var meshGeometry = new MeshGeometry3D(); meshGeometry.Positions = positions; meshGeometry.Normals = normals; meshGeometry.Indices = indices; meshGeometry.TextureCoordinates = null; meshGeometry.Colors = null; meshGeometry.Tangents = null; meshGeometry.BiTangents = null; MeshGeometryModel3D mesh = new MeshGeometryModel3D() { Geometry = meshGeometry }; // var builder = new MeshBuilder(true, false); // builder.Positions.AddRange(positions); // builder.Normals.AddRange(normals); // builder.TriangleIndices.AddRange(indices); // MeshGeometryModel3D mesh = new MeshGeometryModel3D() { Geometry = builder.ToMeshGeometry3D() }; item.Mesh3d = mesh; _meshToIfcItems[mesh] = item; //#if DEBUG // OutputObj(item.ifcID.ToString(), meshGeometry); //#endif FillMeshByIfcColor(item); mesh.Tag = item.ifcType + ":" + item.ifcID; model.Add(mesh); } CreateFaceModels(item.child, center); item = item.next; } }
private static void AppendSphere(Vector3 center, double radius, int thetaSteps, int phiSteps, out Vector3Collection positions, out Vector3Collection normals, out Vector2Collection textureCoordinates, out IntCollection triangleIndices) { positions = new Vector3Collection(); normals = new Vector3Collection(); textureCoordinates = new Vector2Collection(); triangleIndices = new IntCollection(); double dt = DegToRad(360.0) / thetaSteps; double dp = DegToRad(180.0) / phiSteps; for (int pi = 0; pi <= phiSteps; pi++) { double phi = pi * dp; for (int ti = 0; ti <= thetaSteps; ti++) { // we want to start the mesh on the x axis double theta = ti * dt; positions.Add(GetPosition(theta, phi, radius) + center); normals.Add(GetNormal(theta, phi)); textureCoordinates.Add(GetTextureCoordinate(theta, phi)); } } for (int pi = 0; pi < phiSteps; pi++) { for (int ti = 0; ti < thetaSteps; ti++) { int x0 = ti; int x1 = ti + 1; int y0 = pi * (thetaSteps + 1); int y1 = (pi + 1) * (thetaSteps + 1); triangleIndices.Add(x0 + y0); triangleIndices.Add(x0 + y1); triangleIndices.Add(x1 + y0); triangleIndices.Add(x1 + y0); triangleIndices.Add(x0 + y1); triangleIndices.Add(x1 + y1); } } }
public void ComputeTangents(MeshFaces meshFaces) { switch (meshFaces) { case MeshFaces.Default: if (this.positions != null & this.triangleIndices != null & this.normals != null & this.textureCoordinates != null) { Vector3Collection t1, t2; ComputeTangents(this.positions, this.normals, this.textureCoordinates, this.triangleIndices, out t1, out t2); this.tangents = t1; this.bitangents = t2; } break; case MeshFaces.QuadPatches: if (this.positions != null & this.triangleIndices != null & this.normals != null & this.textureCoordinates != null) { Vector3Collection t1, t2; ComputeTangentsQuads(this.positions, this.normals, this.textureCoordinates, this.triangleIndices, out t1, out t2); this.tangents = t1; this.bitangents = t2; } break; default: break; } }
/// <summary> /// Makes sure no triangles share the same vertex. /// </summary> private void NoSharedVertices() { var p = new Vector3Collection(); var ti = new IntCollection(); Vector3Collection n = null; if (this.normals != null) { n = new Vector3Collection(); } Vector2Collection tc = null; if (this.textureCoordinates != null) { tc = new Vector2Collection(); } for (int i = 0; i < this.triangleIndices.Count; i += 3) { int i0 = i; int i1 = i + 1; int i2 = i + 2; int index0 = this.triangleIndices[i0]; int index1 = this.triangleIndices[i1]; int index2 = this.triangleIndices[i2]; var p0 = this.positions[index0]; var p1 = this.positions[index1]; var p2 = this.positions[index2]; p.Add(p0); p.Add(p1); p.Add(p2); ti.Add(i0); ti.Add(i1); ti.Add(i2); if (n != null) { n.Add(this.normals[index0]); n.Add(this.normals[index1]); n.Add(this.normals[index2]); } if (tc != null) { tc.Add(this.textureCoordinates[index0]); tc.Add(this.textureCoordinates[index1]); tc.Add(this.textureCoordinates[index2]); } } this.positions = p; this.triangleIndices = ti; this.normals = n; this.textureCoordinates = tc; }
private static void DrawGridPatch( Vector3Collection positions, IntCollection indices, Color4Collection colors, int startX, int startY) { var c1 = (Color)ColorConverter.ConvertFromString("#c5d1d8"); c1.Clamp(); var c2 = (Color)ColorConverter.ConvertFromString("#ddeaf2"); c2.Clamp(); var darkGridColor = new Color4(new Vector4(c1.ScR,c1.ScG ,c1.ScB, 1)); var lightGridColor = new Color4(new Vector4(c2.ScR, c2.ScG, c2.ScB, 1)); const int size = 10; for (var x = startX; x <= startX + size; x++) { if (x == 0 && startY < 0) continue; var v = new Vector3(x, -.001f, startY); positions.Add(v); indices.Add(positions.Count - 1); positions.Add(new Vector3(x, -.001f, startY + size)); indices.Add(positions.Count - 1); if (x % 5 == 0) { colors.Add(darkGridColor); colors.Add(darkGridColor); } else { colors.Add(lightGridColor); colors.Add(lightGridColor); } } for (var y = startY; y <= startY + size; y++) { if (y == 0 && startX >= 0) continue; positions.Add(new Vector3(startX, -.001f, y)); indices.Add(positions.Count - 1); positions.Add(new Vector3(startX + size, -.001f, y)); indices.Add(positions.Count - 1); if (y % 5 == 0) { colors.Add(darkGridColor); colors.Add(darkGridColor); } else { colors.Add(lightGridColor); colors.Add(lightGridColor); } } }
private Vector3Collection ReadVertexList(BinaryReader reader) { int size = reader.ReadUInt16(); var pts = new Vector3Collection(); for(int i=0;i< size; i++) { float x = reader.ReadSingle(); float y = reader.ReadSingle(); float z = reader.ReadSingle(); pts.Add(new Vector3(x, y, z)); } return pts; }
private void LoadThreadFunction() { PCD wrapper = new PCD(); Points[] rawPoints = wrapper.LoadPointcloud(Filename); var points = new PointGeometry3D(); var col = new Color4Collection(); var ptPos = new Vector3Collection(); var ptIdx = new IntCollection(); var ptNormals = new Vector3Collection(); var numberOfElements = rawPoints.Length; var additionalTurns = 0; foreach (var point in rawPoints) { ptIdx.Add(ptPos.Count); ptPos.Add(new Vector3(point.x, point.z, -point.y)); col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255))); ptNormals.Add(new Vector3(0, 1, 0)); } if ((rawPoints.Length / 3) * 3 != rawPoints.Length) { additionalTurns = ((rawPoints.Length / 3 + 1) * 3) - rawPoints.Length; } for (int i = 0; i < additionalTurns; i++) { ptIdx.Add(ptPos.Count); ptPos.Add(ptPos[ptPos.Count-1]); col.Add(col[col.Count - 1]); ptNormals.Add(ptNormals[ptNormals.Count - 1]); } points.Positions = ptPos; points.Indices = ptIdx; points.Colors = col; //points.Normals = ptNormals; Data = points; }
private static void ComputeNormals(Vector3Collection positions, IntCollection triangleIndices, out Vector3Collection normals) { normals = new Vector3Collection(positions.Count); normals.AddRange(Enumerable.Repeat(Vector3.Zero, positions.Count)); for (int t = 0; t < triangleIndices.Count; t += 3) { var i1 = triangleIndices[t]; var i2 = triangleIndices[t + 1]; var i3 = triangleIndices[t + 2]; var v1 = positions[i1]; var v2 = positions[i2]; var v3 = positions[i3]; var p1 = v2 - v1; var p2 = v3 - v1; var n = Vector3.Cross(p1, p2); // angle p1.Normalize(); p2.Normalize(); var a = (float)Math.Acos(Vector3.Dot(p1, p2)); n.Normalize(); normals[i1] += (a * n); normals[i2] += (a * n); normals[i3] += (a * n); } for (int i = 0; i < normals.Count; i++) { normals[i].Normalize(); } }
public static void ComputeTangentsQuads(IList<Vector3> positions, IList<Vector3> normals, IList<Vector2> textureCoordinates, IList<int> indices, out Vector3Collection tangents, out Vector3Collection bitangents) { var tan1 = new Vector3[positions.Count]; for (int t = 0; t < indices.Count; t += 4) { var i1 = indices[t]; var i2 = indices[t + 1]; var i3 = indices[t + 2]; var i4 = indices[t + 3]; var v1 = positions[i1]; var v2 = positions[i2]; var v3 = positions[i3]; var v4 = positions[i4]; var w1 = textureCoordinates[i1]; var w2 = textureCoordinates[i2]; var w3 = textureCoordinates[i3]; var w4 = textureCoordinates[i4]; float x1 = v2.X - v1.X; float x2 = v4.X - v1.X; float y1 = v2.Y - v1.Y; float y2 = v4.Y - v1.Y; float z1 = v2.Z - v1.Z; float z2 = v4.Z - v1.Z; float s1 = w2.X - w1.X; float s2 = w4.X - w1.X; float t1 = w2.Y - w1.Y; float t2 = w4.Y - w1.Y; float r = 1.0f / (s1 * t2 - s2 * t1); var udir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); //var vdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); tan1[i1] += udir; tan1[i2] += udir; tan1[i3] += udir; tan1[i4] += udir; //tan2[i1] += vdir; //tan2[i2] += vdir; //tan2[i3] += vdir; } tangents = new Vector3Collection(positions.Count); bitangents = new Vector3Collection(positions.Count); for (int i = 0; i < positions.Count; i++) { var n = normals[i]; var t = tan1[i]; t = (t - n * Vector3.Dot(n, t)); t.Normalize(); var b = Vector3.Cross(n, t); tangents.Add(t); bitangents.Add(b); } }
private void CreateWireFrameModels(IFCItem item, Vector3 center) { while (item != null) { if (item.ifcID != IntPtr.Zero && item.noVerticesForWireFrame != 0 && item.noPrimitivesForWireFrame != 0) { var geo = new LineGeometry3D(); geo.Positions=new Vector3Collection(); geo.Indices=new IntCollection(); var points = new Vector3Collection(); if (item.verticesForWireFrame != null) { for (int i = 0; i < item.noVerticesForWireFrame; i++) { points.Add(new Vector3((item.verticesForWireFrame[3 * i + 0] - center.X), (item.verticesForWireFrame[3 * i + 1] - center.Y), (item.verticesForWireFrame[3 * i + 2] - center.Z))); geo.Positions.Add(new Vector3((item.verticesForWireFrame[3 * i + 0] - center.X), (item.verticesForWireFrame[3 * i + 1] - center.Y), (item.verticesForWireFrame[3 * i + 2] - center.Z))); } } if (item.indicesForWireFrameLineParts != null) { for (int i = 0; i < item.noPrimitivesForWireFrame; i++) { var idx = item.indicesForWireFrameLineParts[2 * i + 0]; geo.Indices.Add(idx); idx = item.indicesForWireFrameLineParts[2 * i + 1]; geo.Indices.Add(idx); } } else { for (int i = 0, count = points.Count; i < count; i++) { geo.Indices.Add(i); geo.Indices.Add((i + 1) % count); } } LineGeometryModel3D line = new LineGeometryModel3D(); line.Geometry = geo; line.Color = _defaultLineColor; line.Thickness = 0.5; item.Wireframe = line; line.Tag = item.ifcType + ":" + item.ifcID; model.Add(line); } CreateFaceModels(item.child, center); item = item.next; } }
/// <summary> /// Create a Mesh, with found props /// </summary> /// <param name="positions"></param> /// <param name="textureCoordinates"></param> /// <param name="triangleIndices"></param> /// <param name="normals"></param> /// <param name="tangents"></param> /// <param name="bitangents"></param> /// <param name="material"></param> private void CreateMesh(Vector3Collection positions, Vector2Collection textureCoordinates, IntCollection triangleIndices, out Vector3Collection normals, out Vector3Collection tangents, out Vector3Collection bitangents,Material material) { ComputeNormals(positions, triangleIndices, out normals); if (textureCoordinates == null) { textureCoordinates = new Vector2Collection(); foreach(var pos in positions) { textureCoordinates.Add(Vector2.One); } } MeshBuilder.ComputeTangents(positions, normals, textureCoordinates, triangleIndices, out tangents, out bitangents); MeshGeometry3D mesh = new MeshGeometry3D() { Positions = positions, Normals = normals, TextureCoordinates = textureCoordinates, Indices = triangleIndices, Tangents = tangents, BiTangents = bitangents }; Object3D ob3d = new Object3D(); ob3d.Geometry = mesh; ob3d.Material = material; ob3d.Transform = Matrix.Identity; ob3d.Name = "Default"; this.obGroup.Add(ob3d); }