POV-Ray : Newsgroups : povray.advanced-users : A macro I'm proud of I think (public Beta) Server Time
29 Jul 2024 22:31:18 EDT (-0400)
  A macro I'm proud of I think (public Beta) (Message 11 to 17 of 17)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Tor Olav Kristensen
Subject: Re: A macro I'm proud of I think (public Beta)
Date: 3 May 2001 20:19:31
Message: <3AF1F567.A679D7E2@hotmail.com>
Dan Johnson wrote:
> 
> A while ago I was working on a big complicated image, and I wanted
> to make a precise plane intersection shape far away from the origin.
> I quickly realized that although I knew exact locations, planes are
> described around the origin.  It only takes three points to define 
> a plane, but for use in plane intersections there is an inside and
> an outside, so I decided to use a fourth point to decide witch side
> of the plane is inside.  I thought I could write a macro to do it in
> 15 minutes, I was very wrong.  I think it finally works now, but I
> thought that a few days ago before I found some huge bugs.  If you
> find any bugs or have suggestions, please comment.
>...

I think that the macro below will do the same.


#macro FourPointPlane(p1, p2, p3, pInside)

  #local vA = p2 - p1;
  #local vB = p3 - p1;
  #local vC = pInside - p1;
  #local vN = vcross(vA, vB);
  #local STP = vdot(vN, vC); // Scalar Triple Product of vA, vB and vC
  #if (STP = 0) // Check for coplanar vectors
    #error "\nMacro FourPointPlane: All points lie in the same plane.\n"
  #else
    #local vN = (STP > 0 ? -1 : 1)*vnormalize(vN);
    plane { vN, vdot(p1, vN) }
  #end // if

#end // macro FourPointPlane


- But I haven't tested it much. Sorry.

I also have some suggestions for some of
your support macros if you are interested.


-- 
Best regards,

Tor Olav

mailto:tor### [at] hotmailcom
http://hjem.sol.no/t-o-k/tokpicts.html
http://www.crosswinds.net/~tok


Post a reply to this message

From: Dan Johnson
Subject: Re: A macro I'm proud of I think (public Beta)
Date: 4 May 2001 04:58:10
Message: <3AF3C0C5.BCD4AEFE@hotmail.com>
Tor Olav Kristensen wrote:

> Dan Johnson wrote:
> >
> > A while ago I was working on a big complicated image, and I wanted
> > to make a precise plane intersection shape far away from the origin.
> > I quickly realized that although I knew exact locations, planes are
> > described around the origin.  It only takes three points to define
> > a plane, but for use in plane intersections there is an inside and
> > an outside, so I decided to use a fourth point to decide witch side
> > of the plane is inside.  I thought I could write a macro to do it in
> > 15 minutes, I was very wrong.  I think it finally works now, but I
> > thought that a few days ago before I found some huge bugs.  If you
> > find any bugs or have suggestions, please comment.
> >...
>
> I think that the macro below will do the same.
>
> #macro FourPointPlane(p1, p2, p3, pInside)
>
>   #local vA = p2 - p1;
>   #local vB = p3 - p1;
>   #local vC = pInside - p1;
>   #local vN = vcross(vA, vB);
>   #local STP = vdot(vN, vC); // Scalar Triple Product of vA, vB and vC
>   #if (STP = 0) // Check for coplanar vectors
>     #error "\nMacro FourPointPlane: All points lie in the same plane.\n"
>   #else
>     #local vN = (STP > 0 ? -1 : 1)*vnormalize(vN);
>     plane { vN, vdot(p1, vN) }
>   #end // if
>
> #end // macro FourPointPlane
>
> - But I haven't tested it much. Sorry.
>
> I also have some suggestions for some of
> your support macros if you are interested.
>
> --
> Best regards,
>
> Tor Olav
>
> mailto:tor### [at] hotmailcom
> http://hjem.sol.no/t-o-k/tokpicts.html
> http://www.crosswinds.net/~tok

