Panda3D
nurbsCurveInterface.cxx
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 nurbsCurveInterface.cxx
10  * @author drose
11  * @date 2001-03-02
12  */
13 
14 #include "nurbsCurveInterface.h"
15 #include "parametricCurve.h"
16 #include "config_parametrics.h"
17 
18 TypeHandle NurbsCurveInterface::_type_handle;
19 
20 /**
21  *
22  */
23 NurbsCurveInterface::
24 ~NurbsCurveInterface() {
25 }
26 
27 /**
28  * Sets the weight of the indicated CV without affecting its position in 3-d
29  * space.
30  */
32 set_cv_weight(int n, PN_stdfloat w) {
33  nassertr(n >= 0 && n < get_num_cvs(), false);
34  LVecBase4 cv = get_cv(n);
35  if (cv[3] == 0.0f) {
36  cv.set(0.0f, 0.0f, 0.0f, w);
37  } else {
38  cv *= w / cv[3];
39  }
40  return set_cv(n, cv);
41 }
42 
43 /**
44  *
45  */
46 void NurbsCurveInterface::
47 write_cv(std::ostream &out, int n) const {
48  nassertv(n >= 0 && n < get_num_cvs());
49 
50  out << "CV " << n << ": " << get_cv_point(n) << ", weight "
51  << get_cv_weight(n) << "\n";
52 }
53 
54 /**
55  *
56  */
57 void NurbsCurveInterface::
58 write(std::ostream &out, int indent_level) const {
59  indent(out, indent_level);
60 
61  PN_stdfloat min_t = 0.0f;
62  PN_stdfloat max_t = 0.0f;
63 
64  if (get_num_knots() > 0) {
65  min_t = get_knot(0);
66  max_t = get_knot(get_num_knots() - 1);
67  }
68 
69  out << "NurbsCurve, order " << get_order() << ", " << get_num_cvs()
70  << " CV's. t ranges from " << min_t << " to " << max_t << ".\n";
71 
72  indent(out, indent_level)
73  << "CV's:\n";
74  int i;
75  int num_cvs = get_num_cvs();
76  for (i = 0; i < num_cvs; i++) {
77  indent(out, indent_level)
78  << i << ") " << get_cv_point(i) << ", weight "
79  << get_cv_weight(i) << "\n";
80  }
81 
82  indent(out, indent_level)
83  << "Knots: ";
84  int num_knots = get_num_knots();
85  for (i = 0; i < num_knots; i++) {
86  out << " " << get_knot(i);
87  }
88  out << "\n";
89 }
90 
91 
92 /**
93  * Formats the Nurbs curve for output to an Egg file.
94  */
95 bool NurbsCurveInterface::
96 format_egg(std::ostream &out, const std::string &name, const std::string &curve_type,
97  int indent_level) const {
98  indent(out, indent_level)
99  << "<VertexPool> " << name << ".pool {\n";
100 
101  int num_cvs = get_num_cvs();
102  int cv;
103  for (cv = 0; cv < get_num_cvs(); cv++) {
104  indent(out, indent_level+2)
105  << "<Vertex> " << cv << " { " << get_cv(cv) << " }\n";
106  }
107  indent(out, indent_level)
108  << "}\n";
109 
110  indent(out, indent_level)
111  << "<NurbsCurve> " << name << " {\n";
112 
113  if (!curve_type.empty()) {
114  indent(out, indent_level+2)
115  << "<Scalar> type { " << curve_type << " }\n";
116  }
117 
118  indent(out, indent_level+2) << "<Order> { " << get_order() << " }\n";
119 
120  indent(out, indent_level+2) << "<Knots> {";
121  int k;
122  int num_knots = get_num_knots();
123 
124  for (k = 0; k < num_knots; k++) {
125  if (k%6 == 0) {
126  out << "\n";
127  indent(out, indent_level+4);
128  }
129  out << get_knot(k) << " ";
130  }
131  out << "\n";
132  indent(out, indent_level+2) << "}\n";
133 
134  indent(out, indent_level+2) << "<VertexRef> {";
135  for (cv = 0; cv < num_cvs; cv++) {
136  if (cv%10 == 0) {
137  out << "\n";
138  indent(out, indent_level+3);
139  }
140  out << " " << cv;
141  }
142  out << "\n";
143  indent(out, indent_level+4)
144  << "<Ref> { " << name << ".pool }\n";
145  indent(out, indent_level+2) << "}\n";
146 
147  indent(out, indent_level) << "}\n";
148 
149  return true;
150 }
151 
152 /**
153  * Stores in the indicated NurbsCurve a NURBS representation of an equivalent
154  * curve. Returns true if successful, false otherwise.
155  */
156 bool NurbsCurveInterface::
157 convert_to_nurbs(ParametricCurve *nc) const {
159  nassertr(nurbs != nullptr, false);
160 
161  nurbs->remove_all_cvs();
162  nurbs->set_order(get_order());
163 
164  int num_cvs = get_num_cvs();
165  int i;
166  for (i = 0; i < num_cvs; i++) {
167  nurbs->append_cv(get_cv(i));
168  }
169 
170  int num_knots = get_num_knots();
171  for (i = 0; i < num_knots; i++) {
172  nurbs->set_knot(i, get_knot(i));
173  }
174 
175  return nc->recompute();
176 }
A virtual base class for parametric curves.
virtual bool recompute()
Recalculates the curve, if necessary.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
bool set_cv_weight(int n, PN_stdfloat w)
Sets the weight of the indicated CV without affecting its position in 3-d space.
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:81