Advertisement
Guest User

opentk las viewer

a guest
Jul 24th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.95 KB | None | 0 0
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Graphics.OpenGL;
  4. using OpenTK.Input;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using Filler;
  11. using System.Linq;
  12.  
  13. namespace vertexcloud
  14. {
  15.     public class vertexcloud : GameWindow
  16.     {
  17.         private Matrix4 CameraMatrix = Matrix4.Identity;
  18.         private Matrix4 ViewMatrix = Matrix4.Identity;
  19.         private Vector3 Position = new Vector3(0, 0, 0);
  20.         private float Pitch = -0.8f;
  21.  
  22.         private List<PointCloud> pointclouds = new List<PointCloud>();
  23.  
  24.         private const float CameraSpeed = 615f;
  25.         private const float MoveSpeed = 550f*4;
  26.         private const float HalfPI = 1.570796f;
  27.         private Vector2 MouseSpeed;
  28.         private Vector2 MouseDelta;
  29.         private Vector2 MousePosition;
  30.         private static Vector2 CursorPosition;
  31.         private float Yaw;
  32.         private Point WindowCenter;
  33.         private bool RenderCrossHair = false;
  34.  
  35.         public vertexcloud() : base(1024, 768, GraphicsMode.Default, "Camera")
  36.         {
  37.             GL.Enable(EnableCap.DepthTest);
  38.         }
  39.  
  40.         public Vector3 convertScreenToWorldCoords(int x, int y)
  41.         {
  42.             int[] viewport = new int[4];
  43.             Matrix4 modelViewMatrix, projectionMatrix;
  44.             GL.GetFloat(GetPName.ModelviewMatrix, out modelViewMatrix);
  45.             GL.GetFloat(GetPName.ProjectionMatrix, out projectionMatrix);
  46.             GL.GetInteger(GetPName.Viewport, viewport);
  47.             Vector2 mouse;
  48.             mouse.X = x;
  49.             mouse.Y = y + (viewport[2] - viewport[3]);
  50.             Vector4 vector = UnProject(ref projectionMatrix, CameraMatrix, new Size(viewport[2], viewport[3]), mouse);
  51.  
  52.             return vector.Xyz;
  53.         }
  54.  
  55.         public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
  56.         {
  57.             Vector4 vec;
  58.  
  59.  
  60.             vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
  61.             vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
  62.             vec.Z = 0;
  63.             vec.W = 1.0f;
  64.  
  65.  
  66.             Matrix4 viewInv = Matrix4.Invert(view);
  67.             Matrix4 projInv = Matrix4.Invert(projection);
  68.  
  69.  
  70.             Vector4.Transform(ref vec, ref projInv, out vec);
  71.             Vector4.Transform(ref vec, ref viewInv, out vec);
  72.  
  73.  
  74.             if (vec.W > float.Epsilon || vec.W < float.Epsilon)
  75.             {
  76.                 vec.X /= vec.W;
  77.                 vec.Y /= vec.W;
  78.                 vec.Z /= vec.W;
  79.             }
  80.  
  81.             return vec;
  82.         }
  83.  
  84.         protected override void OnLoad(EventArgs e)
  85.         {
  86.             base.OnLoad(e);
  87.             GL.ClearColor(0.1f, 0.1f, 0.1f, 1f);
  88.             this.WindowCenter = new Point(this.Bounds.Left + this.Bounds.Width / 2, this.Bounds.Top + this.Bounds.Height / 2);
  89.             Cursor.Position = this.WindowCenter;
  90.             Cursor.Hide();
  91.             string[] files = Directory.GetFiles(@"C:\WindowsLP\SAMPLE_PROJECT\brisport2", "*.las");
  92.             int num = 0;
  93.             foreach (string file in files)
  94.             {
  95.                 PointCloud pointCloud = new PointCloud(LasImporter.ReadDataFromFile(file));
  96.                 pointCloud.ApplyColorMap(LasReader.Colors);
  97.                 this.pointclouds.Add(pointCloud);
  98.                 ++num;
  99.                 if (num >= 1)
  100.                     break;
  101.             }
  102.  
  103.  
  104.             float[] pts = new float[180 * 180*3];
  105.             float m = 200f;
  106.             float n = 200f;
  107.             for (int i = 0; i < 200; i++)
  108.             {
  109.                 for (int j = 0; j < 200; j++)
  110.                 {
  111.                     // xyz  (sin(Pi * m/M) cos(2Pi * n/N), sin(Pi * m/M) sin(2Pi * n/N), cos(Pi * m/M))
  112.                     // YZX
  113.                     pts[(j * 100 + i) * 3] = (float)( Math.Sin(MathHelper.Pi  * m/i) * Math.Sin(2 * Math.PI * n/j))*200f;
  114.                     pts[(j * 100 + i) * 3 + 1] = (float)Math.Cos(Math.PI * m / i) * 200f;
  115.                     pts[(j * 100 + i) * 3 + 2] = (float)(Math.Sin(Math.PI * m / i) * Math.Cos(2 * Math.PI * n / j)) * 200f;
  116.                 }
  117.             }
  118.  
  119.  
  120.             pointclouds.Add(new PointCloud(pts) { HasColor = false });
  121.  
  122.  
  123.             this.Mouse.Move += new EventHandler<MouseMoveEventArgs>(this.OnMouseMove);
  124.             this.Position = new Vector3(LasReader.cameraX, LasReader.cameraY, LasReader.cameraZ);
  125.         }
  126.  
  127.         private void OnMouseMove(object sender, MouseMoveEventArgs e)
  128.         {
  129.             if (this.Mouse[MouseButton.Right])
  130.                 this.MousePosition = new Vector2((float)e.X, (float)e.Y);
  131.  
  132.             CursorPosition = new Vector2((float)e.X, (float)e.Y);
  133.         }
  134.  
  135.  
  136.         public static Vector4 UnProject(Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
  137.         {
  138.             Vector4 vec;
  139.  
  140.             vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
  141.             vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
  142.             vec.Z = 0;
  143.             vec.W = 1.0f;
  144.  
  145.             Matrix4 viewInv = Matrix4.Invert(view);
  146.             Matrix4 projInv = Matrix4.Invert(projection);
  147.  
  148.             Vector4.Transform(ref vec, ref projInv, out vec);
  149.             Vector4.Transform(ref vec, ref viewInv, out vec);
  150.  
  151.             if (vec.W > float.Epsilon || vec.W < float.Epsilon)
  152.             {
  153.                 vec.X /= vec.W;
  154.                 vec.Y /= vec.W;
  155.                 vec.Z /= vec.W;
  156.             }
  157.  
  158.  
  159.             return vec;
  160.         }
  161.  
  162.         protected override void OnResize(EventArgs e)
  163.         {
  164.             base.OnResize(e);
  165.             this.WindowCenter = new Point(this.Bounds.Left + this.Bounds.Width / 2, this.Bounds.Top + this.Bounds.Height / 2);
  166.             GL.Viewport(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height);
  167.             ViewMatrix = Matrix4.CreatePerspectiveFieldOfView(0.7853982f, (float)this.Width / (float)this.Height, 1f, 100000f);
  168.             GL.MatrixMode(MatrixMode.Projection);
  169.             GL.LoadMatrix(ref ViewMatrix);
  170.         }
  171.  
  172.         private List<Vector3> points = new List<Vector3>();
  173.  
  174.         protected override void OnUpdateFrame(FrameEventArgs e)
  175.         {
  176.             base.OnUpdateFrame(e);
  177.  
  178.             if (this.Keyboard[Key.Up])
  179.             {
  180.                 this.Position.X += (float)(Math.Cos((double)this.Yaw) * MoveSpeed) * (float)e.Time;
  181.                 this.Position.Y += (float)(Math.Tan((double)this.Pitch) * MoveSpeed) * (float)e.Time;
  182.                 this.Position.Z += (float)(Math.Sin((double)this.Yaw) * MoveSpeed) * (float)e.Time;
  183.             }
  184.             if (this.Keyboard[Key.Down])
  185.             {
  186.                 this.Position.X -= (float)(Math.Cos((double)this.Yaw) * MoveSpeed) * (float)e.Time;
  187.                 this.Position.Y -= (float)(Math.Tan((double)this.Pitch) * MoveSpeed) * (float)e.Time;
  188.                 this.Position.Z -= (float)(Math.Sin((double)this.Yaw) * MoveSpeed) * (float)e.Time;
  189.             }
  190.             if (this.Keyboard[Key.Left])
  191.             {
  192.                 this.Position.X -= (float)(Math.Cos((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
  193.                 this.Position.Z -= (float)(Math.Sin((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
  194.             }
  195.             if (this.Keyboard[Key.Right])
  196.             {
  197.                 this.Position.X += (float)(Math.Cos((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
  198.                 this.Position.Z += (float)(Math.Sin((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
  199.             }
  200.  
  201.             if (this.Keyboard[Key.P])
  202.             {
  203.                 pointclouds.ForEach(i => i.Visible = false);
  204.             }
  205.             else
  206.             {
  207.                 pointclouds.ForEach(i => i.Visible = true);
  208.             }
  209.  
  210.             if (this.Keyboard[Key.D])
  211.             {
  212.                 pointclouds.ForEach(i => i.Delete());
  213.                 pointclouds.Clear();
  214.             }
  215.  
  216.  
  217.             if (Mouse[MouseButton.Left])
  218.             {
  219.                 points.Clear();
  220.                 //var x = UnProject(ViewMatrix, CameraMatrix, this.Size, new Vector2(Mouse.X, Mouse.Y));
  221.  
  222.                 var x = convertScreenToWorldCoords(Mouse.X, Mouse.Y);
  223.  
  224.                 points.Add(new Vector3(x));
  225.  
  226.                 Title = x.ToString();
  227.             }
  228.  
  229.             if (this.Mouse[MouseButton.Right])
  230.             {
  231.                 RenderCrossHair = false;
  232.  
  233.                 this.MouseDelta = new Vector2(this.MousePosition.X - (float)this.PointToClient(this.WindowCenter).X, this.MousePosition.Y - (float)this.PointToClient(this.WindowCenter).Y);
  234.                 Cursor.Position = this.WindowCenter;
  235.                 this.MouseSpeed.X *= 0.7f;
  236.                 this.MouseSpeed.Y *= 0.7f;
  237.                 this.MouseSpeed.X += this.MouseDelta.X / 40f * (float)e.Time;
  238.                 this.MouseSpeed.Y += this.MouseDelta.Y / 40f * (float)e.Time;
  239.                 this.Yaw += this.MouseSpeed.X;
  240.                 this.Pitch -= this.MouseSpeed.Y;
  241.  
  242.                 if ((double)this.Pitch <= -1.56079638004303)
  243.                     this.Pitch = -1.560796f;
  244.                 if ((double)this.Pitch >= 1.56079638004303)
  245.                     this.Pitch = 1.560796f;
  246.  
  247.                 this.CameraMatrix = Matrix4.LookAt(this.Position, this.Position + new Vector3((float)Math.Cos((double)this.Yaw), (float)Math.Tan((double)this.Pitch), (float)Math.Sin((double)this.Yaw)), Vector3.UnitY);
  248.             }
  249.             else
  250.             {
  251.                 RenderCrossHair = true;
  252.             }
  253.  
  254.  
  255.             if (this.Keyboard[Key.Escape])
  256.                 this.Exit();
  257.         }
  258.  
  259.         protected override void OnRenderFrame(FrameEventArgs e)
  260.         {
  261.             base.OnRenderFrame(e);
  262.             GL.MatrixMode(MatrixMode.Modelview);
  263.             GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
  264.             GL.LoadMatrix(ref this.CameraMatrix);
  265.  
  266.  
  267.             GL.PushMatrix();
  268.             this.pointclouds.ForEach((Action<PointCloud>)(i =>
  269.             {
  270.                 //blend
  271.                 GL.Enable(EnableCap.Blend);
  272.  
  273.                 if (i.Visible)
  274.                 {
  275.                     GL.PushMatrix();
  276.                     GL.PointSize(2.0f);
  277.                     if (!i.HasColor)
  278.                     {
  279.                         GL.Color4(Color.White);
  280.                     }
  281.                     i.Render(e);
  282.                     GL.PopMatrix();
  283.                 }
  284.  
  285.  
  286.              
  287.                
  288.  
  289.  
  290.  
  291.             }));
  292.  
  293.             GL.PopMatrix();
  294.  
  295.  
  296.             //line
  297.             //GL.PushMatrix();
  298.             //GL.LineWidth(100.0f);
  299.             //GL.Color4(Color.White);
  300.             //GL.Begin(BeginMode.Lines);
  301.             //GL.Vertex3(0, 0, 0);
  302.             //GL.Vertex3(5000, 5000, 5000);
  303.             //GL.End();
  304.             //GL.PopMatrix();
  305.  
  306.  
  307.             GL.PushMatrix();
  308.             GL.Enable(EnableCap.Blend);
  309.             GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.SrcAlpha);
  310.             GL.MatrixMode(MatrixMode.Projection);
  311.             GL.PushMatrix();
  312.             GL.LoadIdentity();
  313.             GL.Ortho(0, Width, Height, 0, 0.0f, 100.0f);
  314.  
  315.             if (RenderCrossHair)
  316.                 RenderCursor();
  317.  
  318.             GL.PopMatrix();
  319.  
  320.  
  321.             this.SwapBuffers();
  322.  
  323.  
  324.         }
  325.  
  326.         public static void RenderCursor()
  327.         {
  328.             GL.PointSize(3.0f);
  329.             GL.Color4(Color.White);
  330.             GL.Begin(BeginMode.Points);
  331.             GL.Vertex2(CursorPosition.X, CursorPosition.Y);
  332.             GL.Vertex2(CursorPosition.X + 10, CursorPosition.Y);
  333.             GL.Vertex2(CursorPosition.X + 10, CursorPosition.Y + 10);
  334.             GL.Vertex2(CursorPosition.X, CursorPosition.Y + 10);
  335.             GL.End();
  336.         }  
  337.  
  338.         public static void Main()
  339.         {
  340.             using (vertexcloud vertexcloud = new vertexcloud())
  341.                 vertexcloud.Run(60.0, 0.0);
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement