PDA

View Full Version : Adding the (D) in (D)DTMs



Lee Lok Hin
10-05-2008, 09:46 AM
How To Put the (D) in (D)DTMs.

DDTMs = Dynamic Deformable Template Models

This Tutorial Assumes That You Understand How To Make Basic DDTMs.



So, you heard of DDTMs now. Do they sound better than plain old DTMs? Want to give them a try? You have? Good. But there's something missing.

Where's the (D), or Dynamic-ness in the DDTM?

Let us take a look at the following function.

function DDTMOut: Integer;
var
dtmMainPoint: TDTMPointDef;
dtmSubPoints: Array [0..4] of TDTMPointDef;
TempTDTM: TDTM;
begin
dtmMainPoint.x := 994;
dtmMainPoint.y := 244;
dtmMainPoint.AreaSize := 0;
dtmMainPoint.AreaShape := 0;
dtmMainPoint.Color := 7241098;
dtmMainPoint.Tolerance := 20;

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;
dtmSubPoints[0].AreaSize := 0;
dtmSubPoints[0].AreaShape := 0;
dtmSubPoints[0].Color := 7241098;
dtmSubPoints[0].Tolerance := 20;

dtmSubPoints[1].x := 986;
dtmSubPoints[1].y := 258;
dtmSubPoints[1].AreaSize := 0;
dtmSubPoints[1].AreaShape := 0;
dtmSubPoints[1].Color := 7241098;
dtmSubPoints[1].Tolerance := 20;

dtmSubPoints[2].x := 987;
dtmSubPoints[2].y := 227;
dtmSubPoints[2].AreaSize := 0;
dtmSubPoints[2].AreaShape := 0;
dtmSubPoints[2].Color := 7241098;
dtmSubPoints[2].Tolerance := 20;

dtmSubPoints[3].x := 991;
dtmSubPoints[3].y := 216;
dtmSubPoints[3].AreaSize := 0;
dtmSubPoints[3].AreaShape := 0;
dtmSubPoints[3].Color := 7241098;
dtmSubPoints[3].Tolerance := 20;

dtmSubPoints[4].x := 984;
dtmSubPoints[4].y := 267;
dtmSubPoints[4].AreaSize := 0;
dtmSubPoints[4].AreaShape := 0;
dtmSubPoints[4].Color := 7241098;
dtmSubPoints[4].Tolerance := 20;

TempTDTM.MainPoint := dtmMainPoint;
TempTDTM.SubPoints := dtmSubPoints;
Result := AddDTM(TempTDTM);
end;

So, is anything missing from the DDTM? Yes. It uses nothing of the Dynamic-ness in DDTMs. Everything is so static, it is perfectly possible to make that a DTM instead.




So how do you put the (D), I hear you ask?

This is a type of Dynamicity that will add tolerance in steps of 5.

function DDTMOut: TPoint;
var
dtmMainPoint: TDTMPointDef;
dtmSubPoints: Array [0..4] of TDTMPointDef;
TempTDTM: TDTM;
t, Tole: Integer;

begin
repeat
Tole:= Tole + 5;
dtmMainPoint.x := 994;
dtmMainPoint.y := 244;
dtmMainPoint.AreaSize := 0;
dtmMainPoint.AreaShape := 0;
dtmMainPoint.Color := 7241098;
dtmMainPoint.Tolerance := Tole;

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;
dtmSubPoints[0].AreaSize := 0;
dtmSubPoints[0].AreaShape := 0;
dtmSubPoints[0].Color := 7241098;
dtmSubPoints[0].Tolerance := Tole;

dtmSubPoints[1].x := 986;
dtmSubPoints[1].y := 258;
dtmSubPoints[1].AreaSize := 0;
dtmSubPoints[1].AreaShape := 0;
dtmSubPoints[1].Color := 7241098;
dtmSubPoints[1].Tolerance := Tole;

dtmSubPoints[2].x := 987;
dtmSubPoints[2].y := 227;
dtmSubPoints[2].AreaSize := 0;
dtmSubPoints[2].AreaShape := 0;
dtmSubPoints[2].Color := 7241098;
dtmSubPoints[2].Tolerance := Tole;

dtmSubPoints[3].x := 991;
dtmSubPoints[3].y := 216;
dtmSubPoints[3].AreaSize := 0;
dtmSubPoints[3].AreaShape := 0;
dtmSubPoints[3].Color := 7241098;
dtmSubPoints[3].Tolerance := Tole;

