File: mesh.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (950 lines) | stat: -rw-r--r-- 26,503 bytes parent folder | download
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
// K-3D
// Copyright (c) 1995-2004, 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

#include "mesh.h"
#include "result.h"
#include "utility.h"

#include <algorithm>

namespace k3d
{

/////////////////////////////////////////////////////////////////////////////
// point

point::point(const vector3& Position) :
	position(Position)
{
}

point::point(const double X, const double Y, const double Z) :
	position(X, Y, Z)
{
}

/////////////////////////////////////////////////////////////////////////////
// point_group

point_group::point_group() :
	material(0)
{
}

/////////////////////////////////////////////////////////////////////////////
// split_edge

/////////////////////////////////////////////////////////////////////////////
// face

face::face(split_edge* FirstEdge) :
	first_edge(FirstEdge)
{
}

/////////////////////////////////////////////////////////////////////////////
// normal

vector3 normal(const split_edge* const Loop)
{
	/// Calculates the normal for an edge loop using the summation method, which is more robust than the three-point methods (handles zero-length edges)
	vector3 result(0, 0, 0);

	for(const split_edge* edge = Loop; edge && edge->face_clockwise; edge = edge->face_clockwise)
		{
			const vector3& i = edge->vertex->position;
			const vector3& j = edge->face_clockwise->vertex->position;

			result[0] += (i[1] + j[1]) * (j[2] - i[2]);
			result[1] += (i[2] + j[2]) * (j[0] - i[0]);
			result[2] += (i[0] + j[0]) * (j[1] - i[1]);

			if(Loop == edge->face_clockwise)
				break;
		}

	return 0.5 * result;
}

vector3 normal(const face& Face)
{
	return normal(Face.first_edge);
}

/////////////////////////////////////////////////////////////////////////////
// polyhedron

polyhedron::polyhedron() :
	type(POLYGONS),
	material(0)
{
}

polyhedron::~polyhedron()
{
	std::for_each(faces.begin(), faces.end(), delete_object());
	std::for_each(edges.begin(), edges.end(), delete_object());
}

std::ostream& operator<<(std::ostream& Stream, const polyhedron::type_t& RHS)
{
	switch(RHS)
		{
			case polyhedron::POLYGONS:
				Stream << "polygons";
				break;
			case polyhedron::CATMULL_CLARK_SUBDIVISION_MESH:
				Stream << "catmull_clark";
				break;
		}

	return Stream;
}

std::istream& operator>>(std::istream& Stream, polyhedron::type_t& RHS)
{
	std::string buffer;
	Stream >> buffer;

//std::cerr << debug << __PRETTY_FUNCTION__ << " " << buffer << std::endl;

	if(buffer == "polygons")
		RHS = polyhedron::POLYGONS;
	else if(buffer == "catmull_clark")
		RHS = polyhedron::CATMULL_CLARK_SUBDIVISION_MESH;
	else
		std::cerr << error << "Unknown polyhedron type [" << buffer << "]" << std::endl;

	return Stream;
}

/////////////////////////////////////////////////////////////////////////////
// linear_curve

/////////////////////////////////////////////////////////////////////////////
// linear_curve_group

linear_curve_group::linear_curve_group() :
	wrap(false),
	material(0)
{
}

linear_curve_group::~linear_curve_group()
{
	std::for_each(curves.begin(), curves.end(), delete_object());
}

/////////////////////////////////////////////////////////////////////////////
// cubic_curve

/////////////////////////////////////////////////////////////////////////////
// cubic_curve_group

cubic_curve_group::cubic_curve_group() :
	wrap(false),
	material(0)
{
}

cubic_curve_group::~cubic_curve_group()
{
	std::for_each(curves.begin(), curves.end(), delete_object());
}

/////////////////////////////////////////////////////////////////////////////
// nucurve

nucurve::nucurve() :
	order(2)
{
}

/////////////////////////////////////////////////////////////////////////////
// nucurve_group

nucurve_group::nucurve_group() :
	material(0)
{
}

nucurve_group::~nucurve_group()
{
	std::for_each(curves.begin(), curves.end(), delete_object());
}

/////////////////////////////////////////////////////////////////////////////
// bilinear_patch

bilinear_patch::bilinear_patch() :
	material(0)
{
}

/////////////////////////////////////////////////////////////////////////////
// bicubic_patch

bicubic_patch::bicubic_patch() :
	material(0)
{
}

/////////////////////////////////////////////////////////////////////////////
// nupatch

nupatch::nupatch() :
	u_order(2),
	v_order(2),
	material(0)
{
}

/////////////////////////////////////////////////////////////////////////////
// blobby

blobby::blobby(opcode* Opcode) :
	root(Opcode),
	material(0)
{
}

blobby::~blobby()
{
	delete root;
}

void blobby::accept(visitor& Visitor)
{
	if(root)
		root->accept(Visitor);
}

/////////////////////////////////////////////////////////////////////////////
// blobby::constant

blobby::constant::constant(double Value) :
	value(Value)
{
}

blobby::opcode* blobby::constant::clone()
{
	return new constant(*this);
}

void blobby::constant::accept(visitor& Visitor)
{
	Visitor.visit_constant(*this);
}

/////////////////////////////////////////////////////////////////////////////
// blobby::ellipsoid

blobby::ellipsoid::ellipsoid(point* Origin, const matrix4& Transformation) :
	origin(Origin),
	transformation(Transformation)
{
}

blobby::opcode* blobby::ellipsoid::clone()
{
	return new ellipsoid(*this);
}

void blobby::ellipsoid::accept(visitor& Visitor)
{
	Visitor.visit_ellipsoid(*this);
}

////////////////////////////////////////////////////////////////////////////
// blobby::segment

blobby::segment::segment(point* Start, point* End, double Radius, const matrix4& Transformation) :
	start(Start),
	end(End),
	radius(Radius),
	transformation(Transformation)
{
}

blobby::opcode* blobby::segment::clone()
{
	return new segment(*this);
}

void blobby::segment::accept(visitor& Visitor)
{
	Visitor.visit_segment(*this);
}

/////////////////////////////////////////////////////////////////////////////
// blobby::subtract

blobby::subtract::subtract(opcode* Subtrahend, opcode* Minuend) :
	subtrahend(Subtrahend),
	minuend(Minuend)
{
}

blobby::subtract::~subtract()
{
	delete subtrahend;
	delete minuend;
}

blobby::opcode* blobby::subtract::clone()
{
	return new subtract(subtrahend->clone(), minuend->clone());
}

void blobby::subtract::accept(visitor& Visitor)
{
	Visitor.visit_subtract(*this);
}

////////////////////////////////////////////////////////////////////////////
// blobby::divide

blobby::divide::divide(opcode* Dividend, opcode* Divisor) :
	dividend(Dividend),
	divisor(Divisor)
{
}

blobby::divide::~divide()
{
	delete dividend;
	delete divisor;
}

blobby::opcode* blobby::divide::clone()
{
	return new divide(dividend->clone(), divisor->clone());
}

void blobby::divide::accept(visitor& Visitor)
{
	Visitor.visit_divide(*this);
}

////////////////////////////////////////////////////////////////////////////////////////
// blobby::variable_operands

void blobby::variable_operands::add_operand(blobby::opcode* Operand)
{
	operands.push_back(Operand);
}

void blobby::variable_operands::operands_accept(visitor& Visitor)
{
	for(operands_t::iterator operand = operands.begin(); operand != operands.end(); ++operand)
		(*operand)->accept(Visitor);
}

blobby::variable_operands::~variable_operands()
{
	std::for_each(operands.begin(), operands.end(), delete_object());
}

void blobby::variable_operands::clone_operands()
{
	for(operands_t::iterator operand = operands.begin(); operand != operands.end(); ++operand)
		(*operand) = (*operand)->clone();
}

////////////////////////////////////////////////////////////////////////////////////////
// blobby::add

blobby::opcode* blobby::add::clone()
{
	add* result = new add(*this);
	result->clone_operands();
	return result;
}

void blobby::add::accept(visitor& Visitor)
{
	Visitor.visit_add(*this);
}

////////////////////////////////////////////////////////////////////////////////////////
// blobby::multiply

blobby::opcode* blobby::multiply::clone()
{
	multiply* result = new multiply(*this);
	result->clone_operands();
	return result;
}

void blobby::multiply::accept(visitor& Visitor)
{
	Visitor.visit_multiply(*this);
}

////////////////////////////////////////////////////////////////////////////////////////
// blobby::max

blobby::opcode* blobby::max::clone()
{
	max* result = new max(*this);
	result->clone_operands();
	return result;
}

void blobby::max::accept(visitor& Visitor)
{
	Visitor.visit_max(*this);
}

////////////////////////////////////////////////////////////////////////////////////////
// blobby::min

blobby::opcode* blobby::min::clone()
{
	min* result = new min(*this);
	result->clone_operands();
	return result;
}

void blobby::min::accept(visitor& Visitor)
{
	Visitor.visit_min(*this);
}

/////////////////////////////////////////////////////////////////////////////
// mesh

mesh::mesh()
{
}

mesh::~mesh()
{
	std::for_each(bicubic_patches.begin(), bicubic_patches.end(), delete_object());
	std::for_each(bilinear_patches.begin(), bilinear_patches.end(), delete_object());
	std::for_each(cubic_curve_groups.begin(), cubic_curve_groups.end(), delete_object());
	std::for_each(linear_curve_groups.begin(), linear_curve_groups.end(), delete_object());
	std::for_each(polyhedra.begin(), polyhedra.end(), delete_object());
	std::for_each(point_groups.begin(), point_groups.end(), delete_object());
	std::for_each(points.begin(), points.end(), delete_object());
}

/////////////////////////////////////////////////////////////////////////////
// add_unit_cube

void add_unit_cube(mesh& Mesh, polyhedron& Polyhedron)
{
	// Create points ...
	boost::multi_array<point*, 3> points(boost::extents[2][2][2]);
	points[0][0][0] = new point(-0.5, -0.5, -0.5);
	points[1][0][0] = new point(0.5, -0.5, -0.5);
	points[1][1][0] = new point(0.5, 0.5, -0.5);
	points[0][1][0] = new point(-0.5, 0.5, -0.5);
	points[0][0][1] = new point(-0.5, -0.5, 0.5);
	points[1][0][1] = new point(0.5, -0.5, 0.5);
	points[1][1][1] = new point(0.5, 0.5, 0.5);
	points[0][1][1] = new point(-0.5, 0.5, 0.5);

	for(unsigned long i = 0; i != 2; ++i)
		for(unsigned long j = 0; j != 2; ++j)
			for(unsigned long k = 0; k != 2; ++k)
				Mesh.points.push_back(points[i][j][k]);

	// Create edges ...
	boost::multi_array<split_edge*, 2> edges(boost::extents[6][4]);
	edges[0][0] = new split_edge(points[0][1][0]);
	edges[0][1] = new split_edge(points[1][1][0]);
	edges[0][2] = new split_edge(points[1][0][0]);
	edges[0][3] = new split_edge(points[0][0][0]);

	edges[1][0] = new split_edge(points[1][1][0]);
	edges[1][1] = new split_edge(points[1][1][1]);
	edges[1][2] = new split_edge(points[1][0][1]);
	edges[1][3] = new split_edge(points[1][0][0]);

	edges[2][0] = new split_edge(points[1][1][1]);
	edges[2][1] = new split_edge(points[0][1][1]);
	edges[2][2] = new split_edge(points[0][0][1]);
	edges[2][3] = new split_edge(points[1][0][1]);

	edges[3][0] = new split_edge(points[0][1][1]);
	edges[3][1] = new split_edge(points[0][1][0]);
	edges[3][2] = new split_edge(points[0][0][0]);
	edges[3][3] = new split_edge(points[0][0][1]);

	edges[4][0] = new split_edge(points[0][1][1]);
	edges[4][1] = new split_edge(points[1][1][1]);
	edges[4][2] = new split_edge(points[1][1][0]);
	edges[4][3] = new split_edge(points[0][1][0]);

	edges[5][0] = new split_edge(points[0][0][0]);
	edges[5][1] = new split_edge(points[1][0][0]);
	edges[5][2] = new split_edge(points[1][0][1]);
	edges[5][3] = new split_edge(points[0][0][1]);

	edges[0][0]->companion = edges[4][2];
	edges[0][1]->companion = edges[1][3];
	edges[0][2]->companion = edges[5][0];
	edges[0][3]->companion = edges[3][1];

	edges[1][0]->companion = edges[4][1];
	edges[1][1]->companion = edges[2][3];
	edges[1][2]->companion = edges[5][1];
	edges[1][3]->companion = edges[0][1];

	edges[2][0]->companion = edges[4][0];
	edges[2][1]->companion = edges[3][3];
	edges[2][2]->companion = edges[5][2];
	edges[2][3]->companion = edges[1][1];

	edges[3][0]->companion = edges[4][3];
	edges[3][1]->companion = edges[0][3];
	edges[3][2]->companion = edges[5][3];
	edges[3][3]->companion = edges[2][1];

	edges[4][0]->companion = edges[2][0];
	edges[4][1]->companion = edges[1][0];
	edges[4][2]->companion = edges[0][0];
	edges[4][3]->companion = edges[3][0];

	edges[5][0]->companion = edges[0][2];
	edges[5][1]->companion = edges[1][2];
	edges[5][2]->companion = edges[2][2];
	edges[5][3]->companion = edges[3][2];

	for(unsigned long i = 0; i != 6; ++i)
		for(unsigned long j = 0; j != 4; ++j)
			edges[i][j]->face_clockwise = edges[i][(j+1)%4];

	for(unsigned long i = 0; i != 6; ++i)
		for(unsigned long j = 0; j != 4; ++j)
			Polyhedron.edges.push_back(edges[i][j]);

	// Create faces ...
	for(unsigned long i = 0; i != 6; ++i)
		Polyhedron.faces.push_back(new face(edges[i][0]));
}

/////////////////////////////////////////////////////////////////////////////
// add_grid

grid_results_t add_grid(mesh& Mesh, polyhedron& Polyhedron, const unsigned long Rows, const unsigned long Columns, const bool StitchTop, const bool StitchSide)
{
	// Sanity checks ...
	assert(Rows);
	assert(Columns);

	// Calculate the number of faces to create along each axis ...
	const unsigned long face_rows = Rows;
	const unsigned long face_columns = Columns;

	// Calculate the number of points that need to be created along each axis ...
	unsigned long point_rows = face_rows + (StitchTop ? 0 : 1);
	unsigned long point_columns = face_columns + (StitchSide ? 0 : 1);

	// Create points ...
	boost::multi_array<point*, 2> points(boost::extents[point_rows][point_columns]);
	for(unsigned long row = 0; row != point_rows; ++row)
		{
			for(unsigned long column = 0; column != point_columns; ++column)
				{
					points[row][column] = new point(0, 0, 0);
					Mesh.points.push_back(points[row][column]);
				}
		}

	// Create edges ...
	boost::multi_array<split_edge*, 3> edges(boost::extents[face_rows][face_columns][4]);
	for(unsigned long row = 0; row != face_rows; ++row)
		{
			for(unsigned long column = 0; column != face_columns; ++column)
				{
					edges[row][column][0] = new split_edge(points[row][column]);
					edges[row][column][1] = new split_edge(points[row][(column+1) % point_columns]);
					edges[row][column][2] = new split_edge(points[(row+1) % point_rows][(column+1) % point_columns]);
					edges[row][column][3] = new split_edge(points[(row+1) % point_rows][column]);

					for(unsigned long i = 0; i != 4; ++i)
						edges[row][column][i]->face_clockwise = edges[row][column][(i+1)%4];

					for(unsigned long i = 0; i != 4; ++i)
						Polyhedron.edges.push_back(edges[row][column][i]);
				}
		}

	// Join edges ...
	const unsigned long edge_rows = face_rows - (StitchTop ? 0 : 1);
	const unsigned long edge_columns = face_columns - (StitchSide ? 0 : 1);

	for(unsigned long row = 0; row != edge_rows; ++row)
		{
			for(unsigned long column = 0; column != face_columns; ++column)
				join_edges(*edges[row][column][2], *edges[(row+1) % face_rows][column][0]);
		}

	for(unsigned long column = 0; column != edge_columns; ++column)
		{
			for(unsigned long row = 0; row != face_rows; ++row)
				join_edges(*edges[row][column][1], *edges[row][(column+1) % face_columns][3]);
		}

	// Create faces ...
	boost::multi_array<face*, 2> faces(boost::extents[face_rows][face_columns]);
	for(unsigned long row = 0; row != face_rows; ++row)
		{
			for(unsigned long column = 0; column != face_columns; ++column)
				{
					face* const new_face = new face(edges[row][column][0]);
					Polyhedron.faces.push_back(new_face);
				}
		}

	return boost::make_tuple(points, edges, faces);
}

namespace detail
{

/// Provides a mapping of old-to-new points that can be used with std::transform
struct point_map_t :
	public std::map<point*, point*>,
	public blobby::visitor
{
	virtual ~point_map_t()
	{
	}

	point* operator()(point* Key)
	{
		return operator[](Key);
	}

	void visit_constant(blobby::constant& Constant)
	{
	}

	void visit_ellipsoid(blobby::ellipsoid& Ellipsoid)
	{
		Ellipsoid.origin = operator[](Ellipsoid.origin);
	}

	void visit_segment(blobby::segment& Segment)
	{
		Segment.start = operator[](Segment.start);
		Segment.end = operator[](Segment.end);
	}

	void visit_subtract(blobby::subtract& Subtract)
	{
		Subtract.subtrahend->accept(*this);
		Subtract.minuend->accept(*this);
	}

	void visit_divide(blobby::divide& Divide)
	{
		Divide.dividend->accept(*this);
		Divide.divisor->accept(*this);
	}

	void visit_add(blobby::add& Add)
	{
		Add.operands_accept(*this);
	}

	void visit_multiply(blobby::multiply& Multiply)
	{
		Multiply.operands_accept(*this);
	}

	void visit_min(blobby::min& Min)
	{
		Min.operands_accept(*this);
	}

	void visit_max(blobby::max& Max)
	{
		Max.operands_accept(*this);
	}
};

} // namespace detail

/////////////////////////////////////////////////////////////////////////////
// deep_copy

void deep_copy(const mesh& Input, mesh& Output)
{
	// Duplicate points ...
	detail::point_map_t point_map;
	point_map[0] = 0;
	for(mesh::points_t::const_iterator p = Input.points.begin(); p != Input.points.end(); ++p)
		{
			Output.points.push_back(new point(**p));
			point_map.insert(std::make_pair(*p, Output.points.back()));
		}

	// Duplicate point clouds ...
	for(mesh::point_groups_t::const_iterator pt_group = Input.point_groups.begin(); pt_group != Input.point_groups.end(); ++pt_group)
		{
			point_group* const new_point_group = new point_group(**pt_group);
			std::transform(new_point_group->points.begin(), new_point_group->points.end(), new_point_group->points.begin(), point_map);
			Output.point_groups.push_back(new_point_group);
		}

	// Duplicate polyhedra ...
	for(mesh::polyhedra_t::const_iterator pn = Input.polyhedra.begin(); pn != Input.polyhedra.end(); ++pn)
		{
			polyhedron* const new_polyhedron = new polyhedron(**pn);

			// Duplicate edges ...
			typedef std::map<split_edge*, split_edge*> edge_map_t;
			edge_map_t edge_map;
			edge_map[0] = 0;
			for(polyhedron::edges_t::iterator edge = new_polyhedron->edges.begin(); edge != new_polyhedron->edges.end(); ++edge)
				{
					split_edge* const new_edge = new split_edge(**edge);
					edge_map.insert(std::make_pair(*edge, new_edge));
					*edge = new_edge;
				}

			// Re-link edges ...
			for(edge_map_t::iterator edge = edge_map.begin(); edge != edge_map.end(); ++edge)
				{
					if(!edge->first)
						continue;

					edge->second->vertex = point_map[edge->second->vertex];
					edge->second->face_clockwise = edge_map.find(edge->second->face_clockwise)->second;
					edge->second->companion = edge_map.find(edge->second->companion)->second;
				}

			// Duplicate faces ...
			for(polyhedron::faces_t::iterator f = new_polyhedron->faces.begin(); f != new_polyhedron->faces.end(); ++f)
				{
					face* const new_face = new face(**f);
					new_face->first_edge = edge_map[new_face->first_edge];

					// Duplicate holes ...
					for(face::holes_t::iterator hole = new_face->holes.begin(); hole != new_face->holes.end(); ++hole)
						*hole = edge_map.find(*hole)->second;

					*f = new_face;
				}

			Output.polyhedra.push_back(new_polyhedron);
		}

	// Duplicate linear curve groups ...
	for(mesh::linear_curve_groups_t::const_iterator group = Input.linear_curve_groups.begin(); group != Input.linear_curve_groups.end(); ++group)
		{
			linear_curve_group* const new_group = new linear_curve_group(**group);

			for(linear_curve_group::curves_t::iterator curve = new_group->curves.begin(); curve != new_group->curves.end(); ++curve)
				{
					*curve = new linear_curve(**curve);
					std::transform((*curve)->control_points.begin(), (*curve)->control_points.end(), (*curve)->control_points.begin(), point_map);
				}

			Output.linear_curve_groups.push_back(new_group);
		}

	// Duplicate cubic curve groups ...
	for(mesh::cubic_curve_groups_t::const_iterator group = Input.cubic_curve_groups.begin(); group != Input.cubic_curve_groups.end(); ++group)
		{
			cubic_curve_group* const new_group = new cubic_curve_group(**group);

			for(cubic_curve_group::curves_t::iterator curve = new_group->curves.begin(); curve != new_group->curves.end(); ++curve)
				{
					*curve = new cubic_curve(**curve);
					std::transform((*curve)->control_points.begin(), (*curve)->control_points.end(), (*curve)->control_points.begin(), point_map);
				}

			Output.cubic_curve_groups.push_back(new_group);
		}

	// Duplicate nucurve groups ...
	for(mesh::nucurve_groups_t::const_iterator group = Input.nucurve_groups.begin(); group != Input.nucurve_groups.end(); ++group)
		{
			nucurve_group* const new_group = new nucurve_group(**group);

			for(nucurve_group::curves_t::iterator curve = new_group->curves.begin(); curve != new_group->curves.end(); ++curve)
				{
					*curve = new nucurve(**curve);
					for(nucurve::control_points_t::iterator control_point = (*curve)->control_points.begin(); control_point != (*curve)->control_points.end(); ++control_point)
						control_point->position = point_map[control_point->position];
				}

			Output.nucurve_groups.push_back(new_group);
		}

	// Duplicate bilinear patches ...
	for(mesh::bilinear_patches_t::const_iterator patch = Input.bilinear_patches.begin(); patch != Input.bilinear_patches.end(); ++patch)
		{
			bilinear_patch* const new_patch = new bilinear_patch(**patch);
			std::transform(new_patch->control_points.begin(), new_patch->control_points.end(), new_patch->control_points.begin(), point_map);

			Output.bilinear_patches.push_back(new_patch);
		}

	// Duplicate bicubic patches ...
	for(mesh::bicubic_patches_t::const_iterator patch = Input.bicubic_patches.begin(); patch != Input.bicubic_patches.end(); ++patch)
		{
			bicubic_patch* const new_patch = new bicubic_patch(**patch);
			std::transform(new_patch->control_points.begin(), new_patch->control_points.end(), new_patch->control_points.begin(), point_map);

			Output.bicubic_patches.push_back(new_patch);
		}

	// Duplicate nupatches ...
	for(mesh::nupatches_t::const_iterator patch = Input.nupatches.begin(); patch != Input.nupatches.end(); ++patch)
		{
			nupatch* const new_patch = new nupatch(**patch);
			for(nupatch::control_points_t::iterator control_point = new_patch->control_points.begin(); control_point != new_patch->control_points.end(); ++control_point)
				control_point->position = point_map[control_point->position];

			Output.nupatches.push_back(new_patch);
		}

	// Duplicate blobbies ...
	for(mesh::blobbies_t::const_iterator blob = Input.blobbies.begin(); blob != Input.blobbies.end(); blob++)
		{
			blobby* const new_blobby = new blobby((*blob)->root->clone());
			new_blobby->accept(point_map);

			Output.blobbies.push_back(new_blobby);
		}
}

/////////////////////////////////////////////////////////////////////////////
// is_valid

bool is_valid(const polyhedron& Polyhedron)
{
	// For every face ...
	for(polyhedron::faces_t::const_iterator face = Polyhedron.faces.begin(); face != Polyhedron.faces.end(); ++face)
		{
			// This is obviously wrong!!!
			return_val_if_fail(*face, false);
		}

	// For every edge ...
	for(polyhedron::edges_t::const_iterator edge = Polyhedron.edges.begin(); edge != Polyhedron.edges.end(); ++edge)
		{
			// This is obviously wrong!!!
			return_val_if_fail(*edge, false);

			// Every edge should have a vertex ...
			return_val_if_fail((*edge)->vertex, false);

			// Every edge should have a neighbor ...
			return_val_if_fail((*edge)->face_clockwise, false);

			// For edges with companions ...
			if((*edge)->companion)
				{
					// Companions had better point to each other ...
					return_val_if_fail((*edge)->companion->companion == (*edge), false);

					// Companions had better NOT share the same vertex ...
					return_val_if_fail((*edge)->vertex != (*edge)->companion->vertex, false);
				}
		}

	return true;
}

/////////////////////////////////////////////////////////////////////////////
// is_valid

bool is_valid(const nucurve& Curve)
{
	// Order must always be at least 2 (i.e. a linear curve)
	return_val_if_fail(Curve.order >= 2, false);

	// The number of control points must be >= order
	return_val_if_fail(Curve.control_points.size() >= Curve.order, false);

	// The number of knots must be equal to the number of control points plus the order
	return_val_if_fail(Curve.knots.size() == Curve.control_points.size() + Curve.order, false);

	// Knot vector values must always be in ascending order
	for(unsigned long i = 1; i != Curve.knots.size(); ++i)
		return_val_if_fail(Curve.knots[i] >= Curve.knots[i-1], false);

	return true;
}

/////////////////////////////////////////////////////////////////////////////
// is_valid

bool is_valid(const nupatch& Patch)
{
	// Order must always be at least 2 (i.e. linear curves), in each parametric direction
	return_val_if_fail(Patch.u_order >= 2 && Patch.v_order >= 2, false);

	// The number of control points must be >= order, in each parametric direction

	// The number of knots must be equal to the number of control points plus the order, in each parametric direction

	// Knot vector values must always be in ascending order, in each parametric direction
	for(unsigned long i = 1; i != Patch.u_knots.size(); ++i)
		return_val_if_fail(Patch.u_knots[i] >= Patch.u_knots[i-1], false);

	for(unsigned long i = 1; i != Patch.v_knots.size(); ++i)
		return_val_if_fail(Patch.v_knots[i] >= Patch.v_knots[i-1], false);

	return true;
}

/////////////////////////////////////////////////////////////////////////////
// is_solid

bool is_solid(const polyhedron& Polyhedron)
{
	if(!is_valid(Polyhedron))
		return false;

	if(Polyhedron.edges.empty())
		return false;

	for(polyhedron::edges_t::const_iterator edge = Polyhedron.edges.begin(); edge != Polyhedron.edges.end(); ++edge)
		{
			if(0 == (**edge).companion)
				return false;
		}

	return true;
}

/////////////////////////////////////////////////////////////////////////////
// bounds

const bounding_box bounds(const mesh& Mesh)
{
	bounding_box results;
	for(mesh::points_t::const_iterator point = Mesh.points.begin(); point != Mesh.points.end(); ++point)
		results.insert((*point)->position);

	return results;
}

} // namespace k3d