Ooops
November 25, 2020, 07:58:25 AM

Author Topic: [bb] Bezier Interpolation in 3D by Markus Rauch [ 1+ years ago ]  (Read 602 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Bezier Interpolation in 3D
Author : Markus Rauch
Posted : 1+ years ago

Description : Bezier Interpolation in 3D

Code :
Code: BlitzBasic
  1. ;Bezier Interpolation BlitzBasic 3D Example from Markus Rauch
  2.  
  3. ;MR 14.04.2003
  4.  
  5. ;--------------------------------------------------------
  6.  
  7. Graphics3D 800,600,16,0
  8. SetBuffer BackBuffer()
  9.  
  10. AppTitle " Bezier Interpolation BlitzBasic 3D Example from Markus Rauch"
  11.  
  12. ;--------------------------------------------------------
  13.  
  14. Global camp=CreatePivot()
  15. Global cam=CreateCamera(camp)
  16. CameraZoom cam,1
  17. CameraRange cam,1,10000
  18.  
  19. PositionEntity camp,0,0,-200
  20.  
  21. ;--------------------------------------------------------
  22.  
  23. Type Vector3D
  24.  Field x#,y#,z#
  25.  Field Pitch#
  26.  Field Yaw#
  27.  Field Roll#
  28. End Type
  29.  
  30. Dim ve.Vector3D(100)
  31.  
  32. ve.Vector3D(0)=New Vector3D
  33. ve.Vector3D(1)=New Vector3D
  34. ve.Vector3D(2)=New Vector3D
  35. ve.Vector3D(3)=New Vector3D
  36.  
  37. Global VectorMax=4
  38.  
  39. Global p.Vector3D=New Vector3D
  40.  
  41. ve(0)x=-50
  42. ve(0)y=50
  43. ve(0)z=0
  44.  
  45. ve(1)x=-25
  46. ve(1)y=0
  47. ve(1)z=0
  48.  
  49. ve(2)x=25
  50. ve(2)y=50
  51. ve(2)z=0
  52.  
  53. ve(3)x=0 ;Moving
  54. ve(3)y=0
  55. ve(3)z=0
  56.  
  57. While Not KeyHit(1)
  58.  
  59.  RenderWorld
  60.  
  61.  ;-------------------- Moving Point (3)
  62.  
  63.  Local w#
  64.  
  65.  ve(3)x=50+Sin(w)*100
  66.  ve(3)y= 0+Cos(w)*100
  67.  
  68.  w#=w#+1 :If w>360.0 Then w=w-360.0
  69.  
  70.  ;--------------------
  71.  
  72.  Color 255,255,0
  73.  
  74.  Local mu#
  75.  Local st#=0.01
  76.  
  77.  mu=0.0
  78.  Repeat
  79.  
  80.   Bezier4 p,ve(0),ve(1),ve(2),ve(3),mu ;Only 4 Points
  81.  
  82.   ;Bezier p,mu  ;All Points
  83.  
  84.   Plot3D p
  85.  
  86.   mu=mu+st
  87.   If mu>1.0 Then Exit
  88.  Forever
  89.  
  90.  ;Show all Points
  91.  For v=0 To VectorMax-1
  92.   Color 128,128,128
  93.   Text3D ve(v),"V "+V
  94.   Color 255,0,0
  95.   Plot3D ve(v)
  96.  Next
  97.  
  98.  Flip
  99. Wend
  100. End
  101.  
  102. ;##########################################################################
  103.  
  104. Function Bezier3(p.Vector3D,p1.Vector3D,p2.Vector3D,p3.Vector3D,mu#)
  105.  
  106.  ;MR 13.04.2003
  107.  
  108.  ;Three control point Bezier interpolation
  109.  ;mu ranges from 0 To 1, start To End of curve
  110.  
  111.   Local mum1#,mum12#,mu2#
  112.  
  113.   mu2 = mu * mu
  114.   mum1 = 1 - mu
  115.   mum12 = mum1 * mum1
  116.  
  117.   px = p1x * mum12 + 2 * p2x * mum1 * mu + p3x * mu2
  118.   py = p1y * mum12 + 2 * p2y * mum1 * mu + p3y * mu2
  119.   pz = p1z * mum12 + 2 * p2z * mum1 * mu + p3z * mu2
  120.  
  121. End Function
  122.  
  123. Function Bezier4(p.Vector3D,p1.Vector3D,p2.Vector3D,p3.Vector3D,p4.Vector3D,mu#)
  124.  
  125.  ;MR 13.04.2003
  126.  
  127.  ;Four control point Bezier interpolation
  128.  ;mu ranges from 0 To 1, start To End of curve
  129.  
  130.  Local mum1#,mum13#,mu3#
  131.  
  132.  mum1 = 1.0 - mu
  133.  mum13 = mum1 * mum1 * mum1
  134.  mu3 = mu * mu * mu
  135.  
  136.  px = mum13*p1x + 3.0*mu*mum1*mum1*p2x + 3.0*mu*mu*mum1*p3x + mu3*p4x
  137.  py = mum13*p1y + 3.0*mu*mum1*mum1*p2y + 3.0*mu*mu*mum1*p3y + mu3*p4y
  138.  pz = mum13*p1z + 3.0*mu*mum1*mum1*p2z + 3.0*mu*mu*mum1*p3z + mu3*p4z
  139.  
  140. End Function
  141.  
  142. ;##########################################################################
  143.  
  144. Function Bezier(p.Vector3D,mu#)
  145.  
  146.  ;MR 14.04.2003
  147.  
  148.  ;General Bezier curve
  149.  ;Number of control points is n+1
  150.  ;mu 0 bis 1    IMPORTANT, the Last point is Not computed
  151.  
  152.  Local k,kn,nn,nkn
  153.  Local blend#,muk#,munk#
  154.  
  155.  Local px#,py#,pz#
  156.  
  157.  Local n=VectorMax-1
  158.  
  159.    px=0.0
  160.    py=0.0
  161.    pz=0.0
  162.  
  163.    muk = 1.0
  164.    munk = pow(1.0-mu,Float(n))
  165.  
  166.    For k=0 To n
  167.       nn = n
  168.       kn = k
  169.       nkn = n - k
  170.       blend = muk * munk
  171.       muk =muk * mu
  172.       munk = munk / (1.0-mu)
  173.       While nn => 1
  174.          blend=blend * nn
  175.          nn=nn-1
  176.          If kn > 1 Then
  177.             blend=blend / Float(kn)
  178.             kn=kn-1
  179.          EndIf
  180.          If nkn > 1 Then
  181.             blend=blend / Float(nkn)
  182.            nkn=nkn-1
  183.          EndIf
  184.       Wend
  185.       px=px+ve(k)x * blend
  186.       py=py+ve(k)y * blend
  187.       pz=pz+ve(k)z * blend
  188.    Next
  189.  
  190. End Function
  191.  
  192. ;##########################################################################
  193.  
  194. Function pow#(a#,b#)
  195.  
  196.  ;MR 14.04.2003
  197.  
  198.  ;C Like :-)
  199.  
  200.  Return a^b
  201.  
  202. End Function
  203.  
  204. ;##########################################################################
  205.  
  206. Function Plot3D(p.Vector3D)
  207.  
  208.  ;MR 14.04.2003
  209.  
  210.  ;cam ist Global und das Handle der Camera
  211.  
  212.  Local x,y
  213.  
  214.  CameraProject cam,px,py,pz
  215.  x=ProjectedX()
  216.  y=ProjectedY()
  217.  
  218.  Plot x,y
  219.  
  220. End Function
  221.  
  222. ;##########################################################################
  223.  
  224. Function Text3D(p.Vector3D,t$)
  225.  
  226.  ;MR 14.04.2003
  227.  
  228.  ;cam ist Global und das Handle der Camera
  229.  
  230.  Local x,y
  231.  
  232.  CameraProject cam,px,py,pz
  233.  x=ProjectedX()
  234.  y=ProjectedY()
  235.  
  236.  Text x,y,t$,True,True
  237.  
  238. End Function
  239.  
  240. ;##########################################################################
  241.  
  242. Function CubicInterpolate#(y0#,y1#,y2#,y3#,mu#)
  243.  
  244.  ;MR 13.04.2003
  245.  
  246.  Local a0#,a1#,a2#,a3#,mu2#
  247.  
  248.  mu2 = mu*mu
  249.  a0 = y3 - y2 - y0 + y1
  250.  a1 = y0 - y1 - a0
  251.  a2 = y2 - y0
  252.  a3 = y1
  253.  
  254.  Return (a0*mu*mu2+a1*mu2+a2*mu+a3)
  255.  
  256. End Function
  257.  
  258. ;##########################################################################


Comments :


Uncle(Posted 1+ years ago)

 I needed a blitzmax version using minib3d, so here it is if anyone else needs it...
Code: [Select]
SuperStrict
Import "minib3d.bmx"

Graphics3D 800,600,16,0

AppTitle = " Bezier Interpolation BlitzMax Example"

'--------------------------------------------------------

Global camp:tpivot = CreatePivot()
Global cam:tcamera = CreateCamera(camp)
PositionEntity camp, 0, 0, - 200

'--------------------------------------------------------

Type Vector3D
Field x:Float
Field y:Float
Field z:Float
  Field Pitch:Float
  Field Yaw:Float
  Field Roll:Float
End Type

Type VectorLine
Field startNode:Vector3D = New Vector3D
Field endNode:Vector3D = New Vector3D
Field modifierNode1:Vector3D = New Vector3D
Field modifierNode2:Vector3D = New Vector3D

Method setStartNode(inX:Float, inY:Float, inZ:Float)
startNode.x = inX
startNode.y = inY
startNode.z = inZ
End Method

Method setEndNode(inX:Float, inY:Float, inZ:Float)
endNode.x = inX
endNode.y = inY
endNode.z = inZ
End Method

Method setModifierNode(inX:Float, inY:Float, inZ:Float, inNode:Int)
Select inNode
Case 1
modifierNode1.x = inX
modifierNode1.y = inY
modifierNode1.z = inZ
Case 2
modifierNode2.x = inX
modifierNode2.y = inY
modifierNode2.z = inZ
End Select
End Method

Method plotModifierNode(inNode:Int)
Select inNode
Case 1
SetColor 255, 255, 0
Plot3D(modifierNode1, 2)
Text3D(modifierNode1, "M1")
Case 2
SetColor 255, 255, 0
Plot3D(modifierNode2, 2)
Text3D(modifierNode2, "M2")
End Select
End Method

Method plotStartNode()
SetColor 0, 255, 0
Plot3D(startNode, 2)
Text3D(startNode, "S")
End Method

Method plotEndNode()
SetColor 255, 0, 0
Plot3D(endNode, 2)
Text3D(endNode, "E")
End Method

Method plotLine()
SetColor 155, 155, 155
Local mu:Float
Local st:Float = 0.01

mu = 0.0
Repeat
Bezier4 (p, vl.startNode, vl.modifierNode1, vl.modifierNode2, vl.endNode, mu) 'Only 4 Points
  Plot3D p, 2

  mu = mu + st
  If mu > 1.0 Then Exit
Forever
End Method

Method plotPointOnLine(mu:Float)
SetColor 0, 255, 255
Bezier4 (p, vl.startNode, vl.modifierNode1, vl.modifierNode2, vl.endNode, mu) 'Only 4 Points
  Plot3D p, 5
End Method

End Type


Const VECTORMAX:Int = 4
Global vl:VectorLine = New VectorLine
Global p:Vector3D = New Vector3D
Global pointOnLine:Float = 0.5

vl.setStartNode(- 50, 50, 0)
vl.setEndNode(- 25, 0, 0)
vl.setModifierNode(25, 50, 0, 1)
vl.setModifierNode(0, 0, 0, 2)


While Not KeyHit(KEY_ESCAPE)
RenderWorld

If KeyDown(KEY_UP) And pointOnLine < 1 Then pointOnLine:+0.01
If KeyDown(KEY_DOWN) And pointOnLine > 0 Then pointOnLine:-0.01

If KeyHit(KEY_SPACE) Then
vl.setModifierNode(Rand(- 125, 125), Rand(- 125, 125), 0, 1)
vl.setModifierNode(Rand(- 125, 125), Rand(- 125, 125), 0, 2)
End If

beginmax2d()
  '-------------------- Moving Point (3)

Global w:Float

vl.endNode.x = 50 + Sin(w) * 100
vl.endNode.y = 0 + Cos(w) * 100

w:Float = w:Float + 1
If w>360.0 Then w=w-360.0

'--------------------
vl.plotModifierNode(1)
vl.plotModifierNode(2)
vl.plotStartNode()
vl.plotEndNode()
vl.plotLine()
vl.plotPointOnLine(pointOnLine)

SetColor 255, 255, 255
DrawText "Use the up and down keys to move the along the line.", 10, 10
DrawText "Press space to randomise the position of the modifiers", 10, 25
endmax2d()
  Flip 1
Wend

End

'##########################################################################

Function Bezier3(p:Vector3D, p1:Vector3D, p2:Vector3D, p3:Vector3D, mu:Float)

'MR 13.04.2003

'Three control point Bezier interpolation
'mu ranges from 0 To 1, start To End of curve

Local mum1:Float, mum12:Float, mu2:Float

mu2 = mu * mu
mum1 = 1 - mu
mum12 = mum1 * mum1

p.x = p1.x * mum12 + 2 * p2.x * mum1 * mu + p3.x * mu2
p.y = p1.y * mum12 + 2 * p2.y * mum1 * mu + p3.y * mu2
p.z = p1.z * mum12 + 2 * p2.z * mum1 * mu + p3.z * mu2

End Function

Function Bezier4(p:Vector3D, p1:Vector3D, p2:Vector3D, p3:Vector3D, p4:Vector3D, mu:Float)

'MR 13.04.2003

'Four control point Bezier interpolation
'mu ranges from 0 To 1, start To End of curve

Local mum1:Float, mum13:Float, mu3:Float

mum1 = 1.0 - mu
mum13 = mum1 * mum1 * mum1
mu3 = mu * mu * mu

p.x = mum13 * p1.x + 3.0 * mu * mum1 * mum1 * p2.x + 3.0 * mu * mu * mum1 * p3.x + mu3 * p4.x
p.y = mum13 * p1.y + 3.0 * mu * mum1 * mum1 * p2.y + 3.0 * mu * mu * mum1 * p3.y + mu3 * p4.y
p.z = mum13 * p1.z + 3.0 * mu * mum1 * mum1 * p2.z + 3.0 * mu * mu * mum1 * p3.z + mu3 * p4.z

End Function

'##########################################################################

Function Bezier(p:Vector3D, mu:Float)

'MR 14.04.2003

'General Bezier curve
'Number of control points is n+1
'mu 0 bis 1    IMPORTANT, the Last point is Not computed

Local k:Int, kn:Float, nn:Float, nkn:Float
Local blend:Float, muk:Float, munk:Float

Local px:Float, py:Float, pz:Float

Local n:Int = VECTORMAX - 1

p.x = 0.0
p.y = 0.0
p.z = 0.0

muk = 1.0
munk = pow(1.0 - mu, Float(n))

For k = 0 To n
nn = n
   kn = k
   nkn = n - k
   blend = muk * munk
   muk = muk * mu
   munk = munk / (1.0 - mu)
   While nn >= 1
    blend = blend * nn
       nn = nn - 1
       If kn > 1 Then
        blend = blend / Float(kn)
           kn=kn-1
       EndIf
       If nkn > 1 Then
        blend = blend / Float(nkn)
          nkn = nkn - 1
       EndIf
Wend
Select k
Case 0
p.x = p.x + vl.startNode.x * blend
      p.y = p.y + vl.startNode.y * blend
      p.z = p.z + vl.startNode.z * blend
Case 1
p.x = p.x + vl.endNode.x * blend
      p.y = p.y + vl.endNode.y * blend
      p.z = p.z + vl.endNode.z * blend
Case 2
p.x = p.x + vl.modifierNode1.x * blend
      p.y = p.y + vl.modifierNode1.y * blend
      p.z = p.z + vl.modifierNode1.z * blend
Case 0
p.x = p.x + vl.modifierNode2.x * blend
      p.y = p.y + vl.modifierNode2.y * blend
      p.z = p.z + vl.modifierNode2.z * blend
End Select
Next
 
End Function

'##########################################################################

Function pow#(a#,b#)

'MR 14.04.2003

'C Like :-)

Return a^b

End Function

'##########################################################################

Function Plot3D(p:Vector3D, Size:Int)

'MR 14.04.2003

'cam ist Global und das Handle der Camera

Local x:Float, y:Float

CameraProject cam, p.x, p.y, p.z
x=ProjectedX()
y=ProjectedY()

DrawRect x, y, Size, Size

End Function

'##########################################################################

Function Text3D(p:Vector3D, t:String)

'MR 14.04.2003

'cam ist Global und das Handle der Camera

Local x:Float, y:Float

CameraProject cam, p.x, p.y, p.z
x=ProjectedX()
y=ProjectedY()

DrawText t, x, y

End Function

'##########################################################################

Function CubicInterpolate#(y0#,y1#,y2#,y3#,mu#)

'MR 13.04.2003

Local a0#,a1#,a2#,a3#,mu2#

mu2 = mu*mu
a0 = y3 - y2 - y0 + y1
a1 = y0 - y1 - a0
a2 = y2 - y0
a3 = y1

Return (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3)

End Function

'##########################################################################


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal