It is currently Sat Aug 22, 2020 3:32 pm


All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Mini optimization in Raycast
PostPosted: Fri Jan 13, 2012 5:59 pm 

Joined: Fri Jan 13, 2012 5:19 pm
Posts: 1
In the doRaycast() function.
instead of :
Code:
float minx = floorf(x1), maxx = minx + 1.0f;
float tx = ((x1 > x2) ? (x1 - minx) : (maxx - x1)) / std::abs(x2 - x1);
float miny = floorf(y1), maxy = miny + 1.0f;
float ty = ((y1 > y2) ? (y1 - miny) : (maxy - y1)) / std::abs(y2 - y1);
float minz = floorf(z1), maxz = minz + 1.0f;
float tz = ((z1 > z2) ? (z1 - minz) : (maxz - z1)) / std::abs(z2 - z1);

float deltatx = 1.0f / std::abs(x2 - x1);
float deltaty = 1.0f / std::abs(y2 - y1);
float deltatz = 1.0f / std::abs(z2 - z1);

rewrite to :
Code:
float deltatx = 1.0f / std::abs(x2 - x1);
float deltaty = 1.0f / std::abs(y2 - y1);
float deltatz = 1.0f / std::abs(z2 - z1);
   
float minx = floorf(x1), maxx = minx + 1.0f;
float tx = ((x1 > x2) ? (x1 - minx) : (maxx - x1)) * deltatx; //  1.0f / std::abs(x2 - x1)
float miny = floorf(y1), maxy = miny + 1.0f;
float ty = ((y1 > y2) ? (y1 - miny) : (maxy - y1)) * deltaty;
float minz = floorf(z1), maxz = minz + 1.0f;
float tz = ((z1 > z2) ? (z1 - minz) : (maxz - z1)) * deltatz;


Not a bug deal, just less abs call, less subtraction, and overall less divisions.
Sorry I have the "divide desease" for several months. :cry:
Make me feel like Adrian Monk...


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mini optimization in Raycast
PostPosted: Fri Jan 13, 2012 10:29 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Thanks for this, I've made the change to Git master. I wouldn't update though as there's a few breaking changes going on at the moment.

The code was based off Christer Ericson's book 'Real Time Collision Detection' so I was naturally curious about why he doesn't make this optimisation. But after looking back at it, it's because he supports traversing over a grid where each cell can be bigger than one (it's for spatial subdivision).

In our case the cell size is always one, which enables the optimisation you suggest.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net