dtmSubPoints[4].x := 984;
dtmSubPoints[4].y := 267;
dtmSubPoints[4].AreaSize := 0;
dtmSubPoints[4].AreaShape := 0;
dtmSubPoints[4].Color := 7241098;
dtmSubPoints[4].Tolerance := Tole;

TempTDTM.MainPoint := dtmMainPoint;
TempTDTM.SubPoints := dtmSubPoints;
i := AddDTM(TempTDTM);

if DTMRotated(i,Result.x, Result.y, MMX1,MMY1,MMX2,MMY2) then Exit;

try
FreeDTM(i);
except
end;

until(tole > 50)
end;

What's Different? Well, there's Dynamicness in the form of the tolerance increasing in steps of 5! Lets take a look in more detail.

function DDTMOut: TPoint;

Instead of a DTM resulting, we make it result a TPoint.

repeat
Tole:= Tole + 5;

This will start the loop, and make the integer variable (Tole) add 5;

dtmMainPoint.AreaShape := 0;
dtmMainPoint.Color := 7241098;
dtmMainPoint.Tolerance := Tole;

This will make the Tolerance equal to Tole, so for each loop, tole will be 5 higher.

i := AddDTM(TempTDTM);

if DTMRotated(i,Result.x, Result.y, MMX1,MMY1,MMX2,MMY2) then Exit;

We make the variable (i) equal the Temporary DTM with tolerance Tole, and try to find it. If we find it, we make the result equal to the place we found the DDTM in, and exit the function.

try
FreeDTM(i);
except
end;

This will try to Free the DTM(i), so that it will not sue up memory in the computer. It is generally a good idea to free any DTMs you use.

Tthe Try Except Finally End; is used to protect agains RunTime errors. Because Freeing a DTM in a variable that isn't a DTM will give a runtime error, this is just a "Just in case" thing.


try
//RunTime Error?
except
//If RunTime error then do anything here
finally
//Will do this regardless of runtime or no runtime
end;


In case you didn't notice, finally is optional.

until(tole > 50)

If Tole (Our Tolerance) is bigger than 50, it will quit the function, seeing as 50 tolerance is pretty high anyway.

Yay! We made our first true (D)DTM!



What are the other types of (D)s around?
Dynamic-ness for the color is fine too.

function DDTMOut: Integer;
var
dtmMainPoint: TDTMPointDef;
dtmSubPoints: Array [0..4] of TDTMPointDef;
TempTDTM: TDTM;
i,tole:integer;

begin
i:= FindRoadColor;

if not(i > 0) then
begin
i := 7241098;
tole:= 30;
end;

dtmMainPoint.x := 994;
dtmMainPoint.y := 244;
dtmMainPoint.AreaSize := 0;
dtmMainPoint.AreaShape := 0;
dtmMainPoint.Color := i;
dtmMainPoint.Tolerance := tole;

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;
dtmSubPoints[0].AreaSize := 0;
dtmSubPoints[0].AreaShape := 0;
dtmSubPoints[0].Color := i;
dtmSubPoints[0].Tolerance := tole;

dtmSubPoints[1].x := 986;
dtmSubPoints[1].y := 258;
dtmSubPoints[1].AreaSize := 0;
dtmSubPoints[1].AreaShape := 0;
dtmSubPoints[1].Color := i;
dtmSubPoints[1].Tolerance := tole;

dtmSubPoints[2].x := 987;
dtmSubPoints[2].y := 227;
dtmSubPoints[2].AreaSize := 0;
dtmSubPoints[2].AreaShape := 0;
dtmSubPoints[2].Color := i;
dtmSubPoints[2].Tolerance := tole;

dtmSubPoints[3].x := 991;
dtmSubPoints[3].y := 216;
dtmSubPoints[3].AreaSize := 0;
dtmSubPoints[3].AreaShape := 0;
dtmSubPoints[3].Color := i;
dtmSubPoints[3].Tolerance := tole;

dtmSubPoints[4].x := 984;
dtmSubPoints[4].y := 267;
dtmSubPoints[4].AreaSize := 0;
dtmSubPoints[4].AreaShape := 0;
dtmSubPoints[4].Color := i;
dtmSubPoints[4].Tolerance := tole;

TempTDTM.MainPoint := dtmMainPoint;
TempTDTM.SubPoints := dtmSubPoints;
Result := AddDTM(TempTDTM);
end;

More Detail. NAW!

i:= FindRoadColor;

