POV-Ray : Newsgroups : povray.general : Bezier triangular patch Server Time
22 Dec 2024 01:58:29 EST (-0500)
  Bezier triangular patch (Message 1 to 2 of 2)  
From: JimT
Subject: Bezier triangular patch
Date: 14 Aug 2018 08:55:02
Message: <web.5b72d0a41835ff23be7517870@news.povray.org>
Code for image on Bezier triangular patch in images.

#version 3.7;
//
#include "colors.inc"
#include "metals.inc"

global_settings {max_trace_level 15 assumed_gamma 1.0}

camera {orthographic location <50,30,10> look_at <0,20,0>}
#declare llevel = 1.2;
light_source {<-3000,2000,-3000> rgb <llevel, llevel,llevel> shadowless }

background{White}
//*********************************************************************************************************************
*****
// TRIANGULAR BEZIER THIN PATCH
//*********************************************************************************************************************
*****
//
// Topologically, u is along the x axis, v is up the y axis. w = 1 - u - v
// We take v for the rows, u for the columns
//
// LoD          Reduces the number of segments from that passed in. 0 a single
triangle. 4 uses segs as passed.
// polytype     NOT USED. The triangular shape chooses the way we split a
quadrilateral into two triangles.
// debug_on     = 0 nothing, = 1 puts spheres at knots, = 2 puts smaller spheres
at vertices, = 3 puts little cones in the direction of the normals.
// segs         Number of segments on all sides.
// vertex_vects = array[10] contains the control knots as 3 vectors, going round
the edge, starting at a vertex, last is the single middle knot.
//
#macro SF_bezier_thin_tri_patch(LoD,polytype,debug_on,segs,vertex_vects)
//
#if(LoD = 0)
  #local nsegs = 1;
#end
//
#if(LoD = 1)
  #local nsegs = ceil(segs/10);
#end
//
#if(LoD = 2)
  #local nsegs = ceil(segs/5);
#end
//
#if(LoD = 3)
  #local nsegs = ceil(segs/2);
#end
//
#if(LoD > 3)
  #local nsegs = segs;
#end
//
// Extract the control points into a readable format
//
#local p1  = vertex_vects[0];
#local p2  = vertex_vects[1];
#local p3  = vertex_vects[2];
#local p4  = vertex_vects[3];
#local p5  = vertex_vects[4];
#local p6  = vertex_vects[5];
#local p7  = vertex_vects[6];
#local p8  = vertex_vects[7];
#local p9  = vertex_vects[8];
#local p10 = vertex_vects[9];
//
#local onestrip = 0;
#if(nsegs = 1)
  #local onestrip = 1;
#end
//
#if(onestrip = 1)
// Do a single triangle
  #if(debug_on>0)
    union{
  #end
  triangle{p1, p4, p7 }
  #if(debug_on>0)
    #local len_1 = vlength(p1-p4);
    #local len_2 = vlength(p1-p7);
    #local len_3 = vlength(p4-p7);
    #local std_len = max(len_1,len_2,len_3)/100;
    union{
      sphere{p1  std_len }
      sphere{p4  std_len }
      sphere{p7  std_len }
     pigment{Red}
         }
         }
  #end
