 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
I've implemented a Minimum Distance algorithm that estimates the minimum
distance from any point and the surface of an object.
The reason I implemented it is so that I could use it inside an "internal"
function that I can then use in the definition of (for example) and isosurface.
The algorithm does a really short "brute force", checking vectors in a bunch of
directions from the point, and finding the shortest distance in any of those
directions (if there is actually a "hit" in any of those directions), in order
to get an initial estimate, then uses simulated annealing to improve the
estimate.
I have a few questions that are really specific to the development team:
Is there already functionality implementing a minimum distance to an arbitrary
object that I missed? I looked for one, but haven't actually asked directly.
Is this something that could work its way back into the povray code? I was
thinking that I would add a class to implement the simulated annealing in a
general way, a minimum distance implementation, and "internal" function to call
it from POV STL, and then probably a minimum distance pattern that would work
similar to the existing object pattern, except it wouldn't have sharp edges at
the object boundary. Where would be the preferred place to stick the simulated
annealing class? Is that the kind of thing that would go under Core/Math?
I am still playing with it a bit, but am thinking that if it might be useful for
other folks, I should do it "right" so I can submit it over on github.
I've included an attachment showing some of the testing I have been doing while
using the function to define an isosurface. In the two images, the object in the
center is just an object rendered directly, on the left is the object rendered
as an isosurface using a pigment pattern as the defining function (of course it
is terrible, due to the infinite gradient at the object surface), and on the
right the the object rendered as an isosurface using the minimum distance
function with a very short annealing period (BTW, the function flips the sign to
negative for points inside the object, so you have a clean 0.0 threshold at the
surface of the object).
Post a reply to this message
Attachments:
Download 'test_both.png' (118 KB)
Preview of image 'test_both.png'

