Panda3D
nurbsCurveInterface.cxx
1 // Filename: nurbsCurveInterface.cxx
2 // Created by: drose (02Mar01)
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 #include "nurbsCurveInterface.h"
16 #include "parametricCurve.h"
17 #include "config_parametrics.h"
18 
19 TypeHandle NurbsCurveInterface::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: NurbsCurveInterface::Destructor
23 // Access: Published, Virtual
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 NurbsCurveInterface::
27 ~NurbsCurveInterface() {
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: NurbsCurveInterface::set_cv_weight
32 // Access: Published
33 // Description: Sets the weight of the indicated CV without affecting
34 // its position in 3-d space.
35 ////////////////////////////////////////////////////////////////////
37 set_cv_weight(int n, PN_stdfloat w) {
38  nassertr(n >= 0 && n < get_num_cvs(), false);
39  LVecBase4 cv = get_cv(n);
40  if (cv[3] == 0.0f) {
41  cv.set(0.0f, 0.0f, 0.0f, w);
42  } else {
43  cv *= w / cv[3];
44  }
45  return set_cv(n, cv);
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: NurbsCurveInterface::write_cv
50 // Access: Published
51 // Description:
52 ////////////////////////////////////////////////////////////////////
53 void NurbsCurveInterface::
54 write_cv(ostream &out, int n) const {
55  nassertv(n >= 0 && n < get_num_cvs());
56 
57  out << "CV " << n << ": " << get_cv_point(n) << ", weight "
58  << get_cv_weight(n) << "\n";
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: NurbsCurveInterface::write
63 // Access: Protected
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 void NurbsCurveInterface::
67 write(ostream &out, int indent_level) const {
68  indent(out, indent_level);
69 
70  PN_stdfloat min_t = 0.0f;
71  PN_stdfloat max_t = 0.0f;
72 
73  if (get_num_knots() > 0) {
74  min_t = get_knot(0);
75  max_t = get_knot(get_num_knots() - 1);
76  }
77 
78  out << "NurbsCurve, order " << get_order() << ", " << get_num_cvs()
79  << " CV's. t ranges from " << min_t << " to " << max_t << ".\n";
80 
81  indent(out, indent_level)
82  << "CV's:\n";
83  int i;
84  int num_cvs = get_num_cvs();
85  for (i = 0; i < num_cvs; i++) {
86  indent(out, indent_level)
87  << i << ") " << get_cv_point(i) << ", weight "
88  << get_cv_weight(i) << "\n";
89  }
90 
91  indent(out, indent_level)
92  << "Knots: ";
93  int num_knots = get_num_knots();
94  for (i = 0; i < num_knots; i++) {
95  out << " " << get_knot(i);
96  }
97  out << "\n";
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: NurbsCurveInterface::format_egg
103 // Access: Protected
104 // Description: Formats the Nurbs curve for output to an Egg file.
105 ////////////////////////////////////////////////////////////////////
106 bool NurbsCurveInterface::
107 format_egg(ostream &out, const string &name, const string &curve_type,
108  int indent_level) const {
109  indent(out, indent_level)
110  << "<VertexPool> " << name << ".pool {\n";
111 
112  int num_cvs = get_num_cvs();
113  int cv;
114  for (cv = 0; cv < get_num_cvs(); cv++) {
115  indent(out, indent_level+2)
116  << "<Vertex> " << cv << " { " << get_cv(cv) << " }\n";
117  }
118  indent(out, indent_level)
119  << "}\n";
120 
121  indent(out, indent_level)
122  << "<NurbsCurve> " << name << " {\n";
123 
124  if (!curve_type.empty()) {
125  indent(out, indent_level+2)
126  << "<Scalar> type { " << curve_type << " }\n";
127  }
128 
129  indent(out, indent_level+2) << "<Order> { " << get_order() << " }\n";
130 
131  indent(out, indent_level+2) << "<Knots> {";
132  int k;
133  int num_knots = get_num_knots();
134 
135  for (k = 0; k < num_knots; k++) {
136  if (k%6 == 0) {
137  out << "\n";
138  indent(out, indent_level+4);
139  }
140  out << get_knot(k) << " ";
141  }
142  out << "\n";
143  indent(out, indent_level+2) << "}\n";
144 
145  indent(out, indent_level+2) << "<VertexRef> {";
146  for (cv = 0; cv < num_cvs; cv++) {
147  if (cv%10 == 0) {
148  out << "\n";
149  indent(out, indent_level+3);
150  }
151  out << " " << cv;
152  }
153  out << "\n";
154  indent(out, indent_level+4)
155  << "<Ref> { " << name << ".pool }\n";
156  indent(out, indent_level+2) << "}\n";
157 
158  indent(out, indent_level) << "}\n";
159 
160  return true;
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: NurbsCurveInterface::convert_to_nurbs
165 // Access: Protected
166 // Description: Stores in the indicated NurbsCurve a NURBS
167 // representation of an equivalent curve. Returns true
168 // if successful, false otherwise.
169 ////////////////////////////////////////////////////////////////////
170 bool NurbsCurveInterface::
171 convert_to_nurbs(ParametricCurve *nc) const {
173  nassertr(nurbs != (NurbsCurveInterface *)NULL, false);
174 
175  nurbs->remove_all_cvs();
176  nurbs->set_order(get_order());
177 
178  int num_cvs = get_num_cvs();
179  int i;
180  for (i = 0; i < num_cvs; i++) {
181  nurbs->append_cv(get_cv(i));
182  }
183 
184  int num_knots = get_num_knots();
185  for (i = 0; i < num_knots; i++) {
186  nurbs->set_knot(i, get_knot(i));
187  }
188 
189  return nc->recompute();
190 }
A virtual base class for parametric curves.
virtual bool recompute()
Recalculates the curve, if necessary.
This abstract class defines the interface only for a Nurbs-style curve, with knots and coordinates in...
virtual NurbsCurveInterface * get_nurbs_interface()
Returns a pointer to the object as a NurbsCurveInterface object if it happens to be a NURBS-style cur...
PN_stdfloat get_cv_weight(int n) const
Returns the weight of the indicated CV.
bool set_cv_weight(int n, PN_stdfloat w)
Sets the weight of the indicated CV without affecting its position in 3-d space.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
LVecBase3 get_cv_point(int n) const
Returns the position of the indicated CV.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85