POV-Ray : Newsgroups : povray.advanced-users : Solve edge case: is point inside/on object : Re: Solve edge case: is point inside/on object Server Time
23 Feb 2024 04:32:57 EST (-0500)
  Re: Solve edge case: is point inside/on object  
From: ingo
Date: 26 Dec 2018 11:19:44
Message: <XnsA9C4B04878A0seed7@news.povray.org>
in news:XnsA9C49AE1123ECseed7@news.povray.org ingo wrote:

> don't intersect with the sphere

looks less complex now, works for simple objects


#version 3.7;
#include "rand.inc"
#include "transforms.inc"

#global_settings{assumed_gamma 1.0 }
#default{pigment{rgb 1} finish{ ambient 0.2 diffuse 0.9 }} 
light_source{<1000,1000,-1000>, rgb 1}
camera{orthographic location <0,0.5,-2> look_at <0,0.5,0>}


#macro WhereOnObj(Obj, Point, TraceTarget)
  #if(VEq(Point, TraceTarget))
    #local Where = "on";
  #else
    // Simple test first
    #declare Inside = inside(Obj, Point);
    #if (Inside)
      #local Where = "inside";
    #else
      // Not inside but maybe on the surface of Obj.
      // Trace from extended Point to TraceTarget. 
      // If intersection == Point we're on the surface
      #local Norm = <0,0,0>;
      #local NewPoint = Point+(vnormalize((Point-TraceTarget))*0.1);
      #local Inter = trace(Obj, NewPoint, TraceTarget-NewPoint, Norm);
      #local Miss = VEq(Norm, <0,0,0>);
      #local OnSurface = VEq(Inter, Point);
      #if (OnSurface)
        #local Where = "on";
      #else
        #local Where = "outside";
      #end
      #debug concat("Miss  : ",str(Miss,0,0),"\n")
      #debug concat("Norm  : ",vstr(3,Norm,","3,3),"\n")
      #debug concat("Inter : ",vstr(3,Inter,",",3,3),"\n")
      #debug concat("OnSurface : ",str(OnSurface,0,0),"\n")
    #end
    #debug concat("Where : ",Where,"\n")
  #end
  #local ReturnObject = array mixed[2]{Point, Where};
  ReturnObject
#end


// Create an ObjectOfConstraint. A bone should stay within this object
// The object is always enclosed within a unit sphere.
#declare C = object{
    cone{<0,0,0>,0,<0,1,0>,0.5}
};

// Creat a point A (green) somewhere on a unit sphere.
#declare A = vrotate(<0,1,0>,<55,0,30>);
//#declare Stream = seed(7);
//#declare A = VRand_On_Sphere(Stream);

// Test where the point lies with regard to object C
#declare TT = <0,1,0>;
#declare Loc = WhereOnObj(C, A, TT);
#debug concat("\nThe Point ",vstr(3,Loc[0],",",3,3)," lies ",Loc[1]," 
the surface of the Object\n") 

#if(Loc[1] = "outside")
  #debug "project the point on the surface\n"
  #declare Norm = <0,0,0>;
  #declare Inter1 = trace(C, A, TT-A, Norm);
  #debug concat("Norm : ", vstr(3,Norm,",",3,3),"\n")
  #debug concat("Inter: ", vstr(3,Inter1,",",3,3),"\n")
  #declare Np = vnormalize(Inter1);
  #debug concat("Np : ", vstr(3,Np,",",3,3),"\n\n")
  #declare Loc = WhereOnObj(C, Np, TT);
  #debug concat("\nThe Point ",vstr(3,Loc[0],",",3,3)," lies ",Loc[1]," 
the surface of the Object\n") 
#end


Post a reply to this message

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