POV-Ray : Newsgroups : povray.off-topic : kd-trees rule! Server Time
6 Sep 2024 09:14:53 EDT (-0400)
  kd-trees rule! (Message 11 to 20 of 32)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Invisible
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 04:22:37
Message: <49953bdd@news.povray.org>
>> Hardcoded at the present. At some point I have to implement a parser but
>> that is not a fun thing to do and developing a good SDL takes time...
> 
> Yes of course, always the boring bit getting the UI working well, much 
> more fun to work on the actual algorithm!

 From where I'm sitting, building a parser is quite easy. Still, 
designing the language it's supposed to parse is another matter - 
depending on whether you want it to be Turing-complete. ;-)


Post a reply to this message

From: Warp
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 09:02:37
Message: <49957d7b@news.povray.org>
Kd-trees are indeed quite handy. They are eg. used in 3D games in
certain situations where triangles need to be rendered in a certain order.

  For example semi-transparent polygons have to be rendered from
farthest-from-the-camera to closest. This is because if you render them
in the wrong order, the result would not look correct (the blending of
overlapping polygons would be in the wrong order and thus the result
incorrect). Also the Z-buffer cannot be used for semi-transparent polygons.
(If you used the Z-buffer and you render the polygons in the wrong order,
some of the polygons would not be rendered at all, even though they should.)

  So assume that you have, for example 10000 blades of grass, each one with
a semi-transparent texture, and thus you need to render then from farthest
to closest, regardless of where the camera is. How to do this?

  You could, of course, try to sort the triangles at each frame. However,
not only would this be inefficient (after all, you only have about 16 ms
to do *everything* that is needed to render the next frame, and sorting
these polygons would certainly not be the only thing you have to do), but
there's a huge problem with trying to sort the triangles by depth: The
comparison function is far from trivial to implement. Determining which
of two triangles is in front of the other is a surprisingly complicated
task.

  In fact, there is no comparison function to sort triangles by depth in
the general case. This might sound like a surprising result, but it indeed
is so. Three triangles may occlude cyclically each other, in which case
they don't have a well-defined order by depth. In other words, triangle A
may partially occlude triangle B, which partially occludes triangle C,
which partially occludes triangle A. In terms of comparison, you basically
have the situation where A < B and B < C and C < A, in which case these
three elements have no well-defined order.

  With opaque triangles cyclical occlusion is not a problem because the
Z-buffer takes care of hidden surface removal on a per-pixel basis. However,
with semi-transparent triangles you can't use the Z-buffer (and get a
correct result).

  The only way to render correctly cyclically-occluding triangles is
to split at least one of the triangles into two parts and render these
two parts separately (in the proper order compared to the other triangles).
However, you really don't want to start splitting triangles on the fly in
a real-time rendering engine.

  It is possible to split triangles as a preprocessing step (even at the
level design stage) so that they can be ordered unambiguously. However,
using a sorting algorithm to sort the triangles at each frame is not
really something you want to do. Even if the triangles could be sorted
unambiguously by depth, the comparison is complicated and heavy, as well
as the entire sorting algorithm, which would be O(n log n).

  Step in the kd-tree.

  If you add all the semi-transparent triangles into a kd-tree, you will
be able to render the triangles from farthest to closest with a simple
O(n) traversal of the tree, regardless of where the camera is
with respect to the triangles. The comparison to be made at each node
is much simpler than the comparison which would need to be done for a
sorting algorithm.

  And that's not all. When adding triangles to the kd-tree, it's trivial
to detect cyclical occlusion situations, and perform the necessary triangle
splitting, as a preprocessing step. After this has been done, no more splits
are ever necessary, and everything can be rendered in a depth order
regardless of where the camera is (using a simple linear traversal of the
kd-tree).

  (Of course which triangles to split is not unambiguous, and algorithms
can be developed to minimize the number of triangles which need to be
split, but this is only related to the preprocessing. The rendering stays
the same.)

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 09:25:51
Message: <499582ef$1@news.povray.org>
Warp wrote:
>   Kd-trees are indeed quite handy. They are eg. used in 3D games in
> certain situations where triangles need to be rendered in a certain order.
> 
>   For example semi-transparent polygons have to be rendered from
> farthest-from-the-camera to closest.

