Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Request forr bezier.lua file
Started by DevilDev Mar 25 2019 03:35 AM

8 replies to this topic
bezier curve lua
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

DevilDev

[GLOBAL: userInfoPane.html]
DevilDev
  • Enthusiast

  • 66 posts
  • Corona SDK

I couldn't find the bezier.lua file in corona code exchange.

It is not available in github either.

Someone share it here please or post it in github.

Thanks.

 



[TOPIC: post.html]
#2

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Corona Geek

  • 1,044 posts
  • Corona SDK

I suspect you have a project that references this file. Where did you get the project? T&J use to have a spaceship project with bezier curves but that is just a guess. He doesn't sell his templates anymore, but he is still around.

 

Otherwise, you have to give more detail for somebody to help you.

 

Adrian



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,515 posts
  • Corona SDK

Not sure if this is what you want:

-------------------------------------------------
-- bezier.lua
-- Version: 1.0
-- Author: Rajendra Pondel
-- Email: neostar20@gmail.com
-- Licence: MIT

-- Permission is hereby granted, free of charge, to any person obtaining a copy of 
-- this software and associated documentation files (the "Software"), to deal in the 
-- Software without restriction, including without limitation the rights to use, copy, 
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
-- and to permit persons to whom the Software is furnished to do so, subject to the 
-- following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in all copies 
-- or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
-- DEALINGS IN THE SOFTWARE.


local bezier = {}

function bezier:curve(xv, yv)
	local reductor = {__index = function(self, ind)
		return setmetatable({tree = self, level = ind}, {__index = function(curves, ind)
			return function(t)
				local x1, y1 = curves.tree[curves.level-1][ind](t)
				local x2, y2 = curves.tree[curves.level-1][ind+1](t)
				return x1 + (x2 - x1) * t, y1 + (y2 - y1) * t
				end
			end})
		end
	}
	local points = {}
	for i = 1, #xv do
		print(i, xv[i], yv[i])
		points[i] = function(t) return xv[i], yv[i] end
	end
	return setmetatable({points}, reductor)[#points][1]
end

return bezier


  • Michael Flad and horacebury like this

[TOPIC: post.html]
#4

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 936 posts
  • Corona SDK

Thanks, RoamingGamer! I was just looking for this module as well! :)



[TOPIC: post.html]
#5

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

I've got a framework with a catmull-rom library, as well:

 

https://github.com/HoraceBury/CoronaFramework/blob/master/libs/catmull.lua


  • Michael Flad likes this

[TOPIC: post.html]
#6

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 936 posts
  • Corona SDK

Excellent! Thanks Horacebury, that's going in "the vault"! ;)



[TOPIC: post.html]
#7

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 904 posts
  • Corona SDK

There is also this one: 

 

https://github.com/nshafer/Bezier

 

It's not Corona specific but the Bezier.lua file is pretty simple to follow, and the main.lua file has some good sample code for reference.



[TOPIC: post.html]
#8

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 791 posts
  • Corona SDK

I've got some stuff here too.

 

I'm actually employing this at the moment to test a use case for an open-source feature I'm working on: making nearly the full range of uniforms available to shaders that want it, as well as (in software, currently) instancing, mainly for the common situation of stepping through said uniforms.

 

The idea here is that you can make sort of a prototype, some object in its "natural" state, in this case a small axis-aligned rectangular grid. Then you string copies of it together along a curve, molding them to its shape in a vertex kernel. The uniforms are key to keeping large amounts of data (I only maintain a little geometry in the example, but other properties are possible as well), in particular when neighbor information is needed, without breaking batching.

 

This actually flips the curve equations around a bit, since you can precalculate a "geometry matrix" incorporating both the curve coefficents (how the points and / or tangents are combined) as well as sampled times along the prototype segment, e.g. t = 0, 1/4, 1/2, 3/4, 1. This way you only need a few multiplies by the point / tangent info rather than to churn through the curve formula each time.

 

These are quick examples of what I mean, in wireframe:

 

 

and solid:

 

 

The rather messy source behind it:

local cubic = require("spline_ops.cubic")

local verts = {}

