3116{
3118 if (nbox == 0)
3119 {
3120 std::cerr << "HepPolyhedronBoxMesh: Empty box mesh" << std::endl;
3121 return;
3122 }
3123
3124 G4double invx = 1./sizeX, invy = 1./sizeY, invz = 1./sizeZ;
3125
3127 for (const auto& p: positions)
3128 {
3129 if (pmin.
x() > p.x()) pmin.
setX(p.x());
3130 if (pmin.
y() > p.y()) pmin.
setY(p.y());
3131 if (pmin.
z() > p.z()) pmin.
setZ(p.z());
3132 if (pmax.x() < p.x()) pmax.setX(p.x());
3133 if (pmax.y() < p.y()) pmax.setY(p.y());
3134 if (pmax.z() < p.z()) pmax.setZ(p.z());
3135 }
3136
3137 G4int nx = (pmax.x() - pmin.
x())*invx + 1.5;
3138 G4int ny = (pmax.y() - pmin.
y())*invy + 1.5;
3139 G4int nz = (pmax.z() - pmin.
z())*invz + 1.5;
3140
3141 std::vector<char> voxels(nx*ny*nz, 0);
3142 std::vector<G4int> indices((nx+1)*(ny+1)*(nz+1), 0);
3143
3144 G4int kx = ny*nz, ky = nz;
3145 for (const auto& p: positions)
3146 {
3147 G4int ix = (p.x() - pmin.
x())*invx + 0.5;
3148 G4int iy = (p.y() - pmin.
y())*invy + 0.5;
3149 G4int iz = (p.z() - pmin.
z())*invz + 0.5;
3150 G4int i = ix*kx + iy*ky + iz;
3151 voxels[i] = 1;
3152 }
3153
3154
3155 G4int kvx = (ny + 1)*(nz + 1), kvy = nz + 1;
3156 G4int nver = 0, nfac = 0;
3157 for (const auto& p: positions)
3158 {
3159 G4int ix = (p.x() - pmin.
x())*invx + 0.5;
3160 G4int iy = (p.y() - pmin.
y())*invy + 0.5;
3161 G4int iz = (p.z() - pmin.
z())*invz + 0.5;
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3174
3175 vcheck = (ix == 0) ? 0 : voxels[(ix-1)*kx + iy*ky + iz];
3176 if (vcheck == 0)
3177 {
3178 nfac++;
3179 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3180 G4int i2 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3181 G4int i3 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3182 G4int i4 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3183 if (indices[i1] == 0) indices[i1] = ++nver;
3184 if (indices[i2] == 0) indices[i2] = ++nver;
3185 if (indices[i3] == 0) indices[i3] = ++nver;
3186 if (indices[i4] == 0) indices[i4] = ++nver;
3187 }
3188
3189 vcheck = (ix == nx - 1) ? 0 : voxels[(ix+1)*kx + iy*ky + iz];
3190 if (vcheck == 0)
3191 {
3192 nfac++;
3193 G4int i1 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3194 G4int i2 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3195 G4int i3 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3196 G4int i4 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3197 if (indices[i1] == 0) indices[i1] = ++nver;
3198 if (indices[i2] == 0) indices[i2] = ++nver;
3199 if (indices[i3] == 0) indices[i3] = ++nver;
3200 if (indices[i4] == 0) indices[i4] = ++nver;
3201 }
3202
3203 vcheck = (iy == 0) ? 0 : voxels[ix*kx + (iy-1)*ky + iz];
3204 if (vcheck == 0)
3205 {
3206 nfac++;
3207 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3208 G4int i2 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3209 G4int i3 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3210 G4int i4 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3211 if (indices[i1] == 0) indices[i1] = ++nver;
3212 if (indices[i2] == 0) indices[i2] = ++nver;
3213 if (indices[i3] == 0) indices[i3] = ++nver;
3214 if (indices[i4] == 0) indices[i4] = ++nver;
3215 }
3216
3217 vcheck = (iy == ny - 1) ? 0 : voxels[ix*kx + (iy+1)*ky + iz];
3218 if (vcheck == 0)
3219 {
3220 nfac++;
3221 G4int i1 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3222 G4int i2 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3223 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3224 G4int i4 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3225 if (indices[i1] == 0) indices[i1] = ++nver;
3226 if (indices[i2] == 0) indices[i2] = ++nver;
3227 if (indices[i3] == 0) indices[i3] = ++nver;
3228 if (indices[i4] == 0) indices[i4] = ++nver;
3229 }
3230
3231 vcheck = (iz == 0) ? 0 : voxels[ix*kx + iy*ky + (iz-1)];
3232 if (vcheck == 0)
3233 {
3234 nfac++;
3235 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3236 G4int i2 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3237 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3238 G4int i4 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3239 if (indices[i1] == 0) indices[i1] = ++nver;
3240 if (indices[i2] == 0) indices[i2] = ++nver;
3241 if (indices[i3] == 0) indices[i3] = ++nver;
3242 if (indices[i4] == 0) indices[i4] = ++nver;
3243 }
3244
3245 vcheck = (iz == nz - 1) ? 0 : voxels[ix*kx + iy*ky + (iz+1)];
3246 if (vcheck == 0)
3247 {
3248 nfac++;
3249 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3250 G4int i2 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3251 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3252 G4int i4 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3253 if (indices[i1] == 0) indices[i1] = ++nver;
3254 if (indices[i2] == 0) indices[i2] = ++nver;
3255 if (indices[i3] == 0) indices[i3] = ++nver;
3256 if (indices[i4] == 0) indices[i4] = ++nver;
3257 }
3258 }
3259
3261 G4ThreeVector p0(pmin.
x() - 0.5*sizeX, pmin.
y() - 0.5*sizeY, pmin.
z() - 0.5*sizeZ);
3262 for (
G4int ix = 0; ix <= nx; ++ix)
3263 {
3264 for (
G4int iy = 0; iy <= ny; ++iy)
3265 {
3266 for (
G4int iz = 0; iz <= nz; ++iz)
3267 {
3268 G4int i = ix*kvx + iy*kvy + iz;
3269 if (indices[i] == 0) continue;
3271 }
3272 }
3273 }
3274 nfac = 0;
3275 for (const auto& p: positions)
3276 {
3277 G4int ix = (p.x() - pmin.
x())*invx + 0.5;
3278 G4int iy = (p.y() - pmin.
y())*invy + 0.5;
3279 G4int iz = (p.z() - pmin.
z())*invz + 0.5;
3281
3282 vcheck = (ix == 0) ? 0 : voxels[(ix-1)*kx + iy*ky + iz];
3283 if (vcheck == 0)
3284 {
3285 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3286 G4int i2 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3287 G4int i3 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3288 G4int i4 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3289 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3290 }
3291
3292 vcheck = (ix == nx - 1) ? 0 : voxels[(ix+1)*kx + iy*ky + iz];
3293 if (vcheck == 0)
3294 {
3295 G4int i1 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3296 G4int i2 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3297 G4int i3 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3298 G4int i4 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3299 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3300
3301 }
3302
3303 vcheck = (iy == 0) ? 0 : voxels[ix*kx + (iy-1)*ky + iz];
3304 if (vcheck == 0)
3305 {
3306 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3307 G4int i2 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3308 G4int i3 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3309 G4int i4 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3310 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3311 }
3312
3313 vcheck = (iy == ny - 1) ? 0 : voxels[ix*kx + (iy+1)*ky + iz];
3314 if (vcheck == 0)
3315 {
3316 G4int i1 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3317 G4int i2 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3318 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3319 G4int i4 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3320 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3321 }
3322
3323 vcheck = (iz == 0) ? 0 : voxels[ix*kx + iy*ky + (iz-1)];
3324 if (vcheck == 0)
3325 {
3326 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+0);
3327 G4int i2 = (ix+0)*kvx + (iy+1)*kvy + (iz+0);
3328 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+0);
3329 G4int i4 = (ix+1)*kvx + (iy+0)*kvy + (iz+0);
3330 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3331 }
3332
3333 vcheck = (iz == nz - 1) ? 0 : voxels[ix*kx + iy*ky + (iz+1)];
3334 if (vcheck == 0)
3335 {
3336 G4int i1 = (ix+0)*kvx + (iy+0)*kvy + (iz+1);
3337 G4int i2 = (ix+1)*kvx + (iy+0)*kvy + (iz+1);
3338 G4int i3 = (ix+1)*kvx + (iy+1)*kvy + (iz+1);
3339 G4int i4 = (ix+0)*kvx + (iy+1)*kvy + (iz+1);
3340 SetFacet(++nfac, indices[i1], indices[i2], indices[i3], indices[i4]);
3341 }
3342 }
3344}
CLHEP::Hep3Vector G4ThreeVector
void SetVertex(G4int index, const G4Point3D &v)
void SetFacet(G4int index, G4int iv1, G4int iv2, G4int iv3, G4int iv4=0)
void AllocateMemory(G4int Nvert, G4int Nface)