Source

geoalg / geoalg / nurbs / surfaces.py

#coding:utf-8
# Purpose: Spline surfaces for package 'nurbs'
# Created: 2012.01.07
# License: GPLv3

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import

__author__ = 'mozman <mozman@gmx.at>'

from geoalg.nurbs import bernstein_basis

class BezierSurface(object):
    def __init__(self, defpoints):
        """ BezierSurface constructor

        :param defpoints: defpoints is matrix (list of lists) of m rows and n columns.
            [ [m1n1, m1n2, ... ], [m2n1, m2n2, ...] ... ]
            each element is a 3D point (x, y, z) tuple or list
        """
        self._defpoints = defpoints
        self.nrows = len(defpoints)
        self.ncols = len(defpoints[0])

    def appoximate(self, usegs, vsegs):
        stepu = 1.0 / float(usegs)
        stepv = 1.0 / float(vsegs)
        result = [[0.0] * self.ncols] * self.nrows
        for ucounter in range(usegs+1):
            u = stepu * ucounter
            for vcounter in range(vsegs+1):
                v = stepv * vcounter
                result[u][v] = self.get_point(u, v)
        return result

    def get_point(self, u, v):
        """ u, v in range [0.0, 1.0].
        """
        point = [0.0, 0.0, 0.0]
        for irow in range(self.nrows):
            rowbasis = bernstein_basis(self.nrows, irow, u)
            row = self._defpoints[irow]
            for col in range(self.ncols):
                colbasis = bernstein_basis(self.ncols, col, v)
                for axis in (0, 1, 2):
                    point[axis] += row[col][axis] * rowbasis * colbasis
                return point
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.