You have the right idea, my beta program seems to be working lets optimize
it.  Your error catching test is simple, and clever.  I checked all the
math, and it should effectively catch all the bugs my functions do.  It
isn't very helpful to anyone using it, as to what specific thing, or things
they did wrong though.  I would like to use it as a condition for running
the other tests so that it only tries to find out what is wrong, after it
has found out that something is wrong.  I don't understand how the sign of
the triple scalar product is used to find out if the normal needs to be
corrected.  The line " plane { vN, vdot(p1, vN) } " is very similar to what
I get when I write out the formula I used and simplified.  An important
difference is that when I simplified I needed an abs around the result of
the vdot.

Having written my own version I have a fairly comprehensive test scene.  I
decided to replace my macro with your macro in the scene, and see what
happens.  Immediately it got a user error directive.  I commented out all
the function calls, but the first, and it still happened.  Now I knew that
the very first function call was getting an error.  I did the math by hand,
and found out that the value of STP should be -4 not 0.  I dug further, and
found out that the value of vN was <0,0,0>.  This shouldn't be, now I am
very confused.  Here is the test scene maybe you can make sense of it.

#include "colors.inc"
#include "polyhedra.inc"
#include "finish.inc"

#macro ThreePointPlane(P1,P2,P3)
        FourPointPlane(P1,P2,P2,<0,0,0>)
#end

/*
#declare Cube1 = <1,1,1>; // Cube vertexes
#declare Cube2 = <1,1,-1>;  // from polyhedra.inc
#declare Cube3 = <1,-1,1>;
#declare Cube4 = <1,-1,-1>;
#declare Cube5 = <-1,1,1>;
#declare Cube6 = <-1,1,-1>;
#declare Cube7 = <-1,-1,1>;
#declare Cube8 = <-1,-1,-1>;
*/

intersection {
                ThreePointPlane (Cube5,Cube3,Cube2)
                ThreePointPlane (Cube8,Cube3,Cube2)
                ThreePointPlane (Cube2,Cube5,Cube8)
                ThreePointPlane (Cube8,Cube5,Cube3)
                FourPointPlane (<0,1,1>,<0,0,0>,<0,-1,1>,<-1,0,0>)
                texture {pigment {rgbt <1,0,0,0>}} finish {
                ambient .1
                diffuse 1
                reflection .15
                specular 0
                roughness .6
}}
object{Cube_edges(1,.1) texture { finish {
                ambient .1
                diffuse .4
                reflection .25
                specular .6
                roughness .1
}pigment{Green}}}
//object {FourPointPlane (<2,2,1>,<3,3,1>,<4,7,1>,<0,0,20>) pigment
{CornflowerBlue}}
sphere{Cube1,.2 pigment{Blue}}

light_source {<100,30,-100> rgb 2}
camera {location <6,8,-12> look_at 0 angle 20}


--
Dan Johnson

http://www.geocities.com/zapob


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: A macro I'm proud of I think (public Beta)
Date: 4 May 2001 14:06:35
Message: <3AF2EF5D.1193AEE@hotmail.com>
Dan Johnson wrote:
>...
> Having written my own version I have a fairly comprehensive test scene.  I
> decided to replace my macro with your macro in the scene, and see what
> happens.  Immediately it got a user error directive.  I commented out all
> the function calls, but the first, and it still happened.  Now I knew that
> the very first function call was getting an error.  I did the math by hand,
> and found out that the value of STP should be -4 not 0.  I dug further, and
> found out that the value of vN was <0,0,0>.  This shouldn't be, now I am
> very confused.  Here is the test scene maybe you can make sense of it.
>....


Your macro below has an error.

>....
> #macro ThreePointPlane(P1,P2,P3)
>         FourPointPlane(P1,P2,P2,<0,0,0>)
> #end
>....

You're not using P3. (P2 is written twice.)

I'm in a hurry right now, so I don't have 
time for more testing or to try and explain.
Maybe later ...

But if you're going to do further tests,
then I'm interested in hearing about your
results.


-- 
Best regards,

Tor Olav

mailto:tor### [at] hotmailcom
http://hjem.sol.no/t-o-k/tokpicts.html
http://www.crosswinds.net/~tok


Post a reply to this message

From: Dan Johnson
Subject: Re: A macro I'm proud of I think (public Beta)
Date: 4 May 2001 16:30:30
Message: <3AF46305.E4FDE7CA@hotmail.com>
Tor Olav Kristensen wrote:

> Your macro below has an error.
>
> >....
> > #macro ThreePointPlane(P1,P2,P3)
> >         FourPointPlane(P1,P2,P2,<0,0,0>)
> > #end
> >....
>
> You're not using P3. (P2 is written twice.)

Doh!  Such a headache over a typo.  Well now everything works.  I'm sure I will
spend a few hours figuring out why.  Much more enjoyable than figuring out why
something doesn't work.  Then I will obfuscate it.  I am learning more about math
this way than reading the book.  Jon wrote me directly telling me about the
project he is working on that uses my macro.  When he is done maybe he will be
willing to share his code, and we can test this version on it to see if it gets
the same results.  Thanks for pointing out my error, I'm sure it would have taken
me another hour before I spotted it.




>
> I'm in a hurry right now, so I don't have
> time for more testing or to try and explain.
> Maybe later ...
>
> But if you're going to do further tests,
> then I'm interested in hearing about your
> results.
>
> --
> Best regards,
>
> Tor Olav
>
> mailto:tor### [at] hotmailcom
> http://hjem.sol.no/t-o-k/tokpicts.html
> http://www.crosswinds.net/~tok




--
Dan Johnson

http://www.geocities.com/zapob


Post a reply to this message

From: MPunk3
Subject: Re: A macro I'm proud of I think (public Beta)
Date: 7 May 2001 08:10:09
Message: <3af690a1$1@news.povray.org>
If you're interested in this subject, please see my
follow-up and pics in povray.binaries.images.


"Dan Johnson" <zap### [at] hotmailcom> wrote in message
news:3AF324DD.D3777CCE@hotmail.com...
> See povray.binarys.images for reply
> Dan
>


Post a reply to this message

From: Dan Johnson
Subject: Beta 2
Date: 7 May 2001 21:16:06
Message: <3AF89A84.AE68E4FF@hotmail.com>
Ok Tor Olav now that I understand the math I am certain your code works perfectly I
simplified it enough to remove 3 local variables.  I then added descriptive error
messages without halting rendering.  There aren't any support functions now.  Unless
someone thinks of a way to make it better this is the final version.

////////////////////////////////////////////////////////////////
// Four_point_plane  Beta 2  May 7 2001
//
// Original Idea Dan Johnson
// Code Dan Johnson, and Tor Olav Kristensen
//
// In povray everything on one side of the plane is considered inside the plane
object
// P1, P2, and P3 are points on the plane, none of these points can be the same as
any other,
// and they can't be in a strait line.  Inside point is a control point to determine
witch side of the plane
// is considered inside. Tor Olav posted much simpler code to do the same job a few
days after me.
// I believe that I can prove it always works.  The plane generating code is based on
Tor Olav's with a
// few minor simplifications, and the error trapping is mostly mine.
// Dan Johnson


#macro Four_point_plane(P1, P2, P3, I)// P1-3 = points on plane I = inside point

  #local Cross = vcross(P2 - P1, P3 - P1);
  #local STP = vdot(Cross, I - P1); // Scalar Triple Product
  #if (STP = 0) // Check for invalid data
    //determine specific error, or errors.
    #local Errors = "\nFour_point_plane did not create a plane because of the
following bad imput\n"
    #local S1 = (vdot((P1 = P2),<1,1,1>) = 3);  // same checks
    #local S2 = (vdot((P2 = P3),<1,1,1>) = 3);
    #local S3 = (vdot((P3 = P1),<1,1,1>) = 3);
    #if (S1 + S2 + S3 > 0)
     #if (S1) #local Errors = concat (Errors,"Point1 = Point2\n")#end
     #if (S2) #local Errors = concat (Errors,"Point2 = Point3\n")#end
     #if (S3) #local Errors = concat (Errors,"Point3 = Point1\n")#end
    #else
     #if (abs(vdot(vnormalize(P2-P1),vnormalize(P3-P2))) = 1)
      #local Errors = concat (Errors,"All points on same line\n")
     #else
      #local Errors = concat (Errors,"Inside point is on plane\n")
     #end
    #end
    #warning Errors
    sphere {<0,0,0>,0}  //Whatever called Four_point_plane is expecting an object.
  #else
    #local N = (STP > 0 ? -1 : 1)*vnormalize(Cross);// normal vector
    plane { N, vdot(P1, N) }
  #end // if

