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