local N = 16
local Rows, Cols = 4, 8
--[[
for i = 0, N do
  verts[#verts + 1] = i / N
  verts[#verts + 1] = 0
end

for i = N, 0, -1 do
  verts[#verts + 1] = i / N
  verts[#verts + 1] = 1
end
--]]
for col = 1, Cols do
  for row = 1, Rows do
    local ulx, uly = (col - 1) / Cols, (row - 1) / Rows
    local urx, ury = col / Cols, uly
    local llx, lly = ulx, row / Rows
    local lrx, lry = urx, lly

    verts[#verts + 1] = ulx
    verts[#verts + 1] = uly
	
    verts[#verts + 1] = urx
    verts[#verts + 1] = ury
	
    verts[#verts + 1] = llx
    verts[#verts + 1] = lly

    verts[#verts + 1] = llx
    verts[#verts + 1] = lly

    verts[#verts + 1] = urx
    verts[#verts + 1] = ury
		
    verts[#verts + 1] = lrx
    verts[#verts + 1] = lry
  end
end

local back = display.newRect(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)

back.isHitTestable, back.isVisible = true, false

local MaxInstances = 10

local num_points, poly = 0
local p, p1, p2 = {}

local uni = {}
--display.setDrawMode("wireframe")
local function AddUniform4 (arr, index, a, b, c, d)
  uni[1], uni[2], uni[3], uni[4] = a, b, c, d

  arr:setUniforms(index, uni)
end

local K = N + 1
local Offset = 2 * K

back:addEventListener("touch", function(event)
  local phase = event.phase

  if phase == "began" then
    if num_points < MaxInstances + 1 then
        local arr, x, y = poly.fill.uniformArray, event.x, event.y

      if p1 then
        AddUniform4(arr, Offset + num_points - 1, p2.x, p2.y, x - p1.x, y - p1.y)
poly.isVisible=true
poly.fill.numInstances = num_points - 1
      end
			
      if p2 then
        AddUniform4(arr, Offset + num_points, x, y, x - p2.x, y - p2.y)
      end

      p1, p2 = p2, p1 or {}
      num_points, p2.x, p2.y = num_points + 1, x, y

      display.newCircle(x, y, 5):setFillColor(1, 0, 0)
    end
		
    display.getCurrentStage():setFocus(event.target)
  elseif phase == "ended" or phase == "cancelled" then
    display.getCurrentStage():setFocus(nil)
  end

  return true
end)

poly = display.newMesh{ vertices = verts }--newPolygon(display.contentCenterX, display.contentCenterY, verts)

poly.isVisible = false

graphics.defineEffect{
  category = "generator",
  name = "uv",

  usesUniformVectors = true,
  vertexData = {
    { index = 0, name = "index", default = 0 } -- used if doing manual indexing
  },

  vertex = ([[
    #define NUM_SLOTS %i.
	
    P_POSITION vec2 VertexKernel (P_POSITION vec2 pos)
    {
      // ^^ Could also use pos to free up uvs; must account for centering

      // Precalculated coefficients (t^3, t^2, t, 1) and derivatives, mapped
      // by Hermite "geometry matrix" (just the constant coefficients of
      // the point and tangent equations), at t = xpos / N
      P_POSITION vec4 cpos = CoronaUniformVector(xpos);
      P_POSITION vec4 ctan = CoronaUniformVector(xpos + NUM_SLOTS);

      // Nodes (point.xy, tangent.xy) follow these coeffs
      P_UV float index = CoronaInstanceIndex + 2. * NUM_SLOTS;
      P_POSITION vec4 pt1 = CoronaUniformVector(index);
      P_POSITION vec4 pt2 = CoronaUniformVector(index + 1.);

      // 2x4 matrix
      P_POSITION vec4 a = vec4(pt1.x, pt2.x, pt1.z, pt2.z);
      P_POSITION vec4 b = vec4(pt1.y, pt2.y, pt1.w, pt2.w);

      // Transpose vector-matrix multiplies
      P_POSITION vec2 p = vec2(dot(cpos, a), dot(cpos, b));
      P_POSITION vec2 t = vec2(dot(ctan, a), dot(ctan, b));

      P_POSITION vec2 f = normalize(t);
      P_POSITION vec2 u = vec2(-f.y, +f.x);

      pos = p + u * 2. * (CoronaTexCoord.y - .5) * 35.;
			
      return pos;
    }
  ]]):format(K),
	
  fragment = [[
    P_COLOR vec4 FragmentKernel (P_UV vec2 uv)
    {
      return vec4(uv, 0., 1.);
    }
  ]]
}

poly.fill.effect = "generator.custom.uv"

local arr = poly.fill.uniformArray
local pos, tan = {}, {}

local function AddUniform (arr, index, o)
  AddUniform4(arr, index, o.a, o.b, o.c, o.d)
end

for i = 1, K do
  cubic.EvaluateCoeffs("hermite", pos, tan, (i - 1) / N)

  AddUniform(arr, i + 0, pos)
  AddUniform(arr, i + K, tan)
end

The commented-out polygon stuff works passably, but the way a few vertices get laid out, you end up with one triangle outside the curve, which definitely does not look right. :) Thus the grid.

 

Anyhow, this is still rather a work in progress, with some other use cases in idea phase.



[TOPIC: post.html]
#9

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,309 posts
  • Enterprise

there are some really impressive offerings here!

but if you just want something simple...

-- factory
local function CubicBezierEvaluator(x1,y1,x2,y2,x3,y3,x4,y4)
  return function(t)
    local a,b,c,d = (1-t)^3, 3*(1-t)^2*t, 3*(1-t)*t^2, t^3
    return a*x1+b*x2+c*x3+d*x4, a*y1+b*y2+c*y3+d*y4
  end
end

-- instance
local f = CubicBezierEvaluator(100,300, 200,200, 150,400, 250,100)

-- demo
for t = 0,1,0.01 do
  local x,y = f(t)
  display.newCircle(x,y,2,2)
end



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: bezier, curve, lua