File: array_helpers.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 (195 lines) | stat: -rw-r--r-- 5,083 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
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
// K-3D
// Copyright (c) 1995-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 Timothy M. Shead (tshead@k-3d.com)
*/

#include "array_helpers.h"

#include <k3dsdk/utility.h>
#include <k3dsdk/texture3.h>

namespace module
{

namespace renderman
{

namespace painters
{

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// array_copier::implementation

class array_copier::implementation
{
public:
	implementation()
	{
	}

	~implementation()
	{
//		std::for_each(copiers.begin(), copiers.end(), k3d::delete_object());
//		std::for_each(destinations.begin(), destinations.end(), k3d::delete_object());
	}

	void add_arrays(const k3d::mesh::table_t& Sources)
	{
		for(k3d::mesh::table_t::const_iterator array_iterator = Sources.begin(); array_iterator != Sources.end(); ++array_iterator)
		{
			const std::string name = array_iterator->first;
			const k3d::array* const source = array_iterator->second.get();

			add_array(name, *source);
		}
	}

	void add_array(const std::string& Name, const k3d::array& Source)
	{
		if(create_copier<k3d::typed_array<double> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<std::string> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::point3> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::vector3> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::normal3> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::texture3> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::color> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::point4> >(Name, Source))
			return;
		if(create_copier<k3d::typed_array<k3d::matrix4> >(Name, Source))
			return;

		k3d::log() << error << k3d_file_reference << " array [" << Name << "] unknown type [" << typeid(Source).name() << "]" << std::endl;
	}

	void insert(const size_t Begin, const size_t End)
	{
		for(size_t i = 0; i != copiers.size(); ++i)
			copiers[i]->insert(Begin, End);
	}

	void copy_to(const k3d::ri::storage_class_t StorageClass, k3d::ri::parameter_list& Destination)
	{
		for(size_t i = 0; i != names.size(); ++i)
			Destination.push_back(k3d::ri::parameter(names[i], StorageClass, 1, destinations[i]));
	}

private:
	class copier_interface
	{
	public:
		virtual ~copier_interface() {}
		virtual void insert(const size_t Begin, const size_t End) = 0;
	};

	template<typename array_type>
	class copier :
		public copier_interface
	{
	public:
		copier(const array_type& Source, array_type& Destination) :
			source(Source),
			destination(Destination)
		{
		}

		void insert(const size_t Begin, const size_t End)
		{
			destination.insert(destination.end(), source.begin() + Begin, source.begin() + End);
		}

	private:
		const array_type& source;
		array_type& destination;
	};

	template<typename array_type>
	bool create_copier(const std::string& Name, const k3d::array& Source)
	{
		if(const array_type* const source = dynamic_cast<const array_type*>(&Source))
		{
			array_type* const destination = new array_type();

			names.push_back(Name);
			destinations.push_back(destination);
			copiers.push_back(new copier<array_type>(*source, *destination));

			return true;
		}

		return false;
	}

	std::vector<std::string> names;
	std::vector<k3d::array*> destinations;
	std::vector<copier_interface*> copiers;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// array_copier

array_copier::array_copier() :
	m_implementation(new implementation())
{
}

array_copier::~array_copier()
{
	delete m_implementation;
}

void array_copier::add_arrays(const k3d::mesh::table_t& Sources)
{
	m_implementation->add_arrays(Sources);
}

void array_copier::add_array(const std::string& Name, const k3d::array& Source)
{
	m_implementation->add_array(Name, Source);
}

void array_copier::insert(const size_t Begin, const size_t End)
{
	m_implementation->insert(Begin, End);
}

void array_copier::push_back(const size_t Index)
{
	m_implementation->insert(Index, Index + 1);
}

void array_copier::copy_to(const k3d::ri::storage_class_t StorageClass, k3d::ri::parameter_list& Destination)
{
	m_implementation->copy_to(StorageClass, Destination);
}

} // namespace painters

} // namespace renderman

} // namespace module