summaryrefslogtreecommitdiff
path: root/indra/llprimitive/llprimlinkinfo.h
blob: bee25975f13fd3c7a7cfb75a8a1681eda0cb3c20 (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
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
/** 
 * @file llprimlinkinfo.h
 * @author andrew@lindenlab.com
 * @brief A template for determining which prims in a set are linkable
 *
 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */


#ifndef LL_PRIM_LINK_INFO_H
#define LL_PRIM_LINK_INFO_H

// system includes
#include <iostream>
#include <map>
#include <list>
#include <vector>

// common includes
#include "stdtypes.h"
#include "v3math.h"
#include "llquaternion.h"
#include "llsphere.h"


const F32 MAX_OBJECT_SPAN = 54.f;		// max distance from outside edge of an object to the farthest edge
const F32 OBJECT_SPAN_BONUS = 2.f;		// infinitesimally small prims can always link up to this distance
const S32 MAX_PRIMS_PER_OBJECT = 256;


template < typename DATA_TYPE >
class LLPrimLinkInfo
{
public:
	LLPrimLinkInfo();
	LLPrimLinkInfo( DATA_TYPE data, const LLSphere& sphere );
	~LLPrimLinkInfo();

	void set( DATA_TYPE data, const LLSphere& sphere );
	void append( DATA_TYPE data, const LLSphere& sphere );
	void getData( std::list< DATA_TYPE >& data_list ) const;
	F32 getDiameter() const;
	LLVector3 getCenter() const;

	// returns 'true' if this info can link with other_info
	bool canLink( const LLPrimLinkInfo< DATA_TYPE >& other_info );

	S32 getPrimCount() const { return mDataMap.size(); }

	void mergeLinkableSet( typename std::list< LLPrimLinkInfo < DATA_TYPE > >& unlinked );

	void transform(const LLVector3& position, const LLQuaternion& rotation);

private:
	// returns number of merges made
	S32 merge(LLPrimLinkInfo< DATA_TYPE >& other_info);

	// returns number of collapses made
	static S32 collapse(typename std::list< LLPrimLinkInfo < DATA_TYPE > >& unlinked );

	void computeBoundingSphere();

	// Internal utility to encapsulate the link rules
	F32 get_max_linkable_span(const LLSphere& first, const LLSphere& second);
	F32 get_span(const LLSphere& first, const LLSphere& second);

private:
	std::map< DATA_TYPE, LLSphere > mDataMap;
	LLSphere mBoundingSphere;
};



template < typename DATA_TYPE >
LLPrimLinkInfo< DATA_TYPE >::LLPrimLinkInfo()
:	mBoundingSphere( LLVector3(0.f, 0.f, 0.f), 0.f )
{
}

template < typename DATA_TYPE >
LLPrimLinkInfo< DATA_TYPE >::LLPrimLinkInfo( DATA_TYPE data, const LLSphere& sphere)
:	mBoundingSphere(sphere)
{
	mDataMap[data] = sphere;
}

template < typename DATA_TYPE >
LLPrimLinkInfo< DATA_TYPE >::~LLPrimLinkInfo()
{
	mDataMap.clear();
}

template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE>::set( DATA_TYPE data, const LLSphere& sphere )
{
	if (!mDataMap.empty())
	{
		mDataMap.clear();
	}
	mDataMap[data] = sphere;
	mBoundingSphere = sphere;
}

template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE>::append( DATA_TYPE data, const LLSphere& sphere )
{
	mDataMap[data] = sphere;
	if (!mBoundingSphere.contains(sphere))
	{
		computeBoundingSphere();
	}
}

template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE >::getData( std::list< DATA_TYPE >& data_list) const
{
	typename std::map< DATA_TYPE, LLSphere >::const_iterator map_itr;
	for (map_itr = mDataMap.begin(); map_itr != mDataMap.end(); ++map_itr)
	{
		data_list.push_back(map_itr->first);
	}
}

template < typename DATA_TYPE >
F32 LLPrimLinkInfo< DATA_TYPE >::getDiameter() const
{ 
	return 2.f * mBoundingSphere.getRadius();
}

template < typename DATA_TYPE >
LLVector3 LLPrimLinkInfo< DATA_TYPE >::getCenter() const
{ 
	return mBoundingSphere.getCenter(); 
}

template < typename DATA_TYPE >
F32 LLPrimLinkInfo< DATA_TYPE >::get_max_linkable_span(const LLSphere& first, const LLSphere& second)
{
	F32 max_span = 3.f * (first.getRadius() + second.getRadius()) + OBJECT_SPAN_BONUS;
	if (max_span > MAX_OBJECT_SPAN)
	{
		max_span = MAX_OBJECT_SPAN;
	}

	return max_span;
}

template < typename DATA_TYPE >
F32 LLPrimLinkInfo< DATA_TYPE >::get_span(const LLSphere& first, const LLSphere& second)
{
	F32 span = (first.getCenter() - second.getCenter()).length()
				+ first.getRadius() + second.getRadius();
	return span;
}

// static
// returns 'true' if this info can link with any part of other_info
template < typename DATA_TYPE >
bool LLPrimLinkInfo< DATA_TYPE >::canLink(const LLPrimLinkInfo& other_info)
{
	F32 max_span = get_max_linkable_span(mBoundingSphere, other_info.mBoundingSphere);

	F32 span = get_span(mBoundingSphere, other_info.mBoundingSphere);
	
	if (span <= max_span)
	{
		// The entire other_info fits inside the max span.
		return TRUE;
	}
	else if (span > max_span + 2.f * other_info.mBoundingSphere.getRadius())
	{
		// there is no way any piece of other_info could link with this one
		return FALSE;
	}

	// there may be a piece of other_info that is linkable
	typename std::map< DATA_TYPE, LLSphere >::const_iterator map_itr;
	for (map_itr = other_info.mDataMap.begin(); map_itr != other_info.mDataMap.end(); ++map_itr)
	{
		const LLSphere& other_sphere = (*map_itr).second;
		max_span = get_max_linkable_span(mBoundingSphere, other_sphere);

		span = get_span(mBoundingSphere, other_sphere);

		if (span <= max_span)
		{
			// found one piece that is linkable
			return TRUE;
		}
	}
	return FALSE;
}

// merges elements of 'unlinked' 
// returns number of links made (NOT final prim count, NOR linked prim count)
// and removes any linkable infos from 'unlinked' 
template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE >::mergeLinkableSet(std::list< LLPrimLinkInfo< DATA_TYPE > > & unlinked)
{
	bool linked_something = true;
	while (linked_something)
	{
		linked_something = false;

		typename std::list< LLPrimLinkInfo< DATA_TYPE > >::iterator other_itr = unlinked.begin();
		while ( other_itr != unlinked.end()
			   && getPrimCount() < MAX_PRIMS_PER_OBJECT )
		{
			S32 merge_count = merge(*other_itr);
			if (merge_count > 0)
			{
				linked_something = true;
			}
			if (0 == (*other_itr).getPrimCount())
			{
				unlinked.erase(other_itr++);
			}
			else
			{
				++other_itr;
			}
		}
		if (!linked_something
			&& unlinked.size() > 1)
		{
			S32 collapse_count = collapse(unlinked);
			if (collapse_count > 0)
			{
				linked_something = true;
			}
		}
	}
}

// transforms all of the spheres into a new reference frame
template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE >::transform(const LLVector3& position, const LLQuaternion& rotation)
{
	typename std::map< DATA_TYPE, LLSphere >::iterator map_itr;
	for (map_itr = mDataMap.begin(); map_itr != mDataMap.end(); ++map_itr)
	{
		(*map_itr).second.setCenter((*map_itr).second.getCenter() * rotation + position);
	}
	mBoundingSphere.setCenter(mBoundingSphere.getCenter() * rotation + position);
}

// private
// returns number of links made
template < typename DATA_TYPE >
S32 LLPrimLinkInfo< DATA_TYPE >::merge(LLPrimLinkInfo& other_info)
{
	S32 link_count = 0;

//	F32 other_radius = other_info.mBoundingSphere.getRadius();
//	other_info.computeBoundingSphere();
//	if ( other_radius != other_info.mBoundingSphere.getRadius() )
//	{
//		LL_INFOS() << "Other bounding sphere changed!!" << LL_ENDL;
//	}

//	F32 this_radius = mBoundingSphere.getRadius();
//	computeBoundingSphere();
//	if ( this_radius != mBoundingSphere.getRadius() )
//	{
//		LL_INFOS() << "This bounding sphere changed!!" << LL_ENDL;
//	}


	F32 max_span = get_max_linkable_span(mBoundingSphere, other_info.mBoundingSphere);

	//  F32 center_dist = (mBoundingSphere.getCenter() - other_info.mBoundingSphere.getCenter()).length();
	//	LL_INFOS() << "objects are " << center_dist << "m apart" << LL_ENDL;
	F32 span = get_span(mBoundingSphere, other_info.mBoundingSphere);

	F32 span_limit = max_span + (2.f * other_info.mBoundingSphere.getRadius());
	if (span > span_limit)
	{
		// there is no way any piece of other_info could link with this one
		// LL_INFOS() << "span too large:  " << span << " vs. " << span_limit << LL_ENDL;
		return 0;
	}

	bool completely_linkable = (span <= max_span) ? true : false;

	typename std::map< DATA_TYPE, LLSphere >::iterator map_itr = other_info.mDataMap.begin();
	while (map_itr != other_info.mDataMap.end()
			&& getPrimCount() < MAX_PRIMS_PER_OBJECT )
	{
		DATA_TYPE other_data = (*map_itr).first;
		LLSphere& other_sphere = (*map_itr).second;

		if (!completely_linkable)
		{
			max_span = get_max_linkable_span(mBoundingSphere, other_sphere);
	
			F32 span = get_span(mBoundingSphere, other_sphere);

			if (span > max_span)
			{
				++map_itr;
				continue;
			}
		}

		mDataMap[other_data] = other_sphere;
		++link_count;

		if (!mBoundingSphere.contains(other_sphere) )
		{
			computeBoundingSphere();
		}

		// remove from the other info
		other_info.mDataMap.erase(map_itr++);
	}

	if (link_count > 0 && other_info.getPrimCount() > 0)
	{
		other_info.computeBoundingSphere();
	}
	return link_count;
}

// links any linkable elements of unlinked
template < typename DATA_TYPE >
S32 LLPrimLinkInfo< DATA_TYPE >::collapse(std::list< LLPrimLinkInfo< DATA_TYPE > > & unlinked)
{ 
	S32 link_count = 0;
	bool linked_something = true;
	while (linked_something)
	{
		linked_something = false;

		typename std::list< LLPrimLinkInfo< DATA_TYPE > >::iterator this_itr = unlinked.begin();
		typename std::list< LLPrimLinkInfo< DATA_TYPE > >::iterator other_itr = this_itr;
		++other_itr;
		while ( other_itr != unlinked.end() )
			   
		{
			S32 merge_count = (*this_itr).merge(*other_itr);
			if (merge_count > 0)
			{
				linked_something = true;
				link_count += merge_count;
			}
			if (0 == (*other_itr).getPrimCount())
			{
				unlinked.erase(other_itr++);
			}
			else
			{
				++other_itr;
			}
		}
	}
	return link_count;
}


template < typename DATA_TYPE >
void LLPrimLinkInfo< DATA_TYPE >::computeBoundingSphere()
{ 
	std::vector< LLSphere > sphere_list;
	typename std::map< DATA_TYPE, LLSphere >::const_iterator map_itr;
	for (map_itr = mDataMap.begin(); map_itr != mDataMap.end(); ++map_itr)
	{
		sphere_list.push_back(map_itr->second);
	}
	mBoundingSphere = LLSphere::getBoundingSphere(sphere_list);
}


#endif