POV-Ray : Newsgroups : povray.unofficial.patches : Question about applying a user defined function into density functions : Re: Question about applying a user defined function into density functions Server Time 9 Aug 2024 05:03:18 EDT (-0400)
 Re: Question about applying a user defined function into density functions
 From: Grassblade Date: 9 Oct 2007 16:40:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.470be6b3f0b2498d44bccc1e0%40news.povray.org",
"headline": "Re: Question about applying a user defined function into density functions",
"dateCreated": "2007-10-09T20:40:00+00:00",
"datePublished": "2007-10-09T20:40:00+00:00",
"author": {
"@type": "Person",
"name": "Grassblade"
}
}
"Antonio Ferrari" <nomail@nomail> wrote:
> I'd like that the density is a function
> of the minimal distance of V from the external surface of O: for V internal,
> density[V] = density_function(minimal_distance(O,V)). In particular I'd like
> a function the make the density greater when moving toward the interior of
> O, where the increment could be linear, exponential, logarithmic, etc.
>
> Is there a function for calculating the minimal distance of an internal
> point V from the contour of O?
>
If the surface is continuous, I'd imagine that there is such a function.
> <Snip>
> What I'd want isn't a generic Your_Function with parameters x, y or z. What
> I want is a density function with only a parameter d, where d is the
> distance of the vector V=<x,y,z> from the contour of the surface, V inside
> the object. It's obvious that there are some preconditions to impose...
>
Supposing you do have the generic Your_Function (x,y,z) that returns d, all
you have to do is to invert said function. Assuming it can be inverted,
that is. ;-)
> <Snip>
> mdistance(OBJECT_IDENTIFIER, V, nr_samples, [VECTOR_IDENTIFIER])
>
> Parameters are:
>
> - An object ID
> - A vector V
> - samples is an integer greater than 0
> - VECTOR_IDENTIFIER is an optional vector ID
>
> What the function do: it generates nr_samples in random directions trying to
> find the minimal distance of the point V from the surface of the Object. At
> every iteration uses the code of trace and vlength function for calculating
> the distance from the Object. At the end it returns the minimal distance.
Personally, I dislike brute force approaches, but if that's ok for you who
am I to argue. I would rather use a smallish number of samples, pick the
smallest, then use a optimisation algorithm like steepest descent (very
easy to code) or nonlinear conjugate gradient to find the "true" minimum in
a small number of iterations. Optimisation algorithms are local in nature,
so using an adequate number of samples to get in the vicinity of the global
minimum is paramount.
> If
> a vector_identifier is given, the point on the surface with minimal distance
> from V is stored into it.
>
> What TO DO:
>
> 1 - Check if V is inside or outside the object -> if outside, always returns
> 0;
> 2 - Improve random number generator
Mersenne twister is what you want. Available in a number of programming
languages:
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/eversions.html
> 3 - Add some optimization passes. At first step a rough minimal distance is
> found; at second step further rays are traced in a space closed to what is
> found at step 1.
> 4 - Some check, for some particular / complex objects (I dont't know how to
> do it...)
>
> I haven't tested the code yet, because I'm out of Linux and with Cygwin I've
> no time to play.
>
> Now my question: could such a function be used in density functions? For
> example could I write a density function like A+B*mdistance(x, y, z)^C,
> with A, B, C real values ????
You *can* use functions to specify densities. See
http://news.povray.org/povray.advanced-users/thread/%3Cweb.46a617d4ef6472d8f75a90cb0@news.povray.org%3E/
The big problem I see with this method is that you'd have a blocky
definition to your media (at least I *think*). It might work if your
density voxels are of a size with the pixels of the final image, but that
would almost certainly mean really long parsing time to get all the
distances.
The best solution IMO is to make use of isosurfaces' threshold.
```