From c4f4bd34a9db2a21362a8c7a9ceaf99a00db9a3c Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Mon, 11 Mar 2019 14:03:16 -0400 Subject: [PATCH] Refactors subprograms as primitive operations of a new tagged Dtm_Data type --- src/dtm-katie.adb | 2 +- src/dtm-surface.adb | 8 ++------ src/dtm-surface.ads | 3 +-- src/dtm.adb | 29 +++++++++++++++++++++++++++++ src/dtm.ads | 12 +++++++++++- src/stl.adb | 1 + src/tess.adb | 33 +++++---------------------------- 7 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/dtm-katie.adb b/src/dtm-katie.adb index c5855a5..4c75fe7 100644 --- a/src/dtm-katie.adb +++ b/src/dtm-katie.adb @@ -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; diff --git a/src/dtm-surface.adb b/src/dtm-surface.adb index 1fab864..f8cddc2 100644 --- a/src/dtm-surface.adb +++ b/src/dtm-surface.adb @@ -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 diff --git a/src/dtm-surface.ads b/src/dtm-surface.ads index 28e2fe8..2c7453d 100644 --- a/src/dtm-surface.ads +++ b/src/dtm-surface.ads @@ -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; diff --git a/src/dtm.adb b/src/dtm.adb index 3176097..7282b60 100644 --- a/src/dtm.adb +++ b/src/dtm.adb @@ -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; diff --git a/src/dtm.ads b/src/dtm.ads index 4959134..c7b7962 100644 --- a/src/dtm.ads +++ b/src/dtm.ads @@ -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 diff --git a/src/stl.adb b/src/stl.adb index d2acd75..e249e1f 100644 --- a/src/stl.adb +++ b/src/stl.adb @@ -1,3 +1,4 @@ +with Ada.Sequential_IO; with Ada.Text_IO; package body Stl is diff --git a/src/tess.adb b/src/tess.adb index 953e00c..d1b3bfb 100644 --- a/src/tess.adb +++ b/src/tess.adb @@ -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;