1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// K-3D
// Copyright (c) 1995-2009, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/** \file
\author Timothy M. Shead (tshead@k-3d.com)
*/
#include <k3d-i18n-config.h>
#include <k3dsdk/application_plugin_factory.h>
#include <k3dsdk/bicubic_patch.h>
#include <k3dsdk/bilinear_patch.h>
#include <k3dsdk/cubic_curve.h>
#include <k3dsdk/geometry.h>
#include <k3dsdk/imesh_selection_algorithm.h>
#include <k3dsdk/linear_curve.h>
#include <k3dsdk/metadata_keys.h>
#include <k3dsdk/nurbs_curve.h>
#include <k3dsdk/nurbs_patch.h>
#include <k3dsdk/polyhedron.h>
#include <boost/scoped_ptr.hpp>
namespace module
{
namespace selection
{
/////////////////////////////////////////////////////////////////////////////
// make_point_selection
class make_point_selection :
public k3d::imesh_selection_algorithm
{
typedef k3d::imesh_selection_algorithm base;
public:
const k3d::selection::set create_mesh_selection(const k3d::mesh& Mesh)
{
k3d::selection::set results;
boost::scoped_ptr<k3d::geometry::point_selection::storage> point_selection(k3d::geometry::point_selection::create(results));
// For each primitive in the mesh ...
for(k3d::mesh::primitives_t::const_iterator p = Mesh.primitives.begin(); p != Mesh.primitives.end(); ++p)
{
boost::scoped_ptr<k3d::polyhedron::const_primitive> polyhedron(k3d::polyhedron::validate(Mesh, **p));
if(polyhedron)
{
// Convert edge selections to point selections ...
const k3d::uint_t edge_begin = 0;
const k3d::uint_t edge_end = edge_begin + polyhedron->edge_selections.size();
for(k3d::uint_t edge = edge_begin; edge != edge_end; ++edge)
{
if(!polyhedron->edge_selections[edge])
continue;
k3d::geometry::point_selection::append(*point_selection, polyhedron->vertex_points[edge], polyhedron->vertex_points[edge]+1, 1.0);
k3d::geometry::point_selection::append(*point_selection, polyhedron->vertex_points[polyhedron->clockwise_edges[edge]], polyhedron->vertex_points[polyhedron->clockwise_edges[edge]]+1, 1.0);
}
// Convert face selections to point selections ...
const k3d::uint_t face_begin = 0;
const k3d::uint_t face_end = face_begin + polyhedron->face_first_loops.size();
for(k3d::uint_t face = face_begin; face != face_end; ++face)
{
if(!polyhedron->face_selections[face])
continue;
const k3d::uint_t face_loop_begin = polyhedron->face_first_loops[face];
const k3d::uint_t face_loop_end = face_loop_begin + polyhedron->face_loop_counts[face];
for(k3d::uint_t face_loop = face_loop_begin; face_loop != face_loop_end; ++face_loop)
{
const k3d::uint_t first_edge = polyhedron->loop_first_edges[face_loop];
for(k3d::uint_t edge = first_edge; ; )
{
k3d::geometry::point_selection::append(*point_selection, polyhedron->vertex_points[edge], polyhedron->vertex_points[edge]+1, 1.0);
edge = polyhedron->clockwise_edges[edge];
if(edge == first_edge)
break;
}
}
}
continue;
}
// Convert linear curve selections to point selections ...
boost::scoped_ptr<k3d::linear_curve::const_primitive> linear_curve(k3d::linear_curve::validate(Mesh, **p));
if(linear_curve)
{
const k3d::uint_t curve_begin = 0;
const k3d::uint_t curve_end = curve_begin + linear_curve->curve_first_points.size();
for(k3d::uint_t curve = curve_begin; curve != curve_end; ++curve)
{
if(!linear_curve->curve_selections[curve])
continue;
const k3d::uint_t curve_point_begin = linear_curve->curve_first_points[curve];
const k3d::uint_t curve_point_end = curve_point_begin + linear_curve->curve_point_counts[curve];
for(k3d::uint_t curve_point = curve_point_begin; curve_point != curve_point_end; ++curve_point)
k3d::geometry::point_selection::append(*point_selection, linear_curve->curve_points[curve_point], linear_curve->curve_points[curve_point]+1, 1.0);
}
continue;
}
// Convert cubic curve selections to point selections ...
boost::scoped_ptr<k3d::cubic_curve::const_primitive> cubic_curve(k3d::cubic_curve::validate(Mesh, **p));
if(cubic_curve)
{
const k3d::uint_t curve_begin = 0;
const k3d::uint_t curve_end = curve_begin + cubic_curve->curve_first_points.size();
for(k3d::uint_t curve = curve_begin; curve != curve_end; ++curve)
{
if(!cubic_curve->curve_selections[curve])
continue;
const k3d::uint_t curve_point_begin = cubic_curve->curve_first_points[curve];
const k3d::uint_t curve_point_end = curve_point_begin + cubic_curve->curve_point_counts[curve];
for(k3d::uint_t curve_point = curve_point_begin; curve_point != curve_point_end; ++curve_point)
k3d::geometry::point_selection::append(*point_selection, cubic_curve->curve_points[curve_point], cubic_curve->curve_points[curve_point]+1, 1.0);
}
continue;
}
// Convert nurbs curve selections to point selections ...
boost::scoped_ptr<k3d::nurbs_curve::const_primitive> nurbs_curve(k3d::nurbs_curve::validate(Mesh, **p));
if(nurbs_curve)
{
const k3d::uint_t curve_begin = 0;
const k3d::uint_t curve_end = curve_begin + nurbs_curve->curve_first_points.size();
for(k3d::uint_t curve = curve_begin; curve != curve_end; ++curve)
{
if(!nurbs_curve->curve_selections[curve])
continue;
const k3d::uint_t curve_point_begin = nurbs_curve->curve_first_points[curve];
const k3d::uint_t curve_point_end = curve_point_begin + nurbs_curve->curve_point_counts[curve];
for(k3d::uint_t curve_point = curve_point_begin; curve_point != curve_point_end; ++curve_point)
k3d::geometry::point_selection::append(*point_selection, nurbs_curve->curve_points[curve_point], nurbs_curve->curve_points[curve_point]+1, 1.0);
}
continue;
}
// Convert bilinear patch selections to point selections ...
boost::scoped_ptr<k3d::bilinear_patch::const_primitive> bilinear_patch(k3d::bilinear_patch::validate(Mesh, **p));
if(bilinear_patch)
{
const k3d::uint_t patch_begin = 0;
const k3d::uint_t patch_end = patch_begin + bilinear_patch->patch_points.size() / 4;
for(k3d::uint_t patch = patch_begin; patch != patch_end; ++patch)
{
if(!bilinear_patch->patch_selections[patch])
continue;
const k3d::uint_t patch_point_begin = patch * 4;
const k3d::uint_t patch_point_end = patch_point_begin + 4;
for(k3d::uint_t patch_point = patch_point_begin; patch_point != patch_point_end; ++patch_point)
k3d::geometry::point_selection::append(*point_selection, bilinear_patch->patch_points[patch_point], bilinear_patch->patch_points[patch_point]+1, 1.0);
}
continue;
}
// Convert bicubic patch selections to point selections ...
boost::scoped_ptr<k3d::bicubic_patch::const_primitive> bicubic_patch(k3d::bicubic_patch::validate(Mesh, **p));
if(bicubic_patch)
{
const k3d::uint_t patch_begin = 0;
const k3d::uint_t patch_end = patch_begin + bicubic_patch->patch_points.size() / 16;
for(k3d::uint_t patch = patch_begin; patch != patch_end; ++patch)
{
if(!bicubic_patch->patch_selections[patch])
continue;
const k3d::uint_t patch_point_begin = patch * 16;
const k3d::uint_t patch_point_end = patch_point_begin + 16;
for(k3d::uint_t patch_point = patch_point_begin; patch_point != patch_point_end; ++patch_point)
k3d::geometry::point_selection::append(*point_selection, bicubic_patch->patch_points[patch_point], bicubic_patch->patch_points[patch_point]+1, 1.0);
}
}
// Convert nurbs patch selections to point selections ...
boost::scoped_ptr<k3d::nurbs_patch::const_primitive> nurbs_patch(k3d::nurbs_patch::validate(Mesh, **p));
if(nurbs_patch)
{
const k3d::uint_t patch_begin = 0;
const k3d::uint_t patch_end = patch_begin + nurbs_patch->patch_first_points.size();
for(k3d::uint_t patch = patch_begin; patch != patch_end; ++patch)
{
if(!nurbs_patch->patch_selections[patch])
continue;
const k3d::uint_t patch_point_begin = nurbs_patch->patch_first_points[patch];
const k3d::uint_t patch_point_end = patch_point_begin + (nurbs_patch->patch_u_point_counts[patch] * nurbs_patch->patch_v_point_counts[patch]);
for(k3d::uint_t patch_point = patch_point_begin; patch_point != patch_point_end; ++patch_point)
k3d::geometry::point_selection::append(*point_selection, nurbs_patch->patch_points[patch_point], nurbs_patch->patch_points[patch_point]+1, 1.0);
}
}
/*
// Perform automatic conversion
for(k3d::mesh::named_tables_t::const_iterator s = (**p).structure.begin(); s != (**p).structure.end(); ++s)
{
const k3d::mesh::selection_t* selection = 0;
const k3d::mesh::indices_t* points = 0;
for(k3d::mesh::named_arrays_t::const_iterator array = s->second.begin(); array != s->second.end(); ++array)
{
if(!selection)
{
if(array->second->get_metadata_value(k3d::metadata::key::role()) == k3d::metadata::value::selection_role())
{
selection = dynamic_cast<const k3d::mesh::selection_t*>(array->second.get());
}
}
if(!points)
{
if(array->second->get_metadata_value(k3d::metadata::key::domain()) == k3d::metadata::value::point_indices_domain())
{
points = dynamic_cast<const k3d::mesh::indices_t*>(array->second.get());
}
}
}
// Select referenced points
if(selection && points)
{
const k3d::uint_t begin = 0;
const k3d::uint_t end = begin + selection->size();
for(k3d::uint_t i = begin; i != end; ++i)
{
if((*selection)[i])
k3d::geometry::point_selection::append(*point_selection, (*points)[i], (*points)[i]+1, 1.0);
}
}
}
*/
}
return results;
}
static k3d::iplugin_factory& get_factory()
{
static k3d::application_plugin_factory<make_point_selection,
k3d::interface_list<k3d::imesh_selection_algorithm> > factory(
k3d::uuid(0x0d1fd128, 0xf24f51c9, 0x175b158c, 0xf0a00996),
"MakePointSelection",
_("Converts primitive selections to point selections."),
"Selection",
k3d::iplugin_factory::STABLE);
return factory;
}
};
/////////////////////////////////////////////////////////////////////////////
// make_point_selection_factory
k3d::iplugin_factory& make_point_selection_factory()
{
return make_point_selection::get_factory();
}
} // namespace selection
} // namespace module
|