Skip to content
Permalink
Browse files

add TP1

  • Loading branch information...
bbrrck committed Feb 2, 2017
1 parent 985c685 commit 8f2ea6fe261325aea819de4daab12742b617dca3
Showing with 212 additions and 2 deletions.
  1. +25 −2 README.md
  2. +10 −0 TP1/data/infinity.bcv
  3. +5 −0 TP1/data/simple.bcv
  4. +12 −0 TP1/data/spiral.bcv
  5. +160 −0 TP1/tp1.py
  6. BIN _assets/simple.png
@@ -1,2 +1,25 @@
# GeoNum2017
TP Géométrie numérique, spring 2017
# GeoNum2017 : Géométrie numérique, spring 2017

## TP1 : Bézier curves, De Casteljau’s algorithm

### Python
If you have no prior experience with Python whatsoever, I suggest the tutorial
[Learn Python in 10 minutes](https://www.stavros.io/tutorials/python/) by Stavros Korokithakis.
For longer and more complete references, see the [Fast Lane to Python](http://heather.cs.ucdavis.edu/~matloff/Python/PLN/FastLanePython.pdf) by Norm Matloff and, of course, the [official Python 2.7.13 docs](https://docs.python.org/2/).

### NumPy
[NumPy](http://www.numpy.org/) is a Python package supporting N-dimensional arrays and linear algebra operations. We'll mostly use it to manipulate datapoints stored in matrices (two-dimensional arrays).
Here is a useful [numpy cheatsheet](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf).

### Matplotlib
[Matplotlib](http://matplotlib.org/) is a Python 2D plotting library -- we'll use it to visualise 2D curves
via the [plot](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot) command.
{% highlight python %}
# import the modules
import numpy as np
import matplotlib.pyplot as plt
# generate random data
points = np.random.rand(1000,2)
# plot
plt.plot(points[:,0],points[:,1],'bo')
{% endhighlight %}
@@ -0,0 +1,10 @@
8
0.0 0.0
-0.7071 0.7071
-0.9899 0.0
-0.7071 -0.7071
0.0 0.0
0.7071 0.7071
0.9899 0.0
0.7071 -0.7071
0.0 0.0
@@ -0,0 +1,5 @@
3
0 0
3 3
6 4
9 1
@@ -0,0 +1,12 @@
10
10.7112 1.2776
14.1392 3.3787
14.3168 6.2451
12.4736 8.8911
6.0872 8.7484
1.73808 5.2983
6.8796 1.3294
11.164 4.2866
9.8868 6.1284
8.2216 5.7134
8.06 5.1946
@@ -0,0 +1,160 @@
#------------------------------------------------------
#
# TP1 : Bezier curves, De Casteljau's algorithm
# http://tiborstanko.sk/teaching/geo-num-2017/tp1.html
# [03-Feb-2017]
#
#------------------------------------------------------
#
# This file is a part of the course:
# Geometrie numerique (spring 2017)
# https://github.com/GeoNumTP/GeoNum2017
# M1 Informatique
# UFR IM2AG
#
# Course lecturer:
# Georges-Pierre.Bonneau at inria.fr
#
# Practical part:
# Tibor.Stanko at inria.fr
#
#------------------------------------------------------

import sys, os
import matplotlib.pyplot as plt
import numpy as np

TP = os.path.dirname(os.path.realpath(__file__)) + "/"
DATADIR = filename = TP+"data/"


#-------------------------------------------------
# READPOLYGON()
# Read Bezier control points from a file.
#
def ReadPolygon( filename ) :
datafile = open(filename,'r');
l = datafile.readline()
degree = np.fromstring(l,sep=' ',dtype=int)
BezierPts = np.fromfile(datafile,count=2*(degree+1),sep=' ',dtype=float)
BezierPts = BezierPts.reshape(-1,2)
return BezierPts


#-------------------------------------------------
# DECASTELJAU( ... )
# Perform the De Casteljau algorithm.
#
# Input
# BezierPts : #B x 2 matrix of Bezier control points
# n : upper index of the computed point (depth of the algorithm)
# i : lower index of the computed point
# t : curve parameter in [0.0,1.0]
#
# Output
# point b_i^n from the De Casteljau algorithm.
#
def DeCasteljau( BezierPts, n, i, t ) :
pass
#
# TODO : Implement the De Casteljau algorithm.
#


#-------------------------------------------------
# BEZIERCURVE( ... )
# Compute points on the Bezier curve.
#
# Input
# BezierPts : #B x 2 matrix of Bezier control points
# N : number of curve samples
#
def BezierCurve( BezierPts, N ) :

# degree of the curve (one less than the number of control points)
degree = BezierPts.shape[0]-1

# initialize curvepoints as zeros
CurvePts = np.zeros([N,2])

#
# TODO : Compute N curve points for t varying uniformly in [0.0,1.0]

#
# hint1:
# to generate the uniform sampling of the interval [0.0,1.0] with N elements, use:
# >> samples = np.linspace(0.0,1.0,num=N)
#

#
# hint2:
# to access and change i-th row of the matrix CurvePts, use:
# >> CurvePts[i,:]
#

#
# hint3:
# the actual point b_0^degree on the curve is computed by calling DeCasteljau
# with arguments n=degree, i=0.
#

return CurvePts


#-------------------------------------------------
if __name__ == "__main__":

# arg 1 : data name
if len(sys.argv) > 1 :
dataname = sys.argv[1]
else :
dataname = "simple" # simple, infinity, spiral

# arg 2 : sampling density
if len(sys.argv) > 2 :
density = int(sys.argv[2])
else :
density = 10

# filename
filename = DATADIR + dataname + ".bcv"

# check if valid datafile
if not os.path.isfile(filename) :
print "error: invalid dataname '" + dataname + "'"
print "usage: python tp1.py [simple,infinity,spiral] [sampling_density]"

else :
# read control points
BezierPts = ReadPolygon(filename)

# compute points
CurvePts = BezierCurve(BezierPts,density)

# plot
fig = plt.gcf()
fig.canvas.set_window_title('TP1 Bezier curves')
plt.title(dataname+', '+str(density)+" pts")

# set axes with equal proportions
plt.axis('equal')

# plot the control polygon
plt.plot( BezierPts[:,0], BezierPts[:,1], 'bo-' )

# plot the curve
plt.plot( CurvePts[:,0], CurvePts[:,1], 'r-' )

#
# TODO : Uncomment this if you want to save the render as png image.
#
#plt.savefig( DATADIR + dataname + ".png" )

#
# TODO : Compute intermediate polygons b_i^k for k=1,...,degree-1 and i=0,...,degree-k
#
#
# TODO : Add plt.plot commands to plot the intermediate polygons
#

plt.show()
BIN +14.4 KB _assets/simple.png
Binary file not shown.

0 comments on commit 8f2ea6f

Please sign in to comment.
You can’t perform that action at this time.