15 #include "piecewiseCurve.h"
16 #include "cubicCurveseg.h"
17 #include "config_parametrics.h"
20 #include "datagramIterator.h"
21 #include "bamWriter.h"
22 #include "bamReader.h"
56 return !_segs.empty();
68 return _segs.empty() ? 0.0f : _segs.back()._tend;
84 bool result = find_curve(curve, t);
89 return result | curve->get_point(t, point);
102 bool result = find_curve(curve, t);
105 return result | curve->get_tangent(t, tangent);
118 bool result = find_curve(curve, t);
121 return result | curve->get_2ndtangent(t, tangent2);
133 PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz) {
134 if (parametrics_cat.is_debug()) {
135 parametrics_cat.debug()
136 <<
"Adjusting point at " << t <<
" to " << px <<
" " << py <<
" "
141 bool result = find_curve(curve, t);
144 cerr <<
"No curve segment at t = " << t <<
"\n";
149 RT_POINT, t,
LVecBase4(px, py, pz, 1.0f),
150 RT_TANGENT | RT_KEEP_ORIG, t,
LVecBase4(),
151 RT_CV | RT_KEEP_ORIG, 0.0f,
LVecBase4());
164 PN_stdfloat tx, PN_stdfloat ty, PN_stdfloat tz) {
166 bool result = find_curve(curve, t);
169 cerr <<
"No curve segment at t = " << t <<
"\n";
175 RT_TANGENT, t,
LVecBase4(tx, ty, tz, 0.0f),
176 RT_CV | RT_KEEP_ORIG, 0.0f,
LVecBase4());
188 PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz,
189 PN_stdfloat tx, PN_stdfloat ty, PN_stdfloat tz) {
191 bool result = find_curve(curve, t);
194 cerr <<
"No curve segment at t = " << t <<
"\n";
199 RT_POINT, t,
LVecBase4(px, py, pz, 1.0f),
200 RT_TANGENT, t,
LVecBase4(tx, ty, tz, 0.0f),
201 RT_CV | RT_KEEP_ORIG, 0.0f,
LVecBase4());
215 bool result = find_curve(curve, t);
218 return result | curve->get_pt(t, point, tangent);
241 assert(ti >= 0 && ti < (
int)_segs.size());
242 return _segs[ti]._curve;
261 if (ti < 0 || ti > (
int)_segs.size()) {
265 if (ti == (
int)_segs.size()) {
266 _segs.push_back(Curveseg(seg,
get_max_t() + tlength));
269 _segs.insert(_segs.begin(),
270 Curveseg(seg, tlength));
273 _segs.insert(_segs.begin() + ti,
274 Curveseg(seg, _segs[ti-1]._tend + tlength));
290 if (ti < 0 || ti >= (
int)_segs.size()) {
295 _segs.erase(_segs.begin() + ti);
299 while (ti < (
int)_segs.size()) {
300 _segs[ti]._tend -= tlength;
315 _segs.erase(_segs.begin(), _segs.end());
327 assert(ti >= 0 && ti < (
int)_segs.size());
328 return (ti==0) ? _segs[ti]._tend : _segs[ti]._tend - _segs[ti-1]._tend;
339 assert(ti >= 0 && ti <= (
int)_segs.size());
340 return (ti==0) ? 0.0f : _segs[ti-1]._tend;
351 assert(ti >= 0 && ti < (
int)_segs.size());
352 return _segs[ti]._tend;
366 if (ti < 0 || ti >= (
int)_segs.size()) {
392 const PN_stdfloat knots[],
const LVecBase4 cvs[]) {
395 for (
int i=0; i<num_cvs - order + 1; i++) {
396 if (knots[i+order] > knots[i+order-1]) {
400 knots[i+order] - knots[i+order-1]);
418 bz_segs.erase(bz_segs.begin(), bz_segs.end());
421 for (i = 0; i < (int)_segs.size(); i++) {
422 if (!_segs[i]._curve->get_bezier_seg(seg)) {
425 seg._t = _segs[i]._tend;
426 bz_segs.push_back(seg);
446 cerr <<
"rebuild_curveseg not implemented for this curve type.\n";
462 bool PiecewiseCurve::
469 if (_last_ti>0 && _segs[_last_ti-1]._tend>=t) {
482 for (ti = _last_ti; ti < (int)_segs.size(); ti++) {
483 if (_segs[ti]._tend+0.00001f > t) {
488 if (ti < (
int)_segs.size()) {
491 t = (t - _segs[ti-1]._tend) / (_segs[ti]._tend - _segs[ti-1]._tend);
499 curve = _segs[0]._curve;
504 if (ti >= (
int)_segs.size() || !_segs[ti]._curve->is_valid()) {
505 assert(ti <= (
int)_segs.size());
511 if (ti > 0 && t < _segs[ti-1]._tend+0.0001f) {
516 if (ti >= (
int)_segs.size()) {
522 curve = _segs.back()._curve;
526 }
else if (!_segs[ti]._curve->is_valid()) {
527 curve = _segs[ti]._curve;
537 t *= _segs[ti]._curve->get_max_t();
538 curve = _segs[ti]._curve;
557 PN_stdfloat PiecewiseCurve::
558 current_seg_range(PN_stdfloat t)
const {
561 assert(ti < (
int)_segs.size());
565 t = (t - _segs[ti-1]._tend) / (_segs[ti]._tend - _segs[ti-1]._tend);
579 void PiecewiseCurve::
581 ParametricCurve::write_datagram(manager, me);
585 for (i = 0; i < _segs.size(); i++) {
586 const Curveseg &seg = _segs[i];
602 void PiecewiseCurve::
604 ParametricCurve::fillin(scan, manager);
607 _segs.reserve(num_segs);
609 for (i = 0; i < num_segs; i++) {
614 _segs.push_back(seg);
630 for (i = 0; i < _segs.size(); i++) {
634 return used + _segs.size();
This is the base class for all three-component vectors and points.
ParametricCurve * get_curveseg(int ti)
Returns the curve segment corresponding to the given index.
void remove_all_curvesegs()
Removes all curve segments from the curve.
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
virtual bool adjust_point(PN_stdfloat t, PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz)
Recomputes the curve such that it passes through the point (px, py, pz) at time t, but keeps the same tangent value at that point.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
A virtual base class for parametric curves.
bool insert_curveseg(int ti, ParametricCurve *seg, PN_stdfloat tlength)
Inserts a new curve segment at the indicated index.
Base class for objects that can be written to and read from Bam files.
virtual bool is_valid() const
Returns true if the curve is defined.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
virtual bool adjust_tangent(PN_stdfloat t, PN_stdfloat tx, PN_stdfloat ty, PN_stdfloat tz)
Recomputes the curve such that it has the tangent (tx, ty, tz) at time t, but keeps the same position...
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
A CubicCurveseg is any curve that can be completely described by four 4-valued basis vectors...
This is our own Panda specialization on the default STL vector.
virtual bool get_point(PN_stdfloat t, LVecBase3 &point) const
Returns the point of the curve at a given parametric point t.
virtual PN_stdfloat get_max_t() const
Returns the upper bound of t for the entire curve.
A PiecewiseCurve is a curve made up of several curve segments, connected in a head-to-tail fashion...
PN_stdfloat get_tend(int ti) const
Returns the parametric end of the given segment of the curve.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin()...
PN_stdfloat get_tstart(int ti) const
Returns the parametric start of the given segment of the curve.
virtual bool get_bezier_segs(BezierSegs &bz_segs) const
Fills up the indicated vector with a list of BezierSeg structs that describe the curve.
virtual bool adjust_pt(PN_stdfloat t, PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat tx, PN_stdfloat ty, PN_stdfloat tz)
Recomputes the curve such that it passes through the point (px, py, pz) with the tangent (tx...
bool set_tlength(int ti, PN_stdfloat tlength)
Sets the parametric length of the given segment of the curve.
This is the base class for all three-component vectors and points.
PN_float64 get_float64()
Extracts a 64-bit floating-point number.
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the datagram.
virtual bool get_pt(PN_stdfloat t, LVecBase3 &point, LVecBase3 &tangent) const
Simultaneously returns the point and tangent of the curve at a given parametric point t...
virtual bool get_tangent(PN_stdfloat t, LVecBase3 &tangent) const
Returns the tangent of the curve at a given parametric point t.
virtual bool get_2ndtangent(PN_stdfloat t, LVecBase3 &tangent2) const
Returns the tangent of the first derivative of the curve at the point t.
PN_stdfloat get_tlength(int ti) const
Returns the parametric length of the given segment of the curve.
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void make_nurbs(int order, int num_cvs, const PN_stdfloat knots[], const LVecBase4 cvs[])
Defines the curve as a general NURBS curve.
bool remove_curveseg(int ti)
Removes the given curve segment from the curve and frees it.
virtual bool rebuild_curveseg(int rtype0, PN_stdfloat t0, const LVecBase4 &v0, int rtype1, PN_stdfloat t1, const LVecBase4 &v1, int rtype2, PN_stdfloat t2, const LVecBase4 &v2, int rtype3, PN_stdfloat t3, const LVecBase4 &v3)
Rebuilds the current curve segment (as selected by the most recent call to find_curve()) according to...
int get_num_segs() const
Returns the number of curve segments that make up the Piecewise curve.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.