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
|
// K-3D
// Copyright (c) 2005-2008 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 Bart Janssens (bart.janssens@lid.kviv.be)
*/
#include <k3d-i18n-config.h>
#include <k3dsdk/basic_math.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/geometry.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/mesh_selection_modifier.h>
#include <k3dsdk/mesh_selection_sink.h>
#include <k3dsdk/node.h>
#include <k3dsdk/polyhedron.h>
#include <k3dsdk/selection.h>
#include <k3dsdk/utility.h>
#include <k3dsdk/vectors.h>
#include <boost/scoped_ptr.hpp>
namespace module
{
namespace selection
{
namespace detail
{
void select_connected_faces(k3d::mesh::bools_t& TaggedEdges, k3d::mesh::selection_t& FaceSelections, k3d::mesh::indices_t& EdgeFaces, const k3d::mesh::indices_t& ClockwiseEdges, const k3d::mesh::indices_t& AdjacentEdges, const k3d::uint_t Edge)
{
if(TaggedEdges[Edge])
return;
TaggedEdges[Edge] = true;
FaceSelections[EdgeFaces[Edge]] = 1.0;
select_connected_faces(TaggedEdges, FaceSelections, EdgeFaces, ClockwiseEdges, AdjacentEdges, AdjacentEdges[Edge]);
select_connected_faces(TaggedEdges, FaceSelections, EdgeFaces, ClockwiseEdges, AdjacentEdges, ClockwiseEdges[Edge]);
}
void select_connected_edges(k3d::mesh::bools_t& TaggedEdges, k3d::mesh::selection_t& EdgeSelections, const k3d::mesh::indices_t& ClockwiseEdges, const k3d::mesh::indices_t& AdjacentEdges, const k3d::uint_t Edge)
{
if(TaggedEdges[Edge])
return;
TaggedEdges[Edge] = true;
EdgeSelections[Edge] = 1.0;
select_connected_edges(TaggedEdges, EdgeSelections, ClockwiseEdges, AdjacentEdges, AdjacentEdges[Edge]);
select_connected_edges(TaggedEdges, EdgeSelections, ClockwiseEdges, AdjacentEdges, ClockwiseEdges[Edge]);
}
void select_connected_points(k3d::mesh::bools_t& TaggedEdges, k3d::mesh::selection_t& PointSelections, k3d::mesh::indices_t& EdgePoints, const k3d::mesh::indices_t& ClockwiseEdges, const k3d::mesh::indices_t& AdjacentEdges, const k3d::uint_t Edge)
{
if(TaggedEdges[Edge])
return;
TaggedEdges[Edge] = true;
PointSelections[EdgePoints[Edge]] = 1.0;
select_connected_faces(TaggedEdges, PointSelections, EdgePoints, ClockwiseEdges, AdjacentEdges, AdjacentEdges[Edge]);
select_connected_faces(TaggedEdges, PointSelections, EdgePoints, ClockwiseEdges, AdjacentEdges, ClockwiseEdges[Edge]);
}
void select_connected_components(k3d::polyhedron::primitive& Polyhedron, k3d::mesh::selection_t& PointSelection)
{
k3d::mesh::bools_t boundary_edges;
k3d::mesh::indices_t adjacent_edges;
k3d::polyhedron::create_edge_adjacency_lookup(Polyhedron.vertex_points, Polyhedron.clockwise_edges, boundary_edges, adjacent_edges);
const k3d::uint_t face_count = Polyhedron.face_first_loops.size();
const k3d::uint_t edge_count = Polyhedron.clockwise_edges.size();
// Find out what edges are directly connected to a selected component
k3d::mesh::indices_t face_selected_edges;
k3d::mesh::indices_t edge_selected_edges;
k3d::mesh::indices_t point_selected_edges;
k3d::mesh::indices_t edge_faces(edge_count);
for(k3d::uint_t face = 0; face != face_count; ++face)
{
const k3d::uint_t loop_begin = Polyhedron.face_first_loops[face];
const k3d::uint_t loop_end = loop_begin + Polyhedron.face_loop_counts[face];
for(k3d::uint_t loop = loop_begin; loop != loop_end; ++loop)
{
const k3d::uint_t first_edge = Polyhedron.loop_first_edges[loop];
for(k3d::uint_t edge = first_edge; ;)
{
if(Polyhedron.face_selections[face])
face_selected_edges.push_back(edge);
if(PointSelection[Polyhedron.vertex_points[edge]])
point_selected_edges.push_back(edge);
if(Polyhedron.edge_selections[edge])
edge_selected_edges.push_back(edge);
edge_faces[edge] = face; // There is an SDK function for this, but we're here now, so...
edge = Polyhedron.clockwise_edges[edge];
if(edge == first_edge)
break;
}
}
}
// For all of these edges, select all of the components of the same type that are connected
k3d::mesh::bools_t tagged_edges(edge_count, false);
for(k3d::uint_t i = 0; i != face_selected_edges.size(); ++i)
select_connected_faces(tagged_edges, Polyhedron.face_selections, edge_faces, Polyhedron.clockwise_edges, adjacent_edges, face_selected_edges[i]);
tagged_edges.assign(edge_count, false);
for(k3d::uint_t i = 0; i != edge_selected_edges.size(); ++i)
select_connected_edges(tagged_edges, Polyhedron.edge_selections, Polyhedron.clockwise_edges, adjacent_edges, edge_selected_edges[i]);
tagged_edges.assign(edge_count, false);
for(k3d::uint_t i = 0; i != point_selected_edges.size(); ++i)
select_connected_points(tagged_edges, PointSelection, Polyhedron.vertex_points, Polyhedron.clockwise_edges, adjacent_edges, point_selected_edges[i]);
}
} // namespace detail
/////////////////////////////////////////////////////////////////////////////
// select_connected_components
class select_connected_components :
public k3d::mesh_selection_modifier<k3d::mesh_selection_sink<k3d::node > >
{
typedef k3d::mesh_selection_modifier<k3d::mesh_selection_sink<k3d::node > > base;
public:
select_connected_components(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
base(Factory, Document)
{
m_mesh_selection.changed_signal().connect(k3d::hint::converter<
k3d::hint::convert<k3d::hint::any, k3d::hint::none> >(make_update_mesh_slot()));
}
void on_update_selection(const k3d::mesh& Input, k3d::mesh& Output)
{
// Shallow copy of the input (no data is copied, only shared pointers are)
Output = Input;
k3d::geometry::selection::merge(m_mesh_selection.pipeline_value(), Output); // Merges the current document selection with the mesh
for(k3d::uint_t i = 0; i != Input.primitives.size(); ++i)
{
boost::scoped_ptr<k3d::polyhedron::primitive> output_polyhedron(k3d::polyhedron::validate(Output, Output.primitives[i]));
if(!output_polyhedron)
continue;
detail::select_connected_components(*output_polyhedron, Output.point_selection.writable());
}
}
static k3d::iplugin_factory& get_factory()
{
static k3d::document_plugin_factory<select_connected_components,
k3d::interface_list<k3d::imesh_source,
k3d::interface_list<k3d::imesh_sink > > > factory(
k3d::uuid(0xe54a08e3, 0x014445d3, 0x5395c8a6, 0xb3bfaae3),
"SelectConnectedComponents",
_("Select all components that are directly or indirectly connected to the selected component"),
"Selection",
k3d::iplugin_factory::STABLE);
return factory;
}
};
/////////////////////////////////////////////////////////////////////////////
// select_connected_components_factory
k3d::iplugin_factory& select_connected_components_factory()
{
return select_connected_components::get_factory();
}
} // namespace selection
} // namespace module
|