File: ParameterizedSpace.cpp

package info (click to toggle)
transcend 0.3.dfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 5,796 kB
  • ctags: 3,074
  • sloc: cpp: 26,886; ansic: 693; sh: 210; makefile: 99; perl: 67
file content (186 lines) | stat: -rw-r--r-- 5,901 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
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
/*
 * Modification History
 *
 * 2004-August-9   Jason Rohrer
 * Created.
 */



#include "ParameterizedSpace.h"


#include <math.h>



ParameterizedSpace::~ParameterizedSpace() {

    for( int i=0; i<mNumControlPoints; i++ ) {
        delete mControlPoints[i];
        }

    delete [] mControlPoints;
    delete [] mControlPointParameterAnchors;
    }



ParameterSpaceControlPoint *ParameterizedSpace::getBlendedControlPoint(
    double inParameter ) {


       
    if( mNumControlPoints >= 2 ) {
        // find the 2 surrounding control points

        // all distances will be no more than 1
        double distanceFromClosestLargerPoint = 2;
        double distanceFromClosestSmallerPoint = 3;
        
        int indexOfClosestLargerPoint = -1;
        int indexOfClosestSmallerPoint = -1;

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

            double distanceFromThisPoint =
                fabs( mControlPointParameterAnchors[i] - inParameter );  

            if( mControlPointParameterAnchors[i] >= inParameter ) {
                // a larger point
                if( distanceFromThisPoint < distanceFromClosestLargerPoint ) {
                    // closer than our closest largerpoint
                    
                    indexOfClosestLargerPoint = i;
                    distanceFromClosestLargerPoint = distanceFromThisPoint;
                    }
                }
            else {
                // a smaller point
                if( distanceFromThisPoint < distanceFromClosestSmallerPoint ) {
                    // closer than our closest smallerpoint

                    indexOfClosestSmallerPoint = i;
                    distanceFromClosestSmallerPoint = distanceFromThisPoint;
                    }
                }
            }
        if( indexOfClosestLargerPoint != -1  &&
            indexOfClosestSmallerPoint != -1 ) {
            // found two points
            
            // compute weights
            double distanceBetweenSurroundingPoints =
                fabs( mControlPointParameterAnchors[
                          indexOfClosestLargerPoint ] -
                      mControlPointParameterAnchors[
                          indexOfClosestSmallerPoint ] );

            double weightOnLargerPoint =
                distanceFromClosestSmallerPoint /
                distanceBetweenSurroundingPoints;


            // blend the two points, using the distance to weight them
            return 
                mControlPoints[ indexOfClosestSmallerPoint ]->
                    createLinearBlend(
                        mControlPoints[ indexOfClosestLargerPoint ],
                        weightOnLargerPoint );
            }
        else {
            // found only one point

            int indexOfPoint;
            
            if( indexOfClosestLargerPoint != -1 ) {
                indexOfPoint = indexOfClosestLargerPoint;
                }
            else if( indexOfClosestLargerPoint != -1 ) {
                indexOfPoint = indexOfClosestSmallerPoint;
                }
            else {
                printf( "Error:  found no closest space parameter map "
                        " anchor point.\n" );
                indexOfPoint = 0;
                }

            // return copy of this point
            return mControlPoints[ indexOfPoint ]->copy();
            }
        }
    else if( mNumControlPoints == 1 ) {
        // only one anchor point... return its parameters
        
        return mControlPoints[0]->copy();
        }
    else {
        printf( "Error:  no anchor points in sculpture parameter space.\n" );

        return NULL;
        }

    /*
    if( mNumControlPoints >= 2 ) {
        // find the 2 closest control points

        // all distances will be no more than 1
        double distanceFromClosestPoint = 2;
        double distanceFromNextClosestPoint = 3;
        
        int indexOfClosestPoint = -1;
        int indexOfNextClosestPoint = -1;

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

            double distanceFromThisPoint =
                fabs( mControlPointParameterAnchors[i] - inParameter );  
            
            if( distanceFromThisPoint < distanceFromClosestPoint ) {
                // closer than our closest point

                indexOfNextClosestPoint = indexOfClosestPoint;
                distanceFromNextClosestPoint = distanceFromClosestPoint;

                indexOfClosestPoint = i;
                distanceFromClosestPoint = distanceFromThisPoint;
                }
            else if( distanceFromThisPoint < distanceFromNextClosestPoint ) {
                // not as close as our closest point, but closer than
                // our next closest point

                indexOfNextClosestPoint = i;
                distanceFromNextClosestPoint = distanceFromThisPoint;
                }
            }

        // scale the distance from our parameter to the closest point
        // by the distance between the two closest points to get a weight
        // for the closest point
        double distanceBetweenClosestPoints =
            fabs( mControlPointParameterAnchors[ indexOfClosestPoint ] -
                  mControlPointParameterAnchors[ indexOfNextClosestPoint ] );

        double weightOnClosestPoint =
            1 - ( distanceFromClosestPoint / distanceBetweenClosestPoints );


        // blend the two points, using the distance to weight them
        return 
            mControlPoints[ indexOfNextClosestPoint ]->createLinearBlend(
                mControlPoints[ indexOfClosestPoint ],
                weightOnClosestPoint );
        
        }
    else if( mNumControlPoints == 1 ) {
        // only one control point... return a copy
        
        return mControlPoints[0]->copy();
        }
    else {
        printf( "Error:  no control points in space space.\n" );

        return NULL;
        }
    */
    }