13 #ifndef INCLUDED_core_grid_CartGrid_hh
14 #define INCLUDED_core_grid_CartGrid_hh
20 #include <numeric/xyzVector.hh>
22 #include <utility/vector1.hh>
23 #include <utility/tools/make_vector.hh>
24 #include <utility/string_util.hh>
25 #include <utility/json_spirit/json_spirit_value.h>
26 #include <utility/pointer/ReferenceCount.hh>
27 #include <utility/pointer/owning_ptr.hh>
28 #include <utility/vector0.fwd.hh>
29 #include <utility/io/izstream.hh>
30 #include <utility/exit.hh>
31 #include <utility/Binary_Util.hh>
33 #include <ObjexxFCL/string.functions.hh>
113 if (this->
nY_ > ls) ls = this->
nY_;
114 if (this->
nZ_ > ls) ls = this->
nZ_;
139 if (std::abs(this->
bX_ - rhs.bX_) > 0.01)
return false;
140 if (std::abs(this->
bY_ - rhs.bY_) > 0.01)
return false;
141 if (std::abs(this->
bZ_ - rhs.bZ_) > 0.01)
return false;
147 if (x < bX_ || x >
tX_)
151 if (y < bY_ || y >
tY_)
155 if (z < bZ_ || z >
tZ_)
194 assert( index >= 0 && index <
npoints_ );
195 this->
zones_[index] = value;
207 if (ix < 0 || ix >=
nX_)
return 0;
210 if (iy < 0 || iy >=
nY_)
return 0;
213 if (iz < 0 || iz >=
nZ_)
return 0;
216 assert( index >= 0 && index <
npoints_ );
217 this->
zones_[index] = value;
231 if (ix < 0 || ix >=
nX_)
return 0;
234 if (iy < 0 || iy >=
nY_)
return 0;
237 if (iz < 0 || iz >=
nZ_)
return 0;
239 int index = ix*(
nY_*
nZ_) + iy*(
nZ_) + iz;
240 return this->
zones_[index];
262 copy.nX_ = this->
nX_;
263 copy.nY_ = this->
nY_;
264 copy.nZ_ = this->
nZ_;
266 copy.lX_ = this->
lX_;
267 copy.lY_ = this->
lY_;
268 copy.lZ_ = this->
lZ_;
270 copy.bX_ = this->
bX_;
271 copy.bY_ = this->
bY_;
272 copy.bZ_ = this->
bZ_;
274 copy.tX_ = this->
tX_;
275 copy.tY_ = this->
tY_;
276 copy.tZ_ = this->
tZ_;
278 copy.name_ = this->
name_;
282 for (
int i=0; i < this->
npoints_; i++) {
283 copy.zones_[i] = this->
zones_[i];
288 int minx = this->
nX_;
289 int miny = this->
nY_;
290 int minz = this->
nZ_;
296 for (
int ix=0; ix < this->
nX_; ix++) {
297 for (
int iy=0; iy < this->
nY_; iy++) {
298 for (
int iz=0; iz < this->
nZ_; iz++) {
300 minx = std::min(minx,ix);
301 miny = std::min(miny,iy);
302 minz = std::min(minz,iz);
304 maxx = std::max(maxx,ix);
305 maxy = std::max(maxy,iy);
306 maxz = std::max(maxz,iz);
312 if (maxx*maxy*maxz == 0) {
317 this->
clone(tmpgrid);
319 int nx = maxx - minx + 1;
320 int ny = maxy - miny + 1;
321 int nz = maxz - minz + 1;
327 this->
bX_ += minx*this->
lX_;
328 this->
bY_ += miny*this->
lY_;
329 this->
bZ_ += minz*this->
lZ_;
335 for (
int i=0; i < nx; i++) {
336 for (
int j=0; j < ny; j++) {
337 for (
int k=0; k < nz; k++) {
338 index2 = tmpgrid.get_index(minx+i, miny+j, minz+k);
339 value = tmpgrid.getValue(index2);
347 int istart, iend, jstart, jend, kstart, kend;
348 for (
int i=0; i < input->its_nX_; i++) {
349 for (
int j=0; j < input->its_nY_; j++) {
350 for (
int k=0; k < input->its_nZ_; k++) {
351 if (input->getValue(i,j,k) != 0) {
352 istart = std::max(0, (i-amount));
353 iend = std::min(input->its_nX_, (i+amount));
355 jstart = std::max(0, (j-amount));
356 jend = std::min(input->its_nY_, (j+amount));
358 kstart = std::max(0, (k-amount));
359 kend = std::min(input->its_nZ_, (k+amount));
361 for (
int ii=istart; ii < iend; ii++) {
362 for (
int jj=jstart; jj < jend; jj++) {
363 for (
int kk=kstart; kk < kend; kk++) {
364 if (original->getValue(ii,jj,kk) != 0) {
378 utility::io::izstream file;
379 std::istringstream line_stream;
381 file.open(filename.c_str());
384 std::cout <<
"read_gridfile - unable to open gridfile:" << filename << std::endl;
385 std::exit( EXIT_FAILURE );
394 int nx=0, ny=0, nz=0;
395 int ix=0, iy=0, iz=0;
401 if (ObjexxFCL::is_blank(line)) {
409 line_stream.str(line);
410 line_stream.seekg( std::ios::beg );
412 line_stream >> keyword;
413 if (keyword ==
"NAME:") {
417 else if (keyword ==
"BASE:") {
418 line_stream >> bx >> by >> bz;
420 }
else if (keyword ==
"SIZE:") {
421 line_stream >> nx >> ny >> nz;
422 }
else if (keyword ==
"LENGTH:") {
423 line_stream >> lx >> ly >> lz;
427 line_stream.seekg( std::ios::beg );
429 for (
int iz=0; iz < nz; iz++) {
430 line_stream >> occupied;
442 file.open(filename.c_str());
443 file <<
"NAME: " << this->
get_name() << std::endl;
444 file <<
"BASE: " << this->
bX_ <<
" " << this->
bY_ <<
" " << this->
bZ_ << std::endl;
445 file <<
"SIZE: " << this->
nX_ <<
" " << this->
nY_ <<
" " << this->
nZ_ << std::endl;
446 file <<
"LENGTH: " << this->
lX_ <<
" " << this->
lY_ <<
" " << this->
lZ_ << std::endl;
448 for (
int i=0; i < this->
nX_; i++) {
449 for (
int j=0; j < this->
nY_; j++) {
450 for (
int k=0; k < this->
nZ_; k++) {
451 file << this->
getValue(i,j,k) <<
" ";
466 if (this->
zones_[i] != 0) {
475 using utility::json_spirit::Pair;
476 using utility::json_spirit::Value;
477 using utility::json_spirit::Array;
479 Pair base(
"base",utility::tools::make_vector(Value(this->
bX_),Value(this->
bY_),Value(this->
bZ_)));
480 Pair
size(
"size",utility::tools::make_vector(Value(this->
nX_),Value(this->
nY_),Value(this->
nZ_)));
481 Pair length(
"length",utility::tools::make_vector(Value(this->
lX_),Value(this->
lY_),Value(this->
lZ_)));
484 utility::encode6bit((
unsigned char*)
zones_,
npoints_*
sizeof(
T),point_data);
486 utility::json_spirit::Pair data(
"data",Value(point_data));
488 return utility::json_spirit::Value( utility::tools::make_vector(name,base,size,length,data) );
496 utility::json_spirit::mArray base_data = grid_data[
"base"].get_array();
497 assert(base_data.size() == 3);
503 utility::json_spirit::mArray size_data = grid_data[
"size"].get_array();
504 assert(size_data.size() == 3);
506 int nX = size_data[0].get_int();
507 int nY = size_data[1].get_int();
508 int nZ = size_data[2].get_int();
510 utility::json_spirit::mArray length_data = grid_data[
"length"].get_array();
511 assert(length_data.size() == 3);
522 std::string point_data = grid_data[
"data"].get_str();
523 utility::decode6bit((
unsigned char*)
zones_,point_data);
528 int ngrids =
static_cast<int>(list_grids.size());
531 T curr_value, new_value;
532 for (
int i=0; i < ngrids; i++) {
536 if (!this->
equalBase(*(list_grids[i]))) {
540 for (
int j=0; j < this->its_npoints; j++) {
542 new_value = list_grids[i]->getValue(j);
543 curr_value += new_value;
554 this->
nX_ += 2*expansion;
555 this->
nY_ += 2*expansion;
556 this->
nZ_ += 2*expansion;
562 int tsplits = nsplits*nsplits*nsplits;
563 if (igrid < 0 || igrid >= tsplits) {
564 utility_exit_with_message(
"accessing split out of bounds");
571 int iz =
int(igrid / (nsplits*nsplits));
572 int rem = igrid - (iz*nsplits*nsplits);
573 int iy =
int(rem/nsplits);
574 int ix = rem - (iy*nsplits);
576 int padx =
int((pad/
lX_)-0.1);
577 int pady =
int((pad/
lY_)-0.1);
578 int padz =
int((pad/
lZ_)-0.1);
613 grid->its_nX_ = split_x+leftx+rightx;
614 grid->its_nY_ = split_y+lefty+righty;
615 grid->its_nZ_ = split_z+leftz+rightz;
621 grid->its_bX_ = std::max(this->
bX_, bx);
622 grid->its_bY_ = std::max(this->
bY_, by);
623 grid->its_bZ_ = std::max(this->
bZ_, bz);
625 grid->its_lX_ = this->
lX_;
626 grid->its_lY_ = this->
lY_;
627 grid->its_lZ_ = this->
lZ_;
631 grid->setDimensions(grid->its_nX_,grid->its_nY_,grid->its_nZ_,grid->its_lX_,grid->its_lY_,grid->its_lZ_);
635 int xstart = std::max(0,ix*split_x-padx);
636 int ystart = std::max(0,iy*split_y-pady);
637 int zstart = std::max(0,iz*split_z-padz);
639 for (
int i=0; i < grid->its_nX_; i++) {
640 for (
int j=0; j < grid->its_nY_; j++) {
641 for (
int k=0; k < grid->its_nZ_; k++) {
642 int value = this->
getValue(xstart+i,ystart+j,zstart+k);
643 if (value == -1) value = 0;
644 grid->setValue(i,j,k,value);
672 static_cast<int>(std::floor((coords.x() -
bX_)/
lX_)),
673 static_cast<int>(std::floor((coords.y() -
bY_)/
lY_)),
674 static_cast<int>(std::floor((coords.z() -
bZ_)/
lZ_))
682 (
bX_ + (gridpt.x() + 0.5)*
lX_),
683 (
bY_ + (gridpt.y() + 0.5)*
lY_),
684 (
bZ_ + (gridpt.z() + 0.5)*
lZ_)
691 return getValue(gridpt.x(), gridpt.y(), gridpt.z());
696 return getValue(coords.x(), coords.y(), coords.z());
711 setValue(gridpt.x(), gridpt.y(), gridpt.z(), value);
716 setValue(coords.x(), coords.y(), coords.z(), value);
724 std::ofstream out( filename.c_str() );
731 int const plus = 127;
736 out <<
" Origin " << -origin.x() <<
' ' << -origin.y() <<
' ' << -origin.z() ;
737 out <<
" Extent " <<
nX_ <<
' ' <<
nY_ <<
' ' <<
nZ_ ;
738 out <<
" Grid " <<
nX_ <<
' ' <<
nY_ <<
' ' <<
nZ_ ;
739 out <<
" Cell " <<
nX_*
lX_ <<
' ' <<
nY_*
lY_ <<
' ' << nZ_*
lZ_ <<
" 90.0 90.0 90.0";
740 out <<
" Prod 1 Plus " << plus <<
" Sigma 1\f";
742 for(
long i = out.tellp(); i < 512; ++i) out <<
' ';
744 typedef unsigned char ubyte;
745 for(
int zz = 0; zz <
nZ_; zz += 8) {
746 for(
int yy = 0; yy <
nY_; yy += 8) {
747 for(
int xx = 0; xx <
nX_; xx += 8) {
748 for(
int z = zz, z_end = zz+8; z < z_end; ++z) {
749 for(
int y = yy, y_end = yy+8; y < y_end; ++y) {
750 for(
int x = xx, x_end = xx+8; x < x_end; ++x) {
751 if( x < nX_ && y < nY_ && z < nZ_ ) out << ubyte(
getValue(x, y, z) + plus );
752 else out << ubyte( 0 + plus );
855 assert( index >= 0 && index <
npoints_ );
856 this->
zones_[index] = value;
866 return this->
zones_[index];