public CameraModel3D() { var b1 = new MeshBuilder(); b1.AddBox(new Vector3(), 1f, 1f, 1.2f, BoxFaces.All); var body = new MeshGeometryModel3D() { CullMode = CullMode.Back }; body.Geometry = b1.ToMeshGeometry3D(); body.Material = PhongMaterials.Gray; body.SceneNode.OnSetRenderTechnique = (h) => { return(h.EffectsManager[DefaultRenderTechniqueNames.Diffuse]); }; this.Children.Add(body); b1 = new MeshBuilder(); b1.AddCone(new Vector3(0, 0, -1.2f), new Vector3(0, 0f, 0), 0.4f, true, 12); var lens = new MeshGeometryModel3D() { CullMode = CullMode.Back }; lens.Geometry = b1.ToMeshGeometry3D(); lens.Material = PhongMaterials.Yellow; lens.SceneNode.OnSetRenderTechnique = (h) => { return(h.EffectsManager[DefaultRenderTechniqueNames.Diffuse]); }; this.Children.Add(lens); var builder = new LineBuilder(); builder.AddLine(Vector3.Zero, new Vector3(2, 0, 0)); builder.AddLine(Vector3.Zero, new Vector3(0, 2, 0)); builder.AddLine(Vector3.Zero, new Vector3(0, 0, -2)); var mesh = builder.ToLineGeometry3D(); var arrowMeshModel = new LineGeometryModel3D(); arrowMeshModel.Geometry = mesh; arrowMeshModel.Color = System.Windows.Media.Colors.White; arrowMeshModel.IsHitTestVisible = false; int segment = mesh.Positions.Count / 3; var colors = new Core.Color4Collection(Enumerable.Repeat <Color4>(Color.Black, mesh.Positions.Count)); int i = 0; for (; i < segment; ++i) { colors[i] = Color.Red; } for (; i < segment * 2; ++i) { colors[i] = Color.Green; } for (; i < segment * 3; ++i) { colors[i] = Color.Blue; } mesh.Colors = colors; this.Children.Add(arrowMeshModel); SceneNode.OnTransformChanged += SceneNode_OnTransformChanged; }
/// <summary> /// Generates a square grid with a step of 1.0 /// </summary> /// <returns></returns> public static LineGeometry3D GenerateGrid(Vector3 plane, int min = 0, int max = 10) { var grid = new LineBuilder(); //int width = max - min; if (plane == Vector3.UnitX) { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(0, i, min), new Vector3(0, i, max)); grid.AddLine(new Vector3(0, min, i), new Vector3(0, max, i)); } } else if (plane == Vector3.UnitY) { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(i, 0, min), new Vector3(i, 0, max)); grid.AddLine(new Vector3(min, 0, i), new Vector3(max, 0, i)); } } else { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(i, min, 0), new Vector3(i, max, 0)); grid.AddLine(new Vector3(min, i, 0), new Vector3(max, i, 0)); } } return(grid.ToLineGeometry3D()); }
public LineGeometryModel3D GetGrid() { var lineBilder = new LineBuilder(); for (int x = -1000; x < 1000; x += 100) { lineBilder.AddLine(new Vector3(x, 0, -1000), new Vector3(x, 0, 1000)); } for (int z = -1000; z < 1000; z += 100) { lineBilder.AddLine(new Vector3(-1000, 0, z), new Vector3(1000, 0, z)); } LineGeometryModel3D m = new LineGeometryModel3D(); m.Geometry = lineBilder.ToLineGeometry3D(); m.Color = Color.Black; m.Transform = new TranslateTransform3D(new Vector3D(0, 0, 0)); m.Attach(Viewport.RenderHost); return m; }
public static LineGeometry3D GenerateGrid(Vector3 plane, int min0 = 0, int max0 = 10, int min1 = 0, int max1 = 10, int sizew = 1, int sizeh = 1) { var grid = new LineBuilder(); //int width = max - min; if (plane == Vector3.UnitX) { for (int i = min0; i <= max0; i += sizew) { grid.AddLine(new Vector3(0, i, min1), new Vector3(0, i, max1)); } for (int i = min1; i <= max1; i += sizeh) { grid.AddLine(new Vector3(0, min0, i), new Vector3(0, max0, i)); } } else if (plane == Vector3.UnitY) { for (int i = min0; i <= max0; i += sizew) { grid.AddLine(new Vector3(i, 0, min1), new Vector3(i, 0, max1)); } for (int i = min1; i <= max1; i += sizeh) { grid.AddLine(new Vector3(min0, 0, i), new Vector3(max0, 0, i)); } } else { for (int i = min0; i <= max0; i += sizew) { grid.AddLine(new Vector3(i, min1, 0), new Vector3(i, max1, 0)); } for (int i = min1; i <= max1; i += sizeh) { grid.AddLine(new Vector3(min0, i, 0), new Vector3(max0, i, 0)); } } return(grid.ToLineGeometry3D()); }
/// <summary> /// /// </summary> /// <param name="plane"></param> /// <param name="radius"></param> /// <param name="segments"></param> /// <returns></returns> public static LineGeometry3D GenerateCircle(Vector3 plane, float radius, int segments) { if (segments < 3) { throw new ArgumentNullException("too few segments, at least 3"); } var circle = new LineBuilder(); float sectionAngle = (float)(2.0 * Math.PI / segments); if (plane == Vector3.UnitX) { Point3D start = new Point3D(0.0f, 0.0f, radius); Point3D current = new Point3D(0.0f, 0.0f, radius); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.Z = radius * (float)Math.Cos(i * sectionAngle); next.Y = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } else if (plane == Vector3.UnitY) { Point3D start = new Point3D(radius, 0.0f, 0.0f); Point3D current = new Point3D(radius, 0.0f, 0.0f); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.X = radius * (float)Math.Cos(i * sectionAngle); next.Z = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } else { Point3D start = new Point3D(0.0f, radius, 0.0f); Point3D current = new Point3D(0.0f, radius, 0.0f); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.Y = radius * (float)Math.Cos(i * sectionAngle); next.X = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } return(circle.ToLineGeometry3D()); }
/// <summary> /// Returns a line geometry of the axis-aligned bounding-box of the given mesh. /// </summary> /// <param name="mesh">Input mesh for the computation of the b-box</param> /// <returns></returns> public static LineGeometry3D GenerateBoundingBox(global::SharpDX.BoundingBox bb) { var cc = bb.GetCorners(); var ll = new LineBuilder(); ll.AddLine(cc[0], cc[1]); ll.AddLine(cc[1], cc[2]); ll.AddLine(cc[2], cc[3]); ll.AddLine(cc[3], cc[0]); ll.AddLine(cc[4], cc[5]); ll.AddLine(cc[5], cc[6]); ll.AddLine(cc[6], cc[7]); ll.AddLine(cc[7], cc[4]); ll.AddLine(cc[0], cc[4]); ll.AddLine(cc[1], cc[5]); ll.AddLine(cc[2], cc[6]); ll.AddLine(cc[3], cc[7]); return(ll.ToLineGeometry3D()); }
public CameraModel3D() { var b1 = new MeshBuilder(); b1.AddBox(new Vector3(), 1f, 1f, 1.2f, BoxFaces.All); var body = new MeshGeometryModel3D() { CullMode = CullMode.Back }; body.Geometry = b1.ToMeshGeometry3D(); body.Material = new DiffuseMaterial() { DiffuseColor = Color.Gray }; this.Children.Add(body); b1 = new MeshBuilder(); b1.AddCone(new Vector3(0, 0, -1.2f), new Vector3(0, 0f, 0), 0.4f, true, 12); var lens = new MeshGeometryModel3D() { CullMode = CullMode.Back }; lens.Geometry = b1.ToMeshGeometry3D(); lens.Material = new DiffuseMaterial() { DiffuseColor = Color.Yellow }; this.Children.Add(lens); var builder = new LineBuilder(); builder.AddLine(Vector3.Zero, new Vector3(2, 0, 0)); builder.AddLine(Vector3.Zero, new Vector3(0, 2, 0)); builder.AddLine(Vector3.Zero, new Vector3(0, 0, -2)); var mesh = builder.ToLineGeometry3D(); var arrowMeshModel = new LineGeometryModel3D { Geometry = mesh, Color = System.Windows.Media.Colors.White, IsHitTestVisible = false }; int segment = mesh.Positions.Count / 3; var colors = new Color4Collection(Enumerable.Repeat <Color4>(Color.Black, mesh.Positions.Count)); int i = 0; for (; i < segment; ++i) { colors[i] = Color.Red; } for (; i < segment * 2; ++i) { colors[i] = Color.Green; } for (; i < segment * 3; ++i) { colors[i] = Color.Blue; } mesh.Colors = colors; this.Children.Add(arrowMeshModel); SceneNode.TransformChanged += SceneNode_OnTransformChanged; }
/// <summary> /// The on children changed. /// </summary> protected virtual void OnChildrenChanged() { this.translateXL.Length = 0.5; this.translateYL.Length = 0.5; this.translateZL.Length = 0.5; this.translateXR.Length = 0.5; this.translateYR.Length = 0.5; this.translateZR.Length = 0.5; this.Children.Clear(); if (this.CanTranslateX) { this.Children.Add(this.translateXL); this.Children.Add(this.translateXR); } if (this.CanTranslateY) { this.Children.Add(this.translateYL); this.Children.Add(this.translateYR); } if (this.CanTranslateZ) { this.Children.Add(this.translateZL); this.Children.Add(this.translateZR); } { var g = new LineBuilder(); g.AddLine(new Vector3(0, 0, 0), new Vector3(1, 0, 0)); g.AddLine(new Vector3(1, 0, 0), new Vector3(1, 1, 0)); g.AddLine(new Vector3(1, 1, 0), new Vector3(0, 1, 0)); g.AddLine(new Vector3(0, 1, 0), new Vector3(0, 0, 0)); this.selectionBounds = new LineGeometryModel3D() { Thickness = 3, Smoothness = 2, Color = Color.Red, IsThrowingShadow = false, Geometry = g.ToLineGeometry3D(), }; this.Children.Add(this.selectionBounds); } }
/// <summary> /// Generate a simple Polygon and then triangulate it. /// The Result is then Displayed. /// </summary> /// <param name="sender">The Sender (i.e. the Button)</param> /// <param name="e">The routet Event Args</param> private void generatePolygonButton_Click(object sender, RoutedEventArgs e) { // Generate random Polygon var random = new Random(); var cnt = mViewModel.PointCount; mPolygonPoints = new List<Vector2>(); var angle = 0f; var angleDiff = 2f * (Single)Math.PI / cnt; var radius = 4f; // Random Radii for the Polygon var radii = new List<float>(); var innerRadii = new List<float>(); for (int i = 0; i < cnt; i++) { radii.Add(random.NextFloat(radius * 0.9f, radius * 1.1f)); innerRadii.Add(random.NextFloat(radius * 0.2f, radius * 0.3f)); } var hole1 = new List<Vector2>(); var hole2 = new List<Vector2>(); var holeDistance = 2f; var holeAngle = random.NextFloat(0, (float)Math.PI * 2); var cos = (float)Math.Cos(holeAngle); var sin = (float)Math.Sin(holeAngle); var offset1 = new Vector2(holeDistance * cos, holeDistance * sin); var offset2 = new Vector2(-holeDistance * cos, -holeDistance * sin); for (int i = 0; i < cnt; i++) { // Flatten a bit var radiusUse = radii[i]; mPolygonPoints.Add(new Vector2(radii[i] * (Single)Math.Cos(angle), radii[i] * (Single)Math.Sin(angle))); hole1.Add(offset1 + new Vector2(innerRadii[i] * (Single)Math.Cos(-angle), innerRadii[i] * (Single)Math.Sin(-angle))); hole2.Add(offset2 + new Vector2(innerRadii[i] * (Single)Math.Cos(-angle), innerRadii[i] * (Single)Math.Sin(-angle))); angle += angleDiff; } var holes = new List<List<Vector2>>() { hole1, hole2 }; // Triangulate and measure the Time needed for the Triangulation var before = DateTime.Now; var sLTI = SweepLinePolygonTriangulator.Triangulate(mPolygonPoints, holes); var after = DateTime.Now; // Generate the Output var geometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D(); geometry.Positions = new HelixToolkit.Wpf.SharpDX.Core.Vector3Collection(); geometry.Normals = new HelixToolkit.Wpf.SharpDX.Core.Vector3Collection(); foreach (var point in mPolygonPoints.Union(holes.SelectMany(h => h))) { geometry.Positions.Add(new Vector3(point.X, 0, point.Y + 5)); geometry.Normals.Add(new Vector3(0, 1, 0)); } geometry.Indices = new HelixToolkit.Wpf.SharpDX.Core.IntCollection(sLTI); triangulatedPolygon.Geometry = geometry; var lb = new LineBuilder(); for (int i = 0; i < sLTI.Count; i += 3) { lb.AddLine(geometry.Positions[sLTI[i]], geometry.Positions[sLTI[i + 1]]); lb.AddLine(geometry.Positions[sLTI[i + 1]], geometry.Positions[sLTI[i + 2]]); lb.AddLine(geometry.Positions[sLTI[i + 2]], geometry.Positions[sLTI[i]]); } mViewModel.LineGeometry = lb.ToLineGeometry3D(); // Set the Lines if activated if (mViewModel.ShowTriangleLines) { lineTriangulatedPolygon.Geometry = mViewModel.LineGeometry; } else { lineTriangulatedPolygon.Geometry = null; } // Set the InfoLabel Text var timeNeeded = (after - before).TotalMilliseconds; infoLabel.Content = String.Format("Last triangulation of {0} Points took {1:0.##} Milliseconds!", triangulatedPolygon.Geometry.Positions.Count, timeNeeded); }
public static LineGeometry3D CreatePathLines(this IList <BoundingBox> path) { Vector3[] verts = new Vector3[8]; var builder = new LineBuilder(); foreach (var box in path) { verts[0] = box.Minimum; verts[1] = new Vector3(box.Minimum.X, box.Minimum.Y, box.Maximum.Z); //Z verts[2] = new Vector3(box.Minimum.X, box.Maximum.Y, box.Minimum.Z); //Y verts[3] = new Vector3(box.Maximum.X, box.Minimum.Y, box.Minimum.Z); //X verts[7] = box.Maximum; verts[4] = new Vector3(box.Maximum.X, box.Maximum.Y, box.Minimum.Z); //Z verts[5] = new Vector3(box.Maximum.X, box.Minimum.Y, box.Maximum.Z); //Y verts[6] = new Vector3(box.Minimum.X, box.Maximum.Y, box.Maximum.Z); //X builder.AddLine(verts[0], verts[1]); builder.AddLine(verts[0], verts[2]); builder.AddLine(verts[0], verts[3]); builder.AddLine(verts[7], verts[4]); builder.AddLine(verts[7], verts[5]); builder.AddLine(verts[7], verts[6]); builder.AddLine(verts[1], verts[6]); builder.AddLine(verts[1], verts[5]); builder.AddLine(verts[4], verts[2]); builder.AddLine(verts[4], verts[3]); builder.AddLine(verts[2], verts[6]); builder.AddLine(verts[3], verts[5]); } return(builder.ToLineGeometry3D()); }
public static void CreateOctreeLineModel(this IOctree tree, LineBuilder builder) { if (tree == null) { return; } var box = tree.Bound; Vector3[] verts = new Vector3[8]; verts[0] = box.Minimum; verts[1] = new Vector3(box.Minimum.X, box.Minimum.Y, box.Maximum.Z); //Z verts[2] = new Vector3(box.Minimum.X, box.Maximum.Y, box.Minimum.Z); //Y verts[3] = new Vector3(box.Maximum.X, box.Minimum.Y, box.Minimum.Z); //X verts[7] = box.Maximum; verts[4] = new Vector3(box.Maximum.X, box.Maximum.Y, box.Minimum.Z); //Z verts[5] = new Vector3(box.Maximum.X, box.Minimum.Y, box.Maximum.Z); //Y verts[6] = new Vector3(box.Minimum.X, box.Maximum.Y, box.Maximum.Z); //X builder.AddLine(verts[0], verts[1]); builder.AddLine(verts[0], verts[2]); builder.AddLine(verts[0], verts[3]); builder.AddLine(verts[7], verts[4]); builder.AddLine(verts[7], verts[5]); builder.AddLine(verts[7], verts[6]); builder.AddLine(verts[1], verts[6]); builder.AddLine(verts[1], verts[5]); builder.AddLine(verts[4], verts[2]); builder.AddLine(verts[4], verts[3]); builder.AddLine(verts[2], verts[6]); builder.AddLine(verts[3], verts[5]); if (tree.HasChildren) { foreach (IOctree child in tree.ChildNodes) { if (child != null) { CreateOctreeLineModel(child, builder); } } } }
/// <summary> /// /// </summary> /// <param name="plane"></param> /// <param name="radius"></param> /// <param name="segments"></param> /// <returns></returns> public static LineGeometry3D GenerateCircle(Vector3 plane, float radius, int segments) { if (segments < 3) { throw new ArgumentNullException("too few segments, at least 3"); } var circle = new LineBuilder(); float sectionAngle = (float)(2.0 * Math.PI / segments); if (plane == Vector3.UnitX) { Point3D start = new Point3D(0.0f, 0.0f, radius); Point3D current = new Point3D(0.0f, 0.0f, radius); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.Z = radius * (float)Math.Cos(i * sectionAngle); next.Y = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } else if (plane == Vector3.UnitY) { Point3D start = new Point3D(radius, 0.0f, 0.0f); Point3D current = new Point3D(radius, 0.0f, 0.0f); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.X = radius * (float)Math.Cos(i * sectionAngle); next.Z = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } else { Point3D start = new Point3D(0.0f, radius, 0.0f); Point3D current = new Point3D(0.0f, radius, 0.0f); Point3D next = new Point3D(0.0f, 0.0f, 0.0f); for (int i = 1; i < segments; i++) { next.Y = radius * (float)Math.Cos(i * sectionAngle); next.X = radius * (float)Math.Sin(i * sectionAngle); circle.AddLine(current, next); current = next; } circle.AddLine(current, start); } return circle.ToLineGeometry3D(); }
/// <summary> /// Returns a line geometry of the axis-aligned bounding-box of the given mesh. /// </summary> /// <param name="mesh">Input mesh for the computation of the b-box</param> /// <returns></returns> public static LineGeometry3D GenerateBoundingBox(global::SharpDX.BoundingBox bb) { var cc = bb.GetCorners(); var ll = new LineBuilder(); ll.AddLine(cc[0], cc[1]); ll.AddLine(cc[1], cc[2]); ll.AddLine(cc[2], cc[3]); ll.AddLine(cc[3], cc[0]); ll.AddLine(cc[4], cc[5]); ll.AddLine(cc[5], cc[6]); ll.AddLine(cc[6], cc[7]); ll.AddLine(cc[7], cc[4]); ll.AddLine(cc[0], cc[4]); ll.AddLine(cc[1], cc[5]); ll.AddLine(cc[2], cc[6]); ll.AddLine(cc[3], cc[7]); return ll.ToLineGeometry3D(); }
/// <summary> /// Generates a square grid with a step of 1.0 /// </summary> /// <returns></returns> public static LineGeometry3D GenerateGrid(Vector3 plane, int min = 0, int max = 10) { var grid = new LineBuilder(); //int width = max - min; if (plane == Vector3.UnitX) { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(0, i, min), new Vector3(0, i, max)); grid.AddLine(new Vector3(0, min, i), new Vector3(0, max, i)); } } else if (plane == Vector3.UnitY) { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(i, 0, min), new Vector3(i, 0, max)); grid.AddLine(new Vector3(min, 0, i), new Vector3(max, 0, i)); } } else { for (int i = min; i <= max; i++) { grid.AddLine(new Vector3(i, min, 0), new Vector3(i, max, 0)); grid.AddLine(new Vector3(min, i, 0), new Vector3(max, i, 0)); } } return grid.ToLineGeometry3D(); }