home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Disp3d / READ3D.PAS < prev    next >
Pascal/Delphi Source File  |  1995-10-08  |  6KB  |  261 lines

  1.  
  2. {$I-,E+,N+}
  3. unit Read3d;
  4. interface
  5.  
  6. function ReadConfig(Filename:string):boolean;
  7. function ReadData(Filename:string):boolean;
  8. function ReadPatch(Filename:string):boolean;
  9.  
  10. const MaxItem = pred(65520 div sizeof(Single));
  11. type DataArrayType = array[0..MaxItem] of Single;
  12. type DataArrayPtr = ^DataArrayType;
  13. var Xval,Yval,Zval:DataArrayPtr;
  14. var DataItems : word;
  15.  
  16. const MaxPatchPoints = 64;
  17. type PatchPoints = array[0..MaxPatchPoints] of Word;
  18. const MaxPatchLine = pred(65520 div sizeof(PatchPoints));
  19. type PatchArrayType = array[0..MaxPatchLine] of PatchPoints;
  20. type PatchArrayPtr = ^PatchArrayType;
  21. var Patch : PatchArrayPtr;
  22. var PatchItems : word;
  23. var PatchLines : word;
  24.  
  25. const MaxBezierPattern = 31;
  26. type BezierPatternArrayType = array[0..MaxBezierPattern] of word;
  27. type BezierPatternPtr = ^BezierPatternArrayType;
  28. var BezierPattern : BezierPatternPtr;
  29. var BezierPatternItems : word;
  30.  
  31. var Xstart,YStart,Zstart : single;
  32. var Xrange,Yrange,Zrange : single;
  33.  
  34. const Xangle : single = 0;
  35.       Yangle : single = 0;
  36.       Zangle : single = 0;
  37.  
  38. implementation
  39.  
  40. type string20 = string[20];
  41. var f : text;
  42.     s : string;
  43.     Ts,Ts0,Ts1,Ts2,Ts3 : string20;
  44.     Error,R,j,k,l : integer;
  45.  
  46.   procedure findnum;
  47.   begin
  48.     while (s[j]<= ' ') or (s[j] = ',') do {find start}
  49.     begin
  50.       if j >= length(s) then break;
  51.       inc(j);
  52.     end;
  53.     k := j;
  54.     while (s[k] <> ',') do  {find end}
  55.     begin
  56.       if k >= length(s) then break;
  57.       inc(k);
  58.     end;
  59.     if k = length(s) then inc(k);
  60.     l := k-j;
  61.     while (s[j+l-1] > '9') or (s[j+l-1] < '0') do
  62.       dec(l);
  63.   end;
  64.  
  65.   function ReadPoint(var X,Y,Z:single):integer;
  66.   begin
  67.     ReadPoint := -1;
  68.     while true do
  69.     begin
  70.       if eof(f) then
  71.       begin
  72.         ReadPoint := 0;
  73.         Exit;
  74.       end;
  75.       readln(f,s);
  76.       if ioresult <> 0 then Exit;
  77.       if (length(s) > 0) and (s[1] <> ';') then
  78.       begin
  79.         j := 1;
  80.         findnum;
  81.         Ts0 := copy(s,j,l);
  82.         j := succ(k);
  83.         findnum;
  84.         Ts1 := copy(s,j,l);
  85.         j := succ(k);
  86.         findnum;
  87.         Ts2 := copy(s,j,l);
  88.         j := succ(k);
  89.         findnum;
  90.         Ts3 := copy(s,j,l);
  91.         if (length(Ts1) > 0) and (length(Ts2) > 0) and (length(Ts3) > 0) then
  92.         begin
  93.           val(Ts1,X,error);  if error <> 0 then Exit;
  94.           val(Ts2,Y,error);  if error <> 0 then Exit;
  95.           val(Ts3,Z,error);  if error <> 0 then Exit;
  96.           ReadPoint := 1;
  97.           Exit;
  98.         end;
  99.       end;
  100.     end;
  101.   end;
  102.  
  103.   function ReadPatchLine(PatchLines:word):integer;
  104.   begin
  105.     while true do
  106.     begin
  107.       ReadPatchLine := -1;
  108.       if eof(f) then
  109.       begin
  110.         ReadPatchLine := 0;
  111.         Exit;
  112.       end;
  113.       readln(f,s);
  114.       if ioresult <> 0 then Exit;
  115.       if (length(s) > 0) and (s[1] <> ';') then
  116.       begin
  117.         PatchItems := 0;
  118.         j := 1;
  119.         repeat
  120.           findnum;
  121.           Ts := copy(s,j,l);
  122.           j := succ(k);
  123.           if length(Ts) > 0 then
  124.           begin
  125.             val(Ts,Patch^[PatchLines][PatchItems],error);
  126.             if error <> 0 then
  127.             begin
  128.               ReadPatchLine := -1;
  129.               Exit;
  130.             end;
  131.             inc(PatchItems);
  132.             ReadPatchLine := 1;
  133.           end;
  134.         until length(Ts) = 0;
  135.         if PatchItems > 0 then
  136.           dec(PatchItems);
  137.         Exit;
  138.       end;
  139.     end;
  140.   end;
  141.  
  142.  
  143.   function ReadBezierPattern:integer;
  144.   begin
  145.     while true do
  146.     begin
  147.       ReadBezierPattern := -1;
  148.       if eof(f) then
  149.       begin
  150.         ReadBezierPattern := 0;
  151.         Exit;
  152.       end;
  153.       readln(f,s);
  154.       if ioresult <> 0 then Exit;
  155.       if (length(s) > 0) and (s[1] <> ';') then
  156.       begin
  157.         BezierPatternItems := 0;
  158.         j := 1;
  159.         repeat
  160.           findnum;
  161.           Ts := copy(s,j,l);
  162.           j := succ(k);
  163.           if length(Ts) > 0 then
  164.           begin
  165.             val(Ts,BezierPattern^[BezierPatternItems],error);
  166.             if error <> 0 then
  167.             begin
  168.               ReadBezierPattern := -1;
  169.               Exit;
  170.             end;
  171.             inc(BezierPatternItems);
  172.             ReadBezierPattern := 1;
  173.           end;
  174.         until length(Ts) = 0;
  175.         if BezierPatternItems > 0 then
  176.           dec(BezierPatternItems);
  177.         Exit;
  178.       end;
  179.     end;
  180.   end;
  181.  
  182. function ReadConfig(Filename:string):boolean;
  183. begin
  184.   if ioresult = 0 then {nop};
  185.   ReadConfig := false;
  186.   assign(f,filename+'.PLT');
  187.   reset(f);
  188.   if ReadPoint(Xstart,Ystart,Zstart) < 1 then Exit;
  189.   if ReadPoint(Xrange,Yrange,Zrange) < 1 then Exit;
  190.   if ReadPoint(Xangle,Yangle,Zangle) < 1 then Exit;
  191.   if ReadBezierPattern < 1 then Exit;
  192.   ReadConfig := true;
  193. end;
  194.  
  195. function ReadData(Filename:string):boolean;
  196. VAR LST:TEXT;
  197. begin
  198.  
  199. {$ifdef doprint}
  200. ASSIGN(LST,'LPT1');
  201. REWRITE(LST);
  202. WRITELN(LST);
  203. {$endif}
  204.  
  205.   if ioresult = 0 then {nop};
  206.   ReadData := false;
  207.   assign(f,filename+'.DAT');
  208.   reset(f);
  209.   DataItems := 0;
  210.   R := 1;
  211.   while R > 0 do
  212.   begin
  213.     if DataItems >= MaxItem then Exit;
  214.     R := ReadPoint(Xval^[DataItems],Yval^[DataItems],Zval^[DataItems]);
  215.  
  216. {$ifdef doprint}
  217.   WRITE(LST,DataItems+1:3,':',TS0,',',TS1,',',TS2,',',TS3,'  ');
  218.   IF DATAITEMS MOD 2 = 1 THEN WRITELN(LST);
  219. {$endif}
  220.  
  221.     if R < 0 then Exit;
  222.     inc(DataItems);
  223.     ReadData := true;
  224.   end;
  225.  
  226.   {$ifdef doprint}
  227.     WRITE(LST,^L);
  228. {$endif}
  229.  
  230. end;
  231.  
  232. function ReadPatch(Filename:string):boolean;
  233. begin
  234.   if ioresult = 0 then {nop};
  235.   ReadPatch := false;
  236.   assign(f,filename+'.PAT');
  237.   reset(f);
  238.   PatchLines := 0;
  239.   R := 1;
  240.   while R > 0 do
  241.   begin
  242.     if PatchLines >= MaxPatchLine then Exit;
  243.     R := ReadPatchLine(PatchLines);
  244.     if R < 0 then Exit;
  245.     if R > 0 then
  246.       inc(PatchLines);
  247.     ReadPatch := true;
  248.   end;
  249. end;
  250.  
  251. begin
  252.   new(Xval);
  253.   new(Yval);
  254.   new(Zval);
  255.   new(Patch);
  256.   new(BezierPattern);
  257.   fillchar(patch^,sizeof(Patch^),0);
  258.   fillchar(BezierPattern^,sizeof(BezierPattern^),0);
  259. end.
  260.  
  261.