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