File: bcyl.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (732 lines) | stat: -rw-r--r-- 14,444 bytes parent folder | download | duplicates (6)
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
/*******************************************************************************
 * bcyl.cpp
 *
 * This file contains all functions for bounding
 * cylinders used by lathe and sor objects.
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/backend/bounding/bcyl.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "backend/bounding/bcyl.h"
#include "backend/math/vector.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov
{

/*****************************************************************************
* Local preprocessor defines
******************************************************************************/



/*****************************************************************************
* Local typedefs
******************************************************************************/



/*****************************************************************************
* Static functions
******************************************************************************/

static int  intersect_thick_cylinder (const BCYL *BCyl, const BCYL_INT *rint, const BCYL_INT *hint, const BCYL_ENTRY *Entry, DBL *dist);
static void insert_hit (const BCYL_INT *Element, BCYL_INT *intervals, int *cnt);
static void intersect_bound_elements (const BCYL *BCyl, BCYL_INT *rint, BCYL_INT *hint, const VECTOR P, const VECTOR D);


/*****************************************************************************
* Local variables
******************************************************************************/



/*****************************************************************************
*
* FUNCTION
*
*   intersect_thick_cylinder
*
* INPUT
*
*   BCyl - Pointer to lathe structure
*   Entry - Segment whos bounding cylinder to intersect
*   dist  - List of sorted intersection depths
*
* OUTPUT
*
* RETURNS
*
*   int - number of intersections found
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Find all intersections of the current ray with the bounding
*   cylinder of the given segment. The intersection tests are
*   done in intersect_bound_elements() and the results stored
*   in the lathe structure are evaluated here.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

static int intersect_thick_cylinder(const BCYL *BCyl, const BCYL_INT *rint, const BCYL_INT *hint, const BCYL_ENTRY *Entry, DBL *dist)
{
	int i, j, n;
	DBL k, r, h;

	n = 0;

	/* Intersect ray with the cap-plane. */

	if (hint[Entry->h2].n)
	{
		r = hint[Entry->h2].w[0];

		if ((r >= BCyl->radius[Entry->r1]) &&
		    (r <= BCyl->radius[Entry->r2]))
		{
			dist[n++] = hint[Entry->h2].d[0];
		}
	}

	/* Intersect ray with the base-plane. */

	if (hint[Entry->h1].n)
	{
		r = hint[Entry->h1].w[0];

		if ((r >= BCyl->radius[Entry->r1]) &&
		    (r <= BCyl->radius[Entry->r2]))
		{
			dist[n++] = hint[Entry->h1].d[0];
		}
	}

	/* Intersect with inner cylinder. */

	if (rint[Entry->r1].n)
	{
		h = rint[Entry->r1].w[0];

		if ((h >= BCyl->height[Entry->h1]) &&
		    (h <= BCyl->height[Entry->h2]))
		{
			dist[n++] = rint[Entry->r1].d[0];
		}

		h = rint[Entry->r1].w[1];

		if ((h >= BCyl->height[Entry->h1]) &&
		    (h <= BCyl->height[Entry->h2]))
		{
			dist[n++] = rint[Entry->r1].d[1];
		}
	}

	/* Intersect with outer cylinder. */

	if (rint[Entry->r2].n)
	{
		h = rint[Entry->r2].w[0];

		if ((h >= BCyl->height[Entry->h1]) &&
		    (h <= BCyl->height[Entry->h2]))
		{
			dist[n++] = rint[Entry->r2].d[0];
		}

		h = rint[Entry->r2].w[1];

		if ((h >= BCyl->height[Entry->h1]) &&
		    (h <= BCyl->height[Entry->h2]))
		{
			dist[n++] = rint[Entry->r2].d[1];
		}
	}

	/* Sort intersections. */

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - i - 1; j++)
		{
			if (dist[j] > dist[j+1])
			{
				k         = dist[j];
				dist[j]   = dist[j+1];
				dist[j+1] = k;
			}
		}
	}

	return(n);
}



/*****************************************************************************
*
* FUNCTION
*
*   intersect_bound_elements
*
* INPUT
*
*   BCyl - Pointer to lathe structure
*   P, D  - Current ray
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Intersect all bounding discs and cylinders and store
*   the intersections found in the lathe structure.
*
*   By intersecting all different discs and cylinders once
*   we avoid testing the same cylinders and discs more than
*   once. This happened when we tested one bounding cylinder
*   after the other.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

static void intersect_bound_elements(const BCYL *BCyl, BCYL_INT *rint, BCYL_INT *hint, const VECTOR P, const VECTOR  D)
{
	int i;
	DBL a, b, bb, b2, c, d, k;

	/* Init constants. */

	a = D[X] * D[X] + D[Z] * D[Z];

	b = P[X] * D[X] + P[Z] * D[Z];

	bb = b * b;

	b2 = 2.0 * b;

	c = P[X] * P[X] + P[Z] * P[Z];

	/* Intersect all rings. */

	if ((D[Y] < -EPSILON) || (D[Y] > EPSILON))
	{
		for (i = 0; i < BCyl->nheight; i++)
		{
			k = (BCyl->height[i] - P[Y]) / D[Y];

			hint[i].n = 1;

			hint[i].d[0] = k;

			hint[i].w[0] = k * (a * k + b2) + c;
		}
	}
	else
	{
		for (i = 0; i < BCyl->nheight; i++)
		{
			hint[i].n = 0;
		}
	}

	/* Intersect all cylinders. */

	for (i = 0; i < BCyl->nradius; i++)
	{
		rint[i].n = 0;

		if (BCyl->radius[i] > EPSILON)
		{
			d = bb - a * (c - BCyl->radius[i]);

			if (d > 0.0)
			{
				d = sqrt(d);

				k = (-b + d) / a;

				rint[i].n = 2;

				rint[i].d[0] = k;

				rint[i].w[0] = P[Y] + k * D[Y];

				k = (-b - d) / a;

				rint[i].d[1] = k;

				rint[i].w[1] = P[Y] + k * D[Y];
			}
		}
	}
}

