Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

TxFunction.cpp

00001 #include <gl/glut.h>
00002 #include <algorithm>
00003 #include <vector>
00004 #include <cmath>
00005 #include <iostream>
00006 #include <iostream>
00007 
00008 
00009 #include "LifeVariables.h"
00010 #include "TxFunction.h"
00011 
00012 using namespace std;
00013 
00014 
00015 TxFunction::TxFunction(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {    
00016     this->addControlPoint(x1, y1, 0, 0, 0, 1);
00017     this->addControlPoint(x2, y2, 1, 1, 1, 0);
00018 }
00019 
00020 
00021 TxPoint* TxFunction::hitTxPoint(float x, float y) {
00022     vector<TxPoint>::iterator it = this->controlPoints.begin();
00023     for(;it < this->controlPoints.end(); it++) {
00024         if(it->hit(x,y) == true) {
00025             return it;
00026         }
00027     }
00028     return NULL;
00029 }
00030 
00031 
00032 TxPoint* TxFunction::getLastTxPoint() {
00033     return this->controlPoints.end();    
00034 }
00035 
00036 
00037 TxPoint* TxFunction::getFirstTxPoint() {
00038     return this->controlPoints.begin();    
00039 }
00040 
00041 
00042 void TxFunction::moveControlPoint(TxPoint *point, GLfloat x, GLfloat y) {    
00043     //
00044     // QQ add restriction to avoid moving the endpoint
00045     //
00046     point->moveTo(x,y);
00047     sort(this->controlPoints.begin(),this->controlPoints.end());    
00048 }
00049 
00050 
00051 void TxFunction::addControlPoint(float x, float y, 
00052                                  float red, float green, float blue, float alpha) 
00053 {
00054     TxPoint cP(x, y, red, green, blue, alpha);
00055     this->controlPoints.push_back(cP);
00056     // cout << "x: " << x <<" y:"<< y <<" alpha:"<< alpha << " listsize:"<< controlPoints.size()<<endl;
00057     // sort the points from the beginning to the end
00058     sort(this->controlPoints.begin(),this->controlPoints.end());        
00059 }
00060 
00061 
00062 void TxFunction::removeControlPoint(TxPoint *p) {
00063     vector<TxPoint>::iterator it = this->controlPoints.begin()+1;
00064     for(;it < this->controlPoints.end()-1; it++) {
00065         if(it == p) {
00066             this->controlPoints.erase(it,it+1);            
00067             return;            
00068         }
00069     }
00070 }
00071 
00072 
00073 void TxFunction::render() {
00074     vector<TxPoint>::iterator it = this->controlPoints.begin();
00075     for(;it < this->controlPoints.end(); it++) {
00076         it->render();
00077         if(it< this->controlPoints.end()-1) {
00078             glPushAttrib(GL_CURRENT_BIT);
00079             glColor4fv(it->rgba);
00080             glBegin(GL_LINES);
00081                 glVertex2fv(it->position);
00082                 glVertex2fv((it+1)->position);
00083             glEnd();
00084             glPopAttrib();
00085         }
00086     }    
00087 }
00088 
00089 
00090 TxPoint* TxFunction::getNearestNeighbour(unsigned short density) {
00091     vector<TxPoint>::iterator it = this->controlPoints.begin()+1;    
00092     do {
00093         if(it->position[0] > density) {            
00094             return it;
00095         }
00096         it+=1;
00097     }
00098     while(it < this->controlPoints.end());    
00099     return NULL;
00100 }
00101 
00102 
00103 void TxFunction::getLinearInterpolatedColor(unsigned short density, 
00104                                             float &red, float &green, float &blue, float &alpha) 
00105 {
00106 
00107     vector<TxPoint>::iterator upper = this->getNearestNeighbour(density);
00108     vector<TxPoint>::iterator lower = upper-1;
00109 
00110     if((upper == NULL)||(lower == NULL)) { return; }
00111 
00112     // calculate the coefficients
00113     int d = upper->position[0] - lower->position[0];
00114     float ld = (float)(density - lower->position[0])/(float)d;
00115     float ud = (float)(1 - ld);
00116         
00117     float a = ud*lower->rgba[3]+ld*upper->rgba[3];
00118         
00119     red = (ud*lower->rgba[0]+ld*upper->rgba[0])*a;
00120     green = (ud*lower->rgba[1]+ld*upper->rgba[1])*a;
00121     blue = (ud*lower->rgba[2]+ld*upper->rgba[2])*a;
00122     
00123 
00124 }
00125 
00126 
00127 void TxFunction::getLinearInterpolatedVoxelColor(float density, 
00128                                                  float &red, 
00129                                                  float &green,
00130                                                  float &blue) 
00131 {
00132 
00133     vector<TxPoint>::iterator upper = this->getNearestNeighbour(density);
00134     vector<TxPoint>::iterator lower = upper-1;
00135 
00136     if((upper == NULL)||(lower == NULL)) { return; }
00137 
00138     // calculate the coefficients
00139     int d = upper->position[0] - lower->position[0];
00140     float ld = (float)(density - lower->position[0])/(float)d;
00141     float ud = (float)(1 - ld);
00142     
00143     // alpha
00144     float alpha = ud*lower->rgba[3]+ld*upper->rgba[3];
00145 
00146     red = (ud*lower->rgba[0]+ld*upper->rgba[0])*alpha;
00147     green = (ud*lower->rgba[1]+ld*upper->rgba[1])*alpha;
00148     blue = (ud*lower->rgba[2]+ld*upper->rgba[2])*alpha;
00149 }
00150 
00151 
00152 void TxFunction::getSolidVoxelColor(float density, 
00153                                                  float &red, 
00154                                                  float &green,
00155                                                  float &blue) 
00156 {
00157     vector<TxPoint>::iterator upper = this->getNearestNeighbour(density);    
00158 
00159     if(upper == NULL) { return; }
00160     
00161     red = upper->rgba[0];
00162     green = upper->rgba[1];
00163     blue = upper->rgba[2];
00164 }
00165 
00166 
00167 // thickness of surface
00168 int r = 50;
00169 // selected density
00170 int f_v = 1000;
00171 
00172 float a_v = 1.0f;
00173 
00174 void TxFunction::getLevoyVoxelColor(float f_xi,
00175                                     float &red, float &green, float &blue, float gradient) {
00176     
00177     red = 1;
00178     green = 1;
00179     blue = 1;
00180 
00181     float lower = f_xi - r*gradient;
00182     float upper = f_xi + r*gradient;
00183     
00184 
00185     if((gradient == 0)&&(f_xi = f_v)) {
00186         return;    
00187     }
00188     else if((gradient > 0)&&(lower < f_v)&&(upper > f_v)){
00189         a_v = 1.0f -1.0f/r*(abs(f_v-f_xi/gradient));
00190         red = red*a_v;
00191         green = green*a_v;
00192         blue = blue*a_v;
00193     }
00194     else {
00195         red = 0.2;
00196         green = 0.2;
00197         blue = 0.2;
00198     }
00199 }

Generated on Mon Dec 12 15:20:26 2005 for CCube by  doxygen 1.4.1