//--------------------------------------------------------------------------- // RENDERING A DIGITAL ELEVATION MODEL // FROM SCRATCH // // STEP 1: OPEN FILE AND RENDER USING COLOR RAMP // STEP 2: FLIP THE IMAGE VERTICALLY // STEP 3: FIND MINIMUM AND MAXIMUM // STEP 4: ADD SLIDERS // STEP 5: as per your imagination... //---------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------- NOTES // data file contains 562 data items per line // data file contains 492 lines of data // aside from zero, the minimum elevation is 105 // the maximum elevation is 3486 //--------------------------------------------------------- DECLARE VARIABLES int sluis[492][563]; int minElev; int maxElev; int elevation[3500]; int level; int topLeft, bottomRight; int topRight, bottomLeft; int top, bottom; int lastElev; bool gray = false; // ------------------------------------------------------ COLOR RAMP FUNCTION int colorRamp (int range, int value) { if (range==0) range=1; if (value < 0) value = 0; // interesting if deleted if (value > range) value = range; // interesting if deleted int pixelDistanceAlongPath = (value*1792)/range; int red, green, blue; if (pixelDistanceAlongPath < 256) { red=0; green=0; blue=pixelDistanceAlongPath; } else if (pixelDistanceAlongPath < 512) { red=0; green=pixelDistanceAlongPath-256; blue=255; } else if (pixelDistanceAlongPath < 768) { red=0; green=255; blue=255-(pixelDistanceAlongPath-512); } else if (pixelDistanceAlongPath < 1024) { red=(pixelDistanceAlongPath-768); green=255; blue=0; } else if (pixelDistanceAlongPath < 1280) { red= 255; green= 255-(pixelDistanceAlongPath-1024); blue=0; } else if (pixelDistanceAlongPath < 1536) { red=255; green=0; blue=pixelDistanceAlongPath-1280; } else { red=255; green=pixelDistanceAlongPath-1536; blue=255; } if (gray) { int ave = (red + green + blue) / 3; red = ave; green = ave; blue = ave; } return (RGB(red,green,blue)); } //-------------------------------------------------------------------- RENDER void render (void) { for (int e = 0; e < 492; e++) { for (int s = 0; s < 563; s++) { Form1->Canvas-> Pixels[e][s] = static_cast (colorRamp(maxElev - minElev, sluis[e][s] - minElev)); } Form1->ProgressBar1->Position = e; } Form1->ProgressBar1->Position = 0; } //----------------------------------------------------- ELEVATION STATISTICS void elevationStatistics (void) { minElev = 2000; maxElev = 0; for (int e = 0; e < 492; e++) { for (int s = 0; s < 563; s++) { if (sluis[e][s] != 0) minElev = min(minElev, sluis[e][s]); maxElev = max(maxElev, sluis[e][s]); elevation[sluis[e][s]]++; } } Form1->EditMin->Text = minElev; Form1->EditMax->Text = maxElev; Form1->TrackBarMin->Position = minElev; Form1->TrackBarMax->Position = maxElev; Application->ProcessMessages(); // Render elevation histogram for (int i = 0; i < 1500; i++) { Form1->PaintBox1->Canvas->MoveTo(0, (1000 - i) / 2); Form1->PaintBox1->Canvas->LineTo(elevation[i] / 30, (1000 - i) / 2); } Form1->PaintBox1->Canvas->TextOut(0, (1000 - 0) / 2, "0 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 100) / 2, "100 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 200) / 2, "200 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 300) / 2, "300 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 400) / 2, "400 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 500) / 2, "500 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 600) / 2, "600 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 700) / 2, "700 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 800) / 2, "800 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 900) / 2, "900 "); Form1->PaintBox1->Canvas->TextOut(0, (1000 - 1000) / 2, "1000 "); } //----------------------------------------------------------------- OPEN FILE void openFile (void) { if (Form1->OpenDialog1->Execute()) { ifstream infile(Form1->OpenDialog1->FileName.c_str()); for (int e = 0; e < 492; e++) { for (int s = 562; s >= 0; s--) { infile >> sluis[e][s]; } Form1->ProgressBar1->Position = e; } infile.close(); } Form1->ProgressBar1->Position = 0; elevationStatistics(); render(); } //--------------------------------------------------------------------- FLOOD void flood (void) { Form1->ProgressBar1->Max = 1500; for (level = 0; level < 1500; level = level + 2) { for (int e = 0; e < 492; e++) { for (int s = 0; s < 563; s++) { if ((sluis[e][s] < level) && sluis[e][s] > level - 3) { Form1->Canvas->Pixels[e][s] = clBlue; } } } Form1->EditElevation->Text = level; Form1->ProgressBar1->Position = level; Application->ProcessMessages(); } Form1->ProgressBar1->Position = 0; Form1->ProgressBar1->Max = 500; } //---------------------------------------------------------------------- RAZE void raze (void) { Form1->ProgressBar1->Max = 1500; for (level = 1500; level > 0; level = level - 2) { for (int e = 0; e < 492; e++) { for (int s = 0; s < 563; s++) { if ((sluis[e][s] > level) && sluis[e][s] < level + 3) { Form1->Canvas->Pixels[e][s] = clRed; } } } Form1->EditElevation->Text = level; Form1->ProgressBar1->Position = 1500 - level; Application->ProcessMessages(); } Form1->ProgressBar1->Position = 0; Form1->ProgressBar1->Max = 500; } //--------------------------------------------------------------------- SLICE void slice (void) { render(); for (int e = 0; e < 492; e++) { for (int s = 0; s < 563; s++) { if ((sluis[e][s] < level + 50) && (sluis[e][s] > level - 50)) { Form1->Canvas->Pixels[e][s] = clYellow; } } Application->ProcessMessages(); } } // ------------------------------------------------------------------- SUN UL void sunUL (void) { gray = true; for (int e = 1; e < 491; e++) { for (int s = 1; s < 562; s++) { topLeft = sluis[e - 1][s - 1]; bottomRight = sluis[e + 1][s + 1]; Form1->Canvas->Pixels[e][s] = static_cast(colorRamp(200, bottomRight - topLeft + 30)); } } gray = false; } // ------------------------------------------------------------------- SUN UR void sunUR (void) { gray = true; for (int e = 1; e < 491; e++) { for (int s = 1; s < 562; s++) { topRight = sluis[e + 1][s - 1]; bottomLeft = sluis[e - 1][s + 1]; Form1->Canvas->Pixels[e][s] = static_cast(colorRamp(200, bottomLeft - topRight + 30)); } } gray = false; } //============================================================ EVENT HANDLERS //---------------------------------------------------------- OPEN FILE BUTTON void __fastcall TForm1::ButtonOpenFileClick(TObject *Sender) { openFile(); } //------------------------------------------------------- CLEAR SCREEN BUTTON void __fastcall TForm1::ButtonClearClick(TObject *Sender) { Refresh(); } //------------------------------------------------------- RENDER COLOR BUTTON void __fastcall TForm1::ButtonRenderColorClick(TObject *Sender) { render(); } //-------------------------------------------------------- RENDER GRAY BUTTON void __fastcall TForm1::ButtonRenderGrayClick(TObject *Sender) { gray = true; render(); gray = false; } //----------------------------------------------- ELEVATION STATISTICS BUTTON void __fastcall TForm1::ButtonElevationStatsClick(TObject *Sender) { elevationStatistics(); } //-------------------------------------------------------------- FLOOD BUTTON void __fastcall TForm1::ButtonFloodClick(TObject *Sender) { flood(); } //--------------------------------------------------------------- RAZE BUTTON void __fastcall TForm1::ButtonRazeClick(TObject *Sender) { raze(); } //----------------------------------------------------- SUN UPPER LEFT BUTTON void __fastcall TForm1::ButtonSunULClick(TObject *Sender) { sunUL(); } //---------------------------------------------------- SUN UPPER RIGHT BUTTON void __fastcall TForm1::ButtonSunURClick(TObject *Sender) { sunUR(); } //----------------------------------------------------- SLICE TRACKBAR CHANGE void __fastcall TForm1::TrackBarSliceChange(TObject *Sender) { level = 1000 - TrackBarSlice->Position; Form1->EditElevation->Text = level; Application->ProcessMessages(); } //------------------------------------------------------------- MAX TRACK BAR void __fastcall TForm1::TrackBarMaxChange(TObject *Sender) { maxElev = TrackBarMax->Position; Form1->EditMax->Text = maxElev; } //------------------------------------------------------------- MIN TRACK BAR void __fastcall TForm1::TrackBarMinChange(TObject *Sender) { minElev = TrackBarMin->Position; Form1->EditMin->Text = minElev; } //------------------------------------------------------------- ON MOUSE MOVE void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (X < 492 && Y < 563) { EditElevation->Text = sluis[X][Y]; lastElev = sluis[X][Y]; } } //---------------------------------------------------- ON PAINTBOX MOUSE DOWN void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { gray = true; slice(); gray = false; } //---------------------------------------------------------------------------