Panda3D
 All Classes Functions Variables Enumerations
nurbsCurveInterface.cxx
00001 // Filename: nurbsCurveInterface.cxx
00002 // Created by:  drose (02Mar01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "nurbsCurveInterface.h"
00016 #include "parametricCurve.h"
00017 #include "config_parametrics.h"
00018 
00019 TypeHandle NurbsCurveInterface::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: NurbsCurveInterface::Destructor
00023 //       Access: Published, Virtual
00024 //  Description: 
00025 ////////////////////////////////////////////////////////////////////
00026 NurbsCurveInterface::
00027 ~NurbsCurveInterface() {
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: NurbsCurveInterface::set_cv_weight
00032 //       Access: Published
00033 //  Description: Sets the weight of the indicated CV without affecting
00034 //               its position in 3-d space.
00035 ////////////////////////////////////////////////////////////////////
00036 bool NurbsCurveInterface::
00037 set_cv_weight(int n, PN_stdfloat w) {
00038   nassertr(n >= 0 && n < get_num_cvs(), false);
00039   LVecBase4 cv = get_cv(n);
00040   if (cv[3] == 0.0f) {
00041     cv.set(0.0f, 0.0f, 0.0f, w);
00042   } else {
00043     cv *= w / cv[3];
00044   }
00045   return set_cv(n, cv);
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: NurbsCurveInterface::write_cv
00050 //       Access: Published
00051 //  Description:
00052 ////////////////////////////////////////////////////////////////////
00053 void NurbsCurveInterface::
00054 write_cv(ostream &out, int n) const {
00055   nassertv(n >= 0 && n < get_num_cvs());
00056 
00057   out << "CV " << n << ": " << get_cv_point(n) << ", weight "
00058       << get_cv_weight(n) << "\n";
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: NurbsCurveInterface::write
00063 //       Access: Protected
00064 //  Description:
00065 ////////////////////////////////////////////////////////////////////
00066 void NurbsCurveInterface::
00067 write(ostream &out, int indent_level) const {
00068   indent(out, indent_level);
00069 
00070   PN_stdfloat min_t = 0.0f;
00071   PN_stdfloat max_t = 0.0f;
00072 
00073   if (get_num_knots() > 0) {
00074     min_t = get_knot(0);
00075     max_t = get_knot(get_num_knots() - 1);
00076   }
00077 
00078   out << "NurbsCurve, order " << get_order() << ", " << get_num_cvs()
00079       << " CV's.  t ranges from " << min_t << " to " << max_t << ".\n";
00080 
00081   indent(out, indent_level)
00082     << "CV's:\n";
00083   int i;
00084   int num_cvs = get_num_cvs();
00085   for (i = 0; i < num_cvs; i++) {
00086     indent(out, indent_level)
00087       << i << ") " << get_cv_point(i) << ", weight "
00088       << get_cv_weight(i) << "\n";
00089   }
00090 
00091   indent(out, indent_level)
00092     << "Knots: ";
00093   int num_knots = get_num_knots();
00094   for (i = 0; i < num_knots; i++) {
00095     out << " " << get_knot(i);
00096   }
00097   out << "\n";
00098 }
00099 
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: NurbsCurveInterface::format_egg
00103 //       Access: Protected
00104 //  Description: Formats the Nurbs curve for output to an Egg file.
00105 ////////////////////////////////////////////////////////////////////
00106 bool NurbsCurveInterface::
00107 format_egg(ostream &out, const string &name, const string &curve_type,
00108            int indent_level) const {
00109   indent(out, indent_level)
00110     << "<VertexPool> " << name << ".pool {\n";
00111 
00112   int num_cvs = get_num_cvs();
00113   int cv;
00114   for (cv = 0; cv < get_num_cvs(); cv++) {
00115     indent(out, indent_level+2)
00116       << "<Vertex> " << cv << " { " << get_cv(cv) << " }\n";
00117   }
00118   indent(out, indent_level)
00119     << "}\n";
00120 
00121   indent(out, indent_level)
00122     << "<NurbsCurve> " << name << " {\n";
00123 
00124   if (!curve_type.empty()) {
00125     indent(out, indent_level+2)
00126       << "<Scalar> type { " << curve_type << " }\n";
00127   }
00128 
00129   indent(out, indent_level+2) << "<Order> { " << get_order() << " }\n";
00130 
00131   indent(out, indent_level+2) << "<Knots> {";
00132   int k;
00133   int num_knots = get_num_knots();
00134 
00135   for (k = 0; k < num_knots; k++) {
00136     if (k%6 == 0) {
00137       out << "\n";
00138       indent(out, indent_level+4);
00139     }
00140     out << get_knot(k) << " ";
00141   }
00142   out << "\n";
00143   indent(out, indent_level+2) << "}\n";
00144 
00145   indent(out, indent_level+2) << "<VertexRef> {";
00146   for (cv = 0; cv < num_cvs; cv++) {
00147     if (cv%10 == 0) {
00148       out << "\n";
00149       indent(out, indent_level+3);
00150     }
00151     out << " " << cv;
00152   }
00153   out << "\n";
00154   indent(out, indent_level+4)
00155     << "<Ref> { " << name << ".pool }\n";
00156   indent(out, indent_level+2) << "}\n";
00157 
00158   indent(out, indent_level) << "}\n";
00159 
00160   return true;
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: NurbsCurveInterface::convert_to_nurbs
00165 //       Access: Protected
00166 //  Description: Stores in the indicated NurbsCurve a NURBS
00167 //               representation of an equivalent curve.  Returns true
00168 //               if successful, false otherwise.
00169 ////////////////////////////////////////////////////////////////////
00170 bool NurbsCurveInterface::
00171 convert_to_nurbs(ParametricCurve *nc) const {
00172   NurbsCurveInterface *nurbs = nc->get_nurbs_interface();
00173   nassertr(nurbs != (NurbsCurveInterface *)NULL, false);
00174 
00175   nurbs->remove_all_cvs();
00176   nurbs->set_order(get_order());
00177 
00178   int num_cvs = get_num_cvs();
00179   int i;
00180   for (i = 0; i < num_cvs; i++) {
00181     nurbs->append_cv(get_cv(i));
00182   }
00183 
00184   int num_knots = get_num_knots();
00185   for (i = 0; i < num_knots; i++) {
00186     nurbs->set_knot(i, get_knot(i));
00187   }
00188 
00189   return nc->recompute();
00190 }
 All Classes Functions Variables Enumerations