/
bezier.h
118 lines (94 loc) · 4.53 KB
/
bezier.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift 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 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift 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. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/
#ifndef _BEZIER_H
#define _BEZIER_H
#include "mathvector.h"
#include "aabb.h"
#include <fstream>
class Bezier
{
public:
Bezier() {}
~Bezier() {}
///initialize this bezier to the quad defined by the given corner points
void SetFromCorners(const Vec3 & fl, const Vec3 & fr, const Vec3 & bl, const Vec3 & br);
///shortest cubic spline through 4 on-curve points(chord approximation)
///will modify point[1] and point[2] if fit possible
void FitSpline(Vec3 p[]);
///shortest cubic spline through 3 on-curve points(p1 == p2)
///will modify point[1] and point[2]
void FitMidPoint(Vec3 p[]);
///return true if the ray starting at the given origin going in the given direction intersects this bezier.
/// output the contact point and normal to the given outtri and normal variables.
bool CollideSubDivQuadSimple(const Vec3 & origin, const Vec3 & direction, Vec3 &outtri) const;
bool CollideSubDivQuadSimpleNorm(const Vec3 & origin, const Vec3 & direction, Vec3 &outtri, Vec3 & normal) const;
///read/write IO operations (ascii format)
void ReadFrom(std::istream & openfile);
void ReadFromYZX(std::istream & openfile);
void WriteTo(std::ostream & openfile) const;
///flip points on both axes
void Reverse();
///a diagnostic function that checks for a twisted bezier. returns true if there is a problem.
bool CheckForProblems() const;
///halve the bezier defined by the given size 4 points4 array into the output size 4 arrays left4 and right4
void DeCasteljauHalveCurve(Vec3 * points4, Vec3 * left4, Vec3 * right4) const;
///access corners of the patch (front left, front right, back left, back right)
const Vec3 & GetFL() const {return points[0][0];}
const Vec3 & GetFR() const {return points[0][3];}
const Vec3 & GetBL() const {return points[3][0];}
const Vec3 & GetBR() const {return points[3][3];}
///get the AABB that encloses this BEZIER
Aabb <float> GetAABB() const;
///access the bezier points where x = n % 4 and y = n / 4
const Vec3 & operator[](const int n) const
{
assert(n < 16);
int x = n % 4;
int y = n / 4;
return points[x][y];
}
const Vec3 & GetPoint(const unsigned int x, const unsigned int y) const
{
assert(x < 4);
assert(y < 4);
return points[x][y];
}
///return the 3D point on the bezier surface at the given normalized coordinates px and py
Vec3 SurfCoord(float px, float py) const;
///return the normal of the bezier surface at the given normalized coordinates px and py
Vec3 SurfNorm(float px, float py) const;
private:
Vec3 points[4][4];
///return the bernstein given the normalized coordinate u (zero to one) and an array of four points p
Vec3 Bernstein(float u, const Vec3 p[]) const;
///return the bernstein tangent given the normalized coordinate u (zero to one) and an array of four points p
Vec3 BernsteinTangent(float u, const Vec3 p[]) const;
///return true if the ray at orig with direction dir intersects the given quadrilateral.
/// also put the collision depth in t and the collision coordinates in u,v
bool IntersectQuadrilateralF(
const Vec3 & orig,
const Vec3 & dir,
const Vec3 & v_00,
const Vec3 & v_10,
const Vec3 & v_11,
const Vec3 & v_01,
float &t, float &u, float &v) const;
};
std::ostream & operator << (std::ostream &os, const Bezier & b);
#endif