/*****************************************************************************
*
* FUNCTION
*
*   insert_hit
*
* INPUT
*
*   element   - Intersection to insert
*   intervals - List of intervals
*   cnt       - Number of elements in the list
*
* OUTPUT
*
*   intervals, cnt
*
* RETURNS
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Insert an intersection into the depth sorted intersection list.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

static void insert_hit(const BCYL_INT *element, BCYL_INT *intervals, int *cnt)
{
	int k;

	intervals[*cnt] = *element;

	for (k = 0; element->d[0] > intervals[k].d[0]; k++);

	if (k < *cnt)
	{
		// Note: this data structure may be worthwhile optimizing
		POV_MEMMOVE(&intervals[k+1], &intervals[k], (*cnt-k)*sizeof(BCYL_INT));

		intervals[k] = *element;
	}

	(*cnt)++;
}



/*****************************************************************************
*
* FUNCTION
*
*   Intersect_All_Bounds
*
* INPUT
*
*   BCyl     - Pointer to lathe structure
*   P, D      - Current ray
*   intervals - List of intervals
*   cnt       - Number of elements in the list
*
* OUTPUT
*
*   intervals, cnt
*
* RETURNS
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Intersect given ray with all bounding cylinders of the given lathe
*   and return a sorted list of intersection depths and segments hit.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

int Intersect_BCyl(const BCYL *BCyl, BCYL_INT *intervals, BCYL_INT *rint, BCYL_INT *hint, const VECTOR P, const VECTOR  D)
{
	int i, cnt;
	DBL dist[8];
	BCYL_INT Inter;
	BCYL_ENTRY *Entry;

	cnt = 0;

	Inter.d[1] = 0.0;

	/* Intersect all cylinder and plane elements. */

	intersect_bound_elements(BCyl, rint, hint, P, D);

	/* Intersect all spline segments. */
	for (i = 0; i < BCyl->number; i++)
	{
		Entry = &BCyl->entry[i];

		switch (intersect_thick_cylinder(BCyl, rint, hint, Entry, dist))
		{
			case 0:
				break;

			case 2:

				if (dist[0] > EPSILON)
				{
					Inter.d[0] = dist[0];
					Inter.n    = i;

					insert_hit(&Inter, intervals, &cnt);
				}
				else
				{
					if (dist[1] > EPSILON)
					{
						Inter.d[0] = 0.0;
						Inter.n    = i;

						insert_hit(&Inter, intervals, &cnt);
					}
				}

				break;

			case 4:

				if (dist[0] > EPSILON)
				{
					Inter.d[0] = dist[0];
					Inter.n    = i;

					insert_hit(&Inter, intervals, &cnt);
				}
				else
				{
					if (dist[1] > EPSILON)
					{
						Inter.d[0] = 0.0;
						Inter.n    = i;

						insert_hit(&Inter, intervals, &cnt);
					}
					else
					{
						if (dist[2] > EPSILON)
						{
							Inter.d[0] = dist[2];
							Inter.n    = i;

							insert_hit(&Inter, intervals, &cnt);
						}
						else
						{
							if (dist[3] > EPSILON)
							{
								Inter.d[0] = 0.0;
								Inter.n    = i;

								insert_hit(&Inter, intervals, &cnt);
							}
						}
					}
				}

				break;

			default:

				/*
				 * We weren't able to find an even number of intersections. Thus
				 * we can't tell where the ray enters and leaves the bounding
				 * cylinder. To avoid problems we assume that the ray is always
				 * inside the cylinder in that case.
				 */

				Inter.d[0] = dist[0];
				Inter.n    = i;

				insert_hit(&Inter, intervals, &cnt);

				break;
		}
	}

	return(cnt);
}



