File: plane.cpp

package info (click to toggle)
ghemical 2.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,660 kB
  • ctags: 2,106
  • sloc: cpp: 17,100; sh: 8,840; makefile: 184
file content (410 lines) | stat: -rw-r--r-- 9,568 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
// PLANE.CPP

// Copyright (C) 1998 Tommi Hassinen.

// This program is free software; you can redistribute it and/or modify it
// under the terms of the license (GNU GPL) which comes with this package.

/*################################################################################################*/

#include "plane.h"	// config.h is here -> we get ENABLE-macros here...

#include "project.h"
#include "appdefine.h"

#include <GL/gl.h>

#include <fstream>
#include <sstream>
using namespace std;

/*################################################################################################*/

color_plane::color_plane(cp_param & p1)
{
	prj = p1.prj;
	data = p1.data;
	
	my_glname = p1.my_glname;
	
	transparent = p1.transparent;
	automatic_cv2 = p1.automatic_cv2;
	
	np = p1.np;
	
	// now we can allocate memory...
	
	dist = new fGL[np];
	
	color4 = new fGL[np * np * 4];
	point3 = new fGL[np * np * 3];
	
	// we must do these settings...
	// we must do these settings...
	// we must do these settings...
	
	ref = p1.ref;
	GetValue = p1.vf;
	GetColor = p1.cf;
	
	cvalue1 = p1.cvalue1;
	cvalue2 = p1.cvalue2;
	alpha = p1.alpha;
	
	// ...before trying to do this!!!
	// ...before trying to do this!!!
	// ...before trying to do this!!!
	
	SetDimension(p1.dim);
	Update();
	
	// add the primitives, if this is a transparent object...
	// add the primitives, if this is a transparent object...
	// add the primitives, if this is a transparent object...
	
	if (transparent)
	{
		for (i32s n1 = 0;n1 < np - 1;n1++)
		{
			for (i32s n2 = 0;n2 < np - 1;n2++)
			{
				i32s tmp1, tmp2, tmp3;
				
				tmp1 = n1 + 0; tmp2 = n2 + 0; tmp3 = tmp1 * np + tmp2;
				fGL * c1 = & color4[tmp3 * 4]; fGL * p1 = & point3[tmp3 * 3];
				
				tmp1 = n1 + 1; tmp2 = n2 + 0; tmp3 = tmp1 * np + tmp2;
				fGL * c2 = & color4[tmp3 * 4]; fGL * p2 = & point3[tmp3 * 3];
				
				tmp1 = n1 + 1; tmp2 = n2 + 1; tmp3 = tmp1 * np + tmp2;
				fGL * c3 = & color4[tmp3 * 4]; fGL * p3 = & point3[tmp3 * 3];
				
				tmp1 = n1 + 0; tmp2 = n2 + 1; tmp3 = tmp1 * np + tmp2;
				fGL * c4 = & color4[tmp3 * 4]; fGL * p4 = & point3[tmp3 * 3];
				
				tpd_quad_4c * tmp4;
				tmp4 = new tpd_quad_4c(c1, p1, c2, p2, c3, p3, c4, p4);
				
				transparent_primitive * tmp5;
				tmp5 = new transparent_primitive((void *) this, * tmp4);
				
				prj->AddTP((void *) this, (* tmp5));
				
				// tmp4, the tpd_quad_4c-object, will be deleted when
				// prj->RemoveAllTPs() is called for this object...
				
				delete tmp5;
			}
		}
	}
}

color_plane::~color_plane(void)
{
	if (transparent) prj->RemoveAllTPs((void *) this);
	
	delete[] dist;
	
	delete[] color4;
	delete[] point3;
}

