1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
//
// Copyright (c) 2014-2024, Novartis Institutes for BioMedical Research and
// other RDKit contributors
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include "UniformRealValueGrid3D.h"
#include <DataStructs/RealValueVect.h>
#include <RDGeneral/StreamOps.h>
#include <RDGeneral/Exceptions.h>
#include <Geometry/point.h>
#include <fstream>
#include <iomanip>
using namespace RDKit;
namespace {
constexpr double OFFSET_TOL = 1.e-4;
constexpr double SPACING_TOL = 1.e-4;
constexpr const char *INCOMPATIBLE_GRIDS = "incompatible grids";
} // end anonymous namespace
namespace RDGeom {
std::int32_t ci_RealValueGrid3DPICKLE_VERSION = 0x1;
UniformRealValueGrid3D::UniformRealValueGrid3D(
const UniformRealValueGrid3D &other) {
UniformRealValueGrid3D::initGrid(
other.d_numX * other.d_spacing, other.d_numY * other.d_spacing,
other.d_numZ * other.d_spacing, other.d_spacing, other.d_offSet,
&other.d_storage);
}
void UniformRealValueGrid3D::initGrid(double dimX, double dimY, double dimZ,
double spacing,
const RDGeom::Point3D &offSet,
const RDKit::RealValueVect *data) {
PRECONDITION(dimX > 0.0, "Invalid x-dimension for grid");
PRECONDITION(dimY > 0.0, "Invalid y-dimension for grid");
PRECONDITION(dimZ > 0.0, "Invalid z-dimension for grid");
PRECONDITION(spacing > 0.0, "Invalid spacing for grid");
d_numX = static_cast<unsigned int>(floor(dimX / spacing + 0.5));
d_numY = static_cast<unsigned int>(floor(dimY / spacing + 0.5));
d_numZ = static_cast<unsigned int>(floor(dimZ / spacing + 0.5));
// PRECONDITION((!data)||data->getValueType()==double,"grid data type
// mismatch");
PRECONDITION(!data || data->size() == d_numX * d_numY * d_numZ,
"grid data size mismatch");
d_spacing = spacing;
d_offSet = offSet;
if (!data) {
d_storage.setLength(d_numX * d_numY * d_numZ);
d_storage.setToVal(0.0);
} else {
d_storage = *data;
}
}
UniformRealValueGrid3D::UniformRealValueGrid3D(const std::string &pkl) {
initFromText(pkl.c_str(), pkl.size());
}
UniformRealValueGrid3D::UniformRealValueGrid3D(const char *pkl,
unsigned int len) {
initFromText(pkl, len);
}
int UniformRealValueGrid3D::getGridIndex(unsigned int xi, unsigned int yi,
unsigned int zi) const {
if (xi >= d_numX) {
return -1;
}
if (yi >= d_numY) {
return -1;
}
if (zi >= d_numZ) {
return -1;
}
return (zi * d_numX * d_numY + yi * d_numX + xi);
}
void UniformRealValueGrid3D::getGridIndices(unsigned int idx, unsigned int &xi,
unsigned int &yi,
unsigned int &zi) const {
if (idx >= d_numX * d_numY * d_numZ) {
throw IndexErrorException(idx);
}
xi = idx % d_numX;
yi = (idx % (d_numX * d_numY)) / d_numX;
zi = idx / (d_numX * d_numY);
}
int UniformRealValueGrid3D::getGridPointIndex(
const RDGeom::Point3D &point) const {
auto tPt = point;
tPt -= d_offSet; // d_origin;
tPt /= d_spacing;
constexpr double move = 0.5;
auto xi = static_cast<int>(floor(tPt.x + move));
auto yi = static_cast<int>(floor(tPt.y + move));
auto zi = static_cast<int>(floor(tPt.z + move));
if ((xi < 0) || (xi >= static_cast<int>(d_numX))) {
return -1;
}
if ((yi < 0) || (yi >= static_cast<int>(d_numY))) {
return -1;
}
if ((zi < 0) || (zi >= static_cast<int>(d_numZ))) {
return -1;
}
return (zi * d_numX * d_numY + yi * d_numX + xi);
}
double UniformRealValueGrid3D::getVal(const RDGeom::Point3D &point) const {
auto id = getGridPointIndex(point);
if (id < 0) {
return -1;
}
return d_storage.getVal(static_cast<unsigned int>(id));
}
double UniformRealValueGrid3D::getVal(unsigned int pointId) const {
return d_storage.getVal(pointId);
}
void UniformRealValueGrid3D::setVal(const RDGeom::Point3D &point, double val) {
auto id = getGridPointIndex(point);
if (id < 0) {
return;
}
d_storage.setVal(static_cast<unsigned int>(id), val);
}
RDGeom::Point3D UniformRealValueGrid3D::getGridPointLoc(
unsigned int pointId) const {
if (pointId >= d_numX * d_numY * d_numZ) {
throw IndexErrorException(pointId);
}
RDGeom::Point3D res((pointId % d_numX) * d_spacing,
((pointId % (d_numX * d_numY)) / d_numX) * d_spacing,
(pointId / (d_numX * d_numY)) * d_spacing);
res += d_offSet; // d_origin;
return res;
}
void UniformRealValueGrid3D::setVal(unsigned int pointId, double val) {
d_storage.setVal(pointId, val);
}
bool UniformRealValueGrid3D::compareParams(
const UniformRealValueGrid3D &other) const {
if (d_numX != other.getNumX()) {
return false;
}
if (d_numY != other.getNumY()) {
return false;
}
if (d_numZ != other.getNumZ()) {
return false;
}
if (fabs(d_spacing - other.getSpacing()) > SPACING_TOL) {
return false;
}
auto dOffset = d_offSet;
dOffset -= other.getOffset();
return dOffset.lengthSq() <= OFFSET_TOL;
}
bool UniformRealValueGrid3D::compareVectors(
const UniformRealValueGrid3D &other) const {
return d_storage.compareVectors(other.d_storage);
}
bool UniformRealValueGrid3D::compareGrids(
const UniformRealValueGrid3D &other) const {
if (!compareParams(other)) {
return false;
}
return d_storage.compareVectors(other.d_storage);
}
UniformRealValueGrid3D &UniformRealValueGrid3D::operator=(
const UniformRealValueGrid3D &other) {
if (this == &other) {
return *this;
}
d_numX = other.d_numX;
d_numY = other.d_numY;
d_numZ = other.d_numZ;
d_spacing = other.d_spacing;
d_offSet = other.d_offSet;
d_storage = other.d_storage;
return *this;
}
UniformRealValueGrid3D &UniformRealValueGrid3D::operator|=(
const UniformRealValueGrid3D &other) {
PRECONDITION(compareParams(other), INCOMPATIBLE_GRIDS);
d_storage |= other.d_storage;
return *this;
}
UniformRealValueGrid3D &UniformRealValueGrid3D::operator&=(
const UniformRealValueGrid3D &other) {
PRECONDITION(compareParams(other), INCOMPATIBLE_GRIDS);
d_storage &= other.d_storage;
return *this;
}
UniformRealValueGrid3D &UniformRealValueGrid3D::operator+=(
const UniformRealValueGrid3D &other) {
PRECONDITION(compareParams(other), INCOMPATIBLE_GRIDS);
d_storage += other.d_storage;
return *this;
}
UniformRealValueGrid3D &UniformRealValueGrid3D::operator-=(
const UniformRealValueGrid3D &other) {
PRECONDITION(compareParams(other), INCOMPATIBLE_GRIDS);
d_storage -= other.d_storage;
return *this;
}
std::string UniformRealValueGrid3D::toString() const {
std::stringstream ss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
std::int32_t tVers = ci_RealValueGrid3DPICKLE_VERSION * -1;
streamWrite(ss, tVers);
std::uint32_t tInt;
tInt = d_numX;
streamWrite(ss, tInt);
tInt = d_numY;
streamWrite(ss, tInt);
tInt = d_numZ;
streamWrite(ss, tInt);
streamWrite(ss, d_spacing);
streamWrite(ss, d_offSet.x);
streamWrite(ss, d_offSet.y);
streamWrite(ss, d_offSet.z);
std::string storePkl = d_storage.toString();
std::uint32_t pklSz = storePkl.size();
streamWrite(ss, pklSz);
ss.write(storePkl.c_str(), pklSz * sizeof(char));
return ss.str();
}
void UniformRealValueGrid3D::initFromText(const char *pkl,
const unsigned int length) {
std::stringstream ss(std::ios_base::binary | std::ios_base::in |
std::ios_base::out);
ss.write(pkl, length);
std::int32_t tVers;
streamRead(ss, tVers);
tVers = -tVers;
if (tVers != 0x1) {
throw ValueErrorException("bad version in UniformRealValueGrid3D pickle");
}
std::uint32_t tInt;
streamRead(ss, tInt);
d_numX = tInt;
streamRead(ss, tInt);
d_numY = tInt;
streamRead(ss, tInt);
d_numZ = tInt;
streamRead(ss, d_spacing);
double oX, oY, oZ;
streamRead(ss, oX);
streamRead(ss, oY);
streamRead(ss, oZ);
d_offSet = RDGeom::Point3D(oX, oY, oZ);
std::uint32_t pklSz;
streamRead(ss, pklSz);
std::vector<char> buff(pklSz);
ss.read(buff.data(), pklSz * sizeof(char));
d_storage = RDKit::RealValueVect(buff.data(), pklSz);
}
UniformRealValueGrid3D operator&(const UniformRealValueGrid3D &grd1,
const UniformRealValueGrid3D &grd2) {
UniformRealValueGrid3D ans(grd1);
ans &= grd2;
return ans;
};
UniformRealValueGrid3D operator|(const UniformRealValueGrid3D &grd1,
const UniformRealValueGrid3D &grd2) {
UniformRealValueGrid3D ans(grd1);
ans |= grd2;
return ans;
};
UniformRealValueGrid3D operator+(const UniformRealValueGrid3D &grd1,
const UniformRealValueGrid3D &grd2) {
UniformRealValueGrid3D ans(grd1);
ans += grd2;
return ans;
};
UniformRealValueGrid3D operator-(const UniformRealValueGrid3D &grd1,
const UniformRealValueGrid3D &grd2) {
UniformRealValueGrid3D ans(grd1);
ans -= grd2;
return ans;
};
} // namespace RDGeom
|