//
#else
// At least 2 by 2
union{
//
// De-bugging
#if(debug_on>0)
  #local len_1 = vlength(p1-p4);
  #local len_2 = vlength(p1-p7);
  #local len_3 = vlength(p4-p7);
  #local std_len = max(len_1,len_2,len_3)/100;
  union{
    sphere{p1  std_len pigment{Red}}
    sphere{p2  std_len pigment{Orange}}
    sphere{p3  std_len pigment{Yellow}}
    sphere{p4  std_len pigment{Green}}
    sphere{p5  std_len pigment{Cyan}}
    sphere{p6  std_len pigment{Blue}}
    sphere{p7  std_len pigment{Magenta}}
    sphere{p8  std_len pigment{Black}}
    sphere{p9  std_len pigment{Blue}}
    sphere{p10 std_len pigment{Red}}
       }
#end
//
// Generate the low points for the lowest strip
//
  #local row_low_points   = array[nsegs + 1];
  #local row_low_normals  = array[nsegs + 1];
  #local row_high_points  = array[nsegs + 1];
  #local row_high_normals = array[nsegs + 1];
//
  #local jcount = 0;
  #while(jcount < nsegs+1)
    #local uco  = jcount/nsegs;
    #local fu1  =    pow(1-uco,3);
    #local du1  = -3*pow(1-uco,2);
    #local fu2  =  3*pow(1-uco,2)*uco;
    #local du2  =  3*pow(1-uco,2) - 6*(1-uco)*uco;
    #local fu3  =  3*(1-uco)*uco*uco;
    #local du3  = -3*uco*uco + 6*(1-uco)*uco;
    #local fu4  =    pow(uco,3);
    #local du4  =  3*pow(uco,2);
// Points
    #local upoint                  = p1*fu1 + p2*fu2 + p3*fu3 + p4*fu4;
    #local row_low_points[jcount]  = upoint;
// Normals
    #local tangudir                = p1*du1 + p2*du2 + p3*du3 + p4*du4;
    #local tangvdir                = (p9-p1)*3*pow(1-uco,2) +
(p10-p2)*6*(1-uco)*uco + (p5-p3)*uco*uco;
    #local row_low_normals[jcount] = vnormalize(vcross(tangvdir,tangudir));
//
    #local jcount = jcount + 1;
  #end
//
// Main loops over the strips (v changes) and along the strips (u changes)
//
  #local icount = 0;
  #while(icount < nsegs)
    #local vco = (icount+1)/nsegs;
//
    #local fv1 =    pow(1-vco,3);
    #local dv1 = -3*pow(1-vco,2);
    #local fv2 =  3*pow(1-vco,2)*vco;
    #local dv2 =  3*pow(1-vco,2) - 6*(1-vco)*vco;
    #local fv3 =  3*(1-vco)*vco*vco;
    #local dv3 = -3*vco*vco + 6*(1-vco)*vco;
    #local fv4 =    pow(vco,3);
    #local dv4 =  3*pow(vco,2);
//
    #local row_high_points[0]  = p1*fv1 + p9*fv2 + p8*fv3 + p7*fv4;
//
    #local tangvdir            = p1*dv1 + p9*dv2 + p8*dv3 + p7*dv4;;
    #local tangudir            = (p2-p1)*3*(1-vco)*(1-vco) +
(p10-p9)*6*vco*(1-vco) + (p6-p8)*3*vco*vco;
    #local row_high_normals[0] = vnormalize(vcross(tangvdir,tangudir));
//
    #local jcount = 0;
    #while(jcount < nsegs - icount - 1)
      #local uco  = (jcount+1)/nsegs;
      #local wco  = 1 - uco - vco;
      #local f1   =    pow(wco,3);
      #local du1  = -3*pow(wco,2);
      #local dv1  =  du1;
      #local f2   =  3*wco*wco*uco;
      #local du2  =  3*wco*wco - 6*wco*uco;
      #local dv2  = -6*wco*uco;
      #local f3   =  3*wco*uco*uco;
      #local du3  = -3*uco*uco + 6*wco*uco;
      #local dv3  = -3*uco*uco;
      #local f4   =    pow(uco,3);
      #local du4  =  3*pow(uco,2);
      #local dv4  =    0;
      #local f5   =  3*uco*uco*vco;
      #local du5  =  6*uco*vco;
      #local dv5  =  3*uco*uco;
      #local f6   =  3*uco*vco*vco;
      #local du6  =  3*vco*vco;
      #local dv6  =  6*uco*vco;
      #local f7   =    pow(vco,3);
      #local du7  =    0;
      #local dv7  =  3*vco*vco;
      #local f8   =  3*wco*vco*vco;
      #local du8  = -3*vco*vco;
      #local dv8  = -3*vco*vco + 6*wco*vco;
      #local f9   =  3*wco*wco*vco;
      #local du9  = -6*wco*vco;
      #local dv9  =  3*wco*wco -6*wco*vco;
      #local f10  =  6*uco*vco*wco;
      #local du10 =  6*vco*wco - 6*uco*vco;
      #local dv10 =  6*uco*wco - 6*uco*vco;
//
      #local row_high_points[jcount+1]  = p1*f1  + p2*f2  + p3*f3  + p4*f4  +
p5*f5  + p6*f6  + p7*f7  + p8*f8  + p9*f9  + p10*f10;
      #local tangudir                   = p1*du1 + p2*du2 + p3*du3 + p4*du4 +
p5*du5 + p6*du6 + p7*du7 + p8*du8 + p9*du9 + p10*du10;
      #local tangvdir                   = p1*dv1 + p2*dv2 + p3*dv3 + p4*dv4 +
p5*dv5 + p6*dv6 + p7*dv7 + p8*dv8 + p9*dv9 + p10*dv10;
      #local row_high_normals[jcount+1] = vnormalize(vcross(tangvdir,tangudir));
//
      #local pq1 = row_low_points[ jcount];
      #local nq1 = row_low_normals[ jcount];
      #local pq2 = row_low_points[  jcount+1];
      #local nq2 = row_low_normals[ jcount+1];
      #local pq3 = row_high_points[ jcount+1];
      #local nq3 = row_high_normals[jcount+1];
      #local pq4 = row_high_points[ jcount];
      #local nq4 = row_high_normals[jcount];
//
      smooth_triangle{pq1,nq1,pq2,nq2,pq4,nq4}
      smooth_triangle{pq2,nq2,pq3,nq3,pq4,nq4}
//
// Put in some debug vertices
      #if(debug_on > 1)
        sphere{pq1 std_len/2 pigment{Blue}}
        sphere{pq2 std_len/2 pigment{Blue}}
        sphere{pq3 std_len/2 pigment{Blue}}
        sphere{pq4 std_len/2 pigment{Blue}}
      #end
// Put in some debug normals
      #if(debug_on > 2)
        cone{pq1 std_len/2 pq1+5*std_len*nq1 0 pigment{Blue}}
        cone{pq2 std_len/2 pq2+5*std_len*nq2 0 pigment{Blue}}
        cone{pq3 std_len/2 pq3+5*std_len*nq3 0 pigment{Blue}}
        cone{pq4 std_len/2 pq4+5*std_len*nq4 0 pigment{Blue}}
      #end
//
// Last triangle
//
      #local jcount = jcount + 1;
    #end
// Last triangle in row
    #local pq5 = row_low_points[ jcount+1];
    #local nq5 = row_low_normals[jcount+1];
//
    smooth_triangle{pq2,nq2,pq5,nq5,pq3,nq3}
//
    #if(debug_on = 2)
      sphere{pq5 std_len/2 pigment{Blue}}
    #end
    #if(debug_on > 2)
      cone{pq5 std_len/2 pq5+5*std_len*nq5 0 pigment{Blue}}
    #end
//
    #local row_low_points  = row_high_points;
    #local row_low_normals = row_high_normals;
//
    #local icount = icount + 1;
  #end
// Single triangle in top row. row_low_points[0] seems to point to p7 for a
reason I haven't worked out.
  #local vco      = (nsegs-1)/nsegs;
  #local fv1      =    pow(1-vco,3);
  #local dv1      = -3*pow(1-vco,2);
  #local fv2      =  3*pow(1-vco,2)*vco;
  #local dv2      =  3*pow(1-vco,2) - 6*(1-vco)*vco;
  #local fv3      =  3*(1-vco)*vco*vco;
  #local dv3      = -3*vco*vco + 6*(1-vco)*vco;
  #local fv4      =    pow(vco,3);
  #local dv4      =  3*pow(vco,2);
  #local pq1      =  p1*fv1 + p9*fv2 + p8*fv3 + p7*fv4;
  #local tangvdir =  p1*dv1 + p9*dv2 + p8*dv3 + p7*dv4;
  #local tangudir =  (p2-p1)*3*(1-vco)*(1-vco) + (p10-p9)*6*vco*(1-vco) +
(p6-p8)*3*vco*vco;
  #local nq1      =  vnormalize(vcross(tangvdir,tangudir));
  #local pq2 = row_low_points[ 1];
  #local nq2 = row_low_normals[1];
  #local n7  = vnormalize(vcross(p6-p7,p8-p7));
//  sphere{pq1 0.5 pigment{Yellow}}
//  sphere{pq2 0.5 pigment{Yellow}}
//
  smooth_triangle{pq1,nq1,pq2,nq2,p7,n7}
//
  #if(debug_on = 2)
    sphere{p7 std_len/2 pigment{Blue}}
  #end
  #if(debug_on > 2)
    cone{p7 std_len/2 p7+5*std_len*n7 0 pigment{Blue}}
  #end
//
     } // End of the main union
