Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/parametrics/nurbsCurveDrawer.cxx

Go to the documentation of this file.
00001 // Filename: nurbsCurveDrawer.cxx
00002 // Created by:  drose (27Feb98)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "nurbsCurveDrawer.h"
00020 #include "nurbsCurveInterface.h"
00021 #include "parametricCurve.h"
00022 
00023 
00024 TypeHandle NurbsCurveDrawer::_type_handle;
00025 
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: NurbsCurveDrawer::Constructor
00029 //       Access: Published
00030 //  Description:
00031 ////////////////////////////////////////////////////////////////////
00032 NurbsCurveDrawer::
00033 NurbsCurveDrawer() {
00034   set_cv_color(1.0f, 0.0f, 0.0f);
00035   set_hull_color(1.0f, 0.5, 0.5);
00036   set_knot_color(0.0f, 0.0f, 1.0f);
00037 
00038   _cvs.set_thickness(4.0f);
00039   _hull.set_thickness(1.0f);
00040   _knots.set_thickness(4.0f);
00041 
00042   _show_cvs = true;
00043   _show_hull = true;
00044   _show_knots = true;
00045 }
00046 
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: NurbsCurveDrawer::Destructor
00050 //       Access: Published, Virtual
00051 //  Description:
00052 ////////////////////////////////////////////////////////////////////
00053 NurbsCurveDrawer::
00054 ~NurbsCurveDrawer() {
00055 }
00056 
00057 
00058 
00059 ////////////////////////////////////////////////////////////////////
00060 //     Function: NurbsCurveDrawer::set_cv_color
00061 //       Access: Published
00062 //  Description: Specifies the color of the CV's.
00063 ////////////////////////////////////////////////////////////////////
00064 void NurbsCurveDrawer::
00065 set_cv_color(float r, float g, float b) {
00066   _cv_color.set(r, g, b);
00067   _cvs.set_color(r, g, b);
00068 
00069   redraw();
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: NurbsCurveDrawer::set_knot_color
00074 //       Access: Published
00075 //  Description: Specifies the color of the knots.
00076 ////////////////////////////////////////////////////////////////////
00077 void NurbsCurveDrawer::
00078 set_knot_color(float r, float g, float b) {
00079   _knot_color.set(r, g, b);
00080   _knots.set_color(r, g, b);
00081 
00082   redraw();
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: NurbsCurveDrawer::set_hull_color
00087 //       Access: Published
00088 //  Description: Specifies the color of the convex hull.
00089 ////////////////////////////////////////////////////////////////////
00090 void NurbsCurveDrawer::
00091 set_hull_color(float r, float g, float b) {
00092   _hull_color.set(r, g, b);
00093   _hull.set_color(r, g, b);
00094 
00095   redraw();
00096 }
00097 
00098 
00099 
00100 ////////////////////////////////////////////////////////////////////
00101 //     Function: NurbsCurveDrawer::draw
00102 //       Access: Published, Virtual
00103 //  Description:
00104 ////////////////////////////////////////////////////////////////////
00105 bool NurbsCurveDrawer::
00106 draw() {
00107   // First, draw the curve itself.
00108   if (!ParametricCurveDrawer::draw()) {
00109     return false;
00110   }
00111 
00112   ParametricCurve *curve = (ParametricCurve *)NULL;
00113   NurbsCurveInterface *nurbs = (NurbsCurveInterface *)NULL;
00114 
00115   if (_curves != (ParametricCurveCollection *)NULL) {
00116     curve = _curves->get_default_curve();
00117 
00118     if (curve != (ParametricCurve *)NULL) {
00119       nurbs = curve->get_nurbs_interface();
00120     }
00121   }
00122 
00123   if (nurbs == (NurbsCurveInterface *)NULL) {
00124     // The rest of this depends on having an actual NURBS curve.
00125     return true;
00126   }
00127 
00128   int i;
00129   if (_show_knots) {
00130     _num_cvs = nurbs->get_num_cvs();
00131     _knotnums.erase(_knotnums.begin(), _knotnums.end());
00132 
00133     float lt = -1.0f;
00134     int ki = -1;
00135     for (i = 0; i < _num_cvs; i++) {
00136       float t = nurbs->get_knot(i);
00137       if (t != lt) {
00138         lt = t;
00139         LVecBase3f knot_pos;
00140         curve->get_point(nurbs->get_knot(i), knot_pos);
00141         _knots.move_to(knot_pos);
00142         ki++;
00143       }
00144       _knotnums.push_back(ki);
00145     }
00146 
00147     _knots.create(_geom_node, _frame_accurate);
00148   }
00149 
00150   if (_show_cvs) {
00151     _num_cvs = nurbs->get_num_cvs();
00152     for (i = 0; i < _num_cvs; i++) {
00153       _cvs.move_to(nurbs->get_cv_point(i));
00154     }
00155 
00156     _cvs.create(_geom_node, _frame_accurate);
00157   }
00158 
00159   if (_show_hull) {
00160     _num_cvs = nurbs->get_num_cvs();
00161     for (i = 0; i < _num_cvs; i++) {
00162       _hull.draw_to(nurbs->get_cv_point(i));
00163     }
00164 
00165     _hull.create(_geom_node, _frame_accurate);
00166   }
00167 
00168   return true;
00169 }
00170 
00171 
00172 
00173 ////////////////////////////////////////////////////////////////////
00174 //     Function: NurbsCurveDrawer::recompute
00175 //       Access: Published, Virtual
00176 //  Description:
00177 ////////////////////////////////////////////////////////////////////
00178 bool NurbsCurveDrawer::
00179 recompute(float t1, float t2, ParametricCurve *curve) {
00180   return draw();
00181 }
00182 
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: NurbsCurveDrawer::set_show_cvs
00186 //       Access: Published
00187 //  Description: Sets the flag that hides or shows the CV's.
00188 ////////////////////////////////////////////////////////////////////
00189 void NurbsCurveDrawer::
00190 set_show_cvs(bool flag) {
00191   _show_cvs = flag;
00192   redraw();
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: NurbsCurveDrawer::get_show_cvs
00197 //       Access: Published
00198 //  Description: Returns the current state of the show-CV's flag.
00199 ////////////////////////////////////////////////////////////////////
00200 bool NurbsCurveDrawer::
00201 get_show_cvs() const {
00202   return _show_cvs;
00203 }
00204 
00205 ////////////////////////////////////////////////////////////////////
00206 //     Function: NurbsCurveDrawer::set_show_hull
00207 //       Access: Published
00208 //  Description: Sets the flag that hides or shows the convex hull.
00209 ////////////////////////////////////////////////////////////////////
00210 void NurbsCurveDrawer::
00211 set_show_hull(bool flag) {
00212   _show_hull = flag;
00213   redraw();
00214 }
00215 
00216 ////////////////////////////////////////////////////////////////////
00217 //     Function: NurbsCurveDrawer::get_show_hull
00218 //       Access: Published
00219 //  Description: Returns the current state of the show-hull flag.
00220 ////////////////////////////////////////////////////////////////////
00221 bool NurbsCurveDrawer::
00222 get_show_hull() const {
00223   return _show_hull;
00224 }
00225 
00226 ////////////////////////////////////////////////////////////////////
00227 //     Function: NurbsCurveDrawer::set_show_knots
00228 //       Access: Published
00229 //  Description: Sets the flag that hides or shows the knots.
00230 ////////////////////////////////////////////////////////////////////
00231 void NurbsCurveDrawer::
00232 set_show_knots(bool flag) {
00233   _show_knots = flag;
00234   redraw();
00235 }
00236 
00237 ////////////////////////////////////////////////////////////////////
00238 //     Function: NurbsCurveDrawer::get_show_knots
00239 //       Access: Published
00240 //  Description: Returns the current state of the show-knots flag.
00241 ////////////////////////////////////////////////////////////////////
00242 bool NurbsCurveDrawer::
00243 get_show_knots() const {
00244   return _show_knots;
00245 }
00246 
00247 
00248 ////////////////////////////////////////////////////////////////////
00249 //     Function: NurbsCurveDrawer::hilight
00250 //       Access: Published
00251 //  Description: Hilights a particular CV by showing it and its knot
00252 //               in a different color.  Returns true if the CV exists
00253 //               and has been drawn, false otherwise.
00254 ////////////////////////////////////////////////////////////////////
00255 bool NurbsCurveDrawer::
00256 hilight(int n, float hr, float hg, float hb) {
00257   if (n < 0 || n >= _cvs.get_num_vertices()) {
00258     // Return false if we're out of range.
00259     return false;
00260   }
00261 
00262   if (_show_cvs) {
00263     _cvs.set_vertex_color(n, hr, hg, hb);
00264   }
00265   if (_show_knots) {
00266     nassertr(_knotnums[n] >= 0 && _knotnums[n] < _knots.get_num_vertices(), false);
00267     _knots.set_vertex_color(_knotnums[n], hr, hg, hb);
00268   }
00269 
00270   return true;
00271 }
00272 
00273 
00274 ////////////////////////////////////////////////////////////////////
00275 //     Function: NurbsCurveDrawer::unhilight
00276 //       Access: Published
00277 //  Description: Removes the hilight previously set on a CV.
00278 ////////////////////////////////////////////////////////////////////
00279 bool NurbsCurveDrawer::
00280 unhilight(int n) {
00281   if (n < 0 || n >= _cvs.get_num_vertices()) {
00282     return false;
00283   }
00284 
00285   if (_show_cvs) {
00286     _cvs.set_vertex_color(n, _cv_color[0], _cv_color[1], _cv_color[2]);
00287   }
00288   if (_show_knots) {
00289     nassertr(_knotnums[n] >= 0 && _knotnums[n] < _knots.get_num_vertices(), false);
00290     _knots.set_vertex_color(_knotnums[n],
00291                             _knot_color[0], _knot_color[1], _knot_color[2]);
00292   }
00293 
00294   return true;
00295 }
00296 

Generated on Fri May 2 00:40:44 2003 for Panda by doxygen1.3