summaryrefslogtreecommitdiff
path: root/cad/src/experimental/oleksandr/hierarchy.cpp
blob: e448723bdda7668267afaa1e3ce1b5dd9daad55b (plain)
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
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
/*
  Name: hierarchy.cpp
  Author: Oleksandr Shevchenko
  Description: class for hierarchy representation 
*/

#include "hierarchy.h"

//----------------------------------------------------------------------------
// Constructor

Hierarchy::Hierarchy():

	// Private data initialization
	mRm(								// rotation matrix
		0),
	mTv(								// translation vector
		0),
	mS(								    // surface pointer
		0)
{
	mTree = new BoxTree;
}

//----------------------------------------------------------------------------
// Destructor

Hierarchy::~Hierarchy()
{
	if (mTree)
	{
		mTree->Delete();
		delete mTree;
		mTree = 0;
	}
}

//------------------------------------------------------------------------
// Initialize()
//
// create box tree 
//
void Hierarchy::Initialize(Surface * s)
{
	int i,j;
	mS = s;
	int n = 3;
	if (s->Type()) n = 4;
	for (i = 0; i < s->Nt(); i+=n)
	{
		Box b;
		for (j = 0; j < n; j++)
		{
			int ij = s->I(i + j);
			Triple p(s->Px(ij), s->Py(ij), s->Pz(ij));
			b.Enclose(p);
		}
		mBoxes.Add(b);
	}
	for (i = 0; i < s->Nt()/n; i++)
	{
		mTree->Add(&mBoxes[i]);
	}
	mTree->BuildTree();
}

//------------------------------------------------------------------------
// Initialize()
//
// create box tree 
//
void Hierarchy::Initialize(int type, Container<int> I, Container<Triple> P, Container<int> C)
{
	//  initialize by arrays
	mType = type;
	mNE = I.Size();
	mEntities = I.GetPtr();
	mNP = P.Size();
	mPoints = P.GetPtr();
	mColors = C.GetPtr();

	int i,j;
	int n = 3;
	if (mType) n = 4;
	for (i = 0; i < mNE; i+=n)
	{
		Box b;
		for (j = 0; j < n; j++)
		{
			int ij = mEntities[i + j];
			b.Enclose(mPoints[ij]);
		}
		mBoxes.Add(b);
	}
	for (i = 0; i < mNE/n; i++)
	{
		mTree->Add(&mBoxes[i]);
	}
	mTree->BuildTree();
}

//------------------------------------------------------------------------
// Transformation()
//
// rotation and translation entity  
//
int Hierarchy::Transformation(int i, Triple * a)
{
	int n = 3;
	if (mS)
	{
		if (mS->Type()) n = 4;
		for (int j = 0; j < n; j++)
		{
			int ij = mS->I(3*i + j);
			Triple p(mS->Px(ij), mS->Py(ij), mS->Pz(ij));
			if (mRm)
			{
				a[j] = *mTv + p * (*mRm); 
			}
			else
			{
				a[j] = *mTv + p;
			}
		}
	}
	else
	{
		//  calulation from array 
		if (mType) n = 4;
		for (int j = 0; j < n; j++)
		{
			int ij = mEntities[3*i + j];
			if (mRm)
			{
				a[j] = *mTv + mPoints[ij] * (*mRm); 
			}
			else
			{
				a[j] = *mTv + mPoints[ij];
			}
		}
	}
	return n;
}