|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"jceddy" <jce### [at] gmail com> wrote:
> Is there already functionality implementing a minimum distance to an arbitrary
> object that I missed? I looked for one, but haven't actually asked directly.
I don't believe there is - accessible from the SDL, anyway. I know that in the
past, people have asked about ways to make pigment patterns based on the
distance of an object from the camera.
Can't find those threads at the moment...
> I am still playing with it a bit, but am thinking that if it might be useful for
> other folks, I should do it "right" so I can submit it over on github.
You are correct - it would be a useful thing to have.
>on the
> right the the object rendered as an isosurface using the minimum distance
> function with a very short annealing period (BTW, the function flips the sign to
> negative for points inside the object, so you have a clean 0.0 threshold at the
> surface of the object).
So, I'm curious, and confused.
This sounds a lot like ray-marching, mixed with signed distance functions.
And how are you taking an algorithm and having that work with a function {} ?
I don't know what you're exactly doing, or the details of how simulated
annealing work, but I'd likely approach this (naively, wrongly) by using the
bounding box and trace (), and only scanning a grid of points corresponding to
the resolution of the render, arranged on a plane just behind the bounding box
and perpendicular to the camera to look_at vector.
And that sounds like a macro more than a proper function...
- Bill
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"Bald Eagle" <cre### [at] netscape net> wrote:
> "jceddy" <jce### [at] gmail com> wrote:
>
> > Is there already functionality implementing a minimum distance to an arbitrary
> > object that I missed? I looked for one, but haven't actually asked directly.
>
> I don't believe there is - accessible from the SDL, anyway. I know that in the
> past, people have asked about ways to make pigment patterns based on the
> distance of an object from the camera.
> Can't find those threads at the moment...
>
> > I am still playing with it a bit, but am thinking that if it might be useful for
> > other folks, I should do it "right" so I can submit it over on github.
>
> You are correct - it would be a useful thing to have.
>
> >on the
> > right the the object rendered as an isosurface using the minimum distance
> > function with a very short annealing period (BTW, the function flips the sign to
> > negative for points inside the object, so you have a clean 0.0 threshold at the
> > surface of the object).
>
> So, I'm curious, and confused.
> This sounds a lot like ray-marching, mixed with signed distance functions.
> And how are you taking an algorithm and having that work with a function {} ?
>
>
> I don't know what you're exactly doing, or the details of how simulated
> annealing work, but I'd likely approach this (naively, wrongly) by using the
> bounding box and trace (), and only scanning a grid of points corresponding to
> the resolution of the render, arranged on a plane just behind the bounding box
> and perpendicular to the camera to look_at vector.
>
> And that sounds like a macro more than a proper function...
>
>
> - Bill
After cruising around the forums a bit, I realize that I probably should have
posted this in programming instead of general.
I actually changed the povray source code to add a new built-in function type,
and patter type to call directly. The implementation is in the C++ source and
just exposed to SDL via a couple of new parser directives.
On my phone ATM, so will probably put something over in programming later
(unless there is a way for a forum admin to just move this thread or something).
I'm currently experimenting with some other options besides simulated annealing
for improving the initial estimate...simulated annealing is good for general
purpose because it helps avoid finding locally optimal solutions when it is not
the globally optimal one. For a lot of purposes though, a quick simple march of
rays could be good and fast enough, or even doing what I am doing now and then
just a simple gradient descent around the initial estimate to find the nearest
locally optimal solution.
It could be that a minimum_distance implementation could come with a few options
to allow the user to choose between different methods to balance
quality/performance and pick what works best with what they are doing.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 7/6/22 21:32, jceddy wrote:
> I actually changed the povray source code to add a new built-in function type,
> and patter type to call directly. The implementation is in the C++ source and
> just exposed to SDL via a couple of new parser directives.
>
> On my phone ATM, so will probably put something over in programming later
> (unless there is a way for a forum admin to just move this thread or something).
So... I think you are saying you've added a new f_mindist() function
with the appropriate hook in functions.inc - or a stand alone hook in
the SDL? And also a new pattern calling the same mindist code?
I have an interest in the former work more than the latter as have on my
wish list to expose an f_trace() capability in my povr branch play which
is roughly equivalent to the SDL's trace() function. I'd like to see how
you handled the internal code hooks for tracing, inside tests (surface
handling?) and pointers to the objects to trace.
Official development is stalled again since mid 2021, so while a pull
request would be one handy way I could look at your code, it's unlikely
to be acted on any time soon - in any 'official' capacity.
---
Random thoughts
---------------
Over the years there has been a fair bit of play with "proximity
patterns." The work breaking into two approaches. One sampling by
tracing rays and the other by sampling density via inside/out shape
sampling(a) at various distances.
(a) - Inside testing gets slow for complex SDL built shapes because the
inside test bypasses all the bounding search mechanisms used while
tracing. I have an open question in my head as to whether trace()
bypasses the bounding mechanisms too?
There is a proximity pattern in Jerome's hgpovray38 branch IIRC. Long on
my list to look over his implementation, but, I've never gotten to it...
There is in all v3.8 branches a potential pattern hook for all shapes.
Today it's implemented only for blobs(b) and the isosurface(b1) itself.
Extending it to other simple shapes like spheres would be easy, but it's
not been done. I think because it's hard to see the benefit when you can
represent simpler shapes directly with functions for isosurfaces and
these simpler functions can be used as patterns already. Using
'patterns' in isosurface functions comes with significant overhead - and
in the official POV-Ray releases there are bugs and usage issues too.
(b) - There are continuity issues with the blob implementation itself
which handicaps blob potential pattern use (blob use itself in fact).
(b1) I removed the capability to use this potential pattern with
isosurfaces in my povr branch because working with the incoming
isosurface functions directly will always be better. Further, supporting
the isosurface potential pattern introduced new keywords and overhead
for all isosurfaces. I felt it not worth the added complexity or run
time cost.
Truly knowing 'the' mindist for all 3D shapes is likely impossible to
any reasonable cost(c). This of course says nothing about whether a
mindist functionality is useful - a somewhat fuzzy one would be.
(c) - So long as 2D the internal point list representations are
standardized/cleaned this can be done by various direct algorithms - as
happens in VLSI design rule checking tooling. New distance functions to
linear segments represented by point lists could be useful - especially
if the point list can be pre-processed for fast mindist determinations
after parsing. For small numbers of segments this can be done in any
recent POV-Ray release. I've played too with DF3 based methods for large
numbers of 'walls' (maze images from some years back) but it's a clunky
approach.
Anyhow, I'll stop rambling.
I'm interested in what your trying for possible use/adaptation with
stuff I'm trying. :-)
Bill P.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> So... I think you are saying you've added a new f_mindist() function
> with the appropriate hook in functions.inc - or a stand alone hook in
> the SDL? And also a new pattern calling the same mindist code?
I actually implemented it as a "special" function, the way the pattern function
is implemented. So in the scene it looks like:
#declare fn = function { minimum_distance { ObjectIdentifier } };
> I have an interest in the former work more than the latter as have on my
> wish list to expose an f_trace() capability in my povr branch play which
> is roughly equivalent to the SDL's trace() function. I'd like to see how
> you handled the internal code hooks for tracing, inside tests (surface
> handling?) and pointers to the objects to trace.
I'm sure you could do a trace function similar to the way I did the
minimum_distance one. I am currently working on learning the parser a bit better
so I can more easily come up with a general set of arguments. I was thinking I'd
like to be able to do something like:
#declare fn = function {
minimum_distance {
ObjectIdentifier
method brute_force
quality 2
}
}
or similar, and then possible implement brute_force, simulated_annealing, and
gradient_descent methods with appropriate arguments
> Official development is stalled again since mid 2021, so while a pull
> request would be one handy way I could look at your code, it's unlikely
> to be acted on any time soon - in any 'official' capacity.
I had seen that development had been done as recently as 2021, which gave me
hope that it was moving forward again! XD
I have been using POV long enough that I am used to the punctuated equilibrium
model of development. This is the second time I had tried to look into the
source, and the previous time was I believe before the C++ implementation (there
was a before C++ time, right? I'm not just imagining it?) and I had no idea what
was going on. I am having a lot more success navigating it now.
> There is in all v3.8 branches a potential pattern hook for all shapes.
> Today it's implemented only for blobs(b) and the isosurface(b1) itself.
> Extending it to other simple shapes like spheres would be easy, but it's
> not been done. I think because it's hard to see the benefit when you can
> represent simpler shapes directly with functions for isosurfaces and
> these simpler functions can be used as patterns already. Using
> 'patterns' in isosurface functions comes with significant overhead - and
> in the official POV-Ray releases there are bugs and usage issues too.
I actually did my first pass at this implementation by creating a
minumum_distance pattern, and then using that inside a function. As you allude
to here, that was not satisfactory...my code is still messy, though, and I have
"helper" functions in the pattern class that I am calling from the built-in
function. I need to clean all that up, but will probably work on that today and
push it up to my fork in github.
One thing I am curious about, that I haven't dug into yet, is if I can get more
information about the object being passed to the function and possible enable
performance improvements based on that. I started down this path because I have
a mesh object I am working with that I want this functionality for...in the case
where the "top level" object is a mesh, and I could get access to the mesh data,
I have a feeling I could improved this quite a bit for that specific use case.
> Truly knowing 'the' mindist for all 3D shapes is likely impossible to
> any reasonable cost(c). This of course says nothing about whether a
> mindist functionality is useful - a somewhat fuzzy one would be.
Yes, the performance of finding the minimum_distance with *very* good precision
slows things down tremendously. Been doing a lot of testing, though, and it
seems that in a lot of cases you can get a "good enough" solution without it
being *too* painful (of course that is subjective).
For a bunch of objects I've been testing on, just shooting rays out uniformly
45-degrees in polar coordinates (so like 26 ray samples) gets you nearly as good
a solution as a "smarter" method that might still end up taking longer to get
you like a .01% improvement in reality.
> I'm interested in what your trying for possible use/adaptation with
> stuff I'm trying. :-)
I'll work on this more today, and be back later with a link to my github fork.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> I'll work on this more today, and be back later with a link to my github fork.
My fork is over here: https://github.com/jceddy/povray
If you search for "minimum" you are going to find most of my changes.
You can create a minimum distance function with:
#declare fn = function { minimum_distance { object } };
The minimum distance pattern allows a little more control over the simulated
annealing step, but I am currently working on additional changes to expose all
of the relevant tweakable parameters.
At any rate, if you take a look at how I ma using Find_Intersection in the
MinimumDistanceSolver class, that is basically the core trace() functionality
that you had mentioned you are interested in exposing via a function.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"jceddy" <jce### [at] gmail com> wrote:
> One thing I am curious about, that I haven't dug into yet, is if I can get more
> information about the object being passed to the function and possible enable
> performance improvements based on that. I started down this path because I have
> a mesh object I am working with that I want this functionality for...in the case
> where the "top level" object is a mesh, and I could get access to the mesh data,
> I have a feeling I could improved this quite a bit for that specific use case.
So, first off, this is very interesting, and exciting, that you have C++
development experience, and seem to understand enough to be able to write your
own source code function and plug it into POV-Ray.
I've been interested in doing that ever since I read about Leigh Orf's work on
weather data and super cells.
Several of us have bounced around the idea of having the ability to work with
convex hulls and point-clouds, and this seems to be very closely related to
that.
The idea I had was to have a POV-Ray object that would be simply a set of 3D
vectors, or points. The advantage being that one could apply things like matrix
transforms to the data without having to loop through all the points in SDL,
which would be very slow in comparison to a source code subroutine.
So, I'm thinking that for your mesh, you would have a set of vertices - so why
not treat those as a point cloud?
Then you could have a tool to just find the minimum Euclidian distance between
the camera and all of the points in the cloud. I ... feel ... there's some way
to optimize that so you don't have to check every single point. (?)
Then maybe there's a way to generate a true mesh object from the point cloud
object - which would be useful for folks who would like to take a bunch of
points and make a mesh out of its convex hull.
I'm light on details, and fast and loose with mostly everything, because I'm
pretty sure you already understand what I'm suggesting, and have your own ideas
about how best to approach all of this.
Finally, It would be great if you could provide a few posts detailing your
thought processes and some of the pertinent details about how to go about
writing on'es own function and getting POV-Ray to recognize and use it as a
built-in function. That would greatly help anyone who has enough programming
experience to do so, but doesn't have the time to invest in tracking down all of
the details on the learning curve of doing that for POV-Ray - it's not like any
of that is really documented anywhere, and Dr. Orf's explanation, although good,
seemed like it was brief and geared to people who might already know how to do
so.
Thanks, and I hope you have a lot of success with your project!
- Bill
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> So, I'm thinking that for your mesh, you would have a set of vertices - so why
> not treat those as a point cloud?
>
> Then you could have a tool to just find the minimum Euclidian distance between
> the camera and all of the points in the cloud. I ... feel ... there's some way
> to optimize that so you don't have to check every single point. (?)
>
In my case I already have a mesh. I'm the case of a simple mesh object there is
a straightforward way of computing minimum distance, but it would be slow unless
you do some fancy preprocessing to partition the vertices/faces.
Either way, in the case of an arbitrary object defined in SDL, I have not
figured out if there is a way in C++ to be able to go "hey, this is a mesh!" and
then get access to the underlying data.
>
> Finally, It would be great if you could provide a few posts detailing your
> thought processes and some of the pertinent details about how to go about
> writing on'es own function and getting POV-Ray to recognize and use it as a
> built-in function. That would greatly help anyone who has enough programming
> experience to do so, but doesn't have the time to invest in tracking down all of
> the details on the learning curve of doing that for POV-Ray - it's not like any
> of that is really documented anywhere, and Dr. Orf's explanation, although good,
> seemed like it was brief and geared to people who might already know how to do
> so.
>
Yeah, a "developer's guide to patching povray" would be great. 😁
I could possible post some details about how to add keywords and get them
processed, but it would only really be useful to folks that have a compiler and
know how to use it.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"jceddy" <jce### [at] gmail com> wrote:
> In my case I already have a mesh. I'm the case of a simple mesh object there is
> a straightforward way of computing minimum distance,
To quote my research advisor, editing my thesis, "Which is...?" ;)
> but it would be slow unless
> you do some fancy preprocessing to partition the vertices/faces.
>
> Either way, in the case of an arbitrary object defined in SDL, I have not
> figured out if there is a way in C++ to be able to go "hey, this is a mesh!" and
> then get access to the underlying data.
Indeed. I haven't looked too much at the parser and the tokenizing of things,
but I would imagine that every object defined in SDL would have to have some
unique identifier of one sort or another. It would be great to discover what
that is, and have an SDL-accessible table of numbered objects and associated
attributes.
I really have no top-down overview of how everything happens in order to go from
the simplest of scenes to defining the final rgb values of a pixel in a render.
It would be great to make a flowchart, perhaps with one of those automated
software-analyzing packages...
I guess just start by looking at:
povray/source/core/shape/mesh.cpp
and see if anything pops out at you.
> Yeah, a "developer's guide to patching povray" would be great. 😁
>
> I could possible post some details about how to add keywords and get them
> processed, but it would only really be useful to folks that have a compiler and
> know how to use it.
There are a few of us who have compilers, being on linux machines, but I usually
need a bit of hand-holding to build pov-ray from scratch and install it. ;)
I think it also comes down to a sort of chicken-and-egg thing, where people have
reason to use their compiler, so they don't know how, and so they don't do
anything with them.
I can kinda read _most_ of the source code, but there are certain notations and
other things going on that I'm still ignorant of, and therefore remain cryptic.
I've dabbled, and spent quite a while programming several arduino boards for
various different purposes. Which is "C++".
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
>
> Indeed. I haven't looked too much at the parser and the tokenizing of things,
> but I would imagine that every object defined in SDL would have to have some
> unique identifier of one sort or another. It would be great to discover what
> that is, and have an SDL-accessible table of numbered objects and associated
> attributes.
>
> I really have no top-down overview of how everything happens in order to go from
> the simplest of scenes to defining the final rgb values of a pixel in a render.
> It would be great to make a flowchart, perhaps with one of those automated
> software-analyzing packages...
>
> I guess just start by looking at:
> povray/source/core/shape/mesh.cpp
>
> and see if anything pops out at you
You can specify objects and object identifiers in SDL...there are ways to hook
into the parser so that you can get an "object pointer" when an object
definition of object identifier shows up in the scene. It may be that it's
possible given the object pointer to figure out that the top level object is a
mesh, and get the mesh data. I will definitely be looking into that at some
point. (My priority at the moment is cleaning up the code and implementing an
easy way to tweak calls to the minimum distance function easily in SDL).
It is unclear whether that helps with performance of a minimum distance
function, but one thing it *would* be useful for is testing, to evaluate how
accurate various methods are. Of course, I can also evaluate that stuff using
other methods, as well, since I know the mesh definition "offline" already.
>
> > Yeah, a "developer's guide to patching povray" would be great. 😁
> >
> > I could possible post some details about how to add keywords and get them
> > processed, but it would only really be useful to folks that have a compiler and
> > know how to use it.
>
> There are a few of us who have compilers, being on linux machines, but I usually
> need a bit of hand-holding to build pov-ray from scratch and install it. ;)
>
> I think it also comes down to a sort of chicken-and-egg thing, where people have
> reason to use their compiler, so they don't know how, and so they don't do
> anything with them.
>
> I can kinda read _most_ of the source code, but there are certain notations and
> other things going on that I'm still ignorant of, and therefore remain cryptic.
> I've dabbled, and spent quite a while programming several arduino boards for
> various different purposes. Which is "C++".
I could probably help anyone trying to patch/build/run on windows using visual
studio 2015, I don't know what wrinkles might be involves in doing it in Linux,
Mac, etc.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
hi,
"jceddy" <jce### [at] gmail com> wrote:
> ...
> > > Yeah, a "developer's guide to patching povray" would be great. 😁
> > >
> > > I could possible post some details about how to add keywords and get them
> > > processed, but it would only really be useful to folks that have a compiler and
> > > know how to use it.
that sounds like a useful article for the wiki, imo, including a "worked
example" perhaps? <https://wiki.povray.org/content/Main_Page>
regards, jr.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 7/7/22 17:57, jceddy wrote:
>
>> I'll work on this more today, and be back later with a link to my github fork.
>
> My fork is over here: https://github.com/jceddy/povray
>
> If you search for "minimum" you are going to find most of my changes.
>
> You can create a minimum distance function with:
>
> #declare fn = function { minimum_distance { object } };
>
> The minimum distance pattern allows a little more control over the simulated
> annealing step, but I am currently working on additional changes to expose all
> of the relevant tweakable parameters.
>
> At any rate, if you take a look at how I ma using Find_Intersection in the
> MinimumDistanceSolver class, that is basically the core trace() functionality
> that you had mentioned you are interested in exposing via a function.
>
>
Thanks. Is this minimum_distance work the only thing changed in that
fork - or is there (or will there be) other stuff changed in it too?
I implemented my own special function / new parser keyword in
'pattern_modifiers' a couple years back in povr. I wasn't thinking about
that approach for an f_trace(), but maybe it's a way to go...
In a post following this one you were talking about recognizing the mesh
object for special handling. It's a shape with its own internal
bounding. Tracing of rays(a) should be relatively fast with meshes - FWIW.
Bill P.
(a) - A thought that popped into my head yesterday is that if your ray
origin point for is very close to an object's surface you can get fooled
for mindist due the closest root/intersection being inside the minimum
intersection depth for the particular shape/surface - those
intersections are typically thrown out (or are never found depending
upon the solver used). In practice this issue might not come up that
much. The issue is tangled in the isosurface (~parametric~) accuracy
setting which is already useless beyond about 1e-6 as a soft rule. Most
folks run with an accuracy of something more like 1e-3 or a little less
and most shapes support smaller depths than that (blobs in official
POV-Ray versions are at a quite large 1e-2). An advantage of
"proximity/density" approaches based upon inside tests only is they
don't have this exposure to unstable ray traced results near surfaces.
Anyhow. Maybe this a worry for later should you see near surface
noise/issues with your ray traced approach.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> You can specify objects and object identifiers in SDL...there are ways to hook
> into the parser so that you can get an "object pointer" when an object
> definition of object identifier shows up in the scene. It may be that it's
> possible given the object pointer to figure out that the top level object is a
> mesh, and get the mesh data. I will definitely be looking into that at some
> point. (My priority at the moment is cleaning up the code and implementing an
> easy way to tweak calls to the minimum distance function easily in SDL).
>
> It is unclear whether that helps with performance of a minimum distance
> function, but one thing it *would* be useful for is testing, to evaluate how
> accurate various methods are. Of course, I can also evaluate that stuff using
> other methods, as well, since I know the mesh definition "offline" already.
>
So today, I verified that you can, indeed, get at the underlying Mesh data if
the object *is a* Mesh. So, in that case, given the Vertex/Triangle data in the
Mesh, along with the transformation matrix (also easy to get), you should be
able to implement a fast and accurate minimum-distance calculation by applying
the inverse of the object's transformation matrix to the test point (to get the
point in the same coordinates as the original mesh data...I THINK???), and then
doing some math.
Unfortunately the math required to do the calculation quickly is not all that
simple...it requires building a KD-Tree from the vertex data, doing some nearest
neighbor calculations, zeroing in on the nearest triangle, then calculating the
nearest point on that triangle.
The most exciting part of this is that I think that it may be possible to create
a fast and accurate minimum-distance function for each shape supported by pov
(with the possible exceptions of isosurface and blob), and then combine them
such that you can also calculate it for objects defined by CSG operations. (i.e.
for an intersection, calculate the minimum distance for each object in the
intersection first, then do a little logic based on whether the nearest point on
one object is inside the other, etc., to figure out which one to keep for the
intersected object). Been doing some drawings on napkins to suss it out.
Anyway...fun stuff.
My *specific* use case is for a simple Mesh object, maybe with some
transformations applied, so probably the next thing I will work on is pulling in
the necessary operations to support that (like a kdtree class, etc.)
Now need to figure out where in the code the classes fit...figuring out where to
put my code is the hardest part...
P.S. Last night/this morning I was dreaming up a plugin architecture that could
possibly allow developers to expose their own C++-backed functionality in
pov-ray without requiring them to actually learn to program pov source.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Thanks. Is this minimum_distance work the only thing changed in that
> fork - or is there (or will there be) other stuff changed in it too?
There should only be changes in there to support minimum_distance stuff, but
that is also potentially a bunch of general-purpose math stuff, as well.
> In a post following this one you were talking about recognizing the mesh
> object for special handling. It's a shape with its own internal
> bounding. Tracing of rays(a) should be relatively fast with meshes - FWIW.
Yes, I am figuring this all out today. :)
Given that for basically everything other than blob and isosurface there is some
more straightforward way to figure out the minimum distance, it might end up
being that a bunch of the stuff I did for estimating it might not be necessary.
Put a post about it before I realized there were other responses here already.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> So today, I verified that you can, indeed, get at the underlying Mesh data if
> the object *is a* Mesh. So, in that case, given the Vertex/Triangle data in the
> Mesh, along with the transformation matrix (also easy to get), you should be
> able to implement a fast and accurate minimum-distance calculation by applying
> the inverse of the object's transformation matrix to the test point (to get the
> point in the same coordinates as the original mesh data...I THINK???), and then
> doing some math.
Been working on the math. Added a couple of things to the mesh's data that can
be computed and cached as needed:
1) A kd-tree of the vertex data (makes it easy to quickly query the nearest mesh
vertex to a point).
2) A list of triangles for each vertex index. So ones you query the nearest
vertex, you can immediately get a list of triangles with that vertex.
Working on the code to check the minimum distance for each triangle touching
with the nearest vertex...that should be the minimum distance to the mesh.
The next thing will be handling transformations...I realized this morning that I
can't just apply an inverse transform to the point being tested...this won't
work for scale and shear transformations. I think I will need to actually
generate and cache a version of the mesh data with the object transformations
applied, and then use THAT mesh data for calculating minimum distance.
Anyway, one step at a time.
Also, I am thinking about changing "minimum_distance" to "proximity", as it
feels perhaps more succinct? Yea or nay?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> The next thing will be handling transformations...I realized this morning that I
> can't just apply an inverse transform to the point being tested...this won't
> work for scale and shear transformations. I think I will need to actually
> generate and cache a version of the mesh data with the object transformations
> applied, and then use THAT mesh data for calculating minimum distance.
>
So I got the code to calculated minimum distance using nearest-vertex from a
kd-tree working. At least I think so...I have some tiny artefacts that I think
might be a result of a bug in the code, but might also just be a result of the
kinds of tolerance issues that were mentioned above.
I added code to the underlying Mesh object in povray for calculating a
transformed copy of the vertex data, and verified that that seems to be
working...but now thinking there might be an easier solution. Like...
Can I inverse-transform the point being tested, run the minimum-distance
calculation against the original data to get a minimum-distance *VECTOR* from
the test point to the original mesh, then just apply the object's transformation
matrix to that vector before measuring its length?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 2022-07-10 15:27 (-4), jceddy wrote:
>
> So I got the code to calculated minimum distance using nearest-vertex from a
> kd-tree working. At least I think so...I have some tiny artefacts that I think
> might be a result of a bug in the code, but might also just be a result of the
> kinds of tolerance issues that were mentioned above.
>
> I added code to the underlying Mesh object in povray for calculating a
> transformed copy of the vertex data, and verified that that seems to be
> working...but now thinking there might be an easier solution. Like...
>
> Can I inverse-transform the point being tested, run the minimum-distance
> calculation against the original data to get a minimum-distance *VECTOR* from
> the test point to the original mesh, then just apply the object's transformation
> matrix to that vector before measuring its length?
It's been almost 40 years, so I don't remember why, but I seem to recall
learning in university that inverse transformations take a heavy toll on
numeric precision.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 7/10/22 15:27, jceddy wrote:
> Can I inverse-transform the point being tested, run the minimum-distance
> calculation against the original data to get a minimum-distance*VECTOR* from
> the test point to the original mesh, then just apply the object's transformation
> matrix to that vector before measuring its length?
I've never attempted a complete survey, but I'd say this the more common
approach inside POV-Ray's code for various calculations, but it's not
universal.
---
The numerical issues Cousin Ricky suggested as applying to the inverse
transform in particular(a) are very complicated on the whole. Especially
true in POV-Ray's code base where over the years numerically
inconsistent numerical approaches were employed.
The parsing stage today flattens 'some' transforms for some shapes.
Spheres and sphere sweeps come to mind. This kind of xfrm flattening I
believe was done to try and help performance(b). This flattening crashes
into the base v3.7/v3.8/v4.0 issue of pull request:
https://github.com/POV-Ray/povray/pull/358
Though, the discussion thread for that pull req goes on to look at
additional numerical issues; including fundamental limits at the end
related to loss of accuracy due the length of rays and polynomial
representations for the ray-surface intersections.
We can work to reduce the exposure to numerical issues in ray tracing,
but we can never eliminate them. More so the case due needing to use
fast floating point hardware for good performance.
Bill P.
(a) - The span and locality of references for many 'object'
representations are effectively better with inverse transformations
where the working numerical space is more robust. It can be less robust
too. However, I think this less common in practice. In other words, it
doesn't matter if the inverse transform / result-re-transform is
somewhat inaccurate for planet X, if all the local calculations on and
near planet X are more accurate. I'm lying much less here where planet X
exists in 'effective' isolation in the global numerical space of the
scene.
(b) - The issue pull 358 is trying to address is the unfortunate
introduction of a kind of min intersection depth filter in the "global"
numerical space in v3.7+ which results in the trimming or elimination of
shapes from ray-surface intersection calculations. In povr - where this
issue fixed in a way different than the actual #358 pull req - I've got
sphere point cloud test cases which now run 30-40% slower! Why? Well
povr isn't ignoring 30-40% of the ray->sphere calculations. Makes me
wonder what portion of the performance gains from the parser
flattening/eliminating of transforms was fools-gold when measured.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> It's been almost 40 years, so I don't remember why, but I seem to recall
> learning in university that inverse transformations take a heavy toll on
> numeric precision.
Well, based on my tests, this seems to be the case.
When I inverse-transform the test point, do a minimum distance vector to the
original mesh data, then transform that direction vector back to take the
length, the result is slightly off from the version where I keep a parallel
version of the mesh data around that I recalculate by transforming all the
vertices whenever the mesh's Trans changes.
The difference seems small, but the difference in result is stark when you
actually run the ray tracer. The version when I inverse-transform the test
point, then transform the result vector has all kinds of ugly holes. The version
where I carry around a transformed version of the mesh data looks a lot better,
but still has some small artefacts that aren't there using my old ray-sampling
method (although it is quite a bit faster). I think those remaining artefacts
might be due to simple precision issues, and I think I may be able to mitigate
them by introducing a "zero" factor that is a bit larger than zero (like 1e-11
or maybe a bit larger than that even) that I use in the calculation.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"jceddy" <jce### [at] gmail com> wrote:
> When I inverse-transform the test point, do a minimum distance vector to the
> original mesh data, then transform that direction vector back to take the
> length, the result is slightly off from the version where I keep a parallel
> version of the mesh data around that I recalculate by transforming all the
> vertices whenever the mesh's Trans changes.
What if instead of re-transforming the mesh data, you transform the reference
point to measure the length?
Then it's only ever one transform in either direction.
I'm only guessing, based on what I'm envisioning you doing...
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> What if instead of re-transforming the mesh data, you transform the reference
> point to measure the length?
> Then it's only ever one transform in either direction.
>
That is exactly what I tried, if I am understanding you.
I want to find the distance from point P to mesh M (which contains
vertex/triangle data as well as a transformation).
So I tried basically this:
P_inv = InverseTransformPoint(P, M->Trans)
N = NearestPoint(P_inv, M->Data)
V = N - P_inv // get the vector from P_inv to N
V_trans = TransformDirection(V, M->Trans)
If(Inside(P, M))
distance = -Length(V_trans)
Else
distance = Length(V_trans)
....and the result was much worse than finding the distance from point P to mesh
M (which contains transformed vertex/triangle data), but doing this:
N = NearestPoint(P, TransformAllVertices(M->Data, M->Trans))
V = N - P
If(Inside(P, M))
distance = -Length(V)
Else
distance = Length(V)
I mean, obviously the second one looks cleaner, as well, but it hides all the
code required to make sure M->TransformedData is there to use.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 7/11/22 17:25, jceddy wrote:
>> What if instead of re-transforming the mesh data, you transform the reference
>> point to measure the length?
>> Then it's only ever one transform in either direction.
>>
> That is exactly what I tried, if I am understanding you.
>
> I want to find the distance from point P to mesh M (which contains
> vertex/triangle data as well as a transformation).
>
> So I tried basically this:
>
>
> P_inv = InverseTransformPoint(P, M->Trans)
> N = NearestPoint(P_inv, M->Data)
> V = N - P_inv // get the vector from P_inv to N
> V_trans = TransformDirection(V, M->Trans)
>
> If(Inside(P, M))
> distance = -Length(V_trans)
> Else
> distance = Length(V_trans)
FYI. A chance the following inside_vector issue in play.
"Leaving mesh inside_vector untouched on transforms."
https://github.com/POV-Ray/povray/pull/361
IIRC this closed / not merged pull was last Christoph's version over my
original suggested fix on the developer mailing list. For reasons
unknown to me it never got merged into the official code base. I got
tired of re-basing branches on code updates and eventually closed it.
There is too - and also still - the issue that our documentation
suggests an axis aligned inside vector. This an unfortunate suggestion
given things like buildings are often axis aligned and any time the
inside vector is very close to perpendicular to a face's normal, the
mesh inside test becomes noisy.
Bill P.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> FYI. A chance the following inside_vector issue in play.
>
> "Leaving mesh inside_vector untouched on transforms."
> https://github.com/POV-Ray/povray/pull/361
>
That merge request changes the inside_vector test to use MInvTransPoint instead
of MInvTransRay followed by a Normalize, which makes sense to me.
In the version where I don't transform the mesh vertex data, I am doing
MInvTransPoint to tranform the test point, computing the minimum distance Vector
from the transformed test point to the original mesh data, then using
MInvTransDirection to transform that Vector using the mesh's Trans, then using
the length of the transformed vector as the distance.
The result obtained this way is a lot worse.
I wonder if there are cases where it would actually get a *wrong* answer, not
just loss of precision...such as the case where the object is scaled
non-uniformly. I think you can come up with an easy example where this is the
case where a "fat" ellipse that has a larger diameter in the x/y plane is scaled
such that it has a larger diameter in the x/z plane. Looking at the point at the
center of the ellipse, the minimum distance direction vector would switch from
<1, 0, 0> to <0, 1, 0> (for example).
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Well, I think I have decided that doing nearest-vertex calculation using a
kd-tree is *not* the best way to implement this for meshes.
I've attached a file comparing my original simulated-annealing run (using a very
short simulated annealing period after a very short brute force), which does
simple ray sampling, to the version that does a nearest-neighbor vertex search
on the mesh data using a kd-tree, and then finds the nearest point on any
triangles using that vertex.
The upper image is the simulated annealing one, and the lower image is the
nearest-neighbor mesh one. You can see that the simulated annealing approach
results in better output.
*ALSO*, the simulated annealing approach runs more quickly...it completed in
less than 60% of the time of the other one.
I think I am going to put those code changes on the back-burner, after making
sure I store them away somewhere, and then go ahead with another option I was
thinking about: doing a simple gradient descent search from an initial candidate
guess.
Post a reply to this message
Attachments:
Download 'test_compare.png' (252 KB)
Preview of image 'test_compare.png'

