00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _MARCHING_CUBES_H_
00037 #define _MARCHING_CUBES_H_
00038
00039 #include <vector>
00040 using std::vector;
00041
00042 #include "vecmath.h"
00043 #include "isosurface.h"
00044
00045
00046 #define MARCHING_CUBES_DEBUG 0
00047
00048 #include <ostream>
00049 using std::ostream;
00050
00051 #include <climits>
00052
00053
00054 #ifdef __APPLE__
00055 #include "OpenGL/gl.h"
00056 #include "OpenGL/glext.h"
00057 #else // WIN32, LINUX
00058 #include "GL/gl.h"
00059 #include "GL/glext.h"
00060 #endif //__APPLE__
00061
00062 namespace EMAN
00063 {
00077 template<typename type>
00078 class CustomVector
00079 {
00080 public:
00084 explicit CustomVector(unsigned int starting_size=1024) : data(0), size(0), elements(0)
00085 {
00086 resize(starting_size);
00087 }
00088
00089 ~CustomVector()
00090 {
00091 if(data) {free(data); data=0;}
00092 }
00093
00098 inline void clear(unsigned int starting_size=1024)
00099 {
00100 if (data) {free(data); data = 0;}
00101 size = 0;
00102 elements = 0;
00103 resize(starting_size);
00104 }
00105
00112 inline void resize(const unsigned int n)
00113 {
00114 data = (type*)realloc(data, n*sizeof(type));
00115
00116 for(unsigned int i = size; i < n; ++i) data[i] = 0;
00117 size = n;
00118 }
00119
00124 inline void push_back(const type& t)
00125 {
00126 if ( elements == size ) resize(2*size);
00127 data[elements] = t;
00128 ++elements;
00129 }
00130
00135 inline void push_back_3(const type* const p)
00136 {
00137 if ( elements+2 >= size ) resize(2*size);
00138 memcpy( &data[elements], p, 3*sizeof(type));
00139 elements = elements + 3;
00140 }
00141
00144 inline void mult3(const type& v1, const type& v2, const type& v3)
00145 {
00146 for(unsigned int i = 0; (i + 2) < elements; i += 3 ){
00147 data[i] *= v1;
00148 data[i+1] *= v2;
00149 data[i+2] *= v3;
00150 }
00151 }
00152
00159 inline unsigned int elem() { return elements; }
00160
00165 inline type& operator[](const unsigned int idx)
00166 {
00167 unsigned int csize = size;
00168 while (idx >= csize ) {
00169 csize *= 2;
00170 }
00171 if ( csize != size ) resize(csize);
00172 return data[idx];
00173 }
00174
00178 inline type* get_data() { return data; }
00179
00180 private:
00182 type* data;
00184 unsigned int size;
00186 unsigned int elements;
00187 };
00188
00192 class ColorRGBGenerator{
00193 public:
00194 ColorRGBGenerator();
00196 ColorRGBGenerator(EMData* emdata);
00197
00199 float* getRGBColor(int x, int y, int z);
00200
00202 void set_data(EMData* data);
00203
00205 void generateRadialColorMap();
00206
00208 void set_cmap_data(EMData* data);
00209
00211 inline void setOrigin(int orix, int oriy, int oriz)
00212 {
00213 originx = orix;
00214 originy = oriy;
00215 originz = oriz;
00216 needtorecolor = true;
00217 }
00219 inline void setScale(float i, float o)
00220 {
00221 inner = i;
00222 outer = o;
00223 needtorecolor = true;
00224 }
00226 inline void setRGBmode(int mode)
00227 {
00228 rgbmode = mode;
00229 needtorecolor = true;
00230 }
00231
00233 inline void setMinMax(float min, float max)
00234 {
00235 minimum = min;
00236 maximum = max;
00237 needtorecolor = true;
00238 }
00239
00241 inline int getRGBmode()
00242 {
00243 return rgbmode;
00244 }
00245
00247 inline bool getNeedToRecolor()
00248 {
00249 return needtorecolor;
00250 }
00251
00252 inline void setNeedToRecolor(bool recolor)
00253 {
00254 needtorecolor = recolor;
00255 }
00256 private:
00257 int rgbmode;
00258 int originx;
00259 int originy;
00260 int originz;
00261 float inner;
00262 float outer;
00263 float minimum;
00264 float maximum;
00265
00266 bool needtorecolor;
00267
00268 float* colormap;
00269 EMData* em_data;
00270 EMData* cmap;
00271
00272 float rgb[3];
00273 };
00274
00275 class MarchingCubes : public Isosurface {
00276 friend class GLUtil;
00277 public:
00280 MarchingCubes();
00281
00286 MarchingCubes(EMData * em);
00287 virtual ~MarchingCubes();
00288
00294 void set_data(EMData* data);
00295
00299 void set_surface_value(const float value);
00300
00304 float get_surface_value() const { return _surf_value; }
00305
00314 void set_sampling(const int rate) { drawing_level = rate; }
00315
00319 int get_sampling() const { return drawing_level; }
00320
00323 int get_sampling_range() { return minvals.size()-1; }
00324
00327 void color_vertices();
00328
00333 Dict get_isosurface();
00334
00335 void surface_face_z();
00336
00339 inline void setRGBorigin(int x, int y, int z)
00340 {
00341 rgbgenerator.setOrigin(x, y, z);
00342 }
00343
00344 inline void setRGBscale(float i, float o)
00345 {
00346 rgbgenerator.setScale(i, o);
00347 }
00348
00349 inline void setRGBmode(int mode)
00350 {
00351 rgbgenerator.setRGBmode(mode);
00352 }
00353
00355 inline int getRGBmode()
00356 {
00357 return rgbgenerator.getRGBmode();
00358 }
00359
00361 inline void setCmapData(EMData* data)
00362 {
00363 rgbgenerator.set_cmap_data(data);
00364 }
00365
00367 inline void setCmapMinMax(float min, float max)
00368 {
00369 rgbgenerator.setMinMax(min, max);
00370 }
00371
00372 private:
00373 map<int, int> point_map;
00374 unsigned long _isodl;
00375 GLuint buffer[4];
00376
00382 bool calculate_min_max_vals();
00383
00384
00388 void clear_min_max_vals();
00389
00391 vector<EMData*> minvals, maxvals;
00392
00394 int drawing_level;
00395
00405 void draw_cube(const int x, const int y, const int z, const int cur_level );
00406
00407
00415 void marching_cube(int fX, int fY, int fZ, const int cur_level);
00416
00420 void calculate_surface();
00421
00430 float get_offset(float fValue1, float fValue2, float fValueDesired);
00431
00435 int get_edge_num(int x, int y, int z, int edge);
00436
00447 void get_normal(Vector3 &normal, int fX, int fY, int fZ);
00448
00450 CustomVector<float> pp;
00451 CustomVector<float> cc;
00452 CustomVector<int> vv;
00453 CustomVector<float> nn;
00454 CustomVector<unsigned int> ff;
00455
00457 ColorRGBGenerator rgbgenerator;
00458
00459 bool needtobind;
00460 };
00461
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00506 class U3DWriter{
00507 public:
00508 typedef unsigned int U32;
00509 typedef long unsigned int U64;
00510 typedef double F64;
00511 typedef float F32;
00512 typedef short int I16;
00513 typedef short unsigned int U16;
00514 typedef unsigned char U8;
00515
00516
00517
00518 U3DWriter();
00519 ~U3DWriter();
00520
00521 int write(const string& filename);
00522
00523 ostream& write(ostream&);
00524
00525 private:
00526 unsigned int size_of_in_bytes();
00527
00528 void test_type_sizes();
00529
00530 ostream& write_header(ostream&);
00531 ostream& write_clod_mesh_generator_node(ostream& os);
00532
00533
00534 template<typename type>
00535 ostream& write(ostream& os, const type& T) {
00536 os.write( (const char*)(&T), sizeof(type) );
00537 return os;
00538 }
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 U32 DIFFUSE_COLOR_COUNT;
00611 U32 SPECULAR_COLOR_COUNT;
00612
00613
00614 CustomVector<F32> pp;
00615 CustomVector<F32> nn;
00616 CustomVector<unsigned int> ff;
00617 };
00618
00619
00620 template<> ostream& U3DWriter::write(ostream& os, const string& );
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 }
00656
00657 #endif