Advertisement
Robomatics

Best Fit 3D

Apr 26th, 2013
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 23.56 KB | None | 0 0
  1. Imports OpenTK
  2. Imports OpenTK.Graphics
  3. Imports OpenTK.Graphics.OpenGL
  4. Imports System.Threading
  5. Imports Emgu.CV
  6. Imports Emgu.CV.Util
  7. Imports Emgu.CV.Structure
  8.  
  9. Public Class Form1
  10.  
  11.     Dim filelocation() As String
  12.     Dim framecount As Integer = 0
  13.     Dim MaxNum As Integer = 100
  14.     Dim objectcount As Integer
  15.     Dim points(MaxNum) As List(Of Vector3)
  16.     Dim border(MaxNum) As minmax
  17.     'Dim normals(MaxNum) As List(Of Vector3)
  18.     Dim colors(MaxNum) As Drawing.Color
  19.     Dim Gridpic As Bitmap = New Bitmap(640, 480)
  20.     Dim Gridgfx As Drawing.Graphics = Drawing.Graphics.FromImage(Gridpic)
  21.     Dim pointsset As Boolean = False
  22.     'GL
  23.     Dim rotateX As Integer = 0
  24.     Dim rotatey As Integer = 1
  25.     Dim rotatez As Integer = 0
  26.     Dim positionx As Integer = 0 '250
  27.     Dim positiony As Integer = 0 '-250
  28.     Dim Scalez As Decimal = 1
  29.     Dim zpos As Integer = 0 '-100
  30.     Dim mousecontrol As Boolean = False
  31.     Dim mousestartrotate As Point
  32.     Dim mousestartposition As Point
  33.     Dim mousestartZpos As Integer
  34.     Dim mousecontroltype As Integer = 0
  35.     Dim tbar1 As Integer = 0
  36.     Dim tbar2 As Integer = 1
  37.     'Grid
  38.     Dim zdiff As Single
  39.     Dim Zupper As Single
  40.     Dim ZLower As Single
  41.     Dim gridheight As Integer
  42.     Dim gridwidth As Integer
  43.     Dim DisplayPoints As Boolean = True
  44.     Dim DisplayLines As Boolean = False
  45.  
  46.     'File Import
  47.     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
  48.  
  49.         If pointsset = True Then 'Remove this stuff for multiple files
  50.             Dim result As MsgBoxResult = MsgBox("Close Current Project?", MsgBoxStyle.OkCancel)
  51.             If result = MsgBoxResult.Cancel Then
  52.                 Return
  53.             End If
  54.         End If
  55.  
  56.         OpenFileDialog1.ShowDialog()
  57.     End Sub
  58.     Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  59.  
  60.         If pointsset = True Then 'Remove this stuff for multiple files
  61.             points(0).Clear()
  62.             objectcount = 0
  63.         End If
  64.  
  65.         filelocation = OpenFileDialog1.FileNames
  66.  
  67.         For i2 = 0 To filelocation.Length - 1
  68.  
  69.             If System.IO.File.Exists(filelocation(i2)) = True Then
  70.                 Dim subz As String = filelocation(i2).Substring(filelocation(i2).Length - 3, 3)
  71.                 subz = subz.ToLower
  72.                 If subz = "stl" Then
  73.                     fileimportSTL(i2)
  74.                 End If
  75.                 If subz = "obj" Then
  76.                     fileimportOBJ(i2)
  77.                 End If
  78.             End If
  79.  
  80.             Dim maxX, minX, maxY, minY, maxZ, minZ As Single
  81.             maxX = -100000000
  82.             maxY = -100000000
  83.             maxZ = -100000000
  84.             minX = 100000000
  85.             minY = 100000000
  86.             minZ = 100000000
  87.  
  88.             For i = 0 To points(objectcount - 1).Count - 1
  89.                 If points(objectcount - 1)(i).X < minX Then
  90.                     minX = points(objectcount - 1)(i).X
  91.                 End If
  92.                 If points(objectcount - 1)(i).X > maxX Then
  93.                     maxX = points(objectcount - 1)(i).X
  94.                 End If
  95.                 If points(objectcount - 1)(i).Y < minY Then
  96.                     minY = points(objectcount - 1)(i).Y
  97.                 End If
  98.                 If points(objectcount - 1)(i).Y > maxY Then
  99.                     maxY = points(objectcount - 1)(i).Y
  100.                 End If
  101.                 If points(objectcount - 1)(i).Z < minZ Then
  102.                     minZ = points(objectcount - 1)(i).Z
  103.                 End If
  104.                 If points(objectcount - 1)(i).Z > maxZ Then
  105.                     maxZ = points(objectcount - 1)(i).Z
  106.                 End If
  107.             Next
  108.  
  109.             Dim tempminmax As minmax
  110.             tempminmax.Xmax = maxX
  111.             tempminmax.Xmin = minX
  112.             tempminmax.Ymax = maxY
  113.             tempminmax.Ymin = minY
  114.             tempminmax.Zmax = maxZ
  115.             tempminmax.Zmin = minZ
  116.             border(objectcount - 1) = tempminmax
  117.  
  118.         Next
  119.  
  120.         GlControl1.Invalidate()
  121.  
  122.     End Sub
  123.     Public Sub fileimportSTL(ByRef objectnum As Integer)
  124.         Dim pointsstring() As Byte
  125.  
  126.         GC.Collect()
  127.         pointsstring = System.IO.File.ReadAllBytes(filelocation(objectnum))
  128.         GC.Collect()
  129.  
  130.         'normals(objectcount) = New List(Of Vector3)
  131.         points(objectcount) = New List(Of Vector3)
  132.         colors(objectcount) = Color.Cyan
  133.         Dim pointcount As UInt32 = BitConverter.ToUInt32(pointsstring, 80)
  134.         Dim NX, NY, NZ, X1, X2, X3, Y1, Y2, Y3, Z1, Z2, Z3 As Single
  135.  
  136.         For i = 84 To pointsstring.Length - 51 Step 50
  137.  
  138.             'NX = BitConverter.ToSingle(pointsstring, i + 0)
  139.             'NY = BitConverter.ToSingle(pointsstring, i + 4)
  140.             'NZ = BitConverter.ToSingle(pointsstring, i + 8)
  141.             'normals(objectcount).Add(New Vector3(NX, NY, NZ))
  142.  
  143.             X1 = BitConverter.ToSingle(pointsstring, i + 12)
  144.             Y1 = BitConverter.ToSingle(pointsstring, i + 16)
  145.             Z1 = BitConverter.ToSingle(pointsstring, i + 20)
  146.  
  147.             X2 = BitConverter.ToSingle(pointsstring, i + 24)
  148.             Y2 = BitConverter.ToSingle(pointsstring, i + 28)
  149.             Z2 = BitConverter.ToSingle(pointsstring, i + 32)
  150.  
  151.             X3 = BitConverter.ToSingle(pointsstring, i + 36)
  152.             Y3 = BitConverter.ToSingle(pointsstring, i + 40)
  153.             Z3 = BitConverter.ToSingle(pointsstring, i + 44)
  154.  
  155.  
  156.             points(objectcount).Add(New Vector3(X1, Y1, Z1))
  157.             points(objectcount).Add(New Vector3(X2, Y2, Z2))
  158.             points(objectcount).Add(New Vector3(X3, Y3, Z3))
  159.         Next
  160.  
  161.         objectcount = objectcount + 1
  162.  
  163.         pointsset = True
  164.     End Sub
  165.     Public Sub fileimportOBJ(ByRef objectnum As Integer)
  166.         Dim pointsstring() As String
  167.  
  168.         GC.Collect()
  169.         pointsstring = System.IO.File.ReadAllLines(filelocation(objectnum))
  170.         GC.Collect()
  171.  
  172.         points(objectcount) = New List(Of Vector3)
  173.         colors(objectcount) = Color.Cyan
  174.  
  175.         Dim X1, Y1, Z1 As Single
  176.         Dim C As Color
  177.  
  178.         For i = 0 To pointsstring.Length - 1
  179.             If pointsstring(i).Length > 2 Then
  180.                 If pointsstring(i)(0) = "v" And pointsstring(i)(1) = " " Then
  181.  
  182.                     Dim tempstring = pointsstring(i)
  183.  
  184.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  185.                     Dim XST As String = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  186.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  187.                     Dim YST As String = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  188.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  189.  
  190.                     Dim tempint As Integer = pointsstring(i).IndexOf(" ")
  191.                     Dim ZST As String
  192.                     If tempint <> -1 Then
  193.                         ZST = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  194.                     Else
  195.                         ZST = pointsstring(i)
  196.                     End If
  197.  
  198.                     X1 = XST
  199.                     Y1 = YST
  200.                     Z1 = ZST
  201.  
  202.                 End If
  203.             End If
  204.  
  205.             points(objectcount).Add(New Vector3(X1, Y1, Z1))
  206.         Next
  207.  
  208.         objectcount = objectcount + 1
  209.  
  210.         pointsset = True
  211.     End Sub
  212.  
  213.     'Grid Draw
  214.     Public Sub GridDraw()
  215.  
  216.         If points(0) IsNot Nothing Then
  217.  
  218.             Dim drawpoints As List(Of Vector3) = New List(Of Vector3)
  219.             For i = 0 To points(0).Count - 1
  220.                 If points(0)(i).Z < Zupper And points(0)(i).Z > ZLower Then
  221.                     drawpoints.Add(points(0)(i))
  222.                 End If
  223.             Next
  224.  
  225.             Dim drawdots As List(Of Point) = New List(Of Point)
  226.  
  227.             Gridgfx.FillRectangle(Brushes.White, 0, 0, gridwidth, gridheight)
  228.             For i = 0 To drawpoints.Count - 1
  229.                 Dim x As Single = ((drawpoints(i).X - border(0).Xmin) / (border(0).Xmax - border(0).Xmin)) * gridwidth
  230.                 Dim y As Single = ((drawpoints(i).Y - border(0).Ymin) / (border(0).Ymax - border(0).Ymin)) * gridheight
  231.                 y = 480 - y
  232.                 drawdots.Add(New Point(x, y))
  233.             Next
  234.  
  235.             drawdots = drawdots.Distinct().ToList()
  236.             For i = 0 To drawdots.Count - 1
  237.                 Gridgfx.FillRectangle(Brushes.Black, drawdots(i).X - 0, drawdots(i).Y - 0, 2, 2)
  238.             Next
  239.  
  240.  
  241.             If DisplayPoints = True Then
  242.                 PictureBox1.Image = Gridpic
  243.             End If
  244.             If DisplayLines = True Then
  245.                 Dim imagez = New Image(Of Gray, Byte)(Gridpic)
  246.                 Dim imagez2 As Image(Of Gray, Byte) = imagez.Canny(150, 60)
  247.                 Dim imagez3 As Image(Of Gray, Byte) = New Image(Of Gray, Byte)(gridwidth, gridheight)
  248.                 Dim RhoRes As Double = RhoResNMUD.Value
  249.                 Dim Threshold As Double = ThresholdNMUD.Value
  250.                 Dim MinLineWidth As Double = MinLineNMUD.Value
  251.                 Dim linegap As Integer = LineWidthNMUD.Value
  252.                 Dim ThetaRes As Double = Math.PI / 180
  253.  
  254.                 Dim Linez()() As LineSegment2D = imagez2.HoughLinesBinary(RhoRes, ThetaRes, Threshold, MinLineWidth, linegap)
  255.                 If Linez(0).Length >= 0 Then
  256.                     For i = 0 To Linez(0).Length - 1
  257.                         imagez3.Draw(Linez(0)(i), New Gray(255), 2)
  258.                     Next
  259.                 End If
  260.  
  261.                 PictureBox1.Image = imagez3.ToBitmap
  262.             End If
  263.  
  264.         End If
  265.  
  266.     End Sub
  267.  
  268.     'GL Load
  269.     Private Sub GlControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GlControl1.Load
  270.  
  271.         GL.ClearColor(Color.Black)
  272.         GlControl1.Invalidate()
  273.  
  274.     End Sub
  275.  
  276.     'GL mouse control
  277.     Private Sub GlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseDown
  278.         mousecontrol = True
  279.         If e.Button = MouseButtons.Right Then
  280.             mousecontroltype = 2
  281.             mousestartposition = New Point(MousePosition.X - positionx, MousePosition.Y - positiony)
  282.         ElseIf e.Button = MouseButtons.Left Then
  283.             mousestartrotate = New Point(MousePosition.X - rotatey, MousePosition.Y - rotateX)
  284.             mousecontroltype = 1
  285.         ElseIf e.Button = MouseButtons.Middle Then
  286.             mousestartZpos = MousePosition.Y - zpos
  287.             mousecontroltype = 3
  288.         End If
  289.         Timer1.Enabled = True
  290.  
  291.     End Sub
  292.     Private Sub GlControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
  293.         If mousecontrol = True Then
  294.  
  295.             If mousecontroltype = 1 Then
  296.                 rotatey = MousePosition.X - mousestartrotate.X
  297.                 rotateX = MousePosition.Y - mousestartrotate.Y
  298.             ElseIf mousecontroltype = 2 Then
  299.                 positionx = MousePosition.X - mousestartposition.X
  300.                 positiony = MousePosition.Y - mousestartposition.Y
  301.             ElseIf mousecontroltype = 3 Then
  302.                 zpos = MousePosition.Y - mousestartZpos
  303.             End If
  304.  
  305.         End If
  306.     End Sub
  307.     Private Sub GlControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseUp
  308.         mousecontrol = False
  309.         mousecontroltype = 0
  310.         Timer1.Enabled = False
  311.         GlControl1.Invalidate()
  312.     End Sub
  313.     Private Sub GlControl1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseWheel
  314.         Scalez = Scalez + e.Delta / 240
  315.         If Scalez <= 0 Then
  316.             Scalez = 0.1
  317.         End If
  318.         GlControl1.Invalidate()
  319.     End Sub
  320.  
  321.     'Object Drawing
  322.     Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  323.         GL.Clear(ClearBufferMask.ColorBufferBit)
  324.         GL.Clear(ClearBufferMask.DepthBufferBit)
  325.  
  326.         Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000)
  327.         Dim lookat As Matrix4 = Matrix4.LookAt(200, 20, 0, 0, 0, 0, 0, 1, 0)
  328.  
  329.         GL.MatrixMode(MatrixMode.Projection)
  330.         GL.LoadIdentity()
  331.         GL.LoadMatrix(perspective)
  332.  
  333.         GL.MatrixMode(MatrixMode.Modelview)
  334.         GL.LoadIdentity()
  335.         GL.LoadMatrix(lookat)
  336.  
  337.         GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
  338.         GL.Rotate(-rotateX, 0, 0, 1)
  339.         GL.Rotate(rotatey, 0, 1, 0)
  340.  
  341.         GL.Begin(BeginMode.LineLoop)
  342.         GL.Color3(Color.Green)
  343.         GL.Vertex3(50, 0, 0)
  344.         GL.Color3(Color.White)
  345.         GL.Vertex3(50, 0, -50)
  346.         GL.Color3(Color.Red)
  347.         GL.Vertex3(0, 0, -50)
  348.         GL.Color3(Color.White)
  349.         GL.Vertex3(-50, 0, -50)
  350.         GL.Color3(Color.Blue)
  351.         GL.Vertex3(-50, 0, 0)
  352.         GL.Color3(Color.White)
  353.         GL.Vertex3(-50, 0, 50)
  354.         GL.Color3(Color.Yellow)
  355.         GL.Vertex3(0, 0, 50)
  356.         GL.Color3(Color.White)
  357.         GL.Vertex3(50, 0, 50)
  358.         GL.Color3(Color.Green)
  359.         GL.Vertex3(50, 0, 0)
  360.         GL.End()
  361.  
  362.         GL.Translate((zpos * Scalez), (-positiony * Scalez), (-positionx * Scalez))
  363.         GL.Scale(Scalez, Scalez, Scalez)
  364.  
  365.         GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
  366.         GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
  367.         GL.DepthMask(True)
  368.  
  369.         If pointsset = True Then
  370.  
  371.             GL.Color3(Color.LimeGreen)
  372.             GL.Begin(BeginMode.LineLoop)
  373.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymax, zdiff))
  374.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymax, zdiff))
  375.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymin, zdiff))
  376.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymin, zdiff))
  377.             GL.End()
  378.             GL.Color3(Color.Blue)
  379.             GL.Begin(BeginMode.LineLoop)
  380.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymax, Zupper))
  381.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymax, Zupper))
  382.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymin, Zupper))
  383.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymin, Zupper))
  384.             GL.End()
  385.             GL.Begin(BeginMode.LineLoop)
  386.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymax, ZLower))
  387.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymax, ZLower))
  388.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymin, ZLower))
  389.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymin, ZLower))
  390.             GL.End()
  391.             GL.Begin(BeginMode.Lines)
  392.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymax, ZLower))
  393.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymax, Zupper))
  394.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymax, ZLower))
  395.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymax, Zupper))
  396.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymin, ZLower))
  397.             GL.Vertex3(New Vector3(border(0).Xmin, border(0).Ymin, Zupper))
  398.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymin, ZLower))
  399.             GL.Vertex3(New Vector3(border(0).Xmax, border(0).Ymin, Zupper))
  400.             GL.End()
  401.  
  402.             GL.Color3(Color.White)
  403.  
  404.             If DisplayPoints = True Then
  405.                 Dim vertz((points(0).Count) * 3) As Single
  406.                 For i = 0 To points(0).Count - 1
  407.                     vertz((i * 3) + 0) = points(0)(i).X
  408.                     vertz((i * 3) + 1) = points(0)(i).Y
  409.                     vertz((i * 3) + 2) = points(0)(i).Z
  410.                 Next
  411.                 GL.EnableClientState(ArrayCap.VertexArray)
  412.                 GL.VertexPointer(3, VertexPointerType.Float, 0, vertz)
  413.                 GL.DrawArrays(BeginMode.Points, 0, points(0).Count)
  414.                 GL.DisableClientState(ArrayCap.VertexArray)
  415.             End If
  416.  
  417.             If DisplayLines = True Then
  418.                 Dim vertz((points(1).Count) * 3) As Single
  419.                 For i = 0 To points(1).Count - 1
  420.                     vertz((i * 3) + 0) = points(1)(i).X
  421.                     vertz((i * 3) + 1) = points(1)(i).Y
  422.                     vertz((i * 3) + 2) = points(1)(i).Z
  423.                 Next
  424.                 GL.EnableClientState(ArrayCap.VertexArray)
  425.                 GL.VertexPointer(3, VertexPointerType.Float, 0, vertz)
  426.                 GL.DrawArrays(BeginMode.Lines, 0, points(1).Count)
  427.                 GL.DisableClientState(ArrayCap.VertexArray)
  428.             End If
  429.  
  430.  
  431.         End If
  432.  
  433.         GraphicsContext.CurrentContext.VSync = True
  434.         GlControl1.SwapBuffers()
  435.  
  436.     End Sub
  437.  
  438.     'GL Invalidate Tick
  439.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  440.         GlControl1.Invalidate()
  441.     End Sub
  442.  
  443.     Structure minmax
  444.         Public Xmin As Single
  445.         Public Xmax As Single
  446.         Public Ymin As Single
  447.         Public Ymax As Single
  448.         Public Zmin As Single
  449.         Public Zmax As Single
  450.     End Structure
  451.  
  452.     'TrackBar
  453.     Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
  454.         tbar1 = TrackBar1.Value
  455.         zdiff = (((tbar1 / 100) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + border(0).Zmin
  456.         Zupper = (((tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  457.         ZLower = (((-tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  458.  
  459.         GlControl1.Invalidate()
  460.         GridDraw()
  461.     End Sub
  462.     Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
  463.         tbar2 = TrackBar2.Value
  464.         zdiff = (((tbar1 / 100) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + border(0).Zmin
  465.         Zupper = (((tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  466.         ZLower = (((-tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  467.  
  468.         GlControl1.Invalidate()
  469.         GridDraw()
  470.     End Sub
  471.  
  472.     'Form Load
  473.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  474.  
  475.         gridheight = PictureBox1.Height
  476.         gridwidth = PictureBox1.Width
  477.         Gridgfx.FillRectangle(Brushes.White, 0, 0, gridwidth, gridheight)
  478.         PictureBox1.Image = Gridpic
  479.  
  480.     End Sub
  481.  
  482.     'Grid Buttons
  483.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  484.         DisplayPoints = True
  485.         DisplayLines = False
  486.         GridDraw()
  487.         GlControl1.Invalidate()
  488.     End Sub
  489.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  490.         DisplayLines = True
  491.         DisplayPoints = False
  492.         GridDraw()
  493.         GlControl1.Invalidate()
  494.     End Sub
  495.  
  496.     'Numeric Up Down changes
  497.     Private Sub RhoResNMUD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RhoResNMUD.ValueChanged
  498.         GridDraw()
  499.     End Sub
  500.     Private Sub ThresholdNMUD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThresholdNMUD.ValueChanged
  501.         GridDraw()
  502.     End Sub
  503.     Private Sub MinLineNMUD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MinLineNMUD.ValueChanged
  504.         GridDraw()
  505.     End Sub
  506.     Private Sub LineWidthNMUD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LineWidthNMUD.ValueChanged
  507.         GridDraw()
  508.     End Sub
  509.  
  510.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  511.         LineSearchTMR.Enabled = True
  512.         tbar1 = 0
  513.         tbar2 = 1
  514.         points(1) = New List(Of Vector3)
  515.         points(1).Clear()
  516.         objectcount = objectcount + 1
  517.     End Sub
  518.  
  519.     'Line Search, almost same code as grid draw
  520.     Private Sub LineSearchTMR_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LineSearchTMR.Tick
  521.         zdiff = (((tbar1 / 100) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + border(0).Zmin
  522.         Zupper = (((tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  523.         ZLower = (((-tbar2 / 200) * (border(0).Zmax - border(0).Zmin) / (border(0).Zmax - border(0).Zmin)) * (border(0).Zmax - border(0).Zmin)) + zdiff
  524.  
  525.         Dim drawpoints As List(Of Vector3) = New List(Of Vector3)
  526.         For i = 0 To points(0).Count - 1
  527.             If points(0)(i).Z < Zupper And points(0)(i).Z > ZLower Then
  528.                 drawpoints.Add(points(0)(i))
  529.             End If
  530.         Next
  531.  
  532.         Dim drawdots As List(Of Point) = New List(Of Point)
  533.  
  534.         Gridgfx.FillRectangle(Brushes.White, 0, 0, gridwidth, gridheight)
  535.         For i = 0 To drawpoints.Count - 1
  536.             Dim x As Single = ((drawpoints(i).X - border(0).Xmin) / (border(0).Xmax - border(0).Xmin)) * gridwidth
  537.             Dim y As Single = ((drawpoints(i).Y - border(0).Ymin) / (border(0).Ymax - border(0).Ymin)) * gridheight
  538.             y = gridheight - y
  539.             drawdots.Add(New Point(x, y))
  540.         Next
  541.  
  542.         drawdots = drawdots.Distinct().ToList()
  543.         For i = 0 To drawdots.Count - 1
  544.             Gridgfx.FillRectangle(Brushes.Black, drawdots(i).X - 0, drawdots(i).Y - 0, 2, 2)
  545.         Next
  546.  
  547.         Dim imagez = New Image(Of Gray, Byte)(Gridpic)
  548.         Dim imagez2 As Image(Of Gray, Byte) = imagez.Canny(150, 60)
  549.         Dim imagez3 As Image(Of Gray, Byte) = New Image(Of Gray, Byte)(gridwidth, gridheight)
  550.         Dim RhoRes As Double = RhoResNMUD.Value
  551.         Dim Threshold As Double = ThresholdNMUD.Value
  552.         Dim MinLineWidth As Double = MinLineNMUD.Value
  553.         Dim linegap As Integer = LineWidthNMUD.Value
  554.         Dim ThetaRes As Double = Math.PI / 180
  555.  
  556.         Dim Linez()() As LineSegment2D = imagez2.HoughLinesBinary(RhoRes, ThetaRes, Threshold, MinLineWidth, linegap)
  557.         If Linez(0).Length >= 0 Then
  558.             For i = 0 To Linez(0).Length - 1
  559.                 imagez3.Draw(Linez(0)(i), New Gray(255), 2)
  560.                 Dim p1x As Single = ((Linez(0)(i).P1.X / gridwidth) * (border(0).Xmax - border(0).Xmin)) + border(0).Xmin
  561.                 Dim p1y As Single = (((gridheight - Linez(0)(i).P1.Y) / gridheight) * (border(0).Ymax - border(0).Ymin)) + border(0).Ymin
  562.                 Dim p2x As Single = ((Linez(0)(i).P2.X / gridwidth) * (border(0).Xmax - border(0).Xmin)) + border(0).Xmin
  563.                 Dim p2y As Single = (((gridheight - Linez(0)(i).P2.Y) / gridheight) * (border(0).Ymax - border(0).Ymin)) + border(0).Ymin
  564.  
  565.                 points(1).Add(New Vector3(p1x, p1y, zdiff))
  566.                 points(1).Add(New Vector3(p2x, p2y, zdiff))
  567.             Next
  568.         End If
  569.         PictureBox1.Image = imagez3.ToBitmap
  570.         GlControl1.Invalidate()
  571.         tbar1 = tbar1 + 1
  572.         If tbar1 = 101 Then
  573.             LineSearchTMR.Enabled = False
  574.         End If
  575.  
  576.     End Sub
  577. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement