File: utility.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (95 lines) | stat: -rw-r--r-- 3,935 bytes parent folder | download | duplicates (5)
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
// 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 Bart Janssens (bart.janssens@lid.kviv.be)
*/

#include "utility.h"

namespace module
{

namespace nurbs
{

void delete_empty_primitives(k3d::mesh& Mesh)
{
	for(k3d::mesh::primitives_t::iterator prim = Mesh.primitives.begin(); prim != Mesh.primitives.end();)
	{
		bool erase = false;
		for(k3d::mesh::named_tables_t::const_iterator structure = (*prim)->structure.begin(); structure != (*prim)->structure.end(); ++structure)
		{
			for(k3d::mesh::named_arrays_t::const_iterator array = structure->second.begin(); array != structure->second.end(); ++array)
			{
				if(array->second->get_metadata_value(k3d::metadata::key::domain()) == k3d::metadata::value::point_indices_domain())
					if(array->second->empty())
						erase = true;
			}
		}
		prim = erase ? Mesh.primitives.erase(prim) : prim + 1;
	}
}

knot_vector_merger::knot_vector_merger(const k3d::mesh::knots_t& UnifiedKnots, const k3d::uint_t Order) : unified_knots(UnifiedKnots), order(Order)
{
	std::transform(unified_knots.begin(), unified_knots.end(), unified_knots.begin(), knot_normalizer(unified_knots.front(), unified_knots.back()));
}

void knot_vector_merger::operator()(k3d::mesh& OutputMesh, k3d::nurbs_curve::primitive& OutputCurves, const k3d::mesh& InputMesh, const k3d::nurbs_curve::const_primitive& InputCurves, const k3d::uint_t& Curve)
{
	curve_arrays curve(InputMesh, InputCurves, Curve, true);
	k3d::mesh::knots_t& knots = curve.knots;
	if(unified_knots.size() < knots.size())
		throw std::runtime_error("Unified knot vector is too short");
	// First, we check if the old knot vector has knots that are not in the common knot vector (i.e. knots that were added manually using knot vector editing)
	k3d::uint_t i = 0;
	for(k3d::uint_t knot_idx = 0; knot_idx != knots.size(); ++knot_idx)
	{
		const k3d::double_t old_knot = knots[knot_idx];
		const k3d::uint_t new_mul = multiplicity(unified_knots, old_knot, 0, unified_knots.size());
		const k3d::uint_t old_mul = multiplicity(knots, old_knot, 0, knots.size());
		if(new_mul < old_mul && std::abs(unified_knots[i] - knots[knot_idx]) > 0.000001)
		{
			const k3d::double_t previous_knot = knot_idx == 0 ? knots.front() : knots[knot_idx - 1];
			const k3d::double_t next_knot = knot_idx == knots.size() - 1 ? knots.back() : knots[knot_idx - 1];
			k3d::uint_t i = knot_idx;
			while(unified_knots[i] < previous_knot || multiplicity(knots, unified_knots[i], 0, knots.size()) >= multiplicity(unified_knots, unified_knots[i], 0, unified_knots.size()))
				++i;
			knots[knot_idx] = unified_knots[i];
		}
		++i;
	}
	// Next, add the missing knots
	for(k3d::uint_t knot_idx = 0; knot_idx != unified_knots.size(); ++knot_idx)
	{
		const k3d::double_t u = unified_knots[knot_idx];
		const k3d::uint_t old_mul = multiplicity(knots, u, 0, knots.size());
		const k3d::uint_t new_mul = multiplicity(unified_knots, u, 0, unified_knots.size());
		if(old_mul > new_mul)
			throw std::runtime_error("Error inserting knots when creating a common knot vector");
		if(old_mul < new_mul)
			insert_knot(curve, u, 1);
	}
	curve.add_curve(OutputMesh, OutputCurves);
}

} //namespace nurbs

} //namespace module