BrainBugz / source / bbCombineDesires.cpp

  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
//
// Copyright (C) 2002 Carsten Kolve
// 
// File: bbCombineDesires.cpp
//
// Dependency Graph Node: bbCombineDesires
//
// Recompiled for Maya 2012 by Shawn Lipowski
//

#include <iostream>

#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MFnVectorArrayData.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MFnEnumAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MVectorArray.h>
#include <maya/MObjectArray.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>

#include "bbCombineDesiresNode.h"
#include "MTools.h"

MTypeId     bbCombineDesires::id( 0x3a002 );

// definitions

#define CM_LINEAR				0
#define CM_WEIGHTS				1
#define CM_PRIORITY				2
#define CM_STRONGEST			3
#define CM_WEAKEST				4
#define CM_SELECT				5

#define DEFAULT_WEIGHT			1.0
#define DEFAULT_PRIORITY		1
#define DEFAULT_DITHERINGFACTOR	1.0


// attributes

#define declareAttr(_attr)\
	MObject	className##::##_attr;\

declareAttr(inputForce );
declareAttr(outputForce );

declareAttr(weight);
declareAttr(priority);
declareAttr(ditheringFactor);
declareAttr(numForces);

declareAttr(combineMode );
declareAttr(selectedForce);


bbCombineDesires::bbCombineDesires() {}
bbCombineDesires::~bbCombineDesires() {}

/************************************************************************************/
MStatus bbCombineDesires::compute( const MPlug& plug, MDataBlock& data )
{
	MStatus stat;
//	MGlobal::displayInfo("compute");

	if ( plug == outputForce )	
	{
		// get input forces
		MArrayDataHandle inputForceAD = data.inputArrayValue( inputForce, &stat);
		McheckErr(stat,"bbCombineDesires::compute inputPrioritiesArrayData");
		int numForces  = inputForceAD.elementCount();
		inputForceAD.jumpToElement(0);

		if (numForces > 0)
		{

			// check size of input arrays weights priorities dithering factors
			
/*			// check size of influence data
	
			int influenceDataSize = influenceDataV.length();

			// if necessary update influence data size

			if (influenceDataSize != numForces)
			{
				if (influenceDataSize < numForces)
				{
					for (int e=influenceDataSize-1; e<numForces; e++)
						influenceDataV.append(1.0);	
				}
				else
				{
					do
					{
						influenceDataV.remove(influenceDataSize-1);
						influenceDataSize = influenceDataV.length();
					}
					while (influenceDataV.length() > numForces);
				}
			}
*/
			// get combine forces
			short combineModeV = combineModeValue(data);

			// create needed variables
			MVectorArray outForces;
	
			switch ( combineModeV ) 
			{
				case CM_LINEAR: cdLinear(inputForceAD,numForces,outForces);
								break;
				case CM_WEIGHTS: cdWeights(inputForceAD,numForces,data,outForces);
								break;
				case CM_PRIORITY: cdPriorities(inputForceAD,numForces,data,outForces);
								break;
				case CM_SELECT: cdSelect(inputForceAD,data,outForces);
								break;
			}

			// get output force array from block.
			//

			MDataHandle hOut = data.outputValue( outputForce, &stat);
			McheckErr(stat, "ERROR in hOut = bOutArray.addElement.\n");

			MFnVectorArrayData fnOutputForce;
			MObject dOutputForce = fnOutputForce.create( outForces, &stat );
			
			McheckErr(stat, "ERROR in dOutputForce = fnOutputForce.create\n");

			// update data block with new output force data.
			//
			hOut.set( dOutputForce );
			data.setClean( plug );

			// store influence data

//			hOut = data.outputValue( influenceData, &stat);
//			MFnDoubleArrayData fnInfluenceDataV;

//			dInfluenceDataV = fnInfluenceDataV.create( influenceDataV, &stat );

			// update data block with new influence data
//			hOut.set( dInfluenceDataV );


			outForces.clear();
		}		
		return MS::kSuccess;
	}

	return MS::kSuccess;
}

/************************************************************************************/
// linear combination

void bbCombineDesires::cdLinear(MArrayDataHandle &inputForceAD,int numForces,MVectorArray &outForces)
{
	MStatus stat;
	MDataHandle elementHandle;
	MVectorArray currForces;

	int aI; // array iterator;
	int eI; // element iterator;

	// get first force element
	inputForceAD.jumpToElement(0);
	MDataHandle hCurrForces = inputForceAD.inputValue(&stat);
	MObject dCurrForces = hCurrForces.data();
	currForces = MFnVectorArrayData(dCurrForces).array(&stat);

	int forcesSize = currForces.length();

	// variables to store weights
	MIntArray weights(numForces,0);
	MIntArray weightsSum(forcesSize,0);

	outForces.copy(currForces);
	

	for (aI=1; aI<numForces;aI++)
	{
		inputForceAD.next();
		currForces.clear();
						
		hCurrForces = inputForceAD.inputValue(&stat);
		dCurrForces = hCurrForces.data();
				
		currForces.copy(MFnVectorArrayData(dCurrForces).array(&stat));

		for (eI=0; eI<forcesSize; eI++)
			if (currForces[eI] != MVector::zero)
			{
				outForces[eI] += currForces[eI];
				weightsSum[eI] += 1;
			}
	}

	for (eI=0; eI<forcesSize; eI++)
		if (weightsSum[eI]>0)
			outForces[eI] *= 1/weightsSum[eI];

	weights.clear();
	weightsSum.clear();
}

