File: Bezier.cpp

package info (click to toggle)
criticalmass 1%3A1.0.2-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,188 kB
  • sloc: ansic: 47,628; cpp: 25,201; sh: 11,834; xml: 3,532; perl: 3,271; makefile: 610; python: 72; awk: 40; lisp: 33
file content (92 lines) | stat: -rw-r--r-- 2,346 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Description:
//   Bezier segment template.
//
// Copyright (C) 2001 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation;  either version 2 of the License,  or (at your option) any  later
// version.
//
// This program is distributed in the hope that it will be useful,  but  WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include <Trace.hpp>
#include <Bezier.hpp>

//Construct a Bezier segment given 4 points
template<class POINT>
Bezier<POINT>::Bezier( const POINT point[4])
{
    XTRACE();
    int dim = point[0].dimension();

    for( int d=0; d<dim; d++)
    {
        float val[4];

	for( int i=0; i<4; i++)
	{
            POINT pt = point[ i];
	    val[ i] = pt[d];
	}
	calcCoeff( val, pd[d]);
    }
}

//Calculate Bezier segment coefficients
template<class POINT>
void Bezier<POINT>::calcCoeff( const float in[ 4], float out[ 4])
{
    XTRACE();
    out[ 0] =  1*in[0];
    out[ 1] = -3*in[0] +3*in[1];
    out[ 2] =  3*in[0] -6*in[1] +3*in[2];
    out[ 3] = -1*in[0] +3*in[1] -3*in[2] +1*in[3];
}

//Calculate point on the Bezier segment at time 0<=t<=1
template<class POINT>
void Bezier<POINT>::Pos( const float t, POINT &point) const
{
    XTRACE();
    float tt = t*t;
    float ttt = t*tt;

    for( int d=0; d<point.dimension(); d++)
    {
	point.set(d, pd[d][0] + t*pd[d][1] + tt*pd[d][2] + ttt*pd[d][3]);
    }
}

//Calculate perpendicular vector at time 0<=t<=1
//Really only makes sense in 2D
template<class POINT>
void Bezier<POINT>::PVec( const float t, POINT &point) const
{
    XTRACE();
    float t2 = 2*t;
    float tt3 = 3*t*t;

    if( point.dimension() != 2) return;

    //vector perpendicular to tangent at t
    // x/y -> -(y/x)
    point.set(1,  pd[0][1] + t2*pd[0][2] + tt3*pd[0][3]);
    point.set(0,-(pd[1][1] + t2*pd[1][2] + tt3*pd[1][3]));
}

//Calculate tangent vector at time 0<=t<=1
template<class POINT>
void Bezier<POINT>::TVec( const float t, POINT &point) const
{
    XTRACE();
    float t2 = 2*t;
    float tt3 = 3*t*t;

    for( int d=0; d<point.dimension(); d++)
    {
	point.set(d, pd[d][1] + t2*pd[d][2] + tt3*pd[d][3]);
    }
}