#end
//
#end
//*********************************************************************************************************************
*************************

#declare LoD      =  4;
#declare polytype =  1;
#declare debug_on =  3;
#declare segs     = 25;
//
#declare vertex_vects = array[10]{<-60, 0,  0>,
                                  <-40,15,-30>,
                                  <-20,15,-30>,
                                  <  0, 0,-30>,
                                  <  0,40,-10>,
                                  <  0,40, 10>,
                                  <  0, 0, 30>,
                                  <-20,15, 30>,
                                  <-40,15, 30>,
                                  <-30,60,  0>};
//
object{SF_bezier_thin_tri_patch(LoD,polytype,debug_on,segs,vertex_vects)
texture{T_Chrome_3C}}
box{<-70,-1,-40> <10,0,40> pigment{Green}}


Post a reply to this message

From: William F Pokorny
Subject: Re: Bezier triangular patch
Date: 15 Aug 2018 07:33:02
Message: <5b740f6e$1@news.povray.org>
On 08/14/2018 08:52 AM, JimT wrote:
> Code for image on Bezier triangular patch in images.
> 
...
> 

FYI. There is a github issue open for such a feature in POV-Ray so I've 
updated that issue with a link back to this posting.

See: https://github.com/POV-Ray/povray/issues/217

Bill P.


Post a reply to this message

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