00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef VRN_LABELINGMATH_H
00029 #define VRN_LABELINGMATH_H
00030
00031 #include <cmath>
00032 #include <vector>
00033 #include "tgt/math.h"
00034 #include "tgt/vector.h"
00035 #include "tgt/matrix.h"
00036 #include "tgt/tgt_gl.h"
00037 #include "tgt/math.h"
00038
00039 namespace labeling {
00040
00044 struct Polynomial {
00045 int degree;
00046 float* coeff;
00047 };
00048
00049
00050
00056 class Curve3D {
00057 public:
00058
00062 Curve3D();
00063
00067 virtual ~Curve3D() {}
00068
00080 bool setCtrlPoints(std::vector<tgt::vec3> &ctrlPoints, float curveLength=0.f);
00081
00083 std::vector<tgt::vec3> getCtrlPoints(){
00084 return ctrlPoints_;
00085 }
00086
00088 virtual tgt::vec3 getCurvePoint(float t)=0;
00089
00091 virtual tgt::vec3 getTangent(float t)=0;
00092
00094 virtual float getTangentMagnitude(float t)=0;
00095
00100 virtual float getSegmentLength(float t1, float t2)=0;
00101
00108 virtual tgt::vec3 getNextPoint(float &curParam, float offset)=0;
00109
00113 virtual void shift(tgt::vec3 shiftVector)=0;
00114
00115 protected:
00116
00120 virtual bool calcFunction(float curveLength=0)=0;
00121
00123 std::vector<tgt::vec3> ctrlPoints_;
00124
00127 float paramScale_, paramShift_;
00128
00129 };
00130
00131
00132
00137 class Curve2D {
00138 public:
00139
00141 Curve2D();
00142
00144 virtual ~Curve2D() {}
00145
00157 bool setCtrlPoints(std::vector<tgt::vec2> &ctrlPoints, float curveLength=0.f);
00158
00160 std::vector<tgt::vec2> getCtrlPoints(){
00161 return ctrlPoints_;
00162 }
00163
00165 virtual tgt::vec2 getCurvePoint(float t)=0;
00166
00168 virtual tgt::vec2 getTangent(float t)=0;
00169
00171 virtual float getTangentMagnitude(float t)=0;
00172
00177 virtual float getSegmentLength(float t1, float t2)=0;
00178
00185 virtual tgt::vec2 getNextPoint(float &curParam, float offset)=0;
00186
00188 virtual void shift(tgt::vec2 shiftVector)=0;
00189
00190 protected:
00191
00195 virtual bool calcFunction(float curveLength=0)=0;
00196
00198 std::vector<tgt::vec2> ctrlPoints_;
00199
00202 float paramScale_, paramShift_;
00203
00204 };
00205
00206
00207
00212 class Curve3DPolynomial : public Curve3D {
00213 public:
00214
00217 Curve3DPolynomial(int degree);
00218
00220 virtual ~Curve3DPolynomial() {}
00221
00222 tgt::vec3 getCurvePoint(float t);
00223 tgt::vec3 getTangent(float t);
00224 float getTangentMagnitude(float t);
00225 float getSegmentLength(float t1, float t2);
00226 tgt::vec3 getNextPoint(float &curParam, float offset);
00227
00229 void shift(tgt::vec3 shiftVector);
00230
00232 float* getCoefficientsX(){
00233 return xfunc_.coeff;
00234 };
00235
00237 float* getCoefficientsY(){
00238 return yfunc_.coeff;
00239 };
00240
00242 float* getCoefficientsZ(){
00243 return zfunc_.coeff;
00244 };
00245
00246 protected:
00247
00248 bool calcFunction(float curveLength=0);
00249
00250 int degree_;
00251 int numCoeff_;
00252 Polynomial xfunc_;
00253 Polynomial yfunc_;
00254 Polynomial zfunc_;
00255
00256 };
00257
00258
00259
00264 class Curve2DPolynomial : public Curve2D {
00265 public:
00266
00269 Curve2DPolynomial(int degree);
00270
00272 virtual ~Curve2DPolynomial() {}
00273
00274 tgt::vec2 getCurvePoint(float t);
00275 tgt::vec2 getTangent(float t);
00276 float getTangentMagnitude(float t);
00277 float getSegmentLength(float t1, float t2);
00278 tgt::vec2 getNextPoint(float &curParam, float offset);
00279
00281 void shift(tgt::vec2 shiftVector);
00282
00284 float* getCoefficientsX(){
00285 return xfunc_.coeff;
00286 };
00287
00289 float* getCoefficientsY(){
00290 return yfunc_.coeff;
00291 };
00292
00293 private:
00294
00295 bool calcFunction(float curveLength=0);
00296
00297 int degree_;
00298 int numCoeff_;
00299 Polynomial xfunc_;
00300 Polynomial yfunc_;
00301
00302 };
00303
00304
00305
00311 class BezierPatch {
00312 public:
00313
00315 BezierPatch(bool useDisplayList=true);
00316
00318 ~BezierPatch();
00319
00327 void setCtrlPoints(tgt::vec3* ctrlPoints, int degreeS, int degreeT);
00328
00330 int getDegreeS();
00331
00333 int getDegreeT();
00334
00337 tgt::vec3* getCtrlPoints(int °reeS, int °reeT);
00338
00340 tgt::vec3 getPoint(float s, float t);
00341
00344 tgt::vec3 getTangentS(float s, float t);
00345
00348 tgt::vec3 getTangentT(float s, float t);
00349
00352 tgt::vec3 getNormal(float s, float t);
00353
00361 void render(int s_steps = 10, int t_steps = 10,
00362 bool genTexCoords=false, GLuint texUnit=0);
00363
00364 private:
00365 int degreeS_;
00366 int degreeT_;
00367 tgt::vec3* ctrlPoints_;
00368
00369 GLuint displayList_;
00370 bool useDisplayList_;
00371 };
00372
00373
00374
00375
00381 template<class T>
00382 class Bitmap {
00383 public:
00384
00386 Bitmap() {
00387 data_ = NULL;
00388 width_ = 0;
00389 height_ = 0;
00390 channels_ = 1;
00391 };
00392
00396 Bitmap(int width, int height, int channels = 1) {
00397 data_ = new T[width*height*channels];
00398 width_ = width;
00399 height_ = height;
00400 channels_ = channels;
00401 };
00402
00405 Bitmap(T* data, int width, int height, int channels = 1) {
00406 data_ = data;
00407 width_ = width;
00408 height_ = height;
00409 channels_ = channels;
00410 };
00411
00412 ~Bitmap(){
00413 delete[] data_;
00414 data_ = 0;
00415 }
00416
00419 void setData(T* data, int width, int height, int channels = 1){
00420 if(data_) {
00421 delete[] data_;
00422 data_ = 0;
00423 }
00424 data_ = data;
00425 width_ = width;
00426 height_ = height;
00427 channels_ = channels;
00428 }
00429
00431 T* getData(){
00432 return data_;
00433 }
00434
00437 inline void setElem(int x, int y, T elem, int channel=0){
00438 setElem((width_*y + x)*channels_ + channel, elem);
00439 }
00440
00443 inline void setElem(int index, T elem){
00444 data_[index] = elem;
00445 }
00446
00449 inline T getElem(int x, int y, int channel=0){
00450 return getElem((width_*y + x)*channels_ + channel);
00451 }
00452
00455 inline T getElem(int index){
00456 return data_[index];
00457 }
00458
00461 inline tgt::Vector2<T> getPixel2Ch(int x, int y){
00462 return tgt::Vector2<T>(data_[(width_*y + x)*channels_],
00463 data_[(width_*y + x)*channels_ + 1] );
00464 }
00465
00468 inline tgt::Vector3<T> getPixel3Ch(int x, int y){
00469 x = tgt::clamp(x, 0, width_-1);
00470 y = tgt::clamp(y, 0, height_-1);
00471 return tgt::Vector3<T>(data_[(width_*y + x)*channels_],
00472 data_[(width_*y + x)*channels_ + 1],
00473 data_[(width_*y + x)*channels_ + 2] );
00474 }
00475
00478 inline tgt::Vector4<T> getPixel4Ch(int x, int y){
00479 return tgt::Vector4<T>(data_[(width_*y + x)*channels_],
00480 data_[(width_*y + x)*channels_ + 1],
00481 data_[(width_*y + x)*channels_ + 2],
00482 data_[(width_*y + x)*channels_ + 3] );
00483 }
00484
00489 void mergeBitmap(const Bitmap<T> &src, int x_offset=0, int y_offset=0){
00490 for (int x=0; x<src.width_; x++){
00491 for (int y=0; y<src.height_; y++){
00492 int xdst = x+x_offset;
00493 int ydst = y+y_offset;
00494 for (int ch=0; ch<channels_; ch++){
00495 setElem(xdst, ydst,
00496 src.data_[(src.width_*y + x)*src.channels_ + ch], ch);
00497 }
00498 }
00499 }
00500 }
00501
00508 void blendMax(const Bitmap<T> &src, int x_offset=0, int y_offset=0){
00509 for (int x=0; x<src.width_; x++){
00510 for (int y=0; y<src.height_; y++){
00511 int xdst = x+x_offset;
00512 int ydst = y+y_offset;
00513 if (getElem(xdst, ydst, channels_-1) <
00514 src.data_[(src.width_*y+x)*src.channels_+(src.channels_-1)] ){
00515 for (int ch=0; ch<channels_; ch++){
00516 setElem(xdst, ydst,
00517 src.data_[(src.width_*y + x)*src.channels_ + ch],
00518 ch);
00519 }
00520 }
00521 }
00522 }
00523 }
00524
00531 void blend(const Bitmap<T> &src, int x_offset=0, int y_offset=0){
00532 for (int x=0; x<src.width_; x++){
00533 for (int y=0; y<src.height_; y++){
00534 int xdst = x+x_offset;
00535 int ydst = y+y_offset;
00536 T srcAlpha = src.data_[(src.width_*y+x)*src.channels_+(src.channels_-1)];
00537 T dstAlpha = (1-srcAlpha)*getElem(xdst, ydst, 3);
00538 for (int ch=0; ch<channels_; ch++){
00539 T elem = getElem(xdst, ydst, ch)*dstAlpha +
00540 src.data_[(src.width_*y + x)*src.channels_ + ch]*srcAlpha;
00541 setElem(xdst, ydst, elem, ch);
00542 }
00543 setElem(xdst, ydst, srcAlpha + (1-srcAlpha)*dstAlpha, channels_-1);
00544 }
00545 }
00546 }
00547
00549 void freeData(){
00550 delete[] data_;
00551 data_ = 0;
00552 }
00553
00555 inline int getWidth(){
00556 return width_;
00557 }
00559 inline int getHeight(){
00560 return height_;
00561 }
00562
00565 void setDim(int width, int height, int channels=1){
00566 delete[] data_;
00567 data_ = new T[width*height*channels];
00568 width_ = width;
00569 height_ = height;
00570 channels_ = channels;
00571 }
00572
00573 protected:
00574 T* data_;
00575 int width_;
00576 int height_;
00577 int channels_;
00578 };
00579
00580
00581
00584 class FirstHitBitmap : public Bitmap<float> {
00585
00586 public:
00587
00589 tgt::vec3 getPixel3Ch(int x, int y){
00590 return Bitmap<float>::getPixel3Ch(x, y)*2.f*volumeSize_ - volumeSize_;
00591 }
00592
00596 tgt::vec3 calcNormal(int x, int y){
00597 tgt::vec3 xtangent = getPixel3Ch(x+1,y) - getPixel3Ch(x-1,y);
00598 tgt::vec3 ytangent = getPixel3Ch(x,y+1) - getPixel3Ch(x,y-1);
00599 return tgt::normalize( tgt::cross(xtangent, ytangent) );
00600 }
00601
00606 tgt::vec3 projectToViewport(tgt::vec3 point, bool round=true){
00607 tgt::vec4 p4D = transformationMatrix_*tgt::vec4(point, 1.f);
00608 p4D /= p4D.w;
00609 tgt::vec3 result;
00610 if (round)
00611 result = tgt::vec3( tgt::round(p4D.x), tgt::round(p4D.y) , p4D.z);
00612 else
00613 result = tgt::vec3( p4D.x, p4D.y, p4D.z );
00614
00615 return result;
00616 }
00617
00619 tgt::vec3 inverseProject(tgt::vec3 point){
00620 tgt::vec4 p4D = transformationMatrixInverse_*tgt::vec4(point, 1.f);
00621 p4D /= p4D.w;
00622 tgt::vec3 result = tgt::vec3(p4D.x, p4D.y, p4D.z);
00623 return result;
00624 }
00625
00627 void setVolumeSize(tgt::vec3 volumeSize){
00628 volumeSize_ = volumeSize;
00629 }
00630
00633 void calcTransformationMatrix(tgt::mat4 viewMatrix,
00634 tgt::mat4 projectionMatrix,
00635 tgt::ivec2 viewportSize) {
00636 tgt::mat4 scalemat = tgt::mat4::createScale(
00637 tgt::vec3(viewportSize.x/2.f, viewportSize.y/2.f, 1.f/2.f) );
00638 tgt::mat4 transmat = tgt::mat4::createTranslation( tgt::vec3(1.f) );
00639
00640 transformationMatrix_ = scalemat*transmat*projectionMatrix*viewMatrix;
00641 transformationMatrix_.invert(transformationMatrixInverse_);
00642 }
00643
00644 private:
00645
00648 tgt::vec3 volumeSize_;
00649
00652 tgt::mat4 transformationMatrix_;
00653 tgt::mat4 transformationMatrixInverse_;
00654
00655 };
00656
00657 }
00658
00659 #endif //VRN_LABELINGMATH_H