Advertisement
Robomatics

Laser Scanner Mathing

Jul 14th, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.34 KB | None | 0 0
  1. 'Add Corrections, like level-ness correction
  2.  
  3. Imports OpenTK
  4. Imports OpenTK.Graphics
  5. Imports OpenTK.Graphics.OpenGL
  6.  
  7. Public Class Form1
  8.  
  9.     Dim Enc1SetO As New List(Of Integer) 'Horizontal
  10.     Dim Enc2SetO As New List(Of Integer) 'Vertical
  11.     Dim DistSetO As New List(Of Single) 'Laser
  12.     Dim Enc1Set As New List(Of Integer)
  13.     Dim Enc2Set As New List(Of Integer)
  14.     Dim DistSet As New List(Of Single)
  15.  
  16.     Dim PointsX As New List(Of Single)
  17.     Dim PointsY As New List(Of Single) 'Up
  18.     Dim PointsZ As New List(Of Single)
  19.     Dim colordata As New List(Of Color)
  20.  
  21.     'OpenTK Declares
  22.     Dim rotateX As Integer = 0
  23.     Dim rotatey As Integer = 1
  24.     Dim rotatez As Integer = 0
  25.     Dim positionx As Integer = 0 '250
  26.     Dim positiony As Integer = 0 '-250
  27.     Dim Scalez As Decimal = 1
  28.     Dim zpos As Integer = 0 '-100
  29.     Dim mousecontrol As Boolean = False
  30.     Dim mousestartrotate As Point
  31.     Dim mousestartposition As Point
  32.     Dim mousestartZpos As Integer
  33.     Dim mousecurrentpos As Point
  34.     Dim mousecontroltype As Integer = 0
  35.     Dim rotateshift As Boolean = False
  36.     Dim translateshift As Boolean = False
  37.  
  38.     'Open File
  39.     Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
  40.         OpenFileDialog1.ShowDialog()
  41.     End Sub
  42.     Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  43.  
  44.         Enc1Set.Clear()
  45.         Enc2Set.Clear()
  46.         DistSet.Clear()
  47.         Enc1SetO.Clear()
  48.         Enc2SetO.Clear()
  49.         DistSetO.Clear()
  50.  
  51.         GC.Collect()
  52.         Dim filelines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
  53.         GC.Collect()
  54.  
  55.         For i = 0 To filelines.Count - 1
  56.             Dim lines() As String = filelines(i).Split(" ")
  57.             Enc1SetO.Add(lines(0))
  58.             Enc2SetO.Add(lines(1))
  59.             DistSetO.Add(lines(2))
  60.             Enc1Set.Add(lines(0))
  61.             Enc2Set.Add(lines(1))
  62.             DistSet.Add(lines(2))
  63.             colordata.Add(Color.White)
  64.         Next
  65.  
  66.  
  67.         PointsImported0ToolStripMenuItem.Text = Enc1Set.Count
  68.  
  69.         EncoderLevelSet()
  70.         SizeColorSet()
  71.  
  72.     End Sub
  73.  
  74.     'Math
  75.     Private Sub PointsAngtoCart()
  76.  
  77.         PointsX.Clear()
  78.         PointsY.Clear()
  79.         PointsZ.Clear()
  80.  
  81.         For i = 0 To Enc1Set.Count - 1
  82.  
  83.             'Y is up
  84.             Dim tempang As Single = ((((Enc2Set(i) / 4096) * -360) - 0) * (Math.PI / 180)) 'Vertical, X and Y
  85.             Dim tempx As Single = (Math.Cos(tempang) * DistSet(i))
  86.             Dim tempy As Single = (Math.Sin(tempang) * DistSet(i))
  87.             tempang = (((((Enc1Set(i) / 4096) * 360) - 0) * (Math.PI / 180))) 'Horizontal, X and Z
  88.             Dim tempz As Single = -1 * (tempx * Math.Sin(tempang))
  89.             tempx = tempx * Math.Cos(tempang)
  90.  
  91.             PointsX.Add(tempx)
  92.             PointsY.Add(tempy)
  93.             PointsZ.Add(tempz)
  94.             GlControl1.Invalidate()
  95.  
  96.         Next
  97.  
  98.     End Sub
  99.  
  100.     'Save File
  101.     Private Sub SaveAsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
  102.         SaveFileDialog1.ShowDialog()
  103.     End Sub
  104.     Private Sub SaveFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
  105.  
  106.         Dim pointstring(PointsX.Count - 1) As String
  107.         For i = 0 To pointstring.Count - 1
  108.  
  109.             pointstring(i) = "v " & PointsX(i).ToString & " " & PointsY(i).ToString & " " & PointsZ(i).ToString
  110.  
  111.         Next
  112.  
  113.         GC.Collect()
  114.         IO.File.WriteAllLines(SaveFileDialog1.FileName, pointstring)
  115.         GC.Collect()
  116.  
  117.     End Sub
  118.  
  119.     'GL control
  120.     Private Sub GlControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GlControl1.Load
  121.         GL.ClearColor(Color.Black)
  122.         GlControl1.Invalidate()
  123.         Timer1.Enabled = True
  124.     End Sub
  125.     Private Sub GlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseDown
  126.         mousecontrol = True
  127.         If e.Button = MouseButtons.Right Then
  128.             mousecontroltype = 2
  129.             mousestartposition = New Point(MousePosition.X - positionx, MousePosition.Y - zpos)
  130.         ElseIf e.Button = MouseButtons.Left Then
  131.             mousestartrotate = New Point(MousePosition.X - rotatey, MousePosition.Y - rotateX)
  132.             mousecontroltype = 1
  133.         ElseIf e.Button = MouseButtons.Middle Then
  134.             mousestartZpos = MousePosition.Y - positiony
  135.             mousecontroltype = 3
  136.         End If
  137.  
  138.     End Sub
  139.     Private Sub GlControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
  140.         If mousecontrol = True Then
  141.  
  142.             If mousecontroltype = 1 Then
  143.                 rotatey = MousePosition.X - mousestartrotate.X
  144.                 rotateX = MousePosition.Y - mousestartrotate.Y
  145.             ElseIf mousecontroltype = 2 Then
  146.                 positionx = MousePosition.X - mousestartposition.X
  147.                 zpos = MousePosition.Y - mousestartposition.Y
  148.             ElseIf mousecontroltype = 3 Then
  149.                 positiony = MousePosition.Y - mousestartZpos
  150.             End If
  151.  
  152.         End If
  153.  
  154.         mousecurrentpos = e.Location
  155.        
  156.     End Sub
  157.     Private Sub GlControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseUp
  158.         mousecontrol = False
  159.         mousecontroltype = 0
  160.         GlControl1.Invalidate()
  161.     End Sub
  162.     Private Sub GlControl1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseWheel
  163.         Scalez = Scalez + e.Delta / 240
  164.         If Scalez <= 0 Then
  165.             Scalez = 0.1
  166.         End If
  167.         GlControl1.Invalidate()
  168.     End Sub
  169.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  170.         GlControl1.Invalidate()
  171.     End Sub
  172.     Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  173.  
  174.         GL.Clear(ClearBufferMask.ColorBufferBit)
  175.         GL.Clear(ClearBufferMask.DepthBufferBit)
  176.  
  177.         Dim perspective As OpenTK.Matrix4 = OpenTK.Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000)
  178.         Dim lookat As OpenTK.Matrix4 = OpenTK.Matrix4.LookAt(0, 20, 20, 0, 0, 0, 0, 1, 0)
  179.  
  180.         GL.MatrixMode(MatrixMode.Projection)
  181.         GL.LoadIdentity()
  182.         GL.LoadMatrix(perspective)
  183.  
  184.         GL.MatrixMode(MatrixMode.Modelview)
  185.         GL.LoadIdentity()
  186.         GL.LoadMatrix(lookat)
  187.  
  188.         GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
  189.         GL.Rotate(-rotateX, 1, 0, 0)
  190.         GL.Rotate(rotatey, 0, 1, 0)
  191.  
  192.         GL.Translate((-positionx * Scalez), (-positiony * Scalez), (zpos * Scalez))
  193.         GL.Scale(Scalez, Scalez, Scalez)
  194.  
  195.         GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
  196.         GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
  197.         GL.DepthMask(True)
  198.  
  199.         Dim vertz((PointsX.Count) * 3) As Single
  200.         For i = 0 To PointsX.Count - 1
  201.             vertz((i * 3) + 0) = PointsX(i)
  202.             vertz((i * 3) + 1) = PointsY(i)
  203.             vertz((i * 3) + 2) = PointsZ(i)
  204.         Next
  205.  
  206.         'Point Selection
  207.         Dim colorzP((colordata.Count) * 4) As Single
  208.         For i = 0 To colordata.Count - 1
  209.             colorzP((i * 3) + 0) = colordata(i).R / 255
  210.             colorzP((i * 3) + 1) = colordata(i).G / 255
  211.             colorzP((i * 3) + 2) = colordata(i).B / 255
  212.             colorzP((i * 3) + 3) = 0.5
  213.         Next
  214.         GL.PointSize(6)
  215.         GL.EnableClientState(ArrayCap.VertexArray)
  216.         GL.EnableClientState(ArrayCap.ColorArray)
  217.         GL.ColorPointer(4, VertexPointerType.Float, 0, colorzP)
  218.         GL.VertexPointer(3, VertexPointerType.Float, 0, vertz)
  219.         GL.DrawArrays(BeginMode.Points, 0, PointsX.Count)
  220.         GL.DisableClientState(ArrayCap.VertexArray)
  221.         GL.DisableClientState(ArrayCap.ColorArray)
  222.  
  223.         Dim pixelz(3) As Byte
  224.         GL.ReadPixels(mousecurrentpos.X, GlControl1.Height - mousecurrentpos.Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, pixelz)
  225.         Label1.Text = pixelz(0) & " " & pixelz(1) & " " & pixelz(2) & " " & pixelz(3)
  226.         GL.Clear(ClearBufferMask.ColorBufferBit)
  227.         GL.Clear(ClearBufferMask.DepthBufferBit)
  228.  
  229.         Dim colorz((colordata.Count) * 3) As Single
  230.         For i = 0 To colordata.Count - 1
  231.             colorz((i * 3) + 0) = colordata(i).R / 255
  232.             colorz((i * 3) + 1) = colordata(i).G / 255
  233.             colorz((i * 3) + 2) = colordata(i).B / 255
  234.         Next
  235.  
  236.         'Box
  237.         GL.Begin(BeginMode.LineLoop)
  238.         GL.Color3(Color.Green)
  239.         GL.Vertex3(50, 0, 0)
  240.         GL.Color3(Color.White)
  241.         GL.Vertex3(50, 0, -50)
  242.         GL.Color3(Color.Red)
  243.         GL.Vertex3(0, 0, -50)
  244.         GL.Color3(Color.White)
  245.         GL.Vertex3(-50, 0, -50)
  246.         GL.Color3(Color.Blue)
  247.         GL.Vertex3(-50, 0, 0)
  248.         GL.Color3(Color.White)
  249.         GL.Vertex3(-50, 0, 50)
  250.         GL.Color3(Color.Yellow)
  251.         GL.Vertex3(0, 0, 50)
  252.         GL.Color3(Color.White)
  253.         GL.Vertex3(50, 0, 50)
  254.         GL.Color3(Color.Green)
  255.         GL.Vertex3(50, 0, 0)
  256.         GL.Color3(Color.White)
  257.         GL.End()
  258.  
  259.         GL.PointSize(My.Settings.PixelSize)
  260.         GL.EnableClientState(ArrayCap.VertexArray)
  261.         GL.EnableClientState(ArrayCap.ColorArray)
  262.         GL.ColorPointer(3, VertexPointerType.Float, 0, colorz)
  263.         GL.VertexPointer(3, VertexPointerType.Float, 0, vertz)
  264.         GL.DrawArrays(BeginMode.Points, 0, PointsX.Count)
  265.         GL.DisableClientState(ArrayCap.VertexArray)
  266.         GL.DisableClientState(ArrayCap.ColorArray)
  267.  
  268.         GraphicsContext.CurrentContext.VSync = True
  269.         GlControl1.SwapBuffers()
  270.  
  271.  
  272.     End Sub
  273.  
  274.     'Encoder Level
  275.     Private Sub EncoderLevelsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EncoderLevelsToolStripMenuItem.Click
  276.         EncoderLevels.Show()
  277.     End Sub
  278.     Public Sub EncoderLevelSet()
  279.         For i = 0 To Enc2SetO.Count - 1
  280.             Enc2Set(i) = (Enc2SetO(i) - My.Settings.EncoderLevel) Mod 4096
  281.         Next
  282.         PointsAngtoCart()
  283.     End Sub
  284.  
  285.     'Size and color
  286.     Private Sub ColorToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ColorToolStripMenuItem.Click
  287.         ColorSize.Show()
  288.     End Sub
  289.     Public Sub SizeColorSet()
  290.         Select Case My.Settings.Coloring
  291.             Case 0
  292.                 For i = 0 To colordata.Count - 1
  293.                     colordata(i) = Color.White
  294.                 Next
  295.             Case 1
  296.                 Dim max As Single = DistSet.Max()
  297.                 For i = 0 To colordata.Count - 1
  298.                     If DistSet(i) >= (max / 2) Then
  299.                         colordata(i) = Color.FromArgb((((DistSet(i) - (max / 2)) / (max / 2)) * 255), 255 - (((DistSet(i) - (max / 2)) / (max / 2)) * 255), 0)
  300.                     ElseIf DistSet(i) < (max / 2) Then
  301.                         colordata(i) = Color.FromArgb(0, ((DistSet(i) / (max / 2)) * 255), 255 - ((DistSet(i) / (max / 2)) * 255))
  302.                     End If
  303.                 Next
  304.         End Select
  305.  
  306.         GlControl1.Invalidate()
  307.     End Sub
  308. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement