15 #include "nurbsSurfaceResult.h"
16 #include "nurbsVertex.h"
30 int num_u_vertices,
int num_v_vertices) :
34 _num_u_vertices(num_u_vertices),
35 _num_v_vertices(num_v_vertices)
46 int num_segments = num_u_segments * num_v_segments;
48 _composed.reserve(num_segments);
49 for (
int i = 0; i < num_segments; i++) {
50 _composed.push_back(ComposedMats());
53 for (
int vi = 0; vi < num_v_segments; vi++) {
57 nassertv(vn >= 0 && vn + v_order - 1 < _num_v_vertices);
59 for (
int ui = 0; ui < num_u_segments; ui++) {
63 nassertv(un >= 0 && un + u_order - 1 < _num_u_vertices);
67 LMatrix4 geom_x, geom_y, geom_z, geom_w;
68 memset(&geom_x, 0,
sizeof(geom_x));
69 memset(&geom_y, 0,
sizeof(geom_y));
70 memset(&geom_z, 0,
sizeof(geom_z));
71 memset(&geom_w, 0,
sizeof(geom_w));
73 for (
int uni = 0; uni < 4; uni++) {
74 for (
int vni = 0; vni < 4; vni++) {
75 if (uni < u_order && vni < v_order) {
76 const LVecBase4 &vec = vecs[verti(un + uni, vn + vni)];
77 geom_x(uni, vni) = vec[0];
78 geom_y(uni, vni) = vec[1];
79 geom_z(uni, vni) = vec[2];
80 geom_w(uni, vni) = vec[3];
89 nassertv(i >= 0 && i < (
int)_composed.size());
90 ComposedMats &result = _composed[i];
91 result._x = u_basis_mat * geom_x * v_basis_transpose;
92 result._y = u_basis_mat * geom_y * v_basis_transpose;
93 result._z = u_basis_mat * geom_z * v_basis_transpose;
94 result._w = u_basis_mat * geom_w * v_basis_transpose;
119 int i = segi(ui, vi);
120 nassertv(i >= 0 && i < (
int)_composed.size());
122 PN_stdfloat u2 = u*u;
124 PN_stdfloat v2 = v*v;
127 PN_stdfloat weight = vvec.dot(uvec * _composed[i]._w);
129 point.set(vvec.dot(uvec * _composed[i]._x) / weight,
130 vvec.dot(uvec * _composed[i]._y) / weight,
131 vvec.dot(uvec * _composed[i]._z) / weight);
144 int i = segi(ui, vi);
145 nassertv(i >= 0 && i < (
int)_composed.size());
147 PN_stdfloat u2 = u*u;
149 LVecBase4 duvec(3.0f * u2, 2.0f * u, 1.0f, 0.0f);
150 PN_stdfloat v2 = v*v;
152 LVecBase4 dvvec(3.0f * v2, 2.0f * v, 1.0f, 0.0f);
154 LVector3 utan(vvec.dot(duvec * _composed[i]._x),
155 vvec.dot(duvec * _composed[i]._y),
156 vvec.dot(duvec * _composed[i]._z));
158 LVector3 vtan(dvvec.dot(uvec * _composed[i]._x),
159 dvvec.dot(uvec * _composed[i]._y),
160 dvvec.dot(uvec * _composed[i]._z));
162 normal = utan.cross(vtan);
174 int i = segi(ui, vi);
175 nassertr(i >= 0 && i < (
int)_composed.size(), 0.0f);
177 PN_stdfloat u2 = u*u;
179 PN_stdfloat v2 = v*v;
182 PN_stdfloat weight = vvec.dot(uvec * _composed[i]._w);
195 memset(&geom, 0,
sizeof(geom));
197 for (
int uni = 0; uni < 4; uni++) {
198 for (
int vni = 0; vni < 4; vni++) {
199 if (uni < u_order && vni < v_order) {
205 LMatrix4 composed = u_basis_mat * geom * v_basis_transpose;
206 return vvec.dot(uvec * composed) / weight;
220 PN_stdfloat result[],
int num_values)
const {
221 int i = segi(ui, vi);
222 nassertv(i >= 0 && i < (
int)_composed.size());
224 PN_stdfloat u2 = u*u;
226 PN_stdfloat v2 = v*v;
229 PN_stdfloat weight = vvec.dot(uvec * _composed[i]._w);
241 for (
int n = 0; n < num_values; n++) {
243 memset(&geom, 0,
sizeof(geom));
245 for (
int uni = 0; uni < 4; uni++) {
246 for (
int vni = 0; vni < 4; vni++) {
247 if (uni < u_order && vni < v_order) {
254 LMatrix4 composed = u_basis_mat * geom * v_basis_transpose;
255 result[n] = vvec.dot(uvec * composed) / weight;
266 int NurbsSurfaceResult::
267 find_u_segment(PN_stdfloat u) {
277 if (_last_u_segment != -1 && (u >= _last_u_from && u < _last_u_to)) {
278 return _last_u_segment;
284 _last_u_segment = segment;
285 _last_u_from = _u_basis.
get_from(segment);
286 _last_u_to = _u_basis.
get_to(segment);
299 int NurbsSurfaceResult::
300 r_find_u_segment(PN_stdfloat u,
int top,
int bot)
const {
305 int mid = (top + bot) / 2;
308 PN_stdfloat from = _u_basis.
get_from(mid);
309 PN_stdfloat to = _u_basis.
get_to(mid);
312 return r_find_u_segment(u, top, mid - 1);
314 }
else if (to <= u) {
316 return r_find_u_segment(u, mid + 1, bot);
332 int NurbsSurfaceResult::
333 find_v_segment(PN_stdfloat v) {
343 if (_last_v_segment != -1 && (v >= _last_v_from && v < _last_v_to)) {
344 return _last_v_segment;
350 _last_v_segment = segment;
351 _last_v_from = _v_basis.
get_from(segment);
352 _last_v_to = _v_basis.
get_to(segment);
365 int NurbsSurfaceResult::
366 r_find_v_segment(PN_stdfloat v,
int top,
int bot)
const {
371 int mid = (top + bot) / 2;
374 PN_stdfloat from = _v_basis.
get_from(mid);
375 PN_stdfloat to = _v_basis.
get_to(mid);
378 return r_find_v_segment(v, top, mid - 1);
380 }
else if (to <= v) {
382 return r_find_v_segment(v, mid + 1, bot);
This is the base class for all three-component vectors and points.
int get_num_segments() const
Returns the number of piecewise continuous segments in the curve.
NurbsSurfaceResult(const NurbsBasisVector &u_basis, const NurbsBasisVector &v_basis, const LVecBase4 vecs[], const NurbsVertex *verts, int num_u_vertices, int num_v_vertices)
The constructor automatically builds up the result as the product of the indicated set of basis matri...
PN_stdfloat get_extended_vertex(int d) const
Returns an n-dimensional vertex value.
PN_stdfloat get_from(int segment) const
Returns the t value of the beginning of this segment.
void transpose()
Transposes the basis matrices stored in the vector.
void eval_segment_normal(int ui, int vi, PN_stdfloat u, PN_stdfloat v, LVecBase3 &normal) const
As eval_segment_point, but computes the normal to the surface at the indicated point.
PN_stdfloat get_end_u() const
Returns the last legal value of u on the surface.
int get_order() const
Returns the order of the segments in the curve.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
This represents a single control vertex in a NurbsEvaluator.
PN_stdfloat eval_segment_extended_point(int ui, int vi, PN_stdfloat u, PN_stdfloat v, int d) const
Evaluates the surface in n-dimensional space according to the extended vertices associated with the s...
void eval_segment_extended_points(int ui, int vi, PN_stdfloat u, PN_stdfloat v, int d, PN_stdfloat result[], int num_values) const
Simultaneously performs eval_extended_point on a contiguous sequence of dimensions.
PN_stdfloat get_end_v() const
Returns the last legal value of v on the surface.
PN_stdfloat get_start_u() const
Returns the first legal value of u on the surface.
This encapsulates a series of matrices that are used to represent the sequential segments of a NurbsC...
This is a 4-by-4 transform matrix.
const LMatrix4 & get_basis(int segment) const
Returns the basis matrix associated with the nth segment.
int get_vertex_index(int segment) const
Returns the vertex index of the nth segment.
void eval_segment_point(int ui, int vi, PN_stdfloat u, PN_stdfloat v, LVecBase3 &point) const
Evaluates the point on the surface corresponding to the indicated value in parametric time within the...
This is the base class for all three-component vectors and points.
PN_stdfloat get_to(int segment) const
Returns the t value of the end of this segment.
PN_stdfloat get_start_v() const
Returns the first legal value of v on the surface.