Ah yes, the "painter's algorithm".

Incidentally, while playing CSS the game engine appears to very 
occasionally get this wrong. (I.e., distant objects appear *in front* of 
nearer ones - typically textures for things like explosions, window 
glass, etc.) The result looks very weird.

>   Step in the kd-tree.
> 
>   If you add all the semi-transparent triangles into a kd-tree, you will
> be able to render the triangles from farthest to closest with a simple
> O(n) traversal of the tree, regardless of where the camera is
> with respect to the triangles. The comparison to be made at each node
> is much simpler than the comparison which would need to be done for a
> sorting algorithm.
> 
>   And that's not all. When adding triangles to the kd-tree, it's trivial
> to detect cyclical occlusion situations, and perform the necessary triangle
> splitting, as a preprocessing step. After this has been done, no more splits
> are ever necessary, and everything can be rendered in a depth order
> regardless of where the camera is (using a simple linear traversal of the
> kd-tree).

Presumably if the triangles are allowed to move around you can't 
precompute the splits?


Post a reply to this message

From: Darren New
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 14:10:41
Message: <4995c5b1$1@news.povray.org>
Invisible wrote:
>  From where I'm sitting, building a parser is quite easy. Still, 
> designing the language it's supposed to parse is another matter - 
> depending on whether you want it to be Turing-complete. ;-)

Or one could use something like Lua or Tcl, which is specifically designed 
for putting extensibility parsers into existing programs, and just implement 
the bits you want and leave Tcl or Lua to do the subroutines, while loops, 
GUI interactions, and so on.

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

From: Darren New
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 14:16:29
Message: <4995c70d$1@news.povray.org>
triple_r wrote:
> Of course you have to compile and link over and over, but how bad is that, really?

Nah. Just embed a decent language and invoke it. Tcl might be OK for this, 
and I've heard good things about Lua. Python is hooked into a number of 
programs I've seen, including Blender.

In Blender, you write your python code to invoke Blender objects and frob 
around the internals how you like, then tell Blender "hey, here's your new 
scene."  Kind of like the way web languages have a "Request" object and a 
"Response" object, and you (say) assign things to response.cookies to set 
cookies at the browser.

REXX is another way to go - sort of like Windows COM only less Microsoft.

Or go with Windows COM or .NET if you're not worried about non-Windows machines.

> Upon reflection, maybe the answer is, "Pretty bad."  I think it's still the
> approach I'd take since I don't actually know how to write a proper parser.

Use one someone already built for you, with the intention that you use it as 
your parser.

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

From: Darren New
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 15:25:56
Message: <4995d754$1@news.povray.org>
triple_r wrote:
>  All without actually having to write or maintain a parser! 

Or you could have a really simple parser, or something that takes XML (or 
JSON or some other complex format for which there's already a standard and 
parsers), and build up your objects from that. Then your scripts are 
programs that output that. Just don't have any loops or variables or whatever.

I mean, it's not really hard to turn something like this into your library 
calls:

1 union
2 pigment 1.0 0.0 0.0
3 sphere 0.0 0.0 0.0 2
4 sphere 1.0 0.0 0.0 2
5 sphere 2.0 0.0 0.0 2
   ...
push 1 3
push 1 4
push 1 5
   ...

Basically, each call to your library is just the name of the routine 
followed by the arguments, and if the line starts with an integer, it's 
declaring something to go in that slot, and you refer to the slots later 
with numbers. Awful to parse by hand, but trivial to generate in a program, 
or even a macro language or scripting language or whatever.  Then you don't 
have to worry about parsing much, and you can write a different library for 
each language like you described that when you're done you say "output this 
to a file and feed it to the renderer."

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

From: triple r
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 15:35:00
Message: <web.4995d8ab55bca3e6ef2b9ba40@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Nah. Just embed a decent language and invoke it. Tcl might be OK for this,
> and I've heard good things about Lua. Python is hooked into a number of
> programs I've seen, including Blender.