#end // macro Four_point_plane

//  Three_point_plane assumes that the origin is in the object.
#macro Three_point_plane(P1,P2,P3)
        Four_point_plane(P1,P2,P3,<0,0,0>)
#end


--
Dan Johnson

http://www.geocities.com/zapob


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Beta 2
Date: 14 May 2001 20:27:17
Message: <3B0077E8.76846FC@online.no>
Dan Johnson wrote:
> 
> Ok Tor Olav now that I understand the math I am certain your code works perfectly I
> simplified it enough to remove 3 local variables.  I then added descriptive error
> messages without halting rendering.  There aren't any support functions now.  Unless
> someone thinks of a way to make it better this is the final version.

Your macro looks much better now. And it also 
seems that you have got a better understanding 
of vector dot products now ;)

But I still have some more suggestions for you.
=> See my comments below.


-- 
Best regards,

Tor Olav

mailto:tor### [at] hotmailcom
http://hjem.sol.no/t-o-k/tokpicts.html
http://www.crosswinds.net/~tok


>...
> #macro Four_point_plane(P1, P2, P3, I)// P1-3 = points on plane I = inside point
> 
>   #local Cross = vcross(P2 - P1, P3 - P1);
>   #local STP = vdot(Cross, I - P1); // Scalar Triple Product
>   #if (STP = 0) // Check for invalid data
>     //determine specific error, or errors.
>     #local Errors = "\nFour_point_plane did not create a plane because of the
> following bad imput\n"

>     #local S1 = (vdot((P1 = P2),<1,1,1>) = 3);  // same checks
>     #local S2 = (vdot((P2 = P3),<1,1,1>) = 3);
>     #local S3 = (vdot((P3 = P1),<1,1,1>) = 3);


To "check" if any two points are equal you could write:

#local EqualAB = (vlength(PB - PA) = 0);


>     #if (S1 + S2 + S3 > 0)

You may want to try this:

#if (S1 | S2 | S3)


>      #if (S1) #local Errors = concat (Errors,"Point1 = Point2\n")#end
>      #if (S2) #local Errors = concat (Errors,"Point2 = Point3\n")#end
>      #if (S3) #local Errors = concat (Errors,"Point3 = Point1\n")#end
>     #else
>      #if (abs(vdot(vnormalize(P2-P1),vnormalize(P3-P2))) = 1)

You could simply check if the length of the 
normal vector is 0. (If the length of your 
"Cross" vector is greater than 0, then a
proper normal vector was formed.)


>       #local Errors = concat (Errors,"All points on same line\n")
>      #else
>       #local Errors = concat (Errors,"Inside point is on plane\n")
>      #end

But what if Point1, Point2 and Point3 are 
all on a line, while the "inside point" is 
not on the same line ?


>     #end
>     #warning Errors
>     sphere {<0,0,0>,0}  //Whatever called Four_point_plane is expecting an object.
>   #else
>     #local N = (STP > 0 ? -1 : 1)*vnormalize(Cross);// normal vector
>     plane { N, vdot(P1, N) }
>   #end // if
> 
> #end // macro Four_point_plane
> 
> //  Three_point_plane assumes that the origin is in the object.
> #macro Three_point_plane(P1,P2,P3)
>         Four_point_plane(P1,P2,P3,<0,0,0>)
> #end


Further notes:

If the STP test fails while the normal vector
is found to be "sound" (I.e.: The length of
your "Cross" vector is greater than 0), then
you can deduce from this that the "inside 
point" must also lie within the the plane 
defined by the 3 points: P1, P2 and P3. 

To see this, you may form a vector from the
"inside point" to any one of the other three
points. Now it might be easier to understand
that the only way the dot product between 
this vector and the plane's normal vector 
can be orthogonal to each other (I.e.: their
dot product equals zero) is if the "inside 
point" lies within that plane.



Also note that if you calculate the point-
to-point vectors several times, then this 
will probably slow down your macro bitte-
littegranne.


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

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