File: ParameterSpaceControlPoint.cpp

package info (click to toggle)
transcend 0.3.dfsg1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,816 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (128 lines) | stat: -rw-r--r-- 3,116 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
/*
 * Modification History
 *
 * 2004-August-9   Jason Rohrer
 * Created.
 *
 * 2004-August-12   Jason Rohrer
 * Optimized blendColorArrays function.
 *
 * 2005-March-17   Jason Rohrer
 * Fixed bug in blendVertexArrays when both arrays have length 1.
 */



#include "ParameterSpaceControlPoint.h"



#include <math.h>
        

        
ParameterSpaceControlPoint::~ParameterSpaceControlPoint() {

    // does nothing... here to make compilers happy

    }



Vector3D **ParameterSpaceControlPoint::blendVertexArrays(
    Vector3D **inFirstArray,
    int inFirstArrayLength,
    double inWeightFirstArray,
    Vector3D **inSecondArray,
    int inSecondArrayLength,
    int *outResultLength ) {

    
    
    double weightOfSecondArray = 1 - inWeightFirstArray;

    // blend has the same number of elements as the larger control array
    int resultLength = inFirstArrayLength;

    if( inSecondArrayLength > resultLength ) {
        resultLength = inSecondArrayLength;
        }

    Vector3D **blendVertices = new Vector3D*[ resultLength ];
    

    // map the larger array nto the smaller array to
    // blend
    int sizeLargerArray;
    int sizeSmallerArray;

    Vector3D **arrayWithMoreVertices;
    Vector3D **arrayWithFewerVertices;
    
    double weightOfLargerSet;
    double weightOfSmallerSet;
    

    if( inFirstArrayLength > inSecondArrayLength ) {
        sizeLargerArray = inFirstArrayLength;
        sizeSmallerArray = inSecondArrayLength;
        
        arrayWithMoreVertices = inFirstArray;
        arrayWithFewerVertices = inSecondArray;
        weightOfLargerSet = inWeightFirstArray;
        weightOfSmallerSet = weightOfSecondArray;

        
        }
    else {
        sizeLargerArray = inSecondArrayLength;
        sizeSmallerArray = inFirstArrayLength;

        arrayWithMoreVertices = inSecondArray;
        arrayWithFewerVertices = inFirstArray;
        weightOfLargerSet = weightOfSecondArray;
        weightOfSmallerSet = inWeightFirstArray;
        }


    // size of blend array is same as size of larger set
    
    // factor to map large array indices into the smaller array
    double mapFactor;

    if( sizeLargerArray > 1 ) {
        mapFactor =
            (double)( sizeSmallerArray - 1 ) / (double)(sizeLargerArray - 1 );
        }
    else {
        // above formula might involve divide-by-zero

        // use 1 here because smaller array must have 1 element also
        // for a blend to be sensible
        mapFactor = 1;
        }

    
    for( int i=0; i<resultLength; i++ ) {

        // find the index of our blend partner vertex in the smaller set
        int partnerIndex =
            (int)rint( i * mapFactor );
        
        
        Vector3D *largerSetVertex =
            arrayWithMoreVertices[i];
        Vector3D *smallerSetVertex = arrayWithFewerVertices[ partnerIndex ];

        blendVertices[i] =
            Vector3D::linearSum( largerSetVertex,
                                 smallerSetVertex,
                                 weightOfLargerSet );
        }


    *outResultLength = resultLength;

    return blendVertices;
    }