Jamoma API  0.6.0.a19
TTSpatSnapRenderer.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspSpatLib
4  *
5  * @brief TODO
6  *
7  * @details TODO @n
8  *
9  * @authors Trond Lossius, Nils Peters, Timothy Place
10  *
11  * @copyright Copyright © 2011 by Trond Lossius, Nils Peters, and Timothy Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 #include "TTSpatSnapRenderer.h"
17 
18 
19 
20 /// Methods for TTSpatSnapRenderer: //////////////////////
21 
23 {
24  ;
25 }
26 
27 
29 {
30  ;
31 }
32 
33 
35 {
36  TTInt32 nearestSink;
37  TTFloat64 sqrDistance, smallestDist;
38  TTFloat64 sourceX, sourceY, sourceZ;
39  TTFloat64 sinkX, sinkY, sinkZ;
40 
43 
44  for (TTInt32 source=0; source < (TTInt32) sources.size(); source++) {
45 
46  // The source that we want to locate the nearest sink for:
47  sources[source].getPosition(sourceX, sourceY, sourceZ);
48 
49  // In order to find the nearest sink for a source, we'll start by assuming that sink 0 is the nearest
50  sinks[0].getPosition(sinkX, sinkY, sinkZ);
51 
52  // It is more efficient to do comparement on square of the distance
53  sqrDistance = (sourceX-sinkX)*(sourceX-sinkX) + (sourceY-sinkY)*(sourceY-sinkY) + (sourceZ-sinkZ)*(sourceZ-sinkZ);
54  smallestDist = sqrDistance;
55  nearestSink = 0;
56 
57  // We also ensures that the matrix coefficient is set to 0 for the first sink
58  mMixerMatrixCoefficients->set2d(source, 0., 0.);
59 
60  // Now we iterate over the remaining sinks to see if any of them are closer
61  for (TTInt32 sink=1; sink < (TTInt32) sinks.size(); sink++) {
62 
63  sinks[sink].getPosition(sinkX, sinkY, sinkZ);
64 
65  sqrDistance = (sourceX-sinkX)*(sourceX-sinkX) + (sourceY-sinkY)*(sourceY-sinkY) + (sourceZ-sinkZ)*(sourceZ-sinkZ);
66 
67  // Check if sinks[sink] is closer
68  if ((smallestDist>sqrDistance)){
69  smallestDist = sqrDistance;
70  nearestSink = sink;
71 
72  }
73  // In the process we also set all matrix coefficients to 0
74  mMixerMatrixCoefficients->set2d(source, sink, 0.);
75  }
76 
77  // We have now found the nearest sink, and all coefficients have been reset to 0.
78  // The only thing left to do is to send the coefficient of the nearest sink to 1
79  mMixerMatrixCoefficients->set2d(source, nearestSink, 1.);
80  }
81 }
82 
TTErr setRowCount(const TTValue &aNewRowCount)
Attribute accessor.
~TTSpatSnapRenderer()
Destructor.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTErr setColumnCount(const TTValue &aNewColumnCount)
Attribute accessor.
TTSampleMatrixPtr mMixerMatrixCoefficients
A matrix holding all coefficient for matrix-based mixing of sources to sinks.
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
std::vector< TTSpatSource > TTSpatSourceVector
Pointer to a vector of TTSpatSource points.
Definition: TTSpatEntity.h:108
std::vector< TTSpatSink > TTSpatSinkVector
Pointer to a vector of TTSpatSink points.
Definition: TTSpatEntity.h:114
TTErr set2d(TTRowID i, TTColumnID j, T data)
Set the value of a component located at (i,j) in a 2-dimensional matrix.
Definition: TTMatrixBase.h:481
void recalculateMatrixCoefficients(TTSpatSourceVector &sources, TTSpatSinkVector &sinks)
This method is called whenever matrix coefficients need to be updated.
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
A basic proof-of-concept spatial renderer where the audio of a source will snap to the nearest sink...
TTSpatSnapRenderer()
Constructor.