We make the variable (i) equal to the function in SRL, FindRoadColor, which attempts to autocolor the road.

if not(i > 0) then
begin
i := 7241098;
tole:= 30;
end;

Because findRoadColor might occasionally fail, we check that i is bigger than 0. If not, we make i equal to a known road color, and make the tolerance 30.

If we do find the roadcolor, tolerance will be 0. Since when integer variables are initialized, they start out as 0.

dtmMainPoint.Color := i;
dtmMainPoint.Tolerance := tole;


Here, we make the color equal to FindRoadColor, and tolerance = tole.


Shortening DDTMs (With credits to Mixster)

Scroll up to the color DDTM, and see it again. Its a bit long, no?

What about this? This one is shortened considerably.

function DDTMOut: Integer;
var
dtmMainPoint: TDTMPointDef;
dtmSubPoints: Array [0..4] of TDTMPointDef;
TempTDTM: TDTM;
i,tole,t:integer;

begin
i:= FindRoadColor;

if not(i > 0) then
begin
i := 7241098;
tole:= 30;
end;

dtmMainPoint.x := 994;
dtmMainPoint.y := 244;

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;

dtmSubPoints[1].x := 986;
dtmSubPoints[1].y := 258;

dtmSubPoints[2].x := 987;
dtmSubPoints[2].y := 227;

dtmSubPoints[3].x := 991;
dtmSubPoints[3].y := 216;

dtmSubPoints[4].x := 984;
dtmSubPoints[4].y := 267;


for t := 0 to 4 do
with dtmSubPoints[t] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;

TempTDTM.MainPoint := dtmMainPoint;
TempTDTM.SubPoints := dtmSubPoints;
Result := AddDTM(TempTDTM);
end;

Moar Detail?

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;

Just sets the points like usual.

for t := 0 to 4 do
with dtmSubPoints[t] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;

Here's the nice part. For To Do loops will loop from x to y (For X to Y do) by adding 1 each time. Low returns the lowest in an array.

With ? Do will make stuff in a record things.
It's usually dtmSubPoints[t].AreaSize:= 0; right? But because I have a with dtmSubPoints[t] do, it shortens the whole thing considerably

So that's it!


There are a lot of ways to put the (D) in (D)DTMS. It all comes down to your creativity. But if I ever see another DDTM without the (D) in another membership application, I will personally open a vendetta to exterminate you.

DDTMs without the first D are just DTMs.

Thank You.

TheChineseMan
10-05-2008, 09:48 AM
simple yet effective tut nice lee even though i knew this rep++ will help alot of new ppl

EDIT: first post:)

EDIT2: also add more detail on like what ppl can do with the DDTMs and how they can be able to do that

Daniel
10-05-2008, 09:49 AM
Lol, nice tutorial.

People, look at this if you want to create a DDTM! If it's not Dynamic, a normal DTM is perfectly suitable instead of adding large amounts of code for nothing ;) It'll make your script less shorter and faster ;)

tank phobia
10-05-2008, 09:56 AM
But if I ever see another DDTM without the (D) in another membership application, I will personally open a vendetta to exterminate you.

DDTMs without the first D are just DTMs.


Nicely put :p

Thanks Lee after reading today how to create DDTM's I already understood that but since I have little scripting experience I was unsure what exactly they could be used for, I now have a good idea of some sorts of ways they can be used and might be using DDTM's in my future scripts (I will make sure they have the (D)!)

mixster
10-05-2008, 10:45 AM
May I suggest adding a for loop to the DDTM's to make them so much smaller - for some reason everyone thinks they have to be massive, but they don't Using your last as an example:

function DDTMOut: Integer;
var
dtmMainPoint: TDTMPointDef;
dtmSubPoints: Array [0..4] of TDTMPointDef;
TempTDTM: TDTM;
i,tole, x:integer;

begin
i:= FindRoadColor;

if not(i > 0) then
begin
i := 7241098;
tole:= 30;
end;

dtmMainPoint.x := 994;
dtmMainPoint.y := 244;
dtmMainPoint.AreaSize := 0;
dtmMainPoint.AreaShape := 0;
dtmMainPoint.Color := i;
dtmMainPoint.Tolerance := tole;

for x := Low(dtmSubPoints) to High(dtmSubPoints) do
begin
dtmSubPoints[0].AreaSize := 0;
dtmSubPoints[0].AreaShape := 0;
dtmSubPoints[0].Color := i;
dtmSubPoints[0].Tolerance := tole;
end;

