00001 #include "test_app.H"
00002 #include "gtex/smooth_shade.H"
00003 #include "gtex/wireframe.H"
00004 #include "mesh/lmesh.H"
00005
00006 const int HEIGHT = 6;
00007 const double REGULARITY = 20;
00008 const double MIN_DIST = 0.35;
00009
00010
00011
00012
00013
00014
00015 MAKE_PTR_SUBC(BALLwidget_anchor, GEOM);
00016 class BALLwidget_anchor : public GEOM {
00017 public:
00018 BALLwidget_anchor() : GEOM() {
00019
00020 LMESHptr mesh = new LMESH();
00021
00022
00023 mesh->set_geom(this);
00024
00025
00026 mesh->Icosahedron();
00027
00028
00029
00030 mesh->set_render_style(SmoothShadeTexture::static_name());
00031
00032
00033 mesh->patch(0)->set_color(COLOR(0.1, 0.1, 0.9));
00034
00035
00036
00037 _body = mesh;
00038
00039 set_name("BALLwidget_anchor");
00040 }
00041
00042
00043 virtual int draw_vis_ref() { return 0; }
00044
00045 virtual bool needs_blend() const { return false; }
00046 };
00047
00048
00049
00050
00051
00052
00053 MAKE_PTR_SUBC(GRIDwidget_anchor, GEOM);
00054 class GRIDwidget_anchor : public GEOM {
00055 public:
00056 GRIDwidget_anchor(Wtransf ff) : GEOM() {
00057
00058 LMESHptr mesh = new LMESH();
00059
00060
00061 mesh->set_geom(this);
00062
00063
00064 mesh->Cube();
00065 set_xform(ff);
00066
00067
00068
00069 mesh->set_render_style(WireframeTexture::static_name());
00070
00071
00072 mesh->patch(0)->set_color(COLOR::black);
00073 mesh->patch(0)->set_transp(0.5);
00074
00075
00076 _body = mesh;
00077
00078 set_name("GRIDwidget_anchor");
00079 }
00080
00081
00082 virtual int draw_vis_ref() { return 0; }
00083
00084 virtual bool needs_blend() const { return false; }
00085 };
00086
00087
00088
00089
00090 TestSPSapp* TestSPSapp::_instance = 0;
00091
00092 void
00093 TestSPSapp::create_grid(Wtransf ff)
00094 {
00095 _boxes += new GRIDwidget_anchor(ff);
00096 GEOMptr _anchor = _boxes.last();
00097 _anchor->set_pickable(0);
00098 NETWORK.set(_anchor, 0);
00099 CONSTRAINT.set(_anchor, GEOM::SCREEN_WIDGET);
00100 WORLD::create (_anchor, false);
00101 WORLD::undisplay(_anchor, false);
00102 }
00103
00104 void
00105 TestSPSapp::visit(OctreeNode* node)
00106 {
00107 if (node->get_leaf()) {
00108 if (node->get_disp()) {
00109 Wtransf ff = Wtransf(node->min()) * Wtransf::scaling(node->dim());
00110 create_grid(ff);
00111 }
00112 } else {
00113 for (int i = 0; i < 8; i++)
00114 visit(node->get_children()[i]);
00115 }
00116 }
00117
00118 void
00119 TestSPSapp::load_scene()
00120 {
00121 BaseJOTapp::load_scene();
00122
00123 for (int i=0; i<EXIST.num(); i++) {
00124 GEOMptr geom = GEOM::upcast(EXIST[i]);
00125 if (geom && BMESH::isa(geom->body())) {
00126 Bvert_list list;
00127 Bface_list fs;
00128 ARRAY<Wvec> bcs;
00129
00130
00131 _nodes += sps(BMESH::upcast(geom->body()), HEIGHT, REGULARITY, MIN_DIST, fs, bcs);
00132 for (int i = 0; i < fs.num(); i++) {
00133 Wpt pt;
00134 fs[i]->bc2pos(bcs[i], pt);
00135 list += new Bvert(pt);
00136 }
00137 _pts += list;
00138 }
00139 }
00140
00141 for (int i = 0; i < _nodes.num(); i++)
00142 visit(_nodes[i]);
00143
00144 for (int i = 0; i < _pts.num(); i++)
00145 for (int j = 0; j < _pts[i].num(); j++) {
00146 _balls += new BALLwidget_anchor;
00147 GEOMptr _anchor = _balls.last();
00148 _anchor->set_pickable(0);
00149 NETWORK.set(_anchor, 0);
00150 CONSTRAINT.set(_anchor, GEOM::SCREEN_WIDGET);
00151 WORLD::create (_anchor, false);
00152 WORLD::undisplay(_anchor, false);
00153 }
00154 }
00155
00156 void
00157 TestSPSapp::init_kbd(WINDOW &base_window)
00158 {
00159
00160
00161 BaseJOTapp::init_kbd(base_window);
00162
00163
00164 _key_menu->add_menu_item('s', "Toggle Sample", &toggle_sample_cb);
00165 _key_menu->add_menu_item('g', "Toggle Grid", &toggle_grid_cb);
00166 }
00167
00168 int
00169 TestSPSapp::toggle_sample_cb(const Event&, State *&)
00170 {
00171
00172
00173 assert(_instance);
00174 _instance->_show_sample = !_instance->_show_sample;
00175 if (_instance->_show_sample) {
00176 for (int i = 0, k = 0; i < _instance->_pts.num(); i++)
00177 for (int j = 0; j < _instance->_pts[i].num(); j++, k++) {
00178 WORLD::display(_instance->_balls[k], false);
00179 Wpt p = _instance->_pts[i][j]->loc();
00180 Wvec delt(p-Wpt(XYpt(p)+XYvec(VEXEL(3,3)), p));
00181 Wtransf ff = Wtransf(p) * Wtransf::scaling(1.5*delt.length());
00182 _instance->_balls[k]->set_xform(ff);
00183 }
00184 err_msg("showing sample");
00185 }
00186 else {
00187 for (int i = 0; i < _instance->_balls.num(); i++)
00188 WORLD::undisplay(_instance->_balls[i], false);
00189 err_msg("samples not shown");
00190 }
00191
00192 return 0;
00193 }
00194
00195 int
00196 TestSPSapp::toggle_grid_cb(const Event&, State *&)
00197 {
00198
00199
00200 assert(_instance);
00201 _instance->_show_grid = !_instance->_show_grid;
00202 if (_instance->_show_grid) {
00203 for (int i = 0; i < _instance->_boxes.num(); i++)
00204 WORLD::display(_instance->_boxes[i], false);
00205 err_msg("showing grid");
00206 }
00207 else {
00208 for (int i = 0; i < _instance->_boxes.num(); i++)
00209 WORLD::undisplay(_instance->_boxes[i], false);
00210 err_msg("grid not shown");
00211 }
00212
00213 return 0;
00214 }
00215
00216
00217
00218
00219 int
00220 main(int argc, char **argv)
00221 {
00222 TestSPSapp app(argc, argv);
00223
00224
00225 Config::set_var_str("JOT_WINDOW_NAME", "Stratified Point Sampling test app");
00226
00227 app.init();
00228 app.Run();
00229
00230 return 0;
00231 }
00232
00233