void color_plane::Update(void)
{
	xdir = data->ydir.vpr(data->zdir);
	
	i32s tmp1[2];				// point indices...
	fGL * tmp2 = new fGL[np * np];		// color values...
	
	for (tmp1[0] = 0;tmp1[0] < np;tmp1[0]++)
	{
		for (tmp1[1] = 0;tmp1[1] < np;tmp1[1]++)
		{
			i32s index = tmp1[0] * np + tmp1[1];
			GetCRD(tmp1, & point3[index * 3]);
			
			tmp2[tmp1[0] * np + tmp1[1]] = GetValue(ref, & point3[index * 3], NULL);
		}
	}
	
	if (automatic_cv2)
	{
		f64 avrg = 0.0;
		for (i32s n1 = 0;n1 < np * np;n1++)
		{
			avrg += tmp2[n1];
		}
		
		// AUTO makes only partial correction -> just looks better and one can estimate the "bias".
		// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
		
		cvalue2 = -0.80 * avrg / (f64) (np * np);
	}
	
	for (tmp1[0] = 0;tmp1[0] < np;tmp1[0]++)
	{
		for (tmp1[1] = 0;tmp1[1] < np;tmp1[1]++)
		{
			i32s index = tmp1[0] * np + tmp1[1];
			fGL tmp3 = tmp2[tmp1[0] * np + tmp1[1]] + cvalue2;
			GetColor((tmp3 / cvalue1), alpha, & color4[index * 4]);
		}
	}
	
	delete[] tmp2;
	
	// update the primitives, if this is a transparent object...
	// update the primitives, if this is a transparent object...
	// update the primitives, if this is a transparent object...
	
	if (transparent) prj->UpdateMPsForAllTPs((void *) this);
}

