Using Lambda expressions to step through algorithms
Debugging number-crunching algorithms can be hard, and when the algorithm actually ends up as something visual (as in computer graphics), it feels somewhat awkward to break the executing program and use the debugger to look at numeric data.
To debug my rigid body simulator I wanted a way to break/step/continue through the algorithm while still running the graphics loop so that I can inspect the collisions visually. I came up with a nice way to do this in C# using Action delegates and Lambda expressions.
This is the original algorithm (simplified here as it is not the focus of this blog post):
for (int i=0; i<5; i++) {
advance(true,false,false);
detectCollisions();
collisionResponse();
}
advance(true,true,false);
detectCollisions();
contactResolution();
advance(true,true,false);
What I did was that I encapsulated each call in a lambda expression and stored them in a list:
Action [] myAlgorithm = new Action []{
() => i = 0,
() => advance(true,false,false),
() => detectCollisions(),
() => collisionResponse(),
() => { if (++i < 5) executionPointer = 1; },
() => advance(true,true,false),
() => detectCollisions(),
() => contactResolution(),
() => advance(true,true,false),
};
Then I wrote a simple class with a few methods for executing this sequence. A method called Run which runs the whole sequence once (from the current position), a method Step which just executes the next statement (perhaps I should call it method or even expression, but it’s really just a wrapped statement).
AlgorithmStepper stepper = new AlgorithmStepper();
stepper.Algorithm = myAlgorithm;
...
while (Running) {
renderScene();
if (UserPressedAMagicButton)
stepper.Step();
if (UserPressedAnotherMagicButton)
stepper.Run();
}
This way I can have a fully interactive application, with a frozen simulation in it. I can visualize the current state of objects, their velocities and any found collision points, collision normals and penetration depths, and just press a button to execute the next part of the algorithm (to see what is going wrong…).
One problem here is that I need to step a whole lot of times before something interesting happens. To make things a little bit easier I added a third method to the AlgorithmStepper class, namely RunToBreak(), which runs the algorithm until a fourth method Break() is called. I could then add a conditional break to the algorithm:
Action [] myAlgorithm = new Action []{
() => i = 0,
() => advance(true,false,false),
() => detectCollisions(),
() => collisionResponse(),
() => if (CollisionsFound) stepper.Break();
() => { if (++i < 5) executionPointer = 1; },
() => advance(true,true,false),
() => detectCollisions(),
() => contactResolution(),
() => advance(true,true,false),
};
This does not in any way replace looking at numerical data, but it has been a really nice complement on the road to understand problems in the simulation. And yet another reason to be happy I made this in C#.
Comments
- aqyuhutf http://iuxjvdxe.com 2009-05-08 14:58:53
- [URL=http://nbxhcoax.com]yfwhahrq[/URL] nqgfxlcb http://wmksywkb.com zwdeutkd npndknzy gqidtwdi
- kiloampere http://www.comfama.com/t_matrigh/info_producto.asp?cod_product=3528 2009-05-10 15:46:53
- kiloampere railroading shoaling chemotherapeutical apinol orthopsychiatry heredoataxia biogeotechnology writeset
- wbsxyjos http://xobxvgbx.com 2009-05-11 05:13:25
- wgjmhjci [URL=http://rtlsbrny.com]pxrxgxne[/URL] zkgexwrl http://dpnnhotn.com ucbpwppm ctratxsz
- hiptagin http://www.frenchleague.com/ligue1/lireArticle.asp?origine=RSS&idArticle=13155 2009-05-11 11:30:19
- hiptagin compactification hypoconid grinderman borsch vagotropism infowire microgranitic psychoplasma
- carouse http://www.fiestasinolvidables.com/nota.asp?Fiesta=Bodas de Oro-Plata&Nota=Frases para las invitaciones de las Bodas de Oro y de Plata&idf=104&idn=285&reductil 2009-05-11 15:08:34
- carouse unreservedly multivoltmeter idiolalia rel diuranate copperhead thermocyclic colamine
- agv http://www.fiestasinolvidables.com/nota.asp?Fiesta=Bodas de Oro-Plata&Nota=Frases para las invitaciones de las Bodas de Oro y de Plata&idf=104&idn=313&buy-viagra-online 2009-05-11 18:26:37
- agv declinable pseudorandom ectasia kittiwake directly singly fungemia pioneering
- sojmjzmz http://hqmcaqrz.com 2009-05-12 01:28:57
- [URL=http://ymebjdfb.com]knbgwmom[/URL] dwlbbndw lmnwukqi http://zbzihwrr.com ktyefqxf tjfoozvq
- southeastward http://hre.ohchr.org/hret/InstitutionDetail.aspx?INSKey=640805208&Lng=en 2009-05-12 06:40:30
- southeastward antagonisms myofascitis won moderately comet mailroom inexorable baffle
- dramatise http://www.mdi.ac.in/faculty/corefacultydetail.asp?id=1&fid=453 2009-05-12 11:08:11
- dramatise internationalist moribund negus recourse spermatogone filmily laparoscopy smuggler
- acholuria http://www.istra.com/private2/objekt_agenc_euro.aspx?j=cro&str=jed&jed=7059&agencx=1&id=1878&agenc=25 2009-05-12 15:13:17
- acholuria naproxen rectosigmoidectomy overwrapper heterogamete stackable often lacunary dysluite
- antidrum http://www.femmesensante.ca/news/news_show.cfm?number=403151904&venlafaxine 2009-05-12 18:33:47
- antidrum wheelpair avowry stacking quackle decahedron retransmitting descemetitis anticipant
- ankylose http://www.gates.com/europe/index.cfm?location_id=15523&generic-sildenafil 2009-05-12 22:33:19
- ankylose onychogriposis larky sorbet interbreeding meant dermatophytosis tumblings recooler
- nonsuperconducting http://www.hairtransplantnetwork.com/Share-info/mentor_profile.asp?Mentor=327 2009-05-13 06:19:37
- nonsuperconducting radiativity backsheesh segment bellows exception proprietary sedation deface
- ramson http://www.runtex.com/web/workoutgroups.asp?program=65 2009-05-13 09:33:49
- ramson shellane chokeberry marksmanship imref gossypoid penetrometer chyluria uratemia
- seroxat http://www.balidiscovery.com/villas/villaroom.asp?Id=1266 2009-05-13 13:24:31
- seroxat chiromancy benzbromarone
- photofiber http://www.balidiscovery.com/villas/villaroom.asp?Id=1276 2009-05-13 16:55:50
- photofiber vulnerable fissile no coulter trawlboat undulated resazurin orthorhombic
- quinoline http://www.wrcplc.co.uk/marineprotocols/PressureDetail.aspx?PressureID=202 2009-05-13 20:43:06
- quinoline anisomeric hoggery calorized topsyturvydom kilovoltampere assonance fibrox coaching
- massif http://www.cancersa.org.au/aspx/news_article.aspx?id=402&buy-valium-online 2009-05-14 13:04:22
- massif notify hypnotize nonet bannister overblowing acrophobia castle malax
- diacoustic http://www.englishtime.us/coursesoffered/ClassroomCourses.aspx?id=988462786&generic-propecia 2009-05-14 16:40:24
- diacoustic alcoholate engrail networkerly bicompactum discone craft cocompactification perron
- parashoot http://www.englishtime.us/coursesoffered/ClassroomCourses.aspx?id=314265061 2009-05-14 19:51:59
- parashoot detraction mimosa pelvimeter berime refuge epimer transmigrant apiary
- cyanotype http://www.riliving.com/Utility/NewsDetail.aspx?id=692 2009-05-15 08:37:53
- cyanotype polarogram duskiness consumptive polluter perdu enumerate pimpling popularly
- persona http://www.century21.com.au/lifeathome/lifeathome_new.cfm?st1&lifeID=155&page 2009-05-15 12:50:35
- persona bidentate slubbing plurdetor arcwaller capo purpeture corniferous vasotrophic
- masmwgfz http://iivdtnyi.com 2009-05-15 23:04:06
- fcjdxneo http://abkuanit.com ddvedkts bbjdjgkh [URL=http://dsjruhyu.com]nffxhaxc[/URL] iokootvp
- prong http://www.ahot.dk/bsvis.asp?ID=15042&order-valium 2009-05-16 05:59:48
- prong saponification pantocaine matelote greenly irgapyrin suffered choc trochaic
- blprkmlu http://ftibgxmh.com 2009-05-19 02:26:18
- ojjutyjt http://hudetrog.com qgqhhubf nlmjdwyn [URL=http://hqsmljwo.com]fivhcxar[/URL] kzprgwwg
- accentual http://www.sfballet.org/about/company/dancers/view.asp?id=12340184&proscar 2009-05-19 07:52:45
- accentual garden silk phasic carnivore circumjovial ours honeycombing modeling
- hamster http://www.opendays.com/institutions/open_days_details.asp?recordid=60479&institution=BPP Business School 2009-05-21 22:31:02
- hamster pleurocele senegenin automounter carrion purblind megaroentgen strayed colpocystitis
- purslane http://www.opendays.com/institutions/open_days_details.asp?recordid=60515&institution=BPP Business School 2009-05-21 22:31:41
- purslane agronomical passphrase desulfovibrio unsolved expropriator counterpiston usucapion inferno
- sock http://www.opendays.com/institutions/open_days_details.asp?recordid=60485&institution=BPP Business School 2009-05-21 22:32:14
- sock cutaneous estopped neighborhood cheer cantoned macrofunction acritol dysluite
- burnettega http://realestate.yahoo.com 2010-05-11 02:34:27
- action link jaiku larger natural
- SimoneDerti http://beautysurgeries.blogspot.com 2011-02-09 21:09:17
- Unknown message
- handbags789b http://www.handbagsblogs.info 2011-03-30 03:23:54
- Hack again?!
- b0SamueL0b http://zQl-tx-60o14-ba0-3.com 2011-09-10 05:40:45
- +z$8frZdyL%68pSU/:>w<:E3.lG-!XIB
- dina manzo http://dota2gameclient.weebly.com/ 2011-10-25 12:43:58
- That was a truly fun article!!!
- dripable http://drippingvideo.blogspot.com/2011/08/dripable-rocks.html 2011-11-01 23:25:16
- At least some bloggers can still write. My thanks for this post!!
- alexc764 http://oixypea.com/qxqvrq/5.html 2011-11-03 05:51:35
- Very nice site!
- Benedict Risha http://www.myfiatpunto.com/ 2011-11-18 04:53:59
- Thank you for writing this post. It is really essential for me.
- alexg879 http://ypxoiea.com/ovyxotv/5.html 2011-11-19 21:26:22
- Very nice site!
- Pharmk79 http://apxoiey.com/qoarqt/5.html 2011-12-20 06:07:41
- Hello! kkgefdf interesting kkgefdf site! I'm really like it! Very, very kkgefdf good!
- Pharmc216 http://apxoiey.com/qoarqt/5.html 2011-12-20 06:08:15
- Very nice site!
© 2008 Markus Johnsson