/************************************************************************************/
// get weights

MDoubleArray bbCombineDesires::getWeights(MDataBlock& data, int numForces)
{
	MStatus stat;
	MDataHandle elementHandle;
	
	MArrayDataHandle weightAD = data.inputArrayValue( weight, &stat);
	McheckErr(stat,"bbCombineForces::getWeights inputWeightsArrayData");

	int numWeights = weightAD.elementCount();
	MDoubleArray weights(numForces, DEFAULT_WEIGHT);

	for (int i=0; i < numWeights; i++)
	{
		stat = weightAD.jumpToElement(i);
		if (stat == MS::kSuccess )
		{
			elementHandle = weightAD.inputValue(&stat);
			weights[i] = elementHandle.asDouble();
		}
	}

	return weights;
}
/************************************************************************************/
// get priorities

MIntArray bbCombineDesires::getPriorities(MDataBlock& data, int numForces)
{
	MStatus stat;
	MDataHandle elementHandle;
	
	MArrayDataHandle priorityAD = data.inputArrayValue( priority, &stat);
	McheckErr(stat,"bbCombineForces::getPriorities inputPrioritiesArrayData");

	int numPriorities = priorityAD.elementCount();

//	cout << "\nnumPrio: " << 	numPriorities ;

	MIntArray priorities(numForces, DEFAULT_PRIORITY);

	for (int i=0; i < numPriorities; i++)
	{
		stat = priorityAD.jumpToElement(i);
		if (stat == MS::kSuccess )
		{
			elementHandle = priorityAD.inputValue(&stat);
			priorities[i] = elementHandle.asShort();
//				cout << "\n+ ";
		}
	}
	
//	cout << "\nprio: " << 	priorities ;

	return priorities;
}
/************************************************************************************/
// weighted combination

void bbCombineDesires::cdWeights(MArrayDataHandle &inputForceAD,int numForces, MDataBlock& data, MVectorArray &outForces)
{
	MStatus stat;
	MDataHandle elementHandle;
	MVectorArray currForces;

	int aI; // array iterator;
	int eI; // element iterator;


	// get weights
	MDoubleArray weights = getWeights(data, numForces);
	

	// get first force element
	inputForceAD.jumpToElement(0);
	MDataHandle hCurrForces = inputForceAD.inputValue(&stat);
	MObject dCurrForces = hCurrForces.data();
	currForces = MFnVectorArrayData(dCurrForces).array(&stat);

	int forcesSize = currForces.length();

	// variables to store weights
	MDoubleArray weightsSum(forcesSize,0);
	MVectorArray emptyForces(forcesSize, MVector::zero);

	outForces.copy(emptyForces);

	// compute outForce on basis of handles
	for (aI=0; aI<numForces;aI++)
	{
		hCurrForces = inputForceAD.inputValue(&stat);
		dCurrForces = hCurrForces.data();
					
		currForces.copy(MFnVectorArrayData(dCurrForces).array(&stat));

		for (eI=0; eI<forcesSize; eI++)
			if (currForces[eI] != MVector::zero)
			{
				outForces[eI] += currForces[eI] * weights[aI] ;
				weightsSum[eI] += weights[aI];
			}
						
		inputForceAD.next();
		currForces.clear();
	}

	// scale outForce with weightedSum

	for (eI=0; eI<forcesSize; eI++)
		if (weightsSum[eI]>0)
			outForces[eI] = outForces[eI] * 1/double(weightsSum[eI]);

	weightsSum.clear();
}	

/************************************************************************************/
//priorized combination

void bbCombineDesires::cdPriorities(MArrayDataHandle &inputForceAD,int numForces, MDataBlock& data, MVectorArray &outForces)
{
	MStatus stat;
	MDataHandle elementHandle;
	MVectorArray currForces;

	int aI; // array iterator;
	int eI; // element iterator;


	// get priorities
	MIntArray priorities = getPriorities(data, numForces);
	
	// get first force element
	inputForceAD.jumpToElement(0);
	MDataHandle hCurrForces = inputForceAD.inputValue(&stat);
	MObject dCurrForces = hCurrForces.data();
	currForces = MFnVectorArrayData(dCurrForces).array(&stat);

	int forcesSize = currForces.length();

	MVectorArray emptyForces(forcesSize, MVector::zero);
	outForces.copy(emptyForces);

	// indicates per particle which priority is active

	MIntArray currPriorities(forcesSize, MAX_INT);

	for (aI=0; aI<numForces;aI++)
	{
		inputForceAD.jumpToElement(aI);
		hCurrForces = inputForceAD.inputValue(&stat);
		dCurrForces = hCurrForces.data();
					
		currForces.copy(MFnVectorArrayData(dCurrForces).array(&stat));
		
		for (eI=0; eI<forcesSize; eI++)
		{
			if ((priorities[aI]<currPriorities[eI])&&(currForces[eI] != MVector::zero))
			{
				outForces[eI] = currForces[eI];
				currPriorities[eI] = priorities[aI];
			}
		}
		currForces.clear();
					
	}

	priorities.clear();
}