void color_plane::Render(void)
{
	glPushMatrix();
	
	glPushName(GLNAME_OBJECT);
	glPushName(my_glname);
	
	// if this is a transparent object, we will not render the quads here.
	// instead, the quads get rendered at project::Render(), like all other
	// transparent primitives.
	
	if (!transparent)
	{
		glBegin(GL_QUADS);
		
		for (i32s n1 = 0;n1 < np - 1;n1++)
		{
			for (i32s n2 = 0;n2 < np - 1;n2++)
			{
				i32s tmp1, tmp2, tmp3;
				
				tmp1 = n1 + 0; tmp2 = n2 + 0; tmp3 = tmp1 * np + tmp2;
				glColor4fv(& color4[tmp3 * 4]); glVertex3fv(& point3[tmp3 * 3]);
				
				tmp1 = n1 + 1; tmp2 = n2 + 0; tmp3 = tmp1 * np + tmp2;
				glColor4fv(& color4[tmp3 * 4]); glVertex3fv(& point3[tmp3 * 3]);
				
				tmp1 = n1 + 1; tmp2 = n2 + 1; tmp3 = tmp1 * np + tmp2;
				glColor4fv(& color4[tmp3 * 4]); glVertex3fv(& point3[tmp3 * 3]);
				
				tmp1 = n1 + 0; tmp2 = n2 + 1; tmp3 = tmp1 * np + tmp2;
				glColor4fv(& color4[tmp3 * 4]); glVertex3fv(& point3[tmp3 * 3]);
			}
		}
			
		glEnd();
	}
	
	SetModelView(data);
	
	fGL tmp1 = -0.55 * dim;
	fGL tmp2 = -0.45 * dim;
	
	glBegin(GL_LINES);
	glColor3f(1.0, 1.0, 1.0); glVertex3f(tmp1, tmp1, 0.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(tmp2, tmp1, 0.0);
	
	glColor3f(1.0, 1.0, 1.0); glVertex3f(tmp1, tmp1, 0.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(tmp1, tmp2, 0.0);
	
	glColor3f(1.0, 1.0, 1.0); glVertex3f(tmp1, tmp1, 0.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(tmp1, tmp1, 0.1 * dim);
	glEnd();
	
	glPopName();
	glPopName();
	
	glPopMatrix();
}

void color_plane::SetDimension(fGL p1)
{
	dim = p1; fGL tmp1 = np - 1;
	for (i32s n1 = 0;n1 < np;n1++)
	{
		fGL tmp2 = (fGL) n1 / tmp1;
		dist[n1] = dim * (tmp2 - 0.5);
	}
}

void color_plane::GetCRD(i32s * p1, fGL * p2)
{
	for (i32s n1 = 0;n1 < 3;n1++)
	{
		fGL tmp1 = xdir[n1] * dist[p1[0]];
		fGL tmp2 = data->ydir[n1] * dist[p1[1]];
		p2[n1] = data->crd[n1] + tmp1 + tmp2;
	}
}

/*################################################################################################*/

color_plane_object::color_plane_object(const object_location & p1, cp_param & p2, const char * p3) : smart_object(p1)
{
	p2.data = GetLocData();
	p2.my_glname = p2.prj->RegisterGLName((dummy_object *) this);
	
	cp = new color_plane(p2);
	
	ostringstream str;
	str << p3 << "plane" << ends;
	
	copy_of_ref = p2.ref;
	
	object_name = new char[strlen(str.str().c_str()) + 1];
	strcpy(object_name, str.str().c_str());
}

color_plane_object::~color_plane_object(void)
{
	delete cp;
	delete[] object_name;
	
	// TODO : unregister my_glname???
}

void color_plane_object::OrbitObject(const fGL * p1, const camera & p2)
{
	dummy_object::RotateObject(p1, p2);
	Update();
}

void color_plane_object::RotateObject(const fGL * p1, const camera & p2)
{
	dummy_object::OrbitObject(p1, p2);
	Update();
}

void color_plane_object::TranslateObject(const fGL * p1, const obj_loc_data * p2)
{
	fGL tmp1[3] = { p1[0], p1[1], p1[2] };
	
	if (p2 == GetLocData())
	{
		tmp1[0] = -tmp1[0];
		tmp1[2] = -tmp1[2];
	}
	
	dummy_object::TranslateObject(tmp1, p2);
	Update();
}

/*################################################################################################*/

volume_rendering_object::volume_rendering_object(const object_location & p1,
	cp_param & p2, i32s p3, fGL p4, camera & p5, const char * p6) : smart_object(p1)
{
	for (i32s n1 = 0;n1 < p3;n1++)
	{
		data_vector.push_back(new obj_loc_data);
		
		p2.data = data_vector.back();
		p2.my_glname = p2.prj->RegisterGLName((dummy_object *) this);
		
		cp_vector.push_back(new color_plane(p2));
	}
	
	thickness = p4;
	ConnectCamera(p5);
	
	ostringstream str;
	str << p6 << "VR" << ends;
	
	copy_of_ref = p2.ref;
	
	object_name = new char[strlen(str.str().c_str()) + 1];
	strcpy(object_name, str.str().c_str());
	
	CameraEvent(p5);
}

volume_rendering_object::~volume_rendering_object(void)
{
	for (i32u n1 = 0;n1 < cp_vector.size();n1++)
	{
		delete cp_vector[n1];
		delete data_vector[n1];
		
		// TODO : unregister my_glname???
	}
	
	delete[] object_name;
}

void volume_rendering_object::CameraEvent(const camera & p1)
{
	v3d<fGL> newz = v3d<fGL>(GetLocData()->crd, p1.GetLocData()->crd);
	newz = newz / newz.len();
	
	v3d<fGL> newx = newz.vpr(p1.GetLocData()->ydir);
	newx = newx / newx.len();
	
	v3d<fGL> newy = newx.vpr(newz);
	newy = newy / newy.len();
	
	GetLocDataRW()->zdir = newz;
	GetLocDataRW()->ydir = newy;
	
	for (i32u n1 = 0;n1 < cp_vector.size();n1++)
	{
		fGL z1 = (fGL) n1 / (fGL) (cp_vector.size() - 1);
		fGL z2 = 2.0 * (z1 - 0.5) * thickness;
		
		v3d<fGL> pv = v3d<fGL>(GetLocData()->crd);
		pv = pv + (GetLocData()->zdir * z2);
		
		data_vector[n1]->crd[0] = pv.data[0];
		data_vector[n1]->crd[1] = pv.data[1];
		data_vector[n1]->crd[2] = pv.data[2];
		
		data_vector[n1]->zdir = GetLocData()->zdir;
		data_vector[n1]->ydir = GetLocData()->ydir;
	}
	
	Update();	// update the planes!!!!!
}

void volume_rendering_object::OrbitObject(const fGL *, const camera &)
{
}

void volume_rendering_object::RotateObject(const fGL *, const camera &)
{
}

void volume_rendering_object::TranslateObject(const fGL * p1, const obj_loc_data * p2)
{
	fGL tmp1[3] = { p1[0], p1[1], p1[2] };
	
	if (p2 == GetLocData())
	{
		tmp1[0] = -tmp1[0];
		tmp1[2] = -tmp1[2];
	}
	
	dummy_object::TranslateObject(tmp1, p2);
	Update();
}

void volume_rendering_object::Render(void)
{
	for (i32u n1 = 0;n1 < cp_vector.size();n1++)
	{
		cp_vector[n1]->Render();
	}
}

void volume_rendering_object::Update(void)
{
	for (i32u n1 = 0;n1 < cp_vector.size();n1++)
	{
		cp_vector[n1]->Update();
	}
}

/*################################################################################################*/

// eof