POV-Ray : Newsgroups : povray.text.scene-files : Macro for smooth joining of 2 cylinders : Macro for smooth joining of 2 cylinders Server Time
24 Apr 2024 11:09:52 EDT (-0400)
  Macro for smooth joining of 2 cylinders  
From: Tor Olav Kristensen
Date: 3 Jul 2001 11:56:49
Message: <3B41EB6D.3A4D6164@hotmail.com>
If two cylinders share a common endpoint,
then my "TorusBend" macro below can be used
to join them smoothly with "sector" of a
torus with a given major radius.

My code below shows how to use it.

If the 2 cylinders happen to lie on a 
straight line or if the major radius is
set to 0, then a sphere is used instead,
in order to "fill in the gap" between the
cylinders.

If merge and a transparent texture is used,
then it might be wise to put a sphere in-
between the cylinders and the torus
segment. (It's radius should then be equal
to the radius of the cylinders and the
minor radius of the torus segment.)

Note that the code below is not thoroughly
tested yet, so it may contain errors.

(Also note that the "TorusBend" macro needs
my old "Tilt" macro in order to work.)


-- 
Best regards,

Tor Olav


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Copyright 2001 by Tor Olav Kristensen
// mailto:tor### [at] hotmailcom
// http://www.crosswinds.net/~tok
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#version 3.1;

#include "colors.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Define macros

#macro Tilt(Thing, Vector)

  #local xAngle = (Vector.z < 0 ? -1 : 1)*
                  degrees(acos(vnormalize((x + z)*Vector).x));
  #local yAngle = degrees(acos(vnormalize(Vector).y));

  object {
    Thing
    rotate  xAngle*y
    rotate -yAngle*z
    rotate -xAngle*y
  }

#end // macro Tilt



#macro TorusBend(pF, pM, pL, 
                 Rmaj, Rmin, 
                 pS, pC, pE)

  #local vA = vnormalize(pF - pM);
  #local vB = vnormalize(pL - pM);
  #local vUp = vcross(vB, vA);
  #local LL = vlength(vUp);
  #if (Rmaj*LL = 0)
    #declare pS = pM;
    #declare pC = pM;
    #declare pE = pM;
    sphere { pM, Rmin } 
  #else
    #local QQ = 1 - vdot(vA, vB);
    #local RR = Rmaj*LL/QQ;
    #local RAB = Rmaj*sqrt(2/QQ);
    #declare pS = pM + RR*vA;
    #declare pC = pM + RAB*vnormalize(vA + vB);
    #declare pE = pM + RR*vB;
    intersection {
      Tilt(torus { Rmaj, Rmin }, vUp)
      plane { vA, 0 }
      plane { vB, 0 }
      translate pC
    }
  #end // if
  
#end // macro TorusBend

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Define problem

#declare pFirst = <5, -1, -1>;
#declare pMiddle = <0, 1, 2>;
#declare pLast = <-4, 0, -2>;

#declare BendRad = 2;
#declare CylRad = 0.2;
#declare SphRad = CylRad*1.2;

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Solve problem

#declare pBendStart = <0, 0, 0>;
#declare pBendCenter = <0, 0, 0>;
#declare pBendEnd = <0, 0, 0>;

#declare TheBend =
TorusBend(
  pFirst, pMiddle, pLast,
  BendRad, CylRad,
  pBendStart, pBendCenter, pBendEnd
)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// And then visualize it

object {
  TheBend
  pigment { color White }
}

sphere {
  pBendCenter, SphRad
  pigment { color White }
} 



sphere {
  pMiddle, SphRad
  pigment { color Magenta }
} 



sphere {
  pFirst, SphRad
  pigment { color Red }
} 

cylinder {
  pFirst, pBendStart, CylRad
  pigment { color Red }
}

sphere {
  pBendStart, SphRad
  pigment { color Red + Gray }
} 



sphere {
  pLast, SphRad
  pigment { color Blue }
}

cylinder {
  pLast, pBendEnd, CylRad
  pigment { color Blue }
}

sphere {
  pBendEnd, SphRad
  pigment { color Blue + Gray }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Click !

background { color (Blue + Cyan)/2 }

light_source {  50*<-1, 2, -2> color White }

camera {
  location <1, 1, -2>*4
  look_at <0, 0, 0>
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =


Post a reply to this message

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