1
0
Fork 0

Refactors subprograms as primitive operations of a new tagged Dtm_Data type

This commit is contained in:
Shawn Nock 2019-03-11 14:03:16 -04:00
parent 7878a18e17
commit c4f4bd34a9
7 changed files with 50 additions and 38 deletions

View File

@ -16,7 +16,7 @@ package body Dtm.Katie is
return Raw_Value;
end Strip_CR;
begin
for Element of Data loop
for Element of Data.Grid loop
Element := Dtm_Value'Value(Strip_CR(Get_Line(File)));
end loop;
end Get_Data;

View File

@ -2,13 +2,9 @@ with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
package body Dtm.Surface is
function Triangle_Count(Info : Dtm_Info) return Natural is
begin
return 2 * (Info.Get_X - 1) * (Info.Get_Y - 1 );
end;
procedure Surface_From_Grid(Triangles : in out Surface_Vector; Grid : Dtm_Data) is
procedure Surface_From_Grid(Triangles : in out Surface_Vector; Data : Dtm_Data) is
Grid : Dtm_Grid := Data.Grid;
begin
for X in Grid'First(1) .. Grid'Last(1) - 1 loop
for Y in Grid'First(2) .. Grid'Last(2) - 1 loop

View File

@ -12,7 +12,6 @@ package Dtm.Surface is
Element_Type => Triangle);
subtype Surface_Vector is Surface_Vector_Pkg.Vector;
function Triangle_Count(Info : Dtm_Info) return Natural;
procedure Surface_From_Grid(Triangles : in out Surface_Vector; Grid : Dtm_Data);
procedure Surface_From_Grid(Triangles : in out Surface_Vector; Data : Dtm_Data);
end Dtm.Surface;

View File

@ -11,5 +11,34 @@ package body Dtm is
return self.Y_Resolution;
end Get_Y;
function Max(Data : Dtm_Data) return Dtm_Value is
Max_Val : Dtm_Value := Dtm_Value'First;
begin
for Element of Data.Grid loop
if Element > Max_Val then
Max_Val := Element;
end if;
end loop;
return Max_Val;
end Max;
procedure Scale(Data : in out Dtm_Data; Scale_Factor : Dtm_Value := 100.0) is
begin
for Element of Data.Grid loop
Element := Element * Scale_Factor;
end loop;
end Scale;
procedure Pad(Data :in out Dtm_Data; Pad_Factor : Dtm_Value := 2.0) is
begin
for Element of Data.Grid loop
Element := Element + Pad_Factor;
end loop;
end;
function Triangle_Count(Info : Dtm_Info) return Natural is
begin
return 2 * (Info.Get_X - 1) * (Info.Get_Y - 1 );
end;
end Dtm;

View File

@ -2,7 +2,8 @@ package Dtm with SPARK_Mode is
subtype Dtm_Resolution is Natural;
type Dtm_Value is digits 6;
type Dtm_Data is array (Dtm_Resolution range <>, Dtm_Resolution range <>) of Dtm_Value;
type Dtm_Grid is array (Dtm_Resolution range <>, Dtm_Resolution range <>) of Dtm_Value;
type Dtm_Data(X, Y : Integer) is tagged private;
type Dtm_Info is tagged private;
function Get_X(self : Dtm_Info) return Dtm_Resolution with
@ -10,7 +11,16 @@ package Dtm with SPARK_Mode is
function Get_Y(self : Dtm_Info) return Dtm_Resolution with
Global => null;
function Max(Data : Dtm_Data) return Dtm_Value;
procedure Scale(Data : in out Dtm_Data; Scale_Factor : Dtm_Value := 100.0);
procedure Pad(Data :in out Dtm_Data; Pad_Factor : Dtm_Value := 2.0);
function Triangle_Count(Info : Dtm_Info) return Natural;
private
type Dtm_Data(X, Y : Integer) is tagged
record
Grid : Dtm_Grid(1..X, 1..Y);
end record;
type Dtm_Info is tagged
record

View File

@ -1,3 +1,4 @@
with Ada.Sequential_IO;
with Ada.Text_IO;
package body Stl is

View File

@ -11,7 +11,7 @@ procedure Tess is
Info : Dtm_Info;
Input : File_Type;
Minimum_Thickness : constant := 2.0;
Z_Scaling_Value : constant := 100;
Z_Scaling_Value : constant := 200;
begin
Ada.Text_IO.Put_Line("Tess started.");
Open(Input, In_File, "sample-data/katie.dat");
@ -21,32 +21,9 @@ begin
Put_Line(Info.Get_Y'Image);
declare
function Max(Data : Dtm_Data) return Dtm_Value is
Max_Val : Dtm_Value := Dtm_Value'First;
begin
for Element of Data loop
if Element > Max_Val then
Max_Val := Element;
end if;
end loop;
return Max_Val;
end Max;
procedure Scale(Data : in out Dtm_Data; Scale_Factor : Dtm_Value := Dtm_Value(100)) is
begin
for Element of Data loop
Element := Element * Scale_Factor;
end loop;
end Scale;
procedure Pad(Data :in out Dtm_Data) is
begin
for Element of Data loop
Element := Element + Minimum_Thickness;
end loop;
end;
Data : Dtm_Data(1..Info.Get_X, 1..Info.Get_Y);
Data : Dtm_Data(Info.Get_X, Info.Get_Y);
Triangles : Dtm.Surface.Surface_Vector;
Normalize_and_Scale_Factor : Dtm_Value;
@ -54,9 +31,9 @@ begin
Dtm.Katie.Get_Data(Data, Input);
Normalize_and_Scale_Factor :=
Dtm_Value(Dtm_Value(1) / Max(Data)) * Dtm_Value(Z_Scaling_Value);
Scale(Data, Normalize_and_Scale_Factor);
Pad(Data);
Triangles.Reserve_Capacity(Count_Type(Dtm.Surface.Triangle_Count(Info)));
Data.Scale(Normalize_and_Scale_Factor);
Data.Pad(Minimum_Thickness);
Triangles.Reserve_Capacity(Count_Type(Info.Triangle_Count));
Dtm.Surface.Surface_From_Grid(Triangles, Data);
Stl.Write(Triangles, "Farts.stl");
end;