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
00081 ~CustomVector()
00082 {
00083 if(data) {free(data); data=0;}
00084 }
00085
00090 inline void clear(unsigned int starting_size=1024)
00091 {
00092 if (data) {free(data); data = 0;}
00093 size = 0;
00094 elements = 0;
00095 resize(starting_size);
00096 }
00097
00104 inline void resize(const unsigned int n)
00105 {
00106 data = (type*)realloc(data, n*sizeof(type));
00107
00108 for(unsigned int i = size; i < n; ++i) data[i] = 0;
00109 size = n;
00110 }
00111
00116 inline void push_back(const type& t)
00117 {
00118 if ( elements == size ) resize(2*size);
00119 data[elements] = t;
00120 ++elements;
00121 }
00122
00127 inline void push_back_3(const type* const p)
00128 {
00129 if ( elements+2 >= size ) resize(2*size);
00130 memcpy( &data[elements], p, 3*sizeof(type));
00131 elements = elements + 3;
00132 }
00133
00136 inline void mult3(const type& v1, const type& v2, const type& v3)
00137 {
00138 for(unsigned int i = 0; (i + 2) < elements; i += 3 ){
00139 data[i] *= v1;
00140 data[i+1] *= v2;
00141 data[i+2] *= v3;
00142 }
00143 }
00144
00151 inline unsigned int elem() { return elements; }
00152
00157 inline type& operator[](const unsigned int idx)
00158 {
00159 unsigned int csize = size;
00160 while (idx >= csize ) {
00161 csize *= 2;
00162 }
00163 if ( csize != size ) resize(csize);
00164 return data[idx];
00165 }
00166
00170 inline type* get_data() { return data; }
00171
00172 private:
00174 type* data;
00176 unsigned int size;
00178 unsigned int elements;
00179 };
00180
00181 class MarchingCubes : public Isosurface {
00182 friend class GLUtil;
00183 public:
00186 MarchingCubes();
00187
00192 MarchingCubes(EMData * em);
00193 virtual ~MarchingCubes();
00194
00200 void set_data(EMData* data);
00201
00205 void set_surface_value(const float value);
00206
00210 float get_surface_value() const { return _surf_value; }
00211
00220 void set_sampling(const int rate) { drawing_level = rate; }
00221
00225 int get_sampling() const { return drawing_level; }
00226
00229 int get_sampling_range() { return minvals.size()-1; }
00230
00235 Dict get_isosurface();
00236
00237 void surface_face_z();
00238 private:
00239 map<int, int> point_map;
00240 unsigned long _isodl;
00241
00247 bool calculate_min_max_vals();
00248
00249
00253 void clear_min_max_vals();
00254
00256 vector<EMData*> minvals, maxvals;
00257
00259 int drawing_level;
00260
00270 void draw_cube(const int x, const int y, const int z, const int cur_level );
00271
00272
00280 void marching_cube(int fX, int fY, int fZ, const int cur_level);
00281
00285 void calculate_surface();
00286
00295 float get_offset(float fValue1, float fValue2, float fValueDesired);
00296
00300 int get_edge_num(int x, int y, int z, int edge);
00301
00312 void get_normal(Vector3 &normal, int fX, int fY, int fZ);
00313
00315 CustomVector<float> pp;
00316 CustomVector<float> nn;
00317 CustomVector<unsigned int> ff;
00318 };
00319
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
00361
00364 class U3DWriter{
00365 public:
00366 typedef unsigned int U32;
00367 typedef long unsigned int U64;
00368 typedef double F64;
00369 typedef float F32;
00370 typedef short int I16;
00371 typedef short unsigned int U16;
00372 typedef unsigned char U8;
00373
00374
00375
00376 U3DWriter();
00377 ~U3DWriter();
00378
00379 int write(const string& filename);
00380
00381 ostream& write(ostream&);
00382
00383 private:
00384 unsigned int size_of_in_bytes();
00385
00386 void test_type_sizes();
00387
00388 ostream& write_header(ostream&);
00389 ostream& write_clod_mesh_generator_node(ostream& os);
00390
00391
00392 template<typename type>
00393 ostream& write(ostream& os, const type& T) {
00394 os.write( (const char*)(&T), sizeof(type) );
00395 return os;
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
00468 U32 DIFFUSE_COLOR_COUNT;
00469 U32 SPECULAR_COLOR_COUNT;
00470
00471
00472 CustomVector<F32> pp;
00473 CustomVector<F32> nn;
00474 CustomVector<unsigned int> ff;
00475 };
00476
00477
00478 template<> ostream& U3DWriter::write(ostream& os, const string& );
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
00515 #endif