How To Put the (D) in (D)DTMs.

DDTMs =Deformable Template ModelsDynamic

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.

SCAR Code: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.

SCAR Code: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.

SCAR Code:function DDTMOut: TPoint;

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

SCAR Code:repeat

Tole:= Tole + 5;

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

SCAR Code: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.

SCAR Code: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.

SCAR Code: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.

SCAR Code: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.

SCAR Code: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.

SCAR Code: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!

SCAR Code:i:= FindRoadColor;

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

SCAR Code: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.

SCAR Code: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.

SCAR Code: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?

SCAR Code:dtmSubPoints[0].x := 994;

dtmSubPoints[0].y := 244;

Just sets the points like usual.

SCAR Code: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 usuallyright? But because I have a with dtmSubPoints[t] do, it shortens the whole thing considerablySCAR Code:dtmSubPoints[t].AreaSize:= 0;

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.