File: mesh.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 239,928 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (197 lines) | stat: -rw-r--r-- 4,401 bytes parent folder | download | duplicates (8)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: mesh.C,v 1.6.16.1 2007-03-25 22:02:34 oliver Exp $

#include <BALL/VIEW/PRIMITIVES/mesh.h>
#include <BALL/SYSTEM/file.h>
#include <BALL/SYSTEM/binaryFileAdaptor.h>

using namespace std;

namespace BALL
{
	namespace VIEW
	{

		Mesh::Mesh()
			:	GeometricObject(),
				Surface(),
				MultiColorExtension()
		{
		}

		Mesh::Mesh(const Mesh& mesh)
			:	GeometricObject(mesh),
				Surface(mesh),
				MultiColorExtension(mesh)
		{
		}

		Mesh::~Mesh()
		{
			#ifdef BALL_VIEW_DEBUG
				Log.error() << "Destructing object " << (void *)this 
										<< " of class " << RTTI::getName<Mesh>() << endl;
			#endif 
		}

		void Mesh::clear()
		{
			GeometricObject::clear();
			MultiColorExtension::clear();
		}

		void Mesh::set(const Mesh& mesh)
		{
			GeometricObject::set(mesh);
			MultiColorExtension::set(mesh);
		}

		const Mesh& Mesh::operator = (const Mesh& mesh)
		{
			set(mesh);
			return *this;
		}

		void Mesh::swap(Mesh& mesh)
		{
			GeometricObject::swap(mesh);
//   			Surface::swap(mesh);
			MultiColorExtension::swap(mesh);
		}

		bool Mesh::isValid() const
		{
			return (GeometricObject::isValid());
		}

		void Mesh::dump(ostream& s, Size depth) const
		{
			BALL_DUMP_STREAM_PREFIX(s);
			
			BALL_DUMP_DEPTH(s, depth);
			BALL_DUMP_HEADER(s, this, this);

			GeometricObject::dump(s, depth + 1);
			MultiColorExtension::dump(s, depth + 1);
			
			BALL_DUMP_STREAM_SUFFIX(s);
		}

		void Mesh::getVertices(vector<Vector3>& vertices) const
		{
			vertices.reserve(vertices.size() + vertex.size());
			for (Position p = 0; p < vertex.size(); p++)
			{
				vertices.push_back(vertex[p]);
			}
		}

		bool Mesh::binaryWrite(const String& filename)
		{
			BinaryFileAdaptor<float> float_ad;
			BinaryFileAdaptor<Size>   size_ad;
			BinaryFileAdaptor<char>   char_ad;
			try
			{
				File outfile(filename, std::ios::out|std::ios::binary);
				outfile << size_ad << vertex.size();
				outfile << size_ad << triangle.size();
				outfile << size_ad << colors.size();
				for (Position p = 0; p < vertex.size(); p++)
				{
					outfile << float_ad << vertex[p].x;
					outfile << float_ad << vertex[p].y;
					outfile << float_ad << vertex[p].z;
				}
				for (Position p = 0; p < normal.size(); p++)
				{
					outfile << float_ad << normal[p].x;
					outfile << float_ad << normal[p].y;
					outfile << float_ad << normal[p].z;
				}
				for (Position p = 0; p < triangle.size(); p++)
				{
					outfile << size_ad << triangle[p].v1;
					outfile << size_ad << triangle[p].v2;
					outfile << size_ad << triangle[p].v3;
				}
				for (Position p = 0; p < colors.size(); p++)
				{
					outfile << char_ad << (char)colors[p].getRed();
					outfile << char_ad << (char)colors[p].getGreen();
					outfile << char_ad << (char)colors[p].getBlue();
					outfile << char_ad << (char)colors[p].getAlpha();
				}
				outfile.close();
				return true;
			}
			catch(...)
			{
			}

			return false;
		}

		bool Mesh::binaryRead(const String& filename)
		{
			clear();

			BinaryFileAdaptor<float> float_ad;
			BinaryFileAdaptor<Size>   size_ad;
			BinaryFileAdaptor<char>   char_ad;
			try
			{
				File infile(filename, std::ios::in|std::ios::binary);
				Size vs, ts, cs;
				infile >> size_ad >> vs;
				infile >> size_ad >> ts;
				infile >> size_ad >> cs;
				vertex.resize(vs);
				triangle.resize(ts);
				colors.resize(cs);
				normal.resize(vs);
				for (Position p = 0; p < vertex.size(); p++)
				{
					infile >> float_ad >> vertex[p].x;
					infile >> float_ad >> vertex[p].y;
					infile >> float_ad >> vertex[p].z;
				}
				for (Position p = 0; p < normal.size(); p++)
				{
					infile >> float_ad >> normal[p].x;
					infile >> float_ad >> normal[p].y;
					infile >> float_ad >> normal[p].z;
				}
				for (Position p = 0; p < triangle.size(); p++)
				{
					infile >> size_ad >> triangle[p].v1;
					infile >> size_ad >> triangle[p].v2;
					infile >> size_ad >> triangle[p].v3;
				}
				char r,g,b,a;
				for (Position p = 0; p < colors.size(); p++)
				{
					infile >> char_ad >> r;
					infile >> char_ad >> g;
					infile >> char_ad >> b;
					infile >> char_ad >> a;
					colors[p].setRed(r);
					colors[p].setGreen(g);
					colors[p].setBlue(b);
					colors[p].setAlpha(a);
				}
				infile.close();
				return true;
			}
			catch(...)
			{
			}

			return false;
		}


	} // namespace VIEW
} // namespace BALL