00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "Volytope.h"
00021 #include <algorithm>
00022
00023 using namespace glift;
00024 using namespace gutz;
00025 using namespace std;
00026
00027
00028
00029
00030
00031 Volytope::Volytope(unsigned int nElements,
00032 unsigned int nTcoords,
00033 unsigned int nPrims)
00034 : SimBase(),
00035 _verts(new VA_ITYPE(nElements,V_TYPE(0))),
00036 _edges(new EA_ITYPE(nElements,E_TYPE(0)))
00037 {
00038 for(unsigned int i=0; i<nTcoords; ++i)
00039 _tcoords.push_back(new TA_ITYPE(nElements,TC_TYPE(0)));
00040 setSizes();
00041 }
00042
00043
00044
00045
00046
00047 Volytope::~Volytope()
00048 {
00049 if(_verts) delete _verts;
00050 if(_edges) delete _edges;
00051 for(unsigned int i=0; i< _tcoords.size(); ++i)
00052 {
00053 if(_tcoords.back()) delete _tcoords.back();
00054 _tcoords.pop_back();
00055 }
00056 }
00057
00058
00059
00060
00061
00062 void Volytope::operator=(const Volytope &v)
00063 {
00064 if(_verts) delete _verts;
00065 _verts = new VA_ITYPE(*v._verts);
00066 if(_edges) delete _edges;
00067 _edges = new EA_ITYPE(*v._edges);
00068 for(unsigned int i=0; i<_tcoords.size(); ++i)
00069 {
00070 if(_tcoords.back()) delete _tcoords.back();
00071 _tcoords.pop_back();
00072 }
00073 for(unsigned int i=0; i<v._tcoords.size(); ++i)
00074 _tcoords.push_back(new TA_ITYPE(*v._tcoords[i]));
00075 for(unsigned int i=0; i<_textures.size(); ++i)
00076 _textures.pop_back();
00077 for(unsigned int i=0; i<v._textures.size(); ++i)
00078 _textures.push_back(v._textures[i]);
00079 setSizes();
00080 }
00081
00082
00083
00084
00085
00086 void Volytope::setData(VA_RTYPE *verts,
00087 TA_RTYPE *tcoords,
00088 EA_RTYPE *edges)
00089 {
00090 if(_verts) delete _verts;
00091 _verts = new VA_ITYPE(*verts);
00092 if(_tcoords[0]) delete _tcoords[0];
00093 _tcoords[0] = new TA_ITYPE(*tcoords);
00094 if(_edges) delete _edges;
00095 _edges = new EA_ITYPE(*edges);
00096
00097 setSizes();
00098 }
00099
00100
00101
00102
00103
00104 Volytope::VA_RTYPE Volytope::genVerts(int nVerts)
00105 {
00106 if(_verts) delete _verts;
00107 _verts = new VA_ITYPE(nVerts, V_TYPE(0));
00108 return VA_RTYPE(*_verts);
00109 }
00110
00111
00112
00113
00114
00115 Volytope::TA_RTYPE Volytope::genTcoords(int tNum)
00116 {
00117 for(int i = _tcoords.size() -1; i < tNum; ++i)
00118 _tcoords.push_back(0);
00119 if(_tcoords[tNum]) delete _tcoords[tNum];
00120 _tcoords[tNum] = new TA_ITYPE(_verts->size(), TC_TYPE(0));
00121 return TA_RTYPE(*(_tcoords[tNum]));
00122 }
00123
00124
00125
00126
00127
00128 Volytope::EA_RTYPE Volytope::genEdges(int nEdges)
00129 {
00130 if(_edges) delete _edges;
00131 _edges = new EA_ITYPE(nEdges, E_TYPE(0));
00132 return EA_RTYPE(*_edges);
00133 }
00134
00135
00136
00137
00138
00139 const Volytope::TA_RTYPE Volytope::getTcoords(unsigned int tNum) const
00140 {
00141 if(tNum < _tcoords.size())
00142 return TA_RTYPE(*(_tcoords[tNum]));
00143 return TA_RTYPE(0,0);
00144 }
00145
00146 Volytope::TA_RTYPE Volytope::getTcoords(unsigned int tNum)
00147 {
00148 if(tNum < _tcoords.size())
00149 return TA_RTYPE(*_tcoords[tNum]);
00150 return TA_RTYPE(0,0);
00151 }
00152
00153
00154
00155
00156
00157 void Volytope::setSizes()
00158 {
00159 _max = gutz::vec3f_min;
00160 _min = gutz::vec3f_max;
00161 _center = gutz::vec3f_zero;
00162 for(int i=0; i<_verts->size(); ++i)
00163 {
00164 for(int j=0; j<3; ++j)
00165 {
00166 _max[j] = g_max(_max[j], (*_verts)[i][j]);
00167 _min[j] = g_min(_min[j], (*_verts)[i][j]);
00168 _center[j] += (*_verts)[i][j];
00169 }
00170 }
00171 _center /= float(_verts->size());
00172 }
00173
00174
00175
00176
00177 void Volytope::setPointDist(gutz::vec3f pt)
00178 {
00179
00180
00181
00182
00183
00184
00185
00186 _dist = (getCenter() - pt).norm();
00187 }
00188
00189
00190
00191
00192
00193 void Volytope::addTexture(glift::SingleTex *tex)
00194 {
00195 _textures.push_back(SingleTexSP(tex));
00196 }
00197
00198
00199
00200
00201 void Volytope::scale(gutz::vec3f scale)
00202 {
00203 if(!_verts) return;
00204 for(int i=0; i<_verts->size(); ++i)
00205 {
00206 (*_verts)[i] *= scale;
00207 }
00208 setSizes();
00209 }
00210
00211
00212
00213
00214 void Volytope::translate(gutz::vec3f trans)
00215 {
00216 if(!_verts) return;
00217 for(int i=0; i<_verts->size(); ++i)
00218 {
00219 (*_verts)[i] += trans;
00220 }
00221 setSizes();
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 void VolytopeVec::setField(VolFieldSP f)
00240 {
00241 if(!f) return;
00242 if(!f->isActive()) return;
00243
00244 if(f->size() != size())
00245 {
00246 derr("setField(), current number of volytopes and fields do not match");
00247 return;
00248 }
00249
00250 for(unsigned int i=0; i<f->size(); ++i)
00251 {
00252 getVoly(i)->loadField(f, i);
00253 }
00254 }
00255
00256
00257
00258
00259 gutz::vec3f VolytopeVec::getBoxMax() const
00260 {
00261 vec3f bmax(-1e10f,-1e10f,-1e10f);
00262
00263 for(unsigned int i=0; i<size(); ++i)
00264 {
00265 VolytopeSP vt = getVoly(i);
00266 if(!vt.isNull())
00267 {
00268 vec3f vtmax = vt->getBoxMax();
00269 for(int k=0; k<3; ++k)
00270 {
00271 bmax[k] = g_max(bmax[k], vtmax[k]);
00272 }
00273 }
00274 }
00275 return bmax;
00276 }
00277
00278
00279
00280
00281 gutz::vec3f VolytopeVec::getBoxMin() const
00282 {
00283 vec3f bmin(1e10f,1e10f,1e10f);
00284
00285 for(unsigned int i=0; i<size(); ++i)
00286 {
00287 VolytopeSP vt = getVoly(i);
00288 if(!vt.isNull())
00289 {
00290 vec3f vtmin = vt->getBoxMin();
00291 for(int k=0; k<3; ++k)
00292 {
00293 bmin[k] = g_min(bmin[k], vtmin[k]);
00294 }
00295 }
00296 }
00297 return bmin;
00298 }
00299
00300
00301
00302
00303 void VolytopeVec::setSize(float sz)
00304 {
00305
00306 vec3f bmin = getBoxMin();
00307 vec3f bmax = getBoxMax();
00308 vec3f vtsize = bmax - bmin;
00309
00310
00311 unsigned int a = 0;
00312 for(int i=1; i<3; ++i) a = vtsize[a] < vtsize[i] ? i : a;
00313
00314
00315 vec3f scl(sz/vtsize[a]);
00316
00317 scale(scl);
00318
00319
00320 _scale *= vec3f_one/scl;
00321 }
00322
00323
00324
00325
00326 void VolytopeVec::center()
00327 {
00328
00329 vec3f bmin = getBoxMin();
00330 vec3f bmax = getBoxMax();
00331 vec3f vtsize = bmax - bmin;
00332
00333
00334 vec3f trans = -bmin - vtsize/2.0f;
00335
00336 translate(trans);
00337 }
00338
00339
00340
00341
00342
00343
00344 void VolytopeVec::scale(gutz::vec3f scl)
00345 {
00346 for(unsigned int i=0; i<size(); ++i)
00347 {
00348 VolytopeSP vt = getVoly(i);
00349 if(!vt.isNull())
00350 {
00351 vt->scale(scl);
00352 }
00353 }
00354 }
00355
00356
00357
00358
00359 void VolytopeVec::translate(gutz::vec3f trans)
00360 {
00361 for(unsigned int i=0; i<size(); ++i)
00362 {
00363 VolytopeSP vt = getVoly(i);
00364 if(!vt.isNull())
00365 {
00366 vt->translate(trans);
00367 }
00368 }
00369 }
00370
00371
00372
00373
00374
00375 struct gtvsp
00376 {
00377 bool operator()(const VolytopeSP &s1, const VolytopeSP &s2)
00378 {
00379 if(s1.isNull()) return true;
00380 if(s2.isNull()) return false;
00381 return (s1->getPointDist() > s2->getPointDist());
00382 }
00383 };
00384
00385 VolytopeVec VolytopeVec::sortFromPoint(gutz::vec3f pt)
00386 {
00387 for(unsigned int i=0; i<size(); ++i)
00388 {
00389 VolytopeSP vt = getVoly(i);
00390 if(!vt.isNull())
00391 {
00392 vt->setPointDist(pt);
00393 }
00394 }
00395
00396 VolytopeVec vret = (*this);
00397 sort(vret.begin(), vret.end(), gtvsp());
00398 return vret;
00399 }