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 namespace EMAN
00055 {
00069 template<typename type>
00070 class CustomVector
00071 {
00072 public:
00076 explicit CustomVector(unsigned int starting_size=1024) : data(0), size(0), elements(0)
00077 {
00078 resize(starting_size);
00079 }
00080
00085 inline void clear(unsigned int starting_size=1024)
00086 {
00087 if ( data != 0 )
00088 {
00089 delete [] data;
00090 data = 0;
00091 }
00092 size = 0;
00093 elements = 0;
00094 resize(starting_size);
00095 }
00096
00103 inline void resize(const unsigned int n)
00104 {
00105 data = (type*)realloc(data, n*sizeof(type));
00106
00107 for(unsigned int i = size; i < n; ++i) data[i] = 0;
00108 size = n;
00109 }
00110
00115 inline void push_back(const type& t)
00116 {
00117 if ( elements == size ) resize(2*size);
00118 data[elements] = t;
00119 ++elements;
00120 }
00121
00126 inline void push_back_3(const type* const p)
00127 {
00128 if ( elements+2 >= size ) resize(2*size);
00129 memcpy( &data[elements], p, 3*sizeof(type));
00130 elements = elements + 3;
00131 }
00132
00135 inline void mult3(const type& v1, const type& v2, const type& v3)
00136 {
00137 for(unsigned int i = 0; (i + 2) < elements; i += 3 ){
00138 data[i] *= v1;
00139 data[i+1] *= v2;
00140 data[i+2] *= v3;
00141 }
00142 }
00143
00150 inline unsigned int elem() { return elements; }
00151
00156 inline type& operator[](const unsigned int idx)
00157 {
00158 unsigned int csize = size;
00159 while (idx >= csize ) {
00160 csize *= 2;
00161 }
00162 if ( csize != size ) resize(csize);
00163 return data[idx];
00164 }
00165
00169 inline type* get_data() { return data; }
00170
00171 private:
00173 type* data;
00175 unsigned int size;
00177 unsigned int elements;
00178 };
00179
00180 class MarchingCubes : public Isosurface {
00181 friend class GLUtil;
00182 public:
00185 MarchingCubes();
00186
00191 MarchingCubes(EMData * em);
00192 virtual ~MarchingCubes();
00193
00199 void set_data(EMData* data);
00200
00204 void set_surface_value(const float value);
00205
00209 float get_surface_value() const { return _surf_value; }
00210
00219 void set_sampling(const int rate) { drawing_level = rate; }
00220
00224 int get_sampling() const { return drawing_level; }
00225
00228 int get_sampling_range() { return minvals.size()-1; }
00229
00234 Dict get_isosurface();
00235
00236 void surface_face_z();
00237 private:
00238 map<int, int> point_map;
00239 unsigned long _isodl;
00240
00246 bool calculate_min_max_vals();
00247
00248
00252 void clear_min_max_vals();
00253
00255 vector<EMData*> minvals, maxvals;
00256
00258 int drawing_level;
00259
00269 void draw_cube(const int x, const int y, const int z, const int cur_level );
00270
00271
00279 void marching_cube(int fX, int fY, int fZ, const int cur_level);
00280
00284 void calculate_surface();
00285
00294 float get_offset(float fValue1, float fValue2, float fValueDesired);
00295
00299 int get_edge_num(int x, int y, int z, int edge);
00300
00311 void get_normal(Vector3 &normal, int fX, int fY, int fZ);
00312
00314 CustomVector<float> pp;
00315 CustomVector<float> nn;
00316 CustomVector<unsigned int> ff;
00317 };
00318
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00363 class U3DWriter{
00364 public:
00365 typedef unsigned int U32;
00366 typedef long unsigned int U64;
00367 typedef double F64;
00368 typedef float F32;
00369 typedef short int I16;
00370 typedef short unsigned int U16;
00371 typedef unsigned char U8;
00372
00373
00374
00375 U3DWriter();
00376 ~U3DWriter();
00377
00378 int write(const string& filename);
00379
00380 ostream& write(ostream&);
00381
00382 private:
00383 unsigned int size_of_in_bytes();
00384
00385 void test_type_sizes();
00386
00387 ostream& write_header(ostream&);
00388 ostream& write_clod_mesh_generator_node(ostream& os);
00389
00390
00391 template<typename type>
00392 ostream& write(ostream& os, const type& T) {
00393 os.write( (char*)(&T), sizeof(type) );
00394 return os;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 U32 DIFFUSE_COLOR_COUNT;
00468 U32 SPECULAR_COLOR_COUNT;
00469
00470
00471 CustomVector<F32> pp;
00472 CustomVector<F32> nn;
00473 CustomVector<unsigned int> ff;
00474 };
00475
00476
00477 template<> ostream& U3DWriter::write(ostream& os, const string& );
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
00504
00505
00506
00507
00508
00509
00510
00511
00512 }
00513
00514 #endif