Good advice.  This is what happens, though, when people with no business
programming try to solve common problems.  They reinvent the wheel, shape it
like a square, weld two tons of steel to it, and argue that, "Hey, it works."

> In Blender, you write your python code to invoke Blender objects and frob
> around the internals how you like, then tell Blender "hey, here's your new
> scene."

So with this method, do you have direct access via python to methods and
functions?  That sure would be useful.  I think I'll need to figure this out
for my soon-to-be-job anyway.

> Use one someone already built for you, with the intention that you use it as
> your parser.

Point taken.  Thanks for straightening me out on the subject.

 - Ricky


Post a reply to this message

From: Darren New
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 15:56:58
Message: <4995de9a$1@news.povray.org>
triple_r wrote:
> Good advice.  This is what happens, though, when people with no business
> programming try to solve common problems.  They reinvent the wheel, shape it
> like a square, weld two tons of steel to it, and argue that, "Hey, it works."

Yup.

> So with this method, do you have direct access via python to methods and
> functions?  That sure would be useful.  I think I'll need to figure this out
> for my soon-to-be-job anyway.

Yup.  Decent blender tutorial that'll give you the idea how it looks when done:

http://jmsoler.free.fr/didacticiel/blender/tutor/python_script00_en.htm
(Follow that to the next page for an actual Python example script.)

Mind, this is the sort of thing Tcl was built for. Indeed, Tcl was up to 
version 3 before you could run a program written entirely in Tcl. (I.e., 
version 3 was the first version of Tcl that came with main() :-) 
Unfortunately, Tcl never really got the reference-semantics thing going, so 
it's difficult to bolt useful OO constructs onto it. But it's pretty easy to 
build your own OO style constructs that Tcl can invoke.

Blender is also open source, so you can peek and see how they integrated the 
Python stuff.

>> Use one someone already built for you, with the intention that you use it as
>> your parser.
> 
> Point taken.  Thanks for straightening me out on the subject.

No problem. Just offering suggestions. Writing your own parser can be fun, 
too, but there's way too much reinvent-the-wheel in this world. Why make 
someone learn a new scripting language when you can say "use this, that you 
already know"? :-)  Indeed, the point of things like COM and .NET is "use 
whatever you already know to run this, including scripts like WSH and 
PowerShell."

There's also something called YAML (Yet Another Markup Language) which is 
semantically identical to JSON but it's readable. You might want to peek at 
that too, if JSON is sufficient.  (JSON is basically maps and arrays of 
strings and numbers, nested. I.e., data structures common to all the 
scripting languages, but surprisingly flexible for a lot of stuff.)

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

From: Darren New
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 16:03:27
Message: <4995e01f$1@news.povray.org>
Darren New wrote:
> (Follow that to the next page for an actual Python example script.)

Oh, and if you notice, it's simple enough you can probably generate it 
programmatically too. That is, if you wanted to use something other than 
Python, you could probably have your Haskell program output Python script 
that would brute-force its way though the construction of your stuff in a 
very straightforward manner. So you're not losing anything by not having a 
sophisticated scripting language, and it's probably not any harder than the 
"trivial scripting language" example I made up.

The whole blender docs are rooted at blender.org. The manual is broken up 
into pieces that makes it not *too* hard to find stuff, but makes a lot of 
back references, so I'm not sure the "scripting" section would stand on its 
own if you don't know the difference between (say) an Object and an ObData.

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

From: triple r
Subject: Re: kd-trees rule!
Date: 13 Feb 2009 16:10:00
Message: <web.4995e11b55bca3e6ef2b9ba40@news.povray.org>
"triple_r" <nomail@nomail> wrote:
> So with this method, do you have direct access via python to methods and
> functions?  That sure would be useful.  I think I'll need to figure this out
> for my soon-to-be-job anyway.

(Don't worry though.  I know you're thinking, "Wow, I hope they're not paying
him much," but the job has nothing to do with programming.)

 - Ricky


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.