|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> *ALSO*, the simulated annealing approach runs more quickly...it completed in
> less than 60% of the time of the other one.
>
So after some more testing, I think the issue might actually be that the nearest
point algorithm using the kd-tree approach might be yielding suboptimal results,
which is requiring the isosurface code to do more samples, which is what is
actually slowing it down.
Basically, each sample is faster, but it's doing a lot more samples.
Either there is a bug in my kd-tree implementation, or there is something
fundamentally incorrect with the approach. Will have to do some more testing to
narrow down which it is.
It also occurs to me that I should be able to *somehow* use the mesh's bounding
box tree to implement a fast closest point query. Or maybe a parallel bounding
sphere tree?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Either there is a bug in my kd-tree implementation, or there is something
> fundamentally incorrect with the approach. Will have to do some more testing to
> narrow down which it is.
>
Looks like the kd-tree is not always returning the nearest mesh vertex to the
test point. I create a test that compares the minimum distance found using
simulated annealing vs. the one found using the nearest-vertex test, and then
added some code to find the nearest vertex by using a brute-force check of *all*
the vertices in the mesh, which I run in the case the simulated-distance found a
better solution than the kd-tree nearest-vertex approach.
I fairly quickly ran into a case where kd-tree is not returning the correct
vertex, so now need to figure out why that is the case.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
hi,
"jceddy" <jce### [at] gmail com> wrote:
> > Either there is a bug in my kd-tree implementation, ...
> ... using the nearest-vertex test, and then
> added some code to find the nearest vertex by using a brute-force check of *all*
thinking, re "nearest neighbour", perhaps the implementation that comes with the
GTS library "tools" may be of use? written by Sunil Arya & David Mount, "ANN:
Approximate Nearest Neighbours" (though they spell it without the 'u' :-)), I
have a version from May 2005 installed; sorry, no ref link.
regards, jr.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> thinking, re "nearest neighbour", perhaps the implementation that comes with the
> GTS library "tools" may be of use? written by Sunil Arya & David Mount, "ANN:
> Approximate Nearest Neighbours" (though they spell it without the 'u' :-)), I
> have a version from May 2005 installed; sorry, no ref link.
>
I can compare their kd-tree implementation to the one I have and try to figure
out if I'm dealing with a bug there.
Or rather if it helps me track down the but that is definitely there.
For example, I have a test point:
<-0.35977653631284895, 1.2000000000000000, -0.31591251373961082>
My kd-tree is returning this point as the nearest vertex:
<-0.22430989146232605, 0.65443259477615356, -0.19062881171703339>
The distance from the test point is 0.575926
*BUT*, when I brute force it, I find this is the actual nearest vertex:
<0.34019410610198975, 0.44190537929534912, 0.36512014269828796>
And the distance to that one is only 0.350649
That kind of error can definitely throw everything off.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
hi,
"jceddy" <jce### [at] gmail com> wrote:
> ...
> I can compare their kd-tree implementation to the one I have and try to figure
> out if I'm dealing with a bug there.
>
> Or rather if it helps me track down the but that is definitely there.
>
> For example, I have a test point:
>
> <-0.35977653631284895, 1.2000000000000000, -0.31591251373961082>
faces/triangles in GTS are made from edges, so, an extra "layer". agree re bug
hunt, running your points through both implementations should provide useful
info.
regards, jr.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> faces/triangles in GTS are made from edges, so, an extra "layer". agree re bug
> hunt, running your points through both implementations should provide useful
> info.
>
I found and fixed the kd-tree bug. So now it is correctly returning the nearest
vertex to the test point.
So my algorithm tests all triangles that use the nearest vertex, and then
calculates the minimum distance to each triangle, keeping the smallest of those.
It seems that this part is now not returning quite the correct minimum distance,
so I have to focus on that part next.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> So my algorithm tests all triangles that use the nearest vertex, and then
> calculates the minimum distance to each triangle, keeping the smallest of those.
>
Ah, I think I figured out the problem.
Instead of testing the distance to all triangles that share the nearest vertex,
what I *should* be doing is finding a bounding box around the test point based
on the distance to the nearest vertex (probably plus some tolerance), then test
all triangles that overlap that bounding box (which I should be able to use the
bounding box tree to find quickly).
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 7/13/22 3:28 PM, jceddy wrote:
>
>> Either there is a bug in my kd-tree implementation, or there is someth
ing
>> fundamentally incorrect with the approach. Will have to do some more t
esting to
>> narrow down which it is.
>>
>
> Looks like the kd-tree is not always returning the nearest mesh vertex
to the
> test point. I create a test that compares the minimum distance found us
ing
> simulated annealing vs. the one found using the nearest-vertex test, an
d then
> added some code to find the nearest vertex by using a brute-force check
of *all*
> the vertices in the mesh, which I run in the case the simulated-distanc
e found a
> better solution than the kd-tree nearest-vertex approach.
>
> I fairly quickly ran into a case where kd-tree is not returning the cor
rect
> vertex, so now need to figure out why that is the case.
>
There is something fundamentally wrong with your approach: the
closest approach is not necessarily on a face that includes the closest
vertex. If you look at the image I posted to p.b.i, when searching for
the closest point to the cross in the bottom, the closest vertex is the
red one, but the closest face is the orange one, which doesn't include
the red vertex…
Jerome
PS: Sorry I sent that to your email instead of the group.
--
mailto:jeb### [at] free fr
http://jeberger.free.fr
Diaspora*: https://framasphere.org/u/jeberger
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> There is something fundamentally wrong with your approach: the
> closest approach is not necessarily on a face that includes the closest
> vertex. If you look at the image I posted to p.b.i, when searching for
> the closest point to the cross in the bottom, the closest vertex is the
> red one, but the closest face is the orange one, which doesn't include
> the red vertex…
>
Yeah, I figured that out. :)
I am now finding the nearest vertex, then creating a bounding box around the
sample point that is large enough to contain the nearest vertex (plus a small
tolerance factor). Then I traverse the bounding box tree for the mesh and find
all leaves that overlap the sample bounding box, and calculate the minimum
distance for each leaf's triangle. The minimum of all of those is the minimum
distance to the mesh.
It performs fairly well (actually best performance of all my methods so far,
although still slows down quite a bit around detailed portions of the mesh where
it ends up sampling more triangles), and the output is great.
The output actually looks really good even if I turn max_gradient way down on
the isosurface (the actual max gradient is like 451, but I was turning it down
to 4 to get quick test renders).
Going to post some example outputs soon, then turn my attention to cleaning up
the code.
My current plan is to add a virtual method to object (probably called
"proximity") with a default behavior of ray sampling, or even just spitting out
a "not implemented for this object type" error, and then start overriding it for
the various object types.
Then will wire up an internal proximity function (I like proximity better than
"minimum distance"), and a proximity pattern type, as well, I think.
Oh, I also want to tinker around with doing an inverse translation on the sample
point and then translating the result vector back to measure it...I think it
might work the way I want now that I am not sampling the wrong set of triangles,
and if I can get it to work properly will allow me to remove a bunch of code I
added to Mesh to generate transformed versions of the vertex data and bounding
box tree.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Oh, I also want to tinker around with doing an inverse translation on the sample
> point and then translating the result vector back to measure it...I think it
> might work the way I want now that I am not sampling the wrong set of triangles,
> and if I can get it to work properly will allow me to remove a bunch of code I
> added to Mesh to generate transformed versions of the vertex data and bounding
> box tree.
I got the inverse-transform stuff to work correctly.
Cleaned up the code, removed all the simulated annealing stuff I had added,
changed "minimum_distance" to "proximity", implemented the Proximity() virtual
function in ObjectBase (which throws an error if it's called for objects that
don't have it implemented), implemented the Proximity() override for Mesh,
cleaned up pattern parser code for "proximity" pattern, and changed it to call
object->Proximity().
Proximity returns negated values for points inside the object.
The proximity pattern does not attempt to scale or clamp values, so it will look
like the values are just 0.0-1.0 repeating to the user...it is up to them to do
any required scaling that makes sense. This is something that we might want to
give some options for eventually, but it's outside the scope of what I am doing
ATM.
Updated my fork on github with all of the changes.
Next step (after some more testing) will be to implement Proximity() for other
object types.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Next step (after some more testing) will be to implement Proximity() for other
> object types.
Implemented Proximity() now for:
Mesh
Sphere
Box
Triangle
Plane
Disc
Cone (includes cylinder)
For non-solid objects like Triangle, Disc, Cone (if open), the proximity will
always be positive, since there is no well-defined "inside", so for testing
isosurfaces, I set threshold to 0.001 instead of 0.0, so it actually renders a
little thickness around the object.
I also implemented fn_nearest_point, that returns the nearest point calculated
by the Proximity() code.
So for proximity, you define the function like this:
#declare fn_X = function { proximity { shape } }
For nearest_point, you define the function like this:
#declare fn_N = function { nearest_point { shape } }
proximity functions return a double
nearest_point functions return a 3d vector position <x, y, z>
Attached a test render for an open cone, with some test points arrayed around
it.
Post a reply to this message
Attachments:
Download 'test_new.png' (91 KB)
Preview of image 'test_new.png'

|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Implemented Proximity() now for:
>
> Mesh
> Sphere
> Box
> Triangle
> Plane
> Disc
> Cone (includes cylinder)
Found a bug in the bounding box tree traversal code I wrote that made it so
Mesh::Proximity() was checking any triangle whose BBox overlapped the sample
BBox in the x direction, fixing it to narrow down by y and z directions as well
sped it up considerably. XD
Anyone want to hazard a methodology for implementing Proximity() for Blob,
Bicubic Patch, or perhaps for any of the CSG shapes? (or any shape I haven't
listed above for that matter?)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Anyone want to hazard a methodology for implementing Proximity() for Blob,
> Bicubic Patch, or perhaps for any of the CSG shapes? (or any shape I haven't
> listed above for that matter?)
I have done a few more of the easier ones (like Torus, etc.).
I ended up implementing the Bicubic Patch one using similar estimation logic as
is used to create the triangle mesh approximation. It is doing proximity to all
the triangles in the mesh and returning the nearest point overall...could be
slow for very detailed patches.
Been thinking about Blob, but haven't worked up the guts to attempt it yet. It
seems like you should be able to come up with something you can feed to the root
solver, but I'm still not clear on how to do it.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> Been thinking about Blob, but haven't worked up the guts to attempt it yet. It
> seems like you should be able to come up with something you can feed to the root
> solver, but I'm still not clear on how to do it.
For Height Field, I implemented something similar to what I did for Mesh, where
I put all of the points in a kd-tree, then I look at all points with x- or z-
distance within the distance of the sample point to the nearest height field
vertex, plus one, triangulate the field mesh defined by those points, and check
for nearest point on any of those triangles. (Unless, that is, the y-coordinate
of the sample point is above the max-y or below the min-y of the height field,
in which case I just use the nearest vertex as the nearest point on the height
field). I haven't handled water-level yet.
Here is the code:
DBL HField::Proximity(Vector3d &pointOnObject, const Vector3d &samplePoint,
TraceThreadData *threaddata) {
if (Data->kdTree == nullptr) {
return MAX_PROXIMITY_DISTANCE;
}
Vector3d transformedPoint = samplePoint;
Vector3d diff;
if (Trans != nullptr) {
MInvTransPoint(transformedPoint, transformedPoint, Trans);
}
DBL px = transformedPoint[X];
DBL py = transformedPoint[Y];
DBL pz = transformedPoint[Z];
Vector3d nearest = Data->kdTree->nearest_point(transformedPoint);
if (py < Data->min_y || py > Data->max_y) {
diff = nearest - transformedPoint;
}
else {
DBL dist = (nearest - transformedPoint).length();
DBL xmin = px - dist;
DBL zmin = pz - dist;
DBL xmax = px + dist;
DBL zmax = pz + dist;
int minX = max(0, int(xmin) - 1);
int maxX = min(Data->max_x, int(xmax) + 2);
int minZ = max(0, int(zmin) - 1);
int maxZ = min(Data->max_z, int(zmax) + 2);
dist = MAX_PROXIMITY_DISTANCE;
for (int z = minZ; z < maxZ - 1; z++)
{
for (int x = minX; x < maxX - 1; x++)
{
Vector3d p1(x, Data->Map[z][x], z);
Vector3d p2(x + 1, Data->Map[z][x + 1], z);
Vector3d p3(x + 1, Data->Map[z + 1][x + 1], z + 1);
Vector3d p4(x, Data->Map[z + 1][x], z + 1);
Vector3d trianglePt;
DBL test = NearestOnTriangle(trianglePt, transformedPoint, p1, p2, p3);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}
test = NearestOnTriangle(trianglePt, transformedPoint, p1, p3, p4);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}
}
}
diff = nearest - transformedPoint;
}
if (Trans != nullptr) {
MTransDirection(diff, diff, Trans);
}
pointOnObject = samplePoint + diff;
return diff.length();
}
I am getting really high max gradient values (resulting in isosurface holes)
around the highest and lowest points in the field, and I'm not clear on whether
that is due to a problem in the code, or maybe like due to the orientation of
the triangles relative to the camera ray there or something, so thought I would
ask if anyone could take a look at this and let me know if they see any glaring
issue.
Cheers,
Joe
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
> if (py < Data->min_y || py > Data->max_y) {
> diff = nearest - transformedPoint;
> }
After I posted this, I realized that this is not correct and might be causing
the issue, since it will be incorrect for sample points "above" or "below" flat
triangles at min_y or max_y.
I think I can still optimize for points above or below the y bounding planes. I
should only have to check triangles within one "step" of the nearest vertex.
So I modified the code thusly:
if (py < Data->min_y || py > Data->max_y) {
minX = int(nearest[X]) - 1;
maxX = int(nearest[X]) + 2;
minZ = int(nearest[Z]) - 1;
maxZ = int(nearest[Z]) + 2;
}
Here is the new version of the method I am currently testing:
DBL HField::Proximity(Vector3d &pointOnObject, const Vector3d &samplePoint,
TraceThreadData *threaddata) {
if (Data->kdTree == nullptr) {
return MAX_PROXIMITY_DISTANCE;
}
Vector3d transformedPoint = samplePoint;
Vector3d diff;
if (Trans != nullptr) {
MInvTransPoint(transformedPoint, transformedPoint, Trans);
}
DBL px = transformedPoint[X];
DBL py = transformedPoint[Y];
DBL pz = transformedPoint[Z];
Vector3d nearest = Data->kdTree->nearest_point(transformedPoint);
DBL dist = (nearest - transformedPoint).length();
DBL xmin = px - dist;
DBL zmin = pz - dist;
DBL xmax = px + dist;
DBL zmax = pz + dist;
int minX = max(0, int(xmin) - 1);
int maxX = min(Data->max_x, int(xmax) + 2);
int minZ = max(0, int(zmin) - 1);
int maxZ = min(Data->max_z, int(zmax) + 2);
if (py < Data->min_y || py > Data->max_y) {
minX = int(nearest[X]) - 1;
maxX = int(nearest[X]) + 2;
minZ = int(nearest[Z]) - 1;
maxZ = int(nearest[Z]) + 2;
}
dist = MAX_PROXIMITY_DISTANCE;
for (int z = minZ; z < maxZ - 1; z++)
{
for (int x = minX; x < maxX - 1; x++)
{
Vector3d p1(x, Data->Map[z][x], z);
Vector3d p2(x + 1, Data->Map[z][x + 1], z);
Vector3d p3(x + 1, Data->Map[z + 1][x + 1], z + 1);
Vector3d p4(x, Data->Map[z + 1][x], z + 1);
Vector3d trianglePt;
DBL test = NearestOnTriangle(trianglePt, transformedPoint, p1, p2, p3);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}
test = NearestOnTriangle(trianglePt, transformedPoint, p1, p3, p4);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}
}
}
diff = nearest - transformedPoint;
if (Trans != nullptr) {
MTransDirection(diff, diff, Trans);
}
pointOnObject = samplePoint + diff;
return diff.length();
}
Sorry for the SPAM...sometimes just putting my code in front of other people
forces me to thing more deeply about it and I can figure out issues that I
hadn't previously.
Cheers,
Joe
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|
 |