1
0
Fork 0

Initial commit

Top and bottom surface fuction with scaling / normalization in the Z
direction
This commit is contained in:
Shawn Nock 2019-03-05 07:29:06 -05:00
commit ca89f35c64
14 changed files with 389783 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
obj

389481
sample-data/katie.dat Normal file

File diff suppressed because it is too large Load Diff

56
src/dtm-katie.adb Normal file
View File

@ -0,0 +1,56 @@
with Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Dtm.Katie is
procedure Get_Data(Data : out Dtm_Data; File : File_Type) is
function Strip_CR(Raw_Value : String) return String is
begin
if Raw_Value(Raw_Value'Last) = ASCII.CR then
declare
Out_String : String := Raw_Value(Raw_Value'First..Raw_Value'Last - 1);
begin
return Out_String;
end;
end if;
return Raw_Value;
end Strip_CR;
Count : Natural := 0;
begin
for Element of Data loop
Element := Dtm_Value'Value(Strip_CR(Get_Line(File)));
end loop;
end Get_Data;
function Get_Info(File : File_Type) return Dtm_Info is
use Ada.Text_IO;
Info : Dtm_Info;
function Get_Token(S : String; Last_Index : out Natural; Start_Index : Natural := 1; Separator : Character := ' ') return String is
Buffer : Unbounded_String;
begin
for I in Start_Index .. S'Last loop
Last_Index := I;
exit when S(I) = Separator;
Buffer := Buffer & S(I);
end loop;
Last_Index := Last_Index + 1;
return To_String(Buffer);
end;
begin
declare
First_Line : String := Get_Line(File);
Last_Index : Natural;
X : String := Get_Token(First_Line, Last_Index);
Y : String := Get_Token(First_Line, Last_Index, Start_Index => Last_Index);
begin
Info.X_Resolution := Dtm_Resolution'Value(X);
Info.Y_Resolution := Dtm_Resolution'Value(Y);
end;
return Info;
end Get_Info;
end Dtm.Katie;

9
src/dtm-katie.ads Normal file
View File

@ -0,0 +1,9 @@
with Ada.Text_IO; use Ada.Text_IO;
package Dtm.Katie with SPARK_Mode is
function Get_Info(File : File_Type) return Dtm_Info;
procedure Get_Data(Data: out Dtm_Data; File : File_Type);
end Dtm.Katie;

49
src/dtm-surface.adb Normal file
View File

@ -0,0 +1,49 @@
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
begin
for X in Grid'First(1) .. Grid'Last(1) - 1 loop
for Y in Grid'First(2) .. Grid'Last(2) - 1 loop
declare
T1_P1 : Point := (Dtm_Value(X), Dtm_Value(Y), Grid(X, Y));
T1_P2 : Point := (Dtm_Value(X+1), Dtm_Value(Y), Grid(X+1, Y));
T1_P3 : Point := (Dtm_Value(X+1), Dtm_Value(Y+1), Grid(X+1, Y+1));
Tri1 : Triangle := (T1_P1, T1_P2, T1_P3);
T2_P1 : Point := (Dtm_Value(X), Dtm_Value(Y), Grid(X, Y));
T2_P2 : Point := (Dtm_Value(X), Dtm_Value(Y+1), Grid(X+1, Y));
T2_P3 : Point := (Dtm_Value(X+1), Dtm_Value(Y+1), Grid(X+1, Y+1));
Tri2 : Triangle := (T2_P1, T2_P2, T2_P3);
begin
Triangles.Append(Tri1);
Triangles.Append(Tri2);
end;
end loop;
end loop;
-- Base
declare
Min_X : Dtm_Value := Dtm_Value(Grid'First(1));
Min_Y : Dtm_Value := Dtm_Value(Grid'First(2));
Max_X : Dtm_Value := Dtm_Value(Grid'Last(1));
Max_Y : Dtm_Value := Dtm_Value(Grid'Last(2));
Zero : Dtm_Value := Dtm_Value(0);
begin
Triangles.Append(((Min_X, Min_Y, Zero),
(Min_X, Max_Y, Zero),
(Max_X, Max_Y, Zero)));
Triangles.Append(((Min_X, Min_Y, Zero),
(Max_X, Min_Y, Zero),
(Max_X, Max_Y, Zero)));
end;
end Surface_From_Grid;
end Dtm.Surface;

18
src/dtm-surface.ads Normal file
View File

@ -0,0 +1,18 @@
with Ada.Containers.Vectors; use Ada.Containers;
package Dtm.Surface is
type Point is tagged record
X, Y, Z : Dtm_Value;
end record;
type Triangle is array (0 .. 2) of Point;
package Surface_Vector_Pkg is new Ada.Containers.Vectors(Index_Type => Positive,
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);
end Dtm.Surface;

15
src/dtm.adb Normal file
View File

@ -0,0 +1,15 @@
package body Dtm is
function Get_X(self : Dtm_Info) return Dtm_Resolution
is
begin
return self.X_Resolution;
end Get_X;
function Get_Y(self : Dtm_Info) return Dtm_Resolution is
begin
return self.Y_Resolution;
end Get_Y;
end Dtm;

21
src/dtm.ads Normal file
View File

@ -0,0 +1,21 @@
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_Info is tagged private;
function Get_X(self : Dtm_Info) return Dtm_Resolution with
Global => null;
function Get_Y(self : Dtm_Info) return Dtm_Resolution with
Global => null;
private
type Dtm_Info is tagged
record
X_Resolution : Dtm_Resolution;
Y_Resolution : Dtm_Resolution;
end record;
end Dtm;

30
src/split_string.adb Normal file
View File

@ -0,0 +1,30 @@
with Ada.Text_IO; use Ada.Text_IO;
package body Split_String is
procedure Split_By (Output : out Separated_String;
src : Unbounded_String;
separator : Character)
is
use Separated_String_Pkg;
Tmp_String : Unbounded_String;
begin
for char of To_String(src) loop
if char = separator then
Append(Output, Tmp_String);
Put_Line("Appending: " & To_String(Tmp_String));
Tmp_String := To_Unbounded_String("");
else
Tmp_String := Tmp_String & char;
end if;
end loop;
Put_Line("Appending: " & To_String(Tmp_String));
Append(Output, Tmp_String);
end;
procedure Split
(Output : out Separated_String; src : Unbounded_String) is
begin
Split_By(Output => Output, src => src, separator => ' ');
end;
end Split_String;

9
src/split_string.ads Normal file
View File

@ -0,0 +1,9 @@
with Ada.Containers.Formal_Indefinite_Vectors; use Ada.Containers;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Split_String
with Spark_Mode is
type Split_String is array (Natural range <>) of String;
procedure Split_By(output : out Separated_String; src : Unbounded_String; separator : Character);
procedure Split(output: out Separated_String; src : Unbounded_String);
end Split_String;

25
src/stl.adb Normal file
View File

@ -0,0 +1,25 @@
with Ada.Text_IO;
package body Stl is
procedure Write(Surface : Surface_Vector; File_Path : String) is
use Ada.Text_IO;
Output : File_Type;
begin
Create(Output, Out_File, File_Path);
Put_Line(Output, "solid ");
for Triangle of Surface loop
Put_Line(Output, "facet normal 0 0 0");
Put_Line(Output, " outer loop");
for Point of Triangle loop
Put_Line(Output, " vertex " &
Point.X'Image & " " &
Point.Y'Image & " " &
Point.Z'Image);
end loop;
Put_Line(Output, " endloop");
Put_Line(Output, "endfacet");
end loop;
Put_Line(Output, "endsolid ");
end Write;
end Stl;

7
src/stl.ads Normal file
View File

@ -0,0 +1,7 @@
with Dtm.Surface; use Dtm.Surface;
package Stl is
procedure Write(Surface : Surface_Vector; File_Path : String);
end Stl;

57
src/tess.adb Normal file
View File

@ -0,0 +1,57 @@
with Ada.Containers; use Ada.Containers;
with Ada.Text_IO;
with Dtm; use Dtm;
with Dtm.Katie;
with Dtm.Surface;
with Stl;
procedure Tess is
use Ada.Text_IO;
Info : Dtm_Info;
Input : File_Type;
begin
Ada.Text_IO.Put_Line("Tess started.");
Open(Input, In_File, "inelev.dat");
Info := Dtm.Katie.Get_Info(Input);
Put_Line(Get_X(Info)'Image);
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 Normalize(Data : in out Dtm_Data) is
Max_Val : Dtm_Value := Max(Data);
begin
Scale(Data, Dtm_Value(1) / Max_Val);
end Normalize;
Data : Dtm_Data(1..Info.Get_X, 1..Info.Get_Y);
Triangles : Dtm.Surface.Surface_Vector;
begin
Dtm.Katie.Get_Data(Data, Input);
Normalize(Data);
Scale(Data);
Triangles.Reserve_Capacity(Count_Type(Dtm.Surface.Triangle_Count(Info)));
Dtm.Surface.Surface_From_Grid(Triangles, Data);
Stl.Write(Triangles, "Farts.stl");
end;
end Tess;

5
tess.gpr Normal file
View File

@ -0,0 +1,5 @@
project Tess is
for Source_Dirs use ("src");
for Object_Dir use "obj";
for Main use ("tess.adb");
end Tess;