dtmSubPoints[0].x := 994;
dtmSubPoints[0].y := 244;

dtmSubPoints[1].x := 986;
dtmSubPoints[1].y := 258;

dtmSubPoints[2].x := 987;
dtmSubPoints[2].y := 227;

dtmSubPoints[3].x := 991;
dtmSubPoints[3].y := 216;

dtmSubPoints[4].x := 984;
dtmSubPoints[4].y := 267;

TempTDTM.MainPoint := dtmMainPoint;
TempTDTM.SubPoints := dtmSubPoints;
Result := AddDTM(TempTDTM);
end;

Voila, shortened it by a lot and makes it so much easier to view the separate points :) Of course, you can put the for loop at the end and have the points before, but it makes no difference in the end.

Otherwise, nice tutorial, though in your try except end DDTM procedure, if it finds the DTM then it doesn't free it which is very naughty of you!

ShowerThoughts
10-05-2008, 10:49 AM
Your answer on this question -->How To Put the (D) in (D)DTMs.

Can be shortened to You have DTM then add a D to DTM so DDTM :p

Not funny, but nice tutorial ;)

Lee Lok Hin
10-05-2008, 11:13 AM
for x := Low(dtmSubPoints) to High(dtmSubPoints) do
begin
dtmSubPoints[0].AreaSize := 0;
dtmSubPoints[0].AreaShape := 0;
dtmSubPoints[0].Color := i;
dtmSubPoints[0].Tolerance := tole;
end;


You mean


for x := Low(dtmSubPoints) to High(dtmSubPoints) do
begin
dtmSubPoints[x].AreaSize := 0;
dtmSubPoints[x].AreaShape := 0;
dtmSubPoints[x].Color := i;
dtmSubPoints[x].Tolerance := tole;
end;

or (HaH! Even shorter.)


for x := Low(dtmSubPoints) to High(dtmSubPoints) do
with dtmSubPoints[x] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;



But yup, will add that now.

Tniffoc
10-05-2008, 11:56 AM
Nice Tut! Too many people just read the DDTM tutorials and then don't realize how much better you can make a DDTM. I.E. Me a few days ago........ :).

ShowerThoughts
10-05-2008, 12:22 PM
for x := Low(dtmSubPoints) to High(dtmSubPoints) do
with dtmSubPoints[x] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;


ha this 0,0000000001 milisecs faster :p


x := Low(dtmSubPoints);
H := (dtmSubPoints) ;
for X to H do
with dtmSubPoints[x] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;

Should work :p

Lee Lok Hin
10-05-2008, 12:38 PM
IRC LOG:


<mixster> for X to H do
<mixster> HE NEEDS TO DIE
<mixster> I seriously think I should report for crimes against Scarmanity
<LeeLokHin> That is actually faster, by a bit. He just forgot to add high(dtmSubPoints);
<mixster> no, for x to h!
<mixster> no, for x to h!
<mixster> please tell me you don't see the flaw in that
<LeeLokHin> for X to H do
<mixster> for X := 0 to H do
<LeeLokHin> I think he edited.
<LeeLokHin> Oh
<LeeLokHin> F*ck me
<LeeLokHin> Mind me quoting this?
<mixster> nope, go for it
<mixster> Only fail by me was leaving it as 0 instead of changing to x, but he needs to be put down for that :s
<mixster> Do you know what would rock as a Best Before date?
<mixster> Just having it say "yesterday"
<Narcle> "Scarmanity" rofl
<mixster> It made me cringe and shout racial slurs at him


x := Low(dtmSubPoints);
H := High(dtmSubPoints) ; //Mistake Hear!
for t:= X to H do //Mistake Hear Too!
with dtmSubPoints[t] do
begin
AreaSize := 0;
AreaShape := 0;
Color := i;
Tolerance := tole;
end;


/me hands epic phail to Hermpie :p




EDIT:

The more I think about it, the more I think that i'm an idiot, and mixster is correct.


LeeLokHin: Please add "mixster thinks this is fastest" then have just the for loop with "for x := 0 to 4 do" followed by the with begin end crap

triax
11-05-2008, 07:01 AM
It's just kinda hard for me to try becouse I'm barely used to DTM's. Cool guide though...

comanche01
11-06-2008, 10:06 PM
ty very much

Flight
06-05-2013, 04:16 AM
This is really neat, I found it informative. But why is it considered outdated? I'm bumping this.