/************************************************************************************/
void bbCombineDesires::cdSelect(MArrayDataHandle &inputForceAD, MDataBlock& data, MVectorArray &outForces)
{
	MStatus stat;

	MDataHandle hCurrForces; 
	MObject dCurrForces; 

	short selectedForceV = selectedForceValue(data);
					
	stat = inputForceAD.jumpToElement(selectedForceV);
					
	if (stat==MS::kSuccess)
	{
		hCurrForces = inputForceAD.inputValue(&stat);
		dCurrForces = hCurrForces.data();
		outForces.copy(MFnVectorArrayData(dCurrForces).array(&stat));
	}
}


/************************************************************************************/
/************************************************************************************/
/************************************************************************************/
/************************************************************************************/
/************************************************************************************/

void* bbCombineDesires::creator()
{
	return new bbCombineDesires();
}

/************************************************************************************/

MStatus bbCombineDesires::initialize()
{
	MGlobal::displayInfo("bbCombineDesires... loaded");

	MFnNumericAttribute nAttr;
	MStatus				stat;

	MFnTypedAttribute	tAttr;
	MFnEnumAttribute	eAttr;
	
	inputForce=tAttr.create( "inputForce", "if", MFnVectorArrayData::kVectorArray , &stat );
	stat = tAttr.setArray( true );

//	influenceData = tAttr.create( "influenceData", "id", MFnDoubleArrayData::kDoubleArray , &stat );
//	tAttr.setStorable(true);

	selectedForce = nAttr.create("selectedForce","sef",MFnNumericData::kShort,0);
	nAttr.setMin(0);
	nAttr.setMax(50);
	nAttr.setStorable(true);

	outputForce=tAttr.create( "outputForce", "of", MFnVectorArrayData::kVectorArray , &stat );
	stat = tAttr.setStorable( false );

	weight = nAttr.create("weight","wgt",MFnNumericData::kDouble,1.0);
	nAttr.setArray( true );
	nAttr.setReadable( true );
	nAttr.setStorable( true );
	nAttr.setUsesArrayDataBuilder( true );

	
	priority = nAttr.create("priority","pri",MFnNumericData::kShort,1);
	nAttr.setArray( true );
	nAttr.setReadable( true );
	nAttr.setStorable( true );
	nAttr.setUsesArrayDataBuilder( true );	

	ditheringFactor = nAttr.create("ditheringFactor","df",MFnNumericData::kDouble,1.0);
	nAttr.setMin(0.0);
	nAttr.setMax(1.0);
	nAttr.setArray( true );
	nAttr.setReadable( true );
	nAttr.setStorable( true );
	nAttr.setUsesArrayDataBuilder( true );

	numForces = nAttr.create("numForces","nf",MFnNumericData::kShort,0);
	nAttr.setStorable(true);
	nAttr.setReadable( true );
	nAttr.setWritable( true );


	combineMode = eAttr.create( "combineMode", "cm", CM_LINEAR);
		eAttr.addField("Linear", CM_LINEAR);
		eAttr.addField("Weights", CM_WEIGHTS);
		eAttr.addField("Priority", CM_PRIORITY);
//		eAttr.addField("Strongest", CM_STRONGEST);
//		eAttr.addField("Weakest", CM_WEAKEST);
		eAttr.addField("Selection", CM_SELECT);
	//	eAttr.addField("Priorized Dithering", CM_PRIORIZEDDITHERING);

	eAttr.setStorable(true);

	// Add the attributes we have created to the node

	#define nodeAddAttribute(_attr) 	\
			stat = addAttribute( _attr );	\
			if (!stat) { stat.perror("addAttribute"); return stat;} \

	nodeAddAttribute(inputForce);
	nodeAddAttribute(outputForce);
	nodeAddAttribute(combineMode);
	nodeAddAttribute(selectedForce);
	nodeAddAttribute(weight);
	nodeAddAttribute(priority);
	nodeAddAttribute(ditheringFactor);
	nodeAddAttribute(numForces);

	// Set up a dependency between the input and the output.  

	#define nodeSetDependency(_attr1, _attr2) \
			stat = attributeAffects( _attr1, _attr2 ); \
			if (!stat) { stat.perror("attributeAffects"); return stat;} \

  	nodeSetDependency(inputForce,outputForce);
	nodeSetDependency(selectedForce,outputForce);
	nodeSetDependency(combineMode,outputForce);
	nodeSetDependency(weight,outputForce);
	nodeSetDependency(priority,outputForce);
	nodeSetDependency(ditheringFactor,outputForce);

	return MS::kSuccess;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.