Panda3D
 All Classes Functions Variables Enumerations
cubicCurveseg.h
1 // Filename: cubicCurveseg.h
2 // Created by: drose (04Mar01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef CUBICCURVESEG_H
16 #define CUBICCURVESEG_H
17 
18 #include "pandabase.h"
19 
20 #include "parametricCurve.h"
21 
22 
23 // These symbols are used to define the shape of the curve segment to
24 // CubicCurveseg::compute_seg().
25 
26 #define RT_POINT 0x01
27 #define RT_TANGENT 0x02
28 #define RT_CV 0x03
29 #define RT_BASE_TYPE 0xff
30 
31 #define RT_KEEP_ORIG 0x100
32 
33 
34 ////////////////////////////////////////////////////////////////////
35 // Class : CubicCurveseg
36 // Description : A CubicCurveseg is any curve that can be completely
37 // described by four 4-valued basis vectors, one for
38 // each dimension in three-space, and one for the
39 // homogeneous coordinate. This includes Beziers,
40 // Hermites, and NURBS.
41 //
42 // This class encapsulates a single curve segment of the
43 // cubic curve. Normally, when we think of Bezier and
44 // Hermite curves, we think of a piecewise collection of
45 // such segments.
46 //
47 // Although this class includes methods such as
48 // hermite_basis() and nurbs_basis(), to generate a
49 // Hermite and NURBS curve segment, respectively, only
50 // the final basis vectors are stored: the product of
51 // the basis matrix of the corresponding curve type, and
52 // its geometry vectors. This is the minimum
53 // information needed to evaluate the curve. However,
54 // the individual CV's that were used to compute these
55 // basis vectors are not retained; this might be handled
56 // in a subclass (for instance, HermiteCurve).
57 ////////////////////////////////////////////////////////////////////
58 class EXPCL_PANDA_PARAMETRICS CubicCurveseg : public ParametricCurve {
59 PUBLISHED:
60  virtual bool get_point(PN_stdfloat t, LVecBase3 &point) const;
61  virtual bool get_tangent(PN_stdfloat t, LVecBase3 &tangent) const;
62  virtual bool get_pt(PN_stdfloat t, LVecBase3 &point, LVecBase3 &tangent) const;
63  virtual bool get_2ndtangent(PN_stdfloat t, LVecBase3 &tangent2) const;
64 
65 public:
66  CubicCurveseg();
67  CubicCurveseg(const LMatrix4 &basis);
68  CubicCurveseg(const BezierSeg &seg);
69  CubicCurveseg(int order, const PN_stdfloat knots[], const LVecBase4 cvs[]);
70 
71  virtual ~CubicCurveseg();
72 
73  void hermite_basis(const HermiteCurveCV &cv0,
74  const HermiteCurveCV &cv1,
75  PN_stdfloat tlength = 1.0f);
76  void bezier_basis(const BezierSeg &seg);
77  void nurbs_basis(int order, const PN_stdfloat knots[], const LVecBase4 cvs[]);
78 
79  // evaluate_point() and evaluate_vector() both evaluate the curve at
80  // a given point by applying the basis vector against the vector
81  // [t3 t2 t 1] (or some derivative). The difference between the
82  // two is that evaluate_point() is called only with the vector
83  // [t3 t2 t 1] and computes a point in three-space and will scale by
84  // the homogeneous coordinate when the curve demands it (e.g. a
85  // NURBS), while evaluate_vector() is called with some derivative
86  // vector like [3t2 2t 1 0] and computes a vector difference between
87  // points, and will never scale by the homogeneous coordinate (which
88  // would be zero anyway).
89 
90  void evaluate_point(const LVecBase4 &tv, LVecBase3 &result) const {
91  PN_stdfloat recip_h = (rational) ? 1.0f/tv.dot(Bw) : 1.0f;
92  result.set(tv.dot(Bx) * recip_h,
93  tv.dot(By) * recip_h,
94  tv.dot(Bz) * recip_h);
95  }
96 
97  void evaluate_vector(const LVecBase4 &tv, LVecBase3 &result) const {
98  result.set(tv.dot(Bx),
99  tv.dot(By),
100  tv.dot(Bz));
101  }
102 
103  virtual bool get_bezier_seg(BezierSeg &seg) const;
104 
105  static bool compute_seg(int rtype0, PN_stdfloat t0, const LVecBase4 &v0,
106  int rtype1, PN_stdfloat t1, const LVecBase4 &v1,
107  int rtype2, PN_stdfloat t2, const LVecBase4 &v2,
108  int rtype3, PN_stdfloat t3, const LVecBase4 &v3,
109  const LMatrix4 &B,
110  const LMatrix4 &Bi,
111  LMatrix4 &G);
112 
113  LVecBase4 Bx, By, Bz, Bw;
114  bool rational;
115 
116 
117 // TypedWritable stuff
118 public:
119  static void register_with_read_factory();
120 
121 protected:
122  static TypedWritable *make_CubicCurveseg(const FactoryParams &params);
123  virtual void write_datagram(BamWriter *manager, Datagram &me);
124  void fillin(DatagramIterator &scan, BamReader *manager);
125 
126 public:
127  static TypeHandle get_class_type() {
128  return _type_handle;
129  }
130  static void init_type() {
131  ParametricCurve::init_type();
132  register_type(_type_handle, "CubicCurveseg",
133  ParametricCurve::get_class_type());
134  }
135  virtual TypeHandle get_type() const {
136  return get_class_type();
137  }
138  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
139 
140 private:
141  static TypeHandle _type_handle;
142 };
143 
144 // This function is used internally to build the NURBS basis matrix
145 // based on a given knot sequence.
146 void compute_nurbs_basis(int order,
147  const PN_stdfloat knots_in[],
148  LMatrix4 &basis);
149 
150 
151 #endif
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
A virtual base class for parametric curves.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
static void register_with_read_factory()
Tells the BamReader how to create objects of type PandaNode.
Definition: pandaNode.cxx:4153
virtual bool get_bezier_seg(BezierSeg &) const
Fills the BezierSeg structure with a description of the curve segment as a Bezier, if possible, but does not change the _t member of the structure.
A CubicCurveseg is any curve that can be completely described by four 4-valued basis vectors...
Definition: cubicCurveseg.h:58
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A single CV of a Hermite curve.
Definition: hermiteCurve.h:52
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43