Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Imports gmRTL.GUI
Namespace Waves
   Public Class frmWaves : Inherits System.Windows.Forms.Form
      Private Dim _disposed As Boolean = False
      <System.Diagnostics.DebuggerNonUserCode()> _
      Public Sub New()
         MyBase.New()
         'This call is required by the Windows Form Designer.
         InitializeComponent()
         'Add any initialization after the InitializeComponent() call
         If (DesignMode) Then Exit Sub
         SurfaceImage = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Forms.PictureBox)
         SurfaceImage.Add(1, _SurfaceImage_1)
         Form_Load(Nothing, Nothing)

      End Sub
      'Form overrides dispose to clean up the component list.
      <System.Diagnostics.DebuggerNonUserCode()> _
      Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
         If _disposed Then
            Return
         End If
         If Disposing Then
            If Not components Is Nothing Then
               components.Dispose()
            End If
         End If
         _disposed = True
         MyBase.Dispose(Disposing)
      End Sub
      Private Const EyeR As Integer = 10#
      Private Const EyeTheta As Double = PI * 0.2
      Private Const EyePhi As Double = PI * 0.1
      Private Const FocusX As Integer = 0#
      Private Const FocusY As Integer = 0#
      Private Const FocusZ As Integer = 0#
      Dim Projector(4,4) As Single
      Dim ThePicture As objPicture = Nothing
      Dim TheGrid As ObjGrid3D = Nothing
      Dim Running As Integer = 0
      ' Draw the surface.
      Private Sub DrawData(ByVal pic As System.Windows.Forms.PictureBox)
         Dim x As Single = 0.0F
         Dim y As Single = 0.0F
         Dim z As Single = 0.0F
         Dim S(4,4) As Single
         Dim t(4,4) As Single
         Dim ST(4,4) As Single
         Dim PST(4,4) As Single


         On Error Goto ErrHandler

         ' Scale and translate so it looks OK in pixels.
         basWaves.m3Scale(S,35,-35,1)
         basWaves.m3Translate(t,230,175,0)
         basWaves.m3MatMultiplyFull(ST,S,t)
         basWaves.m3MatMultiplyFull(PST,Projector,ST)

         ' Transform the points.
         ThePicture.ApplyFull(PST)

         ' Display the data.
         gmRTL.GUI.ControlHelper.PictureCls(pic)
         ThePicture.Draw(pic,EyeR)
         pic.Refresh()

      ErrHandler:

      End Sub
      Private Sub CmdDisplay_Click(ByVal sender As Object, ByVal e As EventArgs)
         Pict.Visible = True
         If (Running <> 0) Then
            cmdDisplay.Text = "Stopped"
            cmdDisplay.Enabled = False
            Running = IIf(False, -1, 0)
         Else
            Running = IIf(True, -1, 0)
            cmdDisplay.Text = "Stop"
            ShowFrames()
            cmdDisplay.Text = "Run"
            cmdDisplay.Enabled = True
         End If
      End Sub
      Private Sub cmdExit_Click(ByVal sender As Object, ByVal e As EventArgs)
         If (cmdDisplay.Text = "Stop") Then
            System.Windows.Forms.MessageBox.Show("Stop the Function first !", "Waves", MessageBoxButtons.OK,MessageBoxIcon.Information)
            Exit Sub
         Else
            Me.Close()
         End If
      End Sub
      Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
         gmRTL.GUI.ControlHelper.SetAutoRedraw(Pict,True)
         ' Initialize the projection transformation.
         basWaves.m3PProject(Projector,m3Perspective,EyeR,EyePhi,EyeTheta,FocusX,FocusY,FocusZ,0,1,0)

         ' Load empty image controls for later reproduction of saved image transformation
         Dim i As Integer = 0
         For i = 2 To 20 Step 1
            gmRTL.GUI.ControlHelper.LoadCtl(SurfaceImage,i)
         Next

         cmdDisplay.Enabled = False
      End Sub
      Public Sub CmdCreate_click(ByVal sender As Object, ByVal e As EventArgs)
         cmdDisplay.Enabled = True
         lblCounter.Visible = True
         txtCounter.Visible = True
         Pict.Visible = False
         Const PI_10 As Double = PI / 10.0
         Const xmin As Integer = -5
         Const Zmin As Integer = -5
         Const dx As Double = 0.3
         Const dz As Double = 0.3
         Const NumX As Double = -2 * xmin / dx
         Const NumZ As Double = -2 * Zmin / dz
         Const Amp As Double = 0.25

         Dim num As Integer = 0
         Dim offset As Single = 0.0F
         Dim i As Integer = 0
         Dim j As Integer = 0
         Dim x As Single = 0.0F
         Dim y As Single = 0.0F
         Dim z As Single = 0.0F
         Dim D As Single = 0.0F

         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
         Me.Refresh()
         'Save 20 positions of grid(net) as images
         For num = 1 To 20 Step 1
            Dim count As Integer = 0
            count = (20 - num) \ 2
            lblCounter.Text = Environment.NewLine & "Loading ... "
            txtCounter.Text = Convert.ToString(count)
            ThePicture = New objPicture
            TheGrid = New ObjGrid3D
            TheGrid.SetBounds(xmin,dx,NumX,Zmin,dz,NumZ)
            ThePicture.objects.Add(TheGrid,Nothing,Nothing,Nothing)

            offset = CType(num * PI_10,Single)
            x = xmin
            For i = 1 To NumX Step 1
               z = Zmin
               For j = 1 To NumZ Step 1
                  D = CType(Math.Sqrt(x * x + z * z),Single)

                  'This is a Function that can be modified , You can test various
                  'formulas and even ,( I think it is possible ) to get data from Db and
                  'set the function to show graphical ( 3D ) report.
                  'If you perform testing , take care about OVERFLOW error
                  y = CType(Amp * Math.Sin(3 * D - offset),Single)

                  TheGrid.SetValue(x,y,z)
                  z = CType(z + dz,Single)

               Next

               x = CType(x + dx,Single)
            Next

            ' Display the data.
            DrawData(Pict)

            ' Save the bitmap for later.
            SurfaceImage(num).Image = Pict.GetPaintedImage()
            System.Windows.Forms.Application.DoEvents()

         Next
         txtCounter.Visible = False
         lblCounter.Visible = False
         Pict.Visible = True
         cmdCreate.Enabled = False
         cmdDisplay.Enabled = True
         Me.AcceptButton = cmdDisplay
         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

      End Sub
      Private Sub Form_Unload(ByVal sender As Object, ByVal e As EventArgs)
         Environment.Exit(0)
      End Sub
      ' Show the images.
      Dim Shared ShowFrames_num As Integer = 0
      Private Sub ShowFrames()
         Const ms_per_frame As Integer = 50
         Dim next_time As Integer = 0

         Do While Running <> 0
            ShowFrames_num = ShowFrames_num + 1
            If (ShowFrames_num > 20) Then
               ShowFrames_num = 1
            End If
            next_time = basWaves.GetTickCount() + ms_per_frame
            Pict.BackgroundImage = SurfaceImage(ShowFrames_num).Image
            System.Windows.Forms.Application.DoEvents()
            basWaves.WaitTill(next_time)
         Loop
      End Sub
   End Class
End Namespace