/*****************************************************************************
*
* FUNCTION
*
*   Create_BCyl
*
* INPUT
*
*   number - number of cylindrical segments
*   r1, r2 - list of segment radii
*   h1, h2 - list of segment heights
*
* OUTPUT
*
* RETURNS
*
*   BCYL * - created bounding cylinder.
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Create a bounding cylinder data structure from the given
*   radii and heights.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

BCYL *Create_BCyl(int number, const DBL *tmp_r1, const DBL *tmp_r2, const DBL *tmp_h1, const DBL *tmp_h2)
{
	int i, j, nr, nh;
	int *tmp_r1_index;
	int *tmp_r2_index;
	int *tmp_h1_index;
	int *tmp_h2_index;
	DBL *tmp_radius;
	DBL *tmp_height;
	BCYL *bcyl;

	/* Allocate bounding cylinder. */

	bcyl = (BCYL *)POV_MALLOC(sizeof(BCYL), "bounding cylinder");

	/* Allocate entries. */

	bcyl->number = number;

	bcyl->entry = (BCYL_ENTRY *)POV_MALLOC(bcyl->number*sizeof(BCYL_ENTRY), "bounding cylinder data");

	/* Allocate temporary lists. */

	tmp_r1_index = (int *)POV_MALLOC(bcyl->number * sizeof(int), "temp lathe data");
	tmp_r2_index = (int *)POV_MALLOC(bcyl->number * sizeof(int), "temp lathe data");
	tmp_h1_index = (int *)POV_MALLOC(bcyl->number * sizeof(int), "temp lathe data");
	tmp_h2_index = (int *)POV_MALLOC(bcyl->number * sizeof(int), "temp lathe data");

	tmp_radius = (DBL *)POV_MALLOC(2 * bcyl->number * sizeof(DBL), "temp lathe data");
	tmp_height = (DBL *)POV_MALLOC(2 * bcyl->number * sizeof(DBL), "temp lathe data");

	/* Get different bounding radii and heights. */

	nr = 0;
	nh = 0;

	for (i = 0; i < bcyl->number; i++)
	{
		tmp_r1_index[i] = -1;
		tmp_r2_index[i] = -1;
		tmp_h1_index[i] = -1;
		tmp_h2_index[i] = -1;

		for (j = 0; j < nr; j++)
		{
			if (tmp_r1[i] == tmp_radius[j])
			{
				break;
			}
		}

		if (j == nr)
		{
			tmp_radius[nr++] = tmp_r1[i];
		}

		tmp_r1_index[i] = j;

		for (j = 0; j < nr; j++)
		{
			if (tmp_r2[i] == tmp_radius[j])
			{
				break;
			}
		}

		if (j == nr)
		{
			tmp_radius[nr++] = tmp_r2[i];
		}

		tmp_r2_index[i] = j;

		for (j = 0; j < nh; j++)
		{
			if (tmp_h1[i] == tmp_height[j])
			{
				break;
			}
		}

		if (j == nh)
		{
			tmp_height[nh++] = tmp_h1[i];
		}

		tmp_h1_index[i] = j;

		for (j = 0; j < nh; j++)
		{
			if (tmp_h2[i] == tmp_height[j])
			{
				break;
			}
		}

		if (j == nh)
		{
			tmp_height[nh++] = tmp_h2[i];
		}

		tmp_h2_index[i] = j;
	}

	/* Copy lists into the lathe. */

	bcyl->radius = (DBL *)POV_MALLOC(nr * sizeof(DBL), "bounding cylinder data");
	bcyl->height = (DBL *)POV_MALLOC(nh * sizeof(DBL), "bounding cylinder data");

	for (i = 0; i < nr; i++)
	{
		bcyl->radius[i] = Sqr(tmp_radius[i]);
	}

	for (i = 0; i < nh; i++)
	{
		bcyl->height[i] = tmp_height[i];
	}

	/* Assign height and radius indices. */

	bcyl->nradius = nr;
	bcyl->nheight = nh;

	for (i = 0; i < bcyl->number; i++)
	{
		bcyl->entry[i].r1 = tmp_r1_index[i];
		bcyl->entry[i].r2 = tmp_r2_index[i];
		bcyl->entry[i].h1 = tmp_h1_index[i];
		bcyl->entry[i].h2 = tmp_h2_index[i];
	}

/*
	fprintf(stderr, "number of different radii   = %d\n", nr);
	fprintf(stderr, "number of different heights = %d\n", nh);
*/

	/* Get rid of temp. memory. */

	POV_FREE(tmp_height);
	POV_FREE(tmp_radius);
	POV_FREE(tmp_h2_index);
	POV_FREE(tmp_h1_index);
	POV_FREE(tmp_r2_index);
	POV_FREE(tmp_r1_index);

	return(bcyl);
}



/*****************************************************************************
*
* FUNCTION
*
*   Destroy_BCyl
*
* INPUT
*
*   BCyl - bounding cylinder
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*   Dieter Bayer
*
* DESCRIPTION
*
*   Destroy a bounding cylinder.
*
* CHANGES
*
*   Oct 1996 : Creation.
*
******************************************************************************/

void Destroy_BCyl(BCYL *BCyl)
{
	POV_FREE(BCyl->entry);

	POV_FREE(BCyl->radius);

	POV_FREE(BCyl->height);

	POV_FREE(BCyl);
}

}