sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Mathematical Formulas to Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 4 Posters 491 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      I'm not that comfortable with math containing too many Greek symbols.

      But, that's what I'm up against when I'm trying to understand Bรฉzier curves and surfaces.

      http://en.wikipedia.org/wiki/B%C3%A9zier_curve

      http://upload.wikimedia.org/math/f/0/3/f03e0da823da62ab676bd8291d280b7a.png

      http://en.wikipedia.org/wiki/B%C3%A9zier_surface

      http://upload.wikimedia.org/math/5/a/b/5ab0d8d2260c0881a95c776245ee41b2.png

      What I'm trying to tackle first is Bernstein basis polynomials
      http://en.wikipedia.org/wiki/Bernstein_polynomial

      http://upload.wikimedia.org/math/e/4/a/e4a5aa66a7bebf4144ec22a58c3e39f7.png

      I've tried to search to source code that relates to this so I can compare and relate to the functions like that - but without much luck.

      For starters:
      How would the Bernstein polynomial be interpreted in ruby? (Or pseudo code - whatever)

      Thomas Thomassen โ€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • R Offline
        remus
        last edited by

        have you had a look at fred06's bezier scripts? i imagine they'll have a lot of this stuff in ruby (and it could save a lot of brain ache for everyone from trying to understand those equations ๐Ÿ˜› )

        http://remusrendering.wordpress.com/

        1 Reply Last reply Reply Quote 0
        • pilouP Offline
          pilou
          last edited by

          Page 125 126 ๐Ÿ˜‰
          But I believe Fredo6 has made some research about that ๐Ÿ˜’
          He can tell you some tricks ๐Ÿ˜‰

          Frenchy Pilou
          Is beautiful that please without concept!
          My Little site :)

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            I looked at fredo's scripts, yes. But the comments are a bit parse - so I don't quite see what is going on.

            I've also used the bezier.rb script from Google in a script I'm working one. That's fine and all - but I still don't quite understand what it do. And I don't think the Google bezier uses the Bernstein polynomial.

            Thomas Thomassen โ€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              @unknownuser said:

              Page 125 126 ๐Ÿ˜‰
              But I believe Fredo6 has made some research about that ๐Ÿ˜’
              He can tell you some tricks ๐Ÿ˜‰

              Thanks for that link. I'll look through that PDF over the weekend. Looks to be quite a number of pseudo code examples there. ๐Ÿ‘

              Thomas Thomassen โ€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • fredo6F Offline
                fredo6
                last edited by

                @thomthom said:

                I looked at fredo's scripts, yes. But the comments are a bit parse - so I don't quite see what is going on.

                I've also used the original bezier.rb script from Google in a script I'm working one. That's fine and all - but I still don't quite understand what it do. And I don't think the Google bezier uses the Bernstein polynomial.

                Tom,

                Actually the code is adapted from the original version by @Last Software. Though I am not specialist in Algebra, I understand what it does. This is coded this way essentially for pratical reasons. The Cubic Bezier were written by Carlos Falรฉ. I wrote the others myself (BSpline, Catmull, etc...) by taking inspiration from existing algorithms.

                By the way Bezier curves are based on Bernstein Polylomials (the B(i,n) in your formula). A good application of this is also in FFD.

                Fredo

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  Is the @Last code using Bernstein Polylomials? I couldn't relate the code to the formula I read. (mind you - I don't know how to read formulas like this.)

                  Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • fredo6F Offline
                    fredo6
                    last edited by

                    Tom,

                    Formulas and program algorithms often look different, because the code is usually optimized for iteration rather than direct application of the formula.

                    Here is an exerpt of the Wikipedia notice.
                    Bezier and Bernstein.png

                    The notation in parenthesis Cn,i are simply binomial coefficients given by the formula
                    Cni.png

                    t is simply the parametric variable, between 0 and 1 to tell where you wish to interpolate the value of the Bezier curve (when you ask for a precision of 20 segments, then basically t is taken from 0 to 1 by increment of dt = 1/(20+1).

                    
                    def BZ__BezierClassic.evaluate(pts, t)
                    
                        degree = pts.length - 1
                        if degree < 1
                            return nil
                        end
                        
                        t1 = 1.0 - t
                        fact = 1.0
                        n_choose_i = 1
                    
                        x = pts[0].x * t1
                        y = pts[0].y * t1
                        z = pts[0].z * t1
                        
                        for i in 1...degree
                            fact = fact*t
                            n_choose_i = n_choose_i*(degree-i+1)/i
                            fn = fact * n_choose_i
                            x = (x + fn*pts[i].x) * t1
                            y = (y + fn*pts[i].y) * t1
                            z = (z + fn*pts[i].z) * t1
                        end
                    
                        x = x + fact*t*pts[degree].x
                        y = y + fact*t*pts[degree].y
                        z = z + fact*t*pts[degree].z
                    
                        Geom;;Point3d.new(x, y, z)
                        
                    end
                    
                    

                    Then for each t, you compute the value of the coordinates (x, y, z) of the curve point, based on the control points pts.
                    The code is simply computing the same polynomial formula above in a loop on the degree (number of control points - 1), by summing the value of coefficients with the ones calculated at the previous step of the loop.

                    You recognize fact, which actually will hold the factorial for the degree variable and t1 which is simply (1 - t).

                    The rest is simply a matter of arithmetic.

                    I agree this is convoluted, and this is why, the best is to get inspiration of existing algorithms written in whatever language, to convert them to Ruby and then possibly improve them (the one used by @Last seems to be orginally written in Fortran).

                    So, to your question: YES, Bezier uses Bernstein polynomials (based on factorials)

                    Fredo

                    PS: Rereading the code, it gives me the idea to improve it a bit for Ruby.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      Thanks Fredo.
                      I've taken an interests into Bรจzier patches. And while I could easily produce a patch using the @Last bezier.rb, I wanted to try to understand more about how it worked technically.

                      That way I could eventually improve the method I use to create the patch, as at the moment I'm not sure if it's efficient the way I do it.

                      But yea, Bรจzier curves are really cool. Thanks for your feedback Fredo. ๐Ÿ‘

                      Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement