| 235 | } |
| 236 | |
| 237 | bool writePTC(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream) |
| 238 | { |
| 239 | //ofstream output(filename,ios::out|ios::binary); |
| 240 | |
| 241 | unique_ptr<ostream> output( |
| 242 | compressed ? |
| 243 | Gzip_Out(filename,ios::out|ios::binary) |
| 244 | :new ofstream(filename,ios::out|ios::binary)); |
| 245 | |
| 246 | if(!*output){ |
| 247 | if(errorStream) *errorStream <<"Partio Unable to open file "<<filename<<endl; |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // magic & version |
| 252 | write<LITEND>(*output,ptc_magic); |
| 253 | write<LITEND>(*output,(int)0); |
| 254 | |
| 255 | // particle count |
| 256 | double numParticlesAsDouble=p.numParticles(); |
| 257 | write<LITEND>(*output,numParticlesAsDouble); |
| 258 | |
| 259 | ParticleAttribute positionHandle,normalHandle,radiusHandle; |
| 260 | bool foundPosition=p.attributeInfo("position",positionHandle); |
| 261 | bool foundNormal=p.attributeInfo("normal",normalHandle); |
| 262 | bool foundRadius=p.attributeInfo("radius",radiusHandle); |
| 263 | |
| 264 | if(!foundPosition){ |
| 265 | if(errorStream) *errorStream <<"Partio: failed to find attr 'position' for PTC output"<<endl; |
| 266 | return false; |
| 267 | } |
| 268 | if(!foundNormal) if(errorStream) *errorStream <<"Partio: failed to find attr 'normal' for PTC output, using 0,0,0"<<endl; |
| 269 | if(!foundRadius) if(errorStream) *errorStream <<"Partio: failed to find attr 'radius' for PTC output, using 1"<<endl; |
| 270 | |
| 271 | // compute bounding box |
| 272 | float boxmin[3]={FLT_MAX,FLT_MAX,FLT_MAX},boxmax[3]={-FLT_MAX,-FLT_MAX,-FLT_MAX}; |
| 273 | for(int i=0;i<p.numParticles();i++){ |
| 274 | const float* pos=p.data<float>(positionHandle,i); |
| 275 | for(int k=0;k<3;k++){ |
| 276 | boxmin[k]=min(pos[k],boxmin[k]); |
| 277 | boxmax[k]=max(pos[k],boxmax[k]); |
| 278 | } |
| 279 | } |
| 280 | write<LITEND>(*output,boxmin[0],boxmin[1],boxmin[2]); |
| 281 | write<LITEND>(*output,boxmax[0],boxmax[1],boxmax[2]); |
| 282 | |
| 283 | // world-to-eye & |
| 284 | for(int i=0;i<4;i++) for(int j=0;j<4;j++){ |
| 285 | if(i==j) write<LITEND>(*output,(float)1); |
| 286 | else write<LITEND>(*output,(float)0); |
| 287 | } |
| 288 | // eye-to-screen |
| 289 | const float foo[4][4]={{1.8,0,0,0}, {0,2.41,0,0}, {0,0,1,1}, {0,0,-.1,0}}; |
| 290 | for(int i=0;i<4;i++) for(int j=0;j<4;j++){ |
| 291 | write<LITEND>(*output,foo[i][j]); |
| 292 | } |
| 293 | |
| 294 | |