hermiteCurve.h
```00001 // Filename: hermiteCurve.h
00002 // Created by:  drose (27Feb98)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014
00015 #ifndef HERMITECURVE_H
00016 #define HERMITECURVE_H
00017
00018 #include "piecewiseCurve.h"
00019 #include "cubicCurveseg.h"
00020
00021
00022 BEGIN_PUBLISH //[
00023 // Hermite curve continuity types.
00024 #define HC_CUT         1
00025 // The curve is disconnected at this point.  All points between
00026 // this and the following CV are not part of the curve.
00027
00028 #define HC_FREE        2
00029 // Tangents are unconstrained.  The curve is continuous, but its first
00030 // derivative is not.  This is G0 geometric continuity.
00031
00032 #define HC_G1          3
00033 // Tangents are constrained to be collinear.  The curve's derivative
00034 // is not continuous in parametric space, but its geometric slope is.
00035 // The distinction is mainly relevant in the context of animation
00036 // along the curve--when crossing the join point, direction of motion
00037 // will change continuously, but the speed of motion may change
00038 // suddenly.  This is G1 geometric continuity.
00039
00040 #define HC_SMOOTH     4
00041 // Tangents are constrained to be identical.  The curve and its first
00042 // derivative are continuous in parametric space.  When animating
00043 // motion across the join point, speed and direction of motion will
00044 // change continuously.  This is C1 parametric continuity.
00045 END_PUBLISH //]
00046
00047 ////////////////////////////////////////////////////////////////////
00048 //       Class : HermiteCurveCV
00049 // Description : A single CV of a Hermite curve.  Hermite curve CV's
00050 //               include an in and out tangent, as well as a position.
00051 ////////////////////////////////////////////////////////////////////
00052 class HermiteCurveCV {
00053 public:
00054   HermiteCurveCV();
00055   HermiteCurveCV(const HermiteCurveCV &c);
00056   ~HermiteCurveCV();
00057
00058   void set_point(const LVecBase3 &point) { _p = point; }
00059   void set_in(const LVecBase3 &in);
00060   void set_out(const LVecBase3 &out);
00061   void set_type(int type);
00062   void set_name(const string &name);
00063
00064   void format_egg(ostream &out, int indent, int num_dimensions,
00065               bool show_in, bool show_out,
00066               PN_stdfloat scale_in, PN_stdfloat scale_out) const;
00067
00068   void write_datagram(BamWriter *manager, Datagram &me) const;
00069   void fillin(DatagramIterator &scan, BamReader *manager);
00070
00071   LVecBase3 _p, _in, _out;
00072   int _type;
00073   string _name;
00074 };
00075
00076 ////////////////////////////////////////////////////////////////////
00077 //       Class : HermiteCurve
00078 // Description : A parametric curve defined by a sequence of control
00079 //               vertices, each with an in and out tangent.
00080 //
00081 //               This class is actually implemented as a
00082 //               PiecewiseCurve made up of several CubicCurvesegs,
00083 //               each of which is created using the hermite_basis()
00084 //               method.  The HermiteCurve class itself keeps its own
00085 //               list of the CV's that are used to define the curve
00086 //               (since the CubicCurveseg class doesn't retain these).
00087 ////////////////////////////////////////////////////////////////////
00088 class HermiteCurve : public PiecewiseCurve {
00089 PUBLISHED:
00090   HermiteCurve();
00091   HermiteCurve(const ParametricCurve &pc);
00092   virtual ~HermiteCurve();
00093
00094   int get_num_cvs() const;
00095
00096   int insert_cv(PN_stdfloat t);
00097   int append_cv(int type, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00098   inline int append_cv(int type, const LVecBase3 &v) {
00099     return append_cv(type, v[0], v[1], v[2]);
00100   }
00101
00102   bool remove_cv(int n);
00103   void remove_all_cvs();
00104
00105   bool set_cv_type(int n, int type);
00106   bool set_cv_point(int n, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00107   inline bool set_cv_point(int n, const LVecBase3 &v) {
00108     return set_cv_point(n, v[0], v[1], v[2]);
00109   }
00110   bool set_cv_in(int n, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00111   inline bool set_cv_in(int n, const LVecBase3 &v) {
00112     return set_cv_in(n, v[0], v[1], v[2]);
00113   }
00114   bool set_cv_out(int n, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00115   inline bool set_cv_out(int n, const LVecBase3 &v) {
00116     return set_cv_out(n, v[0], v[1], v[2]);
00117   }
00118   bool set_cv_tstart(int n, PN_stdfloat tstart);
00119   bool set_cv_name(int n, const char *name);
00120
00121
00122   int get_cv_type(int n) const;
00123   const LVecBase3 &get_cv_point(int n) const;
00124   void get_cv_point(int n, LVecBase3 &v) const;
00125   const LVecBase3 &get_cv_in(int n) const;
00126   void get_cv_in(int n, LVecBase3 &v) const;
00127   const LVecBase3 &get_cv_out(int n) const;
00128   void get_cv_out(int n, LVecBase3 &v) const;
00129   PN_stdfloat get_cv_tstart(int n) const;
00130   string get_cv_name(int n) const;
00131
00132   virtual void output(ostream &out) const;
00133   void write_cv(ostream &out, int n) const;
00134
00135 public:
00136
00137   CubicCurveseg *get_curveseg(int ti) {
00138     return (CubicCurveseg *)PiecewiseCurve::get_curveseg(ti);
00139   }
00140
00141   virtual bool
00142   rebuild_curveseg(int rtype0, PN_stdfloat t0, const LVecBase4 &v0,
00143                    int rtype1, PN_stdfloat t1, const LVecBase4 &v1,
00144                    int rtype2, PN_stdfloat t2, const LVecBase4 &v2,
00145                    int rtype3, PN_stdfloat t3, const LVecBase4 &v3);
00146
00147 protected:
00148   virtual bool format_egg(ostream &out, const string &name,
00149                           const string &curve_type, int indent_level) const;
00150
00151   void invalidate_cv(int n, bool redo_all);
00152   int find_cv(PN_stdfloat t);
00153   void recompute_basis();
00154
00155   pvector<HermiteCurveCV> _points;
00156
00157 // TypedWritable stuff
00158 public:
00160
00161 protected:
00162   static TypedWritable *make_HermiteCurve(const FactoryParams &params);
00163   virtual void write_datagram(BamWriter *manager, Datagram &me);
00164   void fillin(DatagramIterator &scan, BamReader *manager);
00165
00166 public:
00167   static TypeHandle get_class_type() {
00168     return _type_handle;
00169   }
00170   static void init_type() {
00171     register_type(_type_handle, "HermiteCurve");
00172   }
00173   virtual TypeHandle get_type() const {
00174     return get_class_type();
00175   }
00176   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00177
00178 private:
00179   static TypeHandle _type_handle;
00